Skip to content

Commit

Permalink
[NativeAOT] Reflection Invoke refactoring
Browse files Browse the repository at this point in the history
- Refactored reflection Invoke in NativeAOT to be similar to how it is done in CoreCLR.
- All argument validation and coercion is done via static code now. This makes the reflection invoke stubs much smaller and the AOT compiler side a lot simpler.
- The invoke stub is minimal now and just takes the arguments for the methods to invoke as "Span of byrefs". The two notable differences with CoreCLR are:
   - The invoke stub takes the function pointer to call as an argument to allow sharing of the stubs between methods with the same signature. CoreCLR has the thunks non-sharable currently. We have discussed sharing them among methods with the same signature like it is done here.
   - The return value is returned as byref. CoreCLR thunk does boxing of the return value as part of the stub. Again, we have discussed to do it this way in CoreCLR too, we just did not have time to do it yet.

Fixes #72548
  • Loading branch information
jkotas committed Jul 31, 2022
1 parent 13b4bb7 commit b08f4ee
Show file tree
Hide file tree
Showing 40 changed files with 724 additions and 1,517 deletions.
20 changes: 20 additions & 0 deletions src/coreclr/nativeaot/Runtime/GCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,23 @@ COOP_PINVOKE_HELPER(int64_t, RhGetTotalPauseDuration, ())
{
return GCHeapUtilities::GetGCHeap()->GetTotalPauseDuration();
}

COOP_PINVOKE_HELPER(void, RhRegisterForGCReporting, (GCFrameRegistration* pRegistration))
{
Thread* pThread = ThreadStore::GetCurrentThread();

ASSERT(pRegistration->m_pThread == NULL);
pRegistration->m_pThread = pThread;

pThread->PushGCFrameRegistration(pRegistration);
}

COOP_PINVOKE_HELPER(void, RhUnregisterForGCReporting, (GCFrameRegistration* pRegistration))
{
Thread* pThread = pRegistration->m_pThread;
if (pThread == NULL)
return;

ASSERT(pThread == ThreadStore::GetCurrentThread());
pThread->PopGCFrameRegistration(pRegistration);
}
29 changes: 23 additions & 6 deletions src/coreclr/nativeaot/Runtime/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ void Thread::Construct()
(offsetof(Thread, m_pTransitionFrame)));
#endif // USE_PORTABLE_HELPERS

m_pThreadLocalModuleStatics = NULL;
m_numThreadLocalModuleStatics = 0;

// NOTE: We do not explicitly defer to the GC implementation to initialize the alloc_context. The
// alloc_context will be initialized to 0 via the static initialization of tls_CurrentThread. If the
// alloc_context ever needs different initialization, a matching change to the tls_CurrentThread
Expand Down Expand Up @@ -281,12 +278,19 @@ void Thread::Construct()
m_pThreadStressLog = StressLog::CreateThreadStressLog(this);
#endif // STRESS_LOG

m_threadAbortException = NULL;
// Everything else should be initialized to 0 via the static initialization of tls_CurrentThread.

ASSERT(m_pThreadLocalModuleStatics == NULL);
ASSERT(m_numThreadLocalModuleStatics == 0);

ASSERT(m_pGCFrameRegistrations == NULL);

ASSERT(m_threadAbortException == NULL);

#ifdef FEATURE_SUSPEND_REDIRECTION
m_redirectionContextBuffer = NULL;
ASSERT(m_redirectionContextBuffer == NULL);
#endif //FEATURE_SUSPEND_REDIRECTION
m_interruptedContext = NULL;
ASSERT(m_interruptedContext == NULL);
}

bool Thread::IsInitialized()
Expand Down Expand Up @@ -370,6 +374,8 @@ void Thread::Destroy()
delete[] m_redirectionContextBuffer;
}
#endif //FEATURE_SUSPEND_REDIRECTION

ASSERT(m_pGCFrameRegistrations == NULL);
}

#ifdef HOST_WASM
Expand Down Expand Up @@ -536,6 +542,17 @@ void Thread::GcScanRootsWorker(void * pfnEnumCallback, void * pvCallbackData, St
RedhawkGCInterface::EnumGcRef(pExceptionObj, GCRK_Object, pfnEnumCallback, pvCallbackData);
}

for (GCFrameRegistration* pCurGCFrame = m_pGCFrameRegistrations; pCurGCFrame != NULL; pCurGCFrame = pCurGCFrame->m_pNext)
{
ASSERT(pCurGCFrame->m_pThread == this);

for (uint32_t i = 0; i < pCurGCFrame->m_numObjRefs; i++)
{
RedhawkGCInterface::EnumGcRef(dac_cast<PTR_RtuObjectRef>(pCurGCFrame->m_pObjRefs + i),
pCurGCFrame->m_MaybeInterior ? GCRK_Byref : GCRK_Object, pfnEnumCallback, pvCallbackData);
}
}

// Keep alive the ThreadAbortException that's stored in the target thread during thread abort
PTR_RtuObjectRef pThreadAbortExceptionObj = dac_cast<PTR_RtuObjectRef>(&m_threadAbortException);
RedhawkGCInterface::EnumGcRef(pThreadAbortExceptionObj, GCRK_Object, pfnEnumCallback, pvCallbackData);
Expand Down
13 changes: 13 additions & 0 deletions src/coreclr/nativeaot/Runtime/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ struct ExInfo
volatile void* m_notifyDebuggerSP;
};

struct GCFrameRegistration
{
Thread* m_pThread;
GCFrameRegistration* m_pNext;
void** m_pObjRefs;
uint32_t m_numObjRefs;
int m_MaybeInterior;
};

struct ThreadBuffer
{
uint8_t m_rgbAllocContextBuffer[SIZEOF_ALLOC_CONTEXT];
Expand All @@ -82,6 +91,7 @@ struct ThreadBuffer
Object* m_threadAbortException; // ThreadAbortException instance -set only during thread abort
PTR_PTR_VOID m_pThreadLocalModuleStatics;
uint32_t m_numThreadLocalModuleStatics;
GCFrameRegistration* m_pGCFrameRegistrations;
PTR_VOID m_pStackLow;
PTR_VOID m_pStackHigh;
PTR_UInt8 m_pTEB; // Pointer to OS TEB structure for this thread
Expand Down Expand Up @@ -274,6 +284,9 @@ class Thread : private ThreadBuffer

NATIVE_CONTEXT* GetInterruptedContext();

void PushGCFrameRegistration(GCFrameRegistration* pRegistration);
void PopGCFrameRegistration(GCFrameRegistration* pRegistration);

#ifdef FEATURE_SUSPEND_REDIRECTION
NATIVE_CONTEXT* EnsureRedirectionContext();
#endif //FEATURE_SUSPEND_REDIRECTION
Expand Down
12 changes: 12 additions & 0 deletions src/coreclr/nativeaot/Runtime/thread.inl
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ inline PTR_VOID Thread::GetThreadStressLog() const
{
return m_pThreadStressLog;
}

inline void Thread::PushGCFrameRegistration(GCFrameRegistration* pRegistration)
{
pRegistration->m_pNext = m_pGCFrameRegistrations;
m_pGCFrameRegistrations = pRegistration;
}

inline void Thread::PopGCFrameRegistration(GCFrameRegistration* pRegistration)
{
ASSERT(m_pGCFrameRegistrations == pRegistration);
m_pGCFrameRegistrations = pRegistration->m_pNext;
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ public abstract object ActivatorCreateInstance(

public abstract EnumInfo GetEnumInfo(Type type);

public abstract DelegateDynamicInvokeInfo GetDelegateDynamicInvokeInfo(Type type);
public abstract DynamicInvokeInfo GetDelegateDynamicInvokeInfo(Type type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public abstract class ExecutionEnvironment
//==============================================================================================
public abstract FieldAccessor CreateLiteralFieldAccessor(object value, RuntimeTypeHandle fieldTypeHandle);
public abstract EnumInfo GetEnumInfo(RuntimeTypeHandle typeHandle);
public abstract IntPtr GetDynamicInvokeThunk(MethodInvoker invoker, out IntPtr genericDictionary);
public abstract IntPtr GetDynamicInvokeThunk(MethodInvoker invoker);

//==============================================================================================
// Non-public methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,24 +364,18 @@ public static int GetHighestStaticThreadStaticIndex(TypeManagerHandle typeManage
public static object? CallDynamicInvokeMethod(
object? thisPtr,
IntPtr methodToCall,
IntPtr dynamicInvokeHelperMethod,
IntPtr dynamicInvokeHelperGenericDictionary,
MethodBase targetMethod,
DynamicInvokeInfo dynamicInvokeInfo,
object?[]? parameters,
BinderBundle binderBundle,
bool wrapInTargetInvocationException,
bool methodToCallIsThisCall)
bool wrapInTargetInvocationException)
{
object? result = InvokeUtils.CallDynamicInvokeMethod(
thisPtr,
methodToCall,
dynamicInvokeHelperMethod,
dynamicInvokeHelperGenericDictionary,
targetMethod,
dynamicInvokeInfo,
parameters,
binderBundle,
wrapInTargetInvocationException,
methodToCallIsThisCall);
wrapInTargetInvocationException);
System.Diagnostics.DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand Down Expand Up @@ -712,7 +706,7 @@ public static bool CanPrimitiveWiden(RuntimeTypeHandle srcType, RuntimeTypeHandl

public static object CheckArgument(object srcObject, RuntimeTypeHandle dstType, BinderBundle binderBundle)
{
return InvokeUtils.CheckArgument(srcObject, dstType, binderBundle);
return InvokeUtils.CheckArgument(srcObject, dstType.ToEETypePtr(), InvokeUtils.CheckArgumentSemantics.DynamicInvoke, binderBundle);
}

// FieldInfo.SetValueDirect() has a completely different set of rules on how to coerce the argument from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,5 @@ public static void ThrowArgumentOutOfRangeException()
{
throw new ArgumentOutOfRangeException();
}

public static void ThrowInvokeNullRefReturned()
{
throw new NullReferenceException(SR.NullReference_InvokeNullRefReturned);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<Compile Include="System\Reflection\Attribute.NativeAot.cs" />
<Compile Include="System\Reflection\Assembly.NativeAot.cs" />
<Compile Include="System\Reflection\BinderBundle.cs" />
<Compile Include="System\Reflection\DelegateDynamicInvokeInfo.cs" />
<Compile Include="System\Reflection\DynamicInvokeInfo.cs" />
<Compile Include="System\Reflection\Emit\AssemblyBuilder.cs" />
<Compile Include="System\Reflection\Emit\ConstructorBuilder.cs" />
<Compile Include="System\Reflection\Emit\CustomAttributeBuilder.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,10 @@ internal bool IsDynamicDelegate()
}
else
{
DelegateDynamicInvokeInfo invokeInfo = ReflectionAugments.ReflectionCoreCallbacks.GetDelegateDynamicInvokeInfo(GetType());
DynamicInvokeInfo dynamicInvokeInfo = ReflectionAugments.ReflectionCoreCallbacks.GetDelegateDynamicInvokeInfo(GetType());

object? result = InvokeUtils.CallDynamicInvokeMethod(m_firstParameter, m_functionPointer,
invokeInfo.InvokeThunk, invokeInfo.GenericDictionary, invokeInfo.InvokeMethod,
args, binderBundle: null, wrapInTargetInvocationException: true);
dynamicInvokeInfo, args, binderBundle: null, wrapInTargetInvocationException: true);
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand Down
Loading

0 comments on commit b08f4ee

Please sign in to comment.