Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NativeAOT] Reflection Invoke refactoring #73131

Merged
merged 22 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -360,32 +360,6 @@ public static int GetHighestStaticThreadStaticIndex(TypeManagerHandle typeManage

public static unsafe int ObjectHeaderSize => sizeof(EETypePtr);

[DebuggerGuidedStepThroughAttribute]
public static object? CallDynamicInvokeMethod(
object? thisPtr,
IntPtr methodToCall,
IntPtr dynamicInvokeHelperMethod,
IntPtr dynamicInvokeHelperGenericDictionary,
MethodBase targetMethod,
object?[]? parameters,
BinderBundle binderBundle,
bool wrapInTargetInvocationException,
bool methodToCallIsThisCall)
{
object? result = InvokeUtils.CallDynamicInvokeMethod(
thisPtr,
methodToCall,
dynamicInvokeHelperMethod,
dynamicInvokeHelperGenericDictionary,
targetMethod,
parameters,
binderBundle,
wrapInTargetInvocationException,
methodToCallIsThisCall);
System.Diagnostics.DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}

public static unsafe void EnsureClassConstructorRun(IntPtr staticClassConstructionContext)
{
StaticClassConstructionContext* context = (StaticClassConstructionContext*)staticClassConstructionContext;
Expand Down Expand Up @@ -712,7 +686,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,10 +276,9 @@ 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,
object? result = dynamicInvokeInfo.Invoke(m_firstParameter, m_functionPointer,
args, binderBundle: null, wrapInTargetInvocationException: true);
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
Expand Down
Loading