Skip to content

TypeBuilder::CreateTypeInfo() should not throw COMException #43282

Closed

Description

@marek-safar

When TypeBuilder encounters by-ref field it throws COMException. This seems to be the only reference to COMException and the behaviour is not consistent with other error handling for different type members.

public void DefineField_ByRefFieldType_ThrowsCOMExceptionOnCreation()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
type.DefineField("Name", typeof(int).MakeByRefType(), FieldAttributes.Public);
Assert.Throws<COMException>(() => type.CreateTypeInfo());
}

It should be replaced with ArgumentException as it's done for all other type members. Here is an example with events

[Fact]
public void DefineEvent_ByRefEventType_ThrowsArgumentException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
AssertExtensions.Throws<ArgumentException>(null, () => type.DefineEvent("Name", EventAttributes.None, typeof(int).MakeByRefType()));
}

Activity

  1. added
    untriaged
    on Oct 11, 2020
  2. AaronRobinsonMSFT commented on Oct 14, 2020

    @AaronRobinsonMSFT
    Member

    The error here is VLDTR_E_SIG_BYREFINFIELD (HRESULT 0x801312e4) and is originating in a deep call stack.

    coreclr.dll!ThrowHR(HRESULT hr) Line 965
    	at D:\runtime\src\coreclr\src\utilcode\ex.cpp(965)
    coreclr.dll!IfFailThrow(HRESULT hr) Line 1312
    	at D:\runtime\src\coreclr\src\inc\ex.h(1312)
    coreclr.dll!MethodTableBuilder::InitializeFieldDescs(FieldDesc * pFieldDescList, const LayoutRawFieldInfo * pLayoutRawFieldInfos, MethodTableBuilder::bmtInternalInfo * bmtInternal, const MethodTableBuilder::bmtGenericsInfo * bmtGenerics, MethodTableBuilder::bmtMetaDataInfo * bmtMetaData, MethodTableBuilder::bmtEnumFieldInfo * bmtEnumFields, MethodTableBuilder::bmtErrorInfo * bmtError, MethodTable * * * pByValueClassCache, MethodTableBuilder::bmtMethAndFieldDescs * bmtMFDescs, MethodTableBuilder::bmtFieldPlacement * bmtFP, unsigned int * totalDeclaredSize) Line 3775
    	at D:\runtime\src\coreclr\src\vm\methodtablebuilder.cpp(3775)
    coreclr.dll!MethodTableBuilder::BuildMethodTableThrowing(LoaderAllocator * pAllocator, Module * pLoaderModule, Module * pModule, unsigned int cl, BuildingInterfaceInfo_t * pBuildingInterfaceList, const LayoutRawFieldInfo * pLayoutRawFieldInfos, MethodTable * pParentMethodTable, const MethodTableBuilder::bmtGenericsInfo * bmtGenericsInfo, SigPointer parentInst, unsigned short cBuildingInterfaceList) Line 1705
    	at D:\runtime\src\coreclr\src\vm\methodtablebuilder.cpp(1705)
    coreclr.dll!ClassLoader::CreateTypeHandleForTypeDefThrowing(Module * pModule, unsigned int cl, Instantiation inst, AllocMemTracker * pamTracker) Line 12232
    	at D:\runtime\src\coreclr\src\vm\methodtablebuilder.cpp(12232)
    coreclr.dll!ClassLoader::CreateTypeHandleForTypeKey(TypeKey * pKey, AllocMemTracker * pamTracker) Line 3197
    	at D:\runtime\src\coreclr\src\vm\clsload.cpp(3197)
    coreclr.dll!ClassLoader::DoIncrementalLoad(TypeKey * pTypeKey, TypeHandle typeHnd, ClassLoadLevel currentLevel) Line 3125
    	at D:\runtime\src\coreclr\src\vm\clsload.cpp(3125)
    coreclr.dll!ClassLoader::LoadTypeHandleForTypeKey_Body(TypeKey * pTypeKey, TypeHandle typeHnd, ClassLoadLevel targetLevel) Line 3894
    	at D:\runtime\src\coreclr\src\vm\clsload.cpp(3894)
    coreclr.dll!ClassLoader::LoadTypeHandleForTypeKey(TypeKey * pTypeKey, TypeHandle typeHnd, ClassLoadLevel targetLevel, const InstantiationContext * pInstContext) Line 3613
    	at D:\runtime\src\coreclr\src\vm\clsload.cpp(3613)
    coreclr.dll!COMDynamicWrite::TermCreateClass(QCall::ModuleHandle pModule, int tk, QCall::ObjectHandleOnStack retType) Line 477
    	at D:\runtime\src\coreclr\src\vm\comdynamic.cpp(477)
    

    At the point of the throw, the exception is of type HRException, however it is converted to an EEMessageException which is classified as kCOMException. This exception then gets propagated out to managed as a COMException. I do not know the best way to fix this because I am sure it is going to be a breaking change for someone. Whether that breaking changes matters to anyone is a good question.

    coreclr.dll!CLRException::GetThrowableFromException(Exception * pException) Line 787
    	at D:\runtime\src\coreclr\src\vm\clrex.cpp(787)
    coreclr.dll!UnwindAndContinueRethrowHelperInsideCatch(Frame * pEntryFrame, Exception * pException) Line 8112
    	at D:\runtime\src\coreclr\src\vm\excep.cpp(8112)
    [snip]
    coreclr.dll!COMDynamicWrite::TermCreateClass(QCall::ModuleHandle pModule, int tk, QCall::ObjectHandleOnStack retType) Line 477
    	at D:\runtime\src\coreclr\src\vm\comdynamic.cpp(477)
    

    @davidwrighton or @jkotas Any opinions or suggestions on how to change this exception type? I guess we could catch prior to the QCall exist and convert to a better managed type.

  3. jkotas commented on Oct 14, 2020

    @jkotas
    Member

    We have plans to enabled byref fields in .NET 6. If that happens, there will be no exception thrown for this repro that will take of this specific problem.

    As for the more general problem of mapping HRESULTs to right exception types inside CoreCLR, I am not sure the best way to fix it either.

  4. marek-safar commented on Oct 14, 2020

    @marek-safar
    ContributorAuthor

    Why not add suggested ArgumentException check to DefineField (that's what it'd done for everything else) ?

  5. jkotas commented on Oct 14, 2020

    @jkotas
    Member

    The check for ByRefs in GetTypeTokenWorkerNoLock is not right. I think we should remove it. It is too aggresive. For example, it will incorrectly fire for:

    ILGenerator ilGenerator = ...
    ilGenerator.Emit(OpCodes.Ldtoken,  typeof(int).MakeByRefType());
    

    that is valid IL.

    Reflection.Emit should not really be in the business of validating the emitted IL.

  6. added and removed
    untriaged
    on Nov 13, 2020
  7. steveharter commented on Nov 13, 2020

    @steveharter
    Contributor

    The check for ByRefs in GetTypeTokenWorkerNoLock is not right

    This issue can be re-purposed somewhat then to fix or remove the byref check and add tests.

  8. added this to the 6.0.0 milestone on Nov 13, 2020
  9. added
    size-reduction
    on Apr 12, 2021
  10. modified the milestones: 6.0.0, 7.0.0 on Jul 20, 2021
  11. krwq commented on Jul 20, 2021

    @krwq
    Member

    moving to 7.0 since this is non-essential for 6.0

  12. ghost added
    needs-further-triage
    on Jul 20, 2021
  13. removed
    needs-further-triage
    on Jul 20, 2021
  14. ghost added
    in-pr
    on Jan 20, 2022
  15. Repository owner moved this from Future to Done in Triage POD for Reflection, META, etc.on Jan 23, 2022
  16. ghost removed
    in-pr
    on Jan 23, 2022
  17. ghost locked as resolved and limited conversation to collaborators on Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Reflection.Emitbugsize-reduction

    Type

    No type

    Projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions