NativeCallable bugfixes (#34335)
authorJan Kotas <jkotas@microsoft.com>
Tue, 31 Mar 2020 19:06:57 +0000 (12:06 -0700)
committerGitHub <noreply@github.com>
Tue, 31 Mar 2020 19:06:57 +0000 (12:06 -0700)
- Fix race condition in JIT_ReversePInvokeEnter
- Disable R2R for x86 on all platforms

src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs
src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs
src/coreclr/src/vm/i386/asmhelpers.S
src/coreclr/src/vm/jithelpers.cpp
src/coreclr/src/vm/jitinterface.cpp
src/coreclr/src/zap/zapinfo.cpp

index ebf41f59cd5137c557a6e4ff74897095712b751a..472b1050acb45a3f81e80d553ac50ce371615b21 100644 (file)
@@ -2921,8 +2921,7 @@ namespace Internal.JitInterface
             if (this.MethodBeingCompiled.IsNativeCallable)
             {
 #if READYTORUN
-                if (targetArchitecture == TargetArchitecture.X86
-                    && _compilation.TypeSystemContext.Target.OperatingSystem == TargetOS.Windows)
+                if (targetArchitecture == TargetArchitecture.X86)
                 {
                     throw new RequiresRuntimeJitException("ReadyToRun: Methods with NativeCallableAttribute not implemented");
                 }
index 1f477b034bb586842b56b32fd89cfacfefa5e37b..8aa4adc2799308bb371a820c0c6e9009e65f6fff 100644 (file)
@@ -1661,9 +1661,7 @@ namespace Internal.JitInterface
             pResult->methodFlags = FilterNamedIntrinsicMethodAttribs(pResult->methodFlags, methodToCall);
 
             var targetDetails = _compilation.TypeSystemContext.Target;
-            if (targetDetails.Architecture == TargetArchitecture.X86
-                && targetDetails.OperatingSystem == TargetOS.Windows
-                && targetMethod.IsNativeCallable)
+            if (targetDetails.Architecture == TargetArchitecture.X86 && targetMethod.IsNativeCallable)
             {
                 throw new RequiresRuntimeJitException("ReadyToRun: References to methods with NativeCallableAttribute not implemented");
             }
index dcc210c98bb37f9e5bd155a312ab2dfd8aaaf9ce..c318dc390309612e5f272c4ed8594f8ea6bbc6c7 100644 (file)
@@ -555,22 +555,6 @@ LEAF_ENTRY PrecodeFixupThunk, _TEXT
     jmp     C_FUNC(ThePreStub)
 LEAF_END PrecodeFixupThunk, _TEXT
 
-NESTED_ENTRY UMThunkStubRareDisable, _TEXT, NoHandler
-    push    eax
-    push    ecx
-
-    sub     esp, 12
-    push    eax          // Push the UMEntryThunk
-    push    ecx          // Push thread
-    CHECK_STACK_ALIGNMENT
-    call    C_FUNC(UMThunkStubRareDisableWorker)
-    add     esp, 12
-
-    pop     ecx
-    pop     eax
-    ret
-NESTED_END UMThunkStubRareDisable, _TEXT
-
 //
 // Used to get the current instruction pointer value
 //
index 8d64b2629fffa931d10bd10851a81005eb351d9d..658e4e6cdd07ed018fad696310dd72923be480e3 100644 (file)
@@ -5383,8 +5383,14 @@ NOINLINE static void JIT_ReversePInvokeEnterRare(ReversePInvokeFrame* frame)
     if (thread->PreemptiveGCDisabled())
         ReversePInvokeBadTransition();
 
-    thread->DisablePreemptiveGC();
     frame->currentThread = thread;
+
+    thread->DisablePreemptiveGC();
+}
+
+NOINLINE static void JIT_ReversePInvokeEnterRare2(ReversePInvokeFrame* frame)
+{
+    frame->currentThread->RareDisablePreemptiveGC();
 }
 
 EXTERN_C void JIT_ReversePInvokeEnter(ReversePInvokeFrame* frame)
@@ -5397,13 +5403,17 @@ EXTERN_C void JIT_ReversePInvokeEnter(ReversePInvokeFrame* frame)
     if (thread != NULL
         && !thread->PreemptiveGCDisabled())
     {
+        frame->currentThread = thread;
+
         // Manually inline the fast path in Thread::DisablePreemptiveGC().
         thread->m_fPreemptiveGCDisabled.StoreWithoutBarrier(1);
         if (g_TrapReturningThreads.LoadWithoutBarrier() == 0)
         {
-            frame->currentThread = thread;
             return;
         }
+
+        JIT_ReversePInvokeEnterRare2(frame);
+        return;
     }
 
     JIT_ReversePInvokeEnterRare(frame);
index 356ee2b4352e23b081a19f78bfc9092f5823a2b4..1f71c90307aab85a4e4f23f1586bab2faa10a01b 100644 (file)
@@ -9209,10 +9209,10 @@ void CEEInfo::getFunctionFixedEntryPoint(CORINFO_METHOD_HANDLE   ftn,
 
     pResult->accessType = IAT_VALUE;
 
-// Also see GetBaseCompileFlags() below for an additional check.
-#if defined(TARGET_X86) && defined(TARGET_WINDOWS) && !defined(CROSSGEN_COMPILE)
+#if defined(TARGET_X86) && !defined(CROSSGEN_COMPILE)
     // Deferring X86 support until a need is observed or
     // time permits investigation into all the potential issues.
+    // https://github.com/dotnet/runtime/issues/33582
     if (pMD->HasNativeCallableAttribute())
     {
         pResult->addr = (void*)COMDelegate::ConvertToCallback(pMD);
@@ -9221,12 +9221,9 @@ void CEEInfo::getFunctionFixedEntryPoint(CORINFO_METHOD_HANDLE   ftn,
     {
         pResult->addr = (void*)pMD->GetMultiCallableAddrOfCode();
     }
-
 #else
-
     pResult->addr = (void*)pMD->GetMultiCallableAddrOfCode();
-
-#endif // !(TARGET_X86 && TARGET_WINDOWS) || CROSSGEN_COMPILE
+#endif
 
     EE_TO_JIT_TRANSITION();
 }
@@ -12441,10 +12438,10 @@ CorJitResult CallCompileMethodWithSEHWrapper(EEJitManager *jitMgr,
          }
     }
 
-#if !defined(TARGET_X86) || !defined(TARGET_WINDOWS)
+#if !defined(TARGET_X86)
     if (ftn->HasNativeCallableAttribute())
         flags.Set(CORJIT_FLAGS::CORJIT_FLAG_REVERSE_PINVOKE);
-#endif // !TARGET_X86 || !TARGET_WINDOWS
+#endif // !TARGET_X86
 
     return flags;
 }
index d09204bddb806701823506993637d0aebb941588..f65ec7c8aaf1ff73875b4c461cf6fa85fadad1bb 100644 (file)
@@ -482,14 +482,14 @@ void ZapInfo::CompileMethod()
     }
 #endif
 
-#if defined(TARGET_X86) && defined(TARGET_WINDOWS)
+#ifdef TARGET_X86
     if (GetCompileInfo()->IsNativeCallableMethod(m_currentMethodHandle))
     {
         if (m_zapper->m_pOpt->m_verbose)
             m_zapper->Warning(W("ReadyToRun:  Methods with NativeCallableAttribute not implemented\n"));
         ThrowHR(E_NOTIMPL);
     }
-#endif // (TARGET_X86) && defined(TARGET_WINDOWS)
+#endif // TARGET_X86
 
     if (m_pImage->m_stats)
     {
@@ -2285,14 +2285,14 @@ void ZapInfo::getCallInfo(CORINFO_RESOLVED_TOKEN * pResolvedToken,
     }
 #endif
 
-#if defined(TARGET_X86) && defined(TARGET_WINDOWS)
+#ifdef TARGET_X86
     if (GetCompileInfo()->IsNativeCallableMethod(pResult->hMethod))
     {
         if (m_zapper->m_pOpt->m_verbose)
             m_zapper->Warning(W("ReadyToRun: References to methods with NativeCallableAttribute not implemented\n"));
         ThrowHR(E_NOTIMPL);
     }
-#endif // (TARGET_X86) && defined(TARGET_WINDOWS)
+#endif // TARGET_X86
 
     if (flags & CORINFO_CALLINFO_KINDONLY)
         return;