Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Interop calls into reflection on startup before reflection is online #1596

Closed

Description

@wjk

When I run my CoreRT app, an unhandled InvalidOperationException is thrown in the getter for Internal.Runtime.Augments.RuntimeAugments.Callbacks. This occurs within the call to InitializeModules, prior to entry into my application code. I haven't seen this happen before. The getter is being called here. Inspection of the call stack gives references to COM interop, which was completely unimplemented the last time I checked. This just started happening now, right after I updated my clone of CoreRT to mirror the head of master. Can anyone tell me what's going on here? Thanks!

Activity

  1. MichalStrehovsky commented on Jul 29, 2016

    @MichalStrehovsky
    Member

    Can you paste the stack that leads to the exception? I have a suspicion it's related to the new eager constructor in 388a79a, but a stack would help.

  2. wjk commented on Jul 29, 2016

    @wjk
    ContributorAuthor
        ChocoTester.Application.dll!Assert(const char * expr, const char * file, unsigned int line_num, const char * message) Line 50   C++
        ChocoTester.Application.dll!StackFrameIterator::NextInternal() Line 1266    C++
        ChocoTester.Application.dll!StackFrameIterator::Next() Line 1088    C++
        ChocoTester.Application.dll!RhpSfiNext(StackFrameIterator * pThis, unsigned int * puExCollideClauseIdx, unsigned char * pfUnwoundReversePInvoke) Line 1653  C++
        ChocoTester.Application.dll!System_Private_CoreLib_System_Runtime_EH__DispatchEx()  Unknown
        ChocoTester.Application.dll!RhThrowEx() Unknown
        ChocoTester.Application.dll!RhpThrowEx() Line 198   Unknown
        ChocoTester.Application.dll!System_Private_CoreLib_Internal_Runtime_Augments_RuntimeAugments__get_Callbacks()   Unknown
        ChocoTester.Application.dll!System_Private_CoreLib_Internal_Reflection_Core_NonPortable_RuntimeTypeUnifier_RuntimeTypeHandleToTypeCache__Factory()  Unknown
        ChocoTester.Application.dll!System_Private_CoreLib_System_Collections_Concurrent_ConcurrentUnifierW_2__System_Private_CoreLib_Internal_Reflection_Core_NonPortable_RawRuntimeTypeHandleKey__System_Private_CoreLib_System_Type__System_Private_CoreLib_System_Collections_Concurrent_ConcurrentUnifierW_2__GetOrAdd()   Unknown
        ChocoTester.Application.dll!System_Private_CoreLib_Internal_Reflection_Core_NonPortable_RuntimeTypeUnifier__GetTypeForRuntimeTypeHandle()   Unknown
        ChocoTester.Application.dll!System_Private_Interop_System_Runtime_InteropServices_ComCallableObject__InitRefCountedHandleCallback() Unknown
        ChocoTester.Application.dll!System_Private_Interop_System_Runtime_InteropServices___vtable_IUnknown___cctor()   Unknown
        ChocoTester.Application.dll!System_Private_CoreLib_Internal_Runtime_CompilerHelpers_StartupCodeHelpers__RunEagerClassConstructors() Unknown
        ChocoTester.Application.dll!System_Private_CoreLib_Internal_Runtime_CompilerHelpers_StartupCodeHelpers__InitializeEagerClassConstructorsForModule() Unknown
        ChocoTester.Application.dll!InitializeModules() Unknown
        ChocoTester.Application.dll!AppMain(int argc, wchar_t * * argv) Line 312    C++
        choco-tester.exe!wWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * __formal, wchar_t * cmdLine, int nCmdShow) Line 23 C++
    
  3. MichalStrehovsky commented on Jul 29, 2016

    @MichalStrehovsky
    Member

    Mmmm, yeah, the EagerStaticConstructorOrder enum defines the order of how class constructors run and indeed reflection is initialized only after interop initializes.

    We need this order because if I remember correctly, reflection ends up calling into either globalization garbage or resource manager garbage, both of which depend on interop. This use of reflection is not a problem on Project N because we optimize typeof(X).TypeHandle to not actually depend on reflection, but on CoreRT this is not optimized away, and once we change TypeHandle to be actually a wrapper for RuntimeType (just like CoreCLR), this will become reflection use anyway. I think we need interop to stop doing this in the startup path.

  4. wjk commented on Jul 29, 2016

    @wjk
    ContributorAuthor

    @MichalStrehovsky So what, if anything, should be changed?

  5. MichalStrehovsky commented on Jul 29, 2016

    @MichalStrehovsky
    Member

    Ideally, fix up RyuJIT so that it can do the intrinsic expansion of typeof(x).TypeHandle. That way CoreRT compiler will be in the same bucket as the Project N compiler (i.e. interop calling into reflection during startup will work for now).

    This means:

    1. Fix intrinsic expansion of CORINFO_INTRINSIC_GetTypeFromHandle to work for the case when RuntimeTypeHandle isn't just a thin wrapper over RuntimeType.
    2. Add intrinsic expansion for Type.get_TypeHandle.

    None of this work is easy for a person with no RyuJIT experience and none of it will be done any time soon.

  6. MichalStrehovsky commented on Jul 29, 2016

    @MichalStrehovsky
    Member

    Alternatively, @yizhang82 could get someone from his team to fix interop not to call reflection in the startup path.

  7. jkotas commented on Jul 29, 2016

    @jkotas
    Member

    The quick fix should be to put the call to InitRefCountedHandleCallback under #if !CORERT with TODO and link to this issue. (And define CORERT in System.Private.Runtime.InteropServices.csproj so that it actually works - like https://github.com/dotnet/corert/blob/master/src/System.Private.Reflection.Core/src/System.Private.Reflection.Core.csproj#L13.)

  8. wjk commented on Jul 29, 2016

    @wjk
    ContributorAuthor

    @jkotas Should I prepare a PR?

  9. jkotas commented on Jul 29, 2016

    @jkotas
    Member

    @wjk That would be perfect.

  10. added a commit that references this issue on Jul 29, 2016
  11. changed the title [-]"Too Early" exception on runtime startup[/-] [+]Interop calls into reflection on startup before reflection is online[/+] on Oct 20, 2016
  12. MichalStrehovsky commented on Jul 27, 2019

    @MichalStrehovsky
    Member

    We don't compile this in CoreRT anymore.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions