Fix ProfileEnter / ProfileLeave (#88134)
authorJan Vorlicek <janvorli@microsoft.com>
Fri, 28 Jul 2023 07:54:14 +0000 (09:54 +0200)
committerGitHub <noreply@github.com>
Fri, 28 Jul 2023 07:54:14 +0000 (09:54 +0200)
* Fix ProfileEnter / ProfileLeave

These two functions can be entered in preemptive mode for
UnmanagedCallersOnly methods and also on thread that was not known to
runtime. This change switches to cooperative mode if the thread is known
to runtime and early outs when the thread was not known.

* Add runtime thread setup if it doesn't exist

* Undo the thread struct setup for ProfileLeave

src/coreclr/vm/proftoeeinterfaceimpl.cpp

index 845296f..1f2e4f2 100644 (file)
@@ -10754,10 +10754,23 @@ void __stdcall ProfilerUnmanagedToManagedTransitionMD(MethodDesc *pMD,
 // These do a lot of work for us, setting up Frames, gathering arg info and resolving generics.
   //*******************************************************************************************
 
-HCIMPL2(EXTERN_C void, ProfileEnter, UINT_PTR clientData, void * platformSpecificHandle)
+HCIMPL2_RAW(EXTERN_C void, ProfileEnter, UINT_PTR clientData, void * platformSpecificHandle)
+GCX_COOP_THREAD_EXISTS(GET_THREAD());
+HCIMPL_PROLOG(ProfileEnter)
 {
     FCALL_CONTRACT;
 
+    if (GetThreadNULLOk() == NULL)
+    {
+        Thread *pThread = SetupThreadNoThrow();
+        if (pThread == NULL)
+        {
+            return;
+        }
+    }
+
+    GCX_COOP();
+
 #ifdef PROFILING_SUPPORTED
 
 #ifdef PROF_TEST_ONLY_FORCE_ELT
@@ -10924,7 +10937,9 @@ LExit:
 }
 HCIMPLEND
 
-HCIMPL2(EXTERN_C void, ProfileLeave, UINT_PTR clientData, void * platformSpecificHandle)
+HCIMPL2_RAW(EXTERN_C void, ProfileLeave, UINT_PTR clientData, void * platformSpecificHandle)
+GCX_COOP();
+HCIMPL_PROLOG(ProfileLeave)
 {
     FCALL_CONTRACT;