You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Repository navigation
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
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!
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.
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:
Fix intrinsic expansion of CORINFO_INTRINSIC_GetTypeFromHandle to work for the case when RuntimeTypeHandle isn't just a thin wrapper over RuntimeType.
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.
changed the title [-]"Too Early" exception on runtime startup[/-][+]Interop calls into reflection on startup before reflection is online[/+]on Oct 20, 2016
Activity
MichalStrehovsky commentedon Jul 29, 2016
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.
wjk commentedon Jul 29, 2016
MichalStrehovsky commentedon Jul 29, 2016
Mmmm, yeah, the
EagerStaticConstructorOrderenum 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).TypeHandleto not actually depend on reflection, but on CoreRT this is not optimized away, and once we changeTypeHandleto be actually a wrapper forRuntimeType(just like CoreCLR), this will become reflection use anyway. I think we need interop to stop doing this in the startup path.wjk commentedon Jul 29, 2016
@MichalStrehovsky So what, if anything, should be changed?
MichalStrehovsky commentedon Jul 29, 2016
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:
CORINFO_INTRINSIC_GetTypeFromHandleto work for the case whenRuntimeTypeHandleisn't just a thin wrapper overRuntimeType.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.
MichalStrehovsky commentedon Jul 29, 2016
Alternatively, @yizhang82 could get someone from his team to fix interop not to call reflection in the startup path.
jkotas commentedon Jul 29, 2016
The quick fix should be to put the call to
InitRefCountedHandleCallbackunder#if !CORERTwith 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.)wjk commentedon Jul 29, 2016
@jkotas Should I prepare a PR?
jkotas commentedon Jul 29, 2016
@wjk That would be perfect.
Put currently broken code under #if !CORERT
Fix crashing on startup (#1596) (#1604)
[-]"Too Early" exception on runtime startup[/-][+]Interop calls into reflection on startup before reflection is online[/+]MichalStrehovsky commentedon Jul 27, 2019
We don't compile this in CoreRT anymore.