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

Block P/Invokes with UnmanagedCallersOnlyAttribute #38493

Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public static bool IsMarshallingRequired(MethodDesc targetMethod)
{
Debug.Assert(targetMethod.IsPInvoke);

if (targetMethod.IsUnmanagedCallersOnly)
return true;

PInvokeFlags flags = targetMethod.GetPInvokeMethodMetadata().Flags;

if (flags.SetLastError)
Expand Down
33 changes: 26 additions & 7 deletions src/coreclr/src/vm/dllimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3046,9 +3046,21 @@ BOOL NDirect::MarshalingRequired(MethodDesc *pMD, PCCOR_SIGNATURE pSig /*= NULL*
if (GetLCIDParameterIndex(pMD) != -1)
return TRUE;

// making sure that cctor has run may be handled by stub
if (pMD->IsNDirect() && ((NDirectMethodDesc *)pMD)->IsClassConstructorTriggeredByILStub())
return TRUE;
if (pMD->IsNDirect())
{
// A P/Invoke marked with UnmanagedCallersOnlyAttribute
// doesn't technically require marshalling. However, we
// don't support a DllImport with this attribute and we
// error out during IL Stub generation so we indicate that
// when checking if an IL Stub is needed.
if (pMD->HasUnmanagedCallersOnlyAttribute())
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
return TRUE;

NDirectMethodDesc* pNMD = (NDirectMethodDesc*)pMD;
// Make sure running cctor can be handled by stub
if (pNMD->IsClassConstructorTriggeredByILStub())
return TRUE;
}

callConv = sigInfo.GetCallConv();
}
Expand Down Expand Up @@ -4179,15 +4191,21 @@ static void CreateNDirectStubAccessMetadata(
}

int lcidArg = -1;

// Check if we have a MethodDesc to query for additional data.
if (pSigDesc->m_pMD != NULL)
{
lcidArg = GetLCIDParameterIndex(pSigDesc->m_pMD);
MethodDesc* pMD = pSigDesc->m_pMD;

// P/Invoke marked with UnmanagedCallersOnlyAttribute is not
// presently supported.
if (pMD->HasUnmanagedCallersOnlyAttribute())
COMPlusThrow(kNotSupportedException, IDS_EE_NDIRECT_UNSUPPORTED_SIG);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved

// Check to see if we need to do LCID conversion.
lcidArg = GetLCIDParameterIndex(pMD);
if (lcidArg != -1 && lcidArg > (*pNumArgs))
{
COMPlusThrow(kIndexOutOfRangeException, IDS_EE_INVALIDLCIDPARAM);
}
}

(*piLCIDArg) = lcidArg;
Expand Down Expand Up @@ -6621,7 +6639,8 @@ EXTERN_C LPVOID STDCALL NDirectImportWorker(NDirectMethodDesc* pMD)
//
INDEBUG(Thread *pThread = GetThread());
{
_ASSERTE(pMD->ShouldSuppressGCTransition() || pThread->GetFrame()->GetVTablePtr() == InlinedCallFrame::GetMethodFrameVPtr());
_ASSERTE(pMD->ShouldSuppressGCTransition()
|| pThread->GetFrame()->GetVTablePtr() == InlinedCallFrame::GetMethodFrameVPtr());

CONSISTENCY_CHECK(pMD->IsNDirect());
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public static class UnmanagedCallersOnlyDll
[DllImport(nameof(UnmanagedCallersOnlyDll))]
// Returns -1 if exception was throw and caught.
public static extern int CallManagedProcCatchException(IntPtr callbackProc, int n);

[UnmanagedCallersOnly]
[DllImport(nameof(UnmanagedCallersOnlyDll), EntryPoint = "DoesntExist")]
public static extern int PInvokeMarkedWithUnmanagedCallersOnly(int n);
}

private delegate int IntNativeMethodInvoker();
Expand All @@ -50,6 +54,7 @@ public static int Main(string[] args)
NegativeTest_InstantiatedGenericArguments();
NegativeTest_FromInstantiatedGenericClass();
TestUnmanagedCallersOnlyViaUnmanagedCalli();
TestPInvokeMarkedWithUnmanagedCallersOnly();

// Exception handling is only supported on Windows.
if (TestLibrary.Utilities.IsWindows)
Expand Down Expand Up @@ -706,4 +711,54 @@ .locals init (native int V_0)
Assert.AreEqual(CallbackThrowsErrorCode, e.HResult);
}
}

public static void TestPInvokeMarkedWithUnmanagedCallersOnly()
{
Console.WriteLine($"Running {nameof(TestPInvokeMarkedWithUnmanagedCallersOnly)}...");

// Call P/Invoke directly
Assert.Throws<NotSupportedException>(() => UnmanagedCallersOnlyDll.PInvokeMarkedWithUnmanagedCallersOnly(0));

// Call P/Invoke via reflection
var method = typeof(UnmanagedCallersOnlyDll).GetMethod(nameof(UnmanagedCallersOnlyDll.PInvokeMarkedWithUnmanagedCallersOnly));
Assert.Throws<NotSupportedException>(() => method.Invoke(null, BindingFlags.DoNotWrapExceptions, null, new[] { (object)0 }, null));

// Call P/Invoke as a function pointer
/*
void TestPInvokeMarkedWithUnmanagedCallersOnly_Throws()
{
.locals init (native int V_0)
IL_0000: nop
IL_0001: ldftn int UnmanagedCallersOnlyDll.PInvokeMarkedWithUnmanagedCallersOnly(int32)
IL_0007: stloc.0

IL_0008: ldc.i4 1234
IL_000d: ldloc.0
IL_000e: calli int32 stdcall(int32)

IL_0014: ret
}
*/

DynamicMethod testUnmanagedCallersOnly = new DynamicMethod("TestPInvokeMarkedWithUnmanagedCallersOnly_Throws", typeof(int), null, typeof(Program).Module);
ILGenerator il = testUnmanagedCallersOnly.GetILGenerator();
il.DeclareLocal(typeof(IntPtr));
il.Emit(OpCodes.Nop);

// Get native function pointer of the callback
il.Emit(OpCodes.Ldftn, method);
il.Emit(OpCodes.Stloc_0);

int n = 1234;

il.Emit(OpCodes.Ldc_I4, n);
il.Emit(OpCodes.Ldloc_0);
il.EmitCalli(OpCodes.Calli, CallingConvention.StdCall, typeof(int), new Type[] { typeof(int) });

il.Emit(OpCodes.Ret);

IntNativeMethodInvoker testNativeMethod = (IntNativeMethodInvoker)testUnmanagedCallersOnly.CreateDelegate(typeof(IntNativeMethodInvoker));

Assert.Throws<NotSupportedException>(() => testNativeMethod());
}
}