Skip to content

Commit

Permalink
NativeCallable bugfixes (#34335)
Browse files Browse the repository at this point in the history
- Fix race condition in JIT_ReversePInvokeEnter
- Disable R2R for x86 on all platforms
  • Loading branch information
jkotas authored Mar 31, 2020
1 parent 57eb3bd commit a9ace0f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2921,8 +2921,7 @@ private uint getJitFlags(ref CORJIT_FLAGS flags, uint sizeInBytes)
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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1661,9 +1661,7 @@ private void getCallInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_RESO
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");
}
Expand Down
16 changes: 0 additions & 16 deletions src/coreclr/src/vm/i386/asmhelpers.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
14 changes: 12 additions & 2 deletions src/coreclr/src/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
13 changes: 5 additions & 8 deletions src/coreclr/src/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:/dotnet/runtime/issues/33582
if (pMD->HasNativeCallableAttribute())
{
pResult->addr = (void*)COMDelegate::ConvertToCallback(pMD);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/src/zap/zapinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a9ace0f

Please sign in to comment.