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

Move some more UnmanagedCallersOnly tests to IL now that they're invalid C# #43366

Merged
merged 4 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
104 changes: 101 additions & 3 deletions src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

.assembly extern System.Runtime { }
.assembly extern System.Runtime.InteropServices { }
.assembly extern System.Runtime
{
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
.ver 5:0:0:0
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
}
.assembly extern System.Runtime.InteropServices
{
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
.ver 5:0:0:0
}

.assembly InvalidCSharp { }

.class public auto ansi beforefieldinit InvalidCSharp.GenericClass`1<T>
extends System.Object
extends [System.Runtime]System.Object
{
.method public hidebysig static
void CallbackMethod (
Expand Down Expand Up @@ -73,3 +81,93 @@
IL_0006: ret
}
}

.class public auto ansi beforefieldinit InvalidCSharp.CallingUnmanagedCallersOnlyDirectly
extends [System.Runtime]System.Object
{
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
IL_0006: ret
}

.method public hidebysig static
int32 ManagedDoubleCallback (
int32 n
) cil managed
{
.custom instance void [System.Runtime.InteropServices]System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute::.ctor() = (
01 00 00 00
)
.maxstack 1
.locals init (
[0] int32
)

IL_0000: nop
IL_0001: ldarg.0
IL_0002: call int32 InvalidCSharp.CallingUnmanagedCallersOnlyDirectly::DoubleImpl(int32)
IL_0007: stloc.0
IL_0008: br.s IL_000a

IL_000a: ldloc.0
IL_000b: ret
}

.method private hidebysig static
int32 DoubleImpl (
int32 n
) cil managed
{
.maxstack 2
.locals init (
[0] int32
)

IL_0000: nop
IL_0001: ldc.i4.2
IL_0002: ldarg.0
IL_0003: mul
IL_0004: stloc.0
IL_0005: br.s IL_0007

IL_0007: ldloc.0
IL_0008: ret
}

.method public hidebysig static
class [System.Runtime]System.Func`2<int32, int32> GetDoubleDelegate () cil managed
{
.maxstack 8

IL_0000: ldnull
IL_0001: ldftn int32 InvalidCSharp.CallingUnmanagedCallersOnlyDirectly::ManagedDoubleCallback(int32)
IL_0007: newobj instance void class [System.Runtime]System.Func`2<int32, int32>::.ctor(object, native int)
IL_000c: ret
}

.method public hidebysig static pinvokeimpl("UnmanagedCallersOnlyDll" as "DoesntExist" winapi)
int32 PInvokeMarkedWithUnmanagedCallersOnly (
int32 n
) cil managed preservesig
{
.custom instance void [System.Runtime.InteropServices]System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute::.ctor() = (
01 00 00 00
)
}

.method public hidebysig static
int32 CallPInvokeMarkedWithUnmanagedCallersOnly (
int32 n
) cil managed
{
.maxstack 1

ldarg.0
call int32 InvalidCSharp.CallingUnmanagedCallersOnlyDirectly::PInvokeMarkedWithUnmanagedCallersOnly(int32)
ret
}
}
37 changes: 10 additions & 27 deletions src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Runtime.InteropServices;
using System.Threading;
using TestLibrary;
using InvalidCSharp;

public class Program
{
Expand All @@ -29,24 +30,6 @@ 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 const string InvalidCSharpAssemblyName = "InvalidCSharp";

public static Type GetCallbacksType()
{
var asm = Assembly.Load(InvalidCSharpAssemblyName);
return asm.GetType("InvalidCSharp.Callbacks");
}

public static Type GetGenericClassOfIntType()
{
var asm = Assembly.Load(InvalidCSharpAssemblyName);
return asm.GetType("InvalidCSharp.GenericClass`1").MakeGenericType(typeof(int));
}

private delegate int IntNativeMethodInvoker();
Expand Down Expand Up @@ -347,7 +330,7 @@ public static void NegativeTest_ViaDelegate()
// Local function to delay exception thrown during JIT
void CallAsDelegate()
{
Func<int, int> invoker = ManagedDoubleCallback;
Func<int, int> invoker = CallingUnmanagedCallersOnlyDirectly.GetDoubleDelegate();
invoker(0);
}
}
Expand All @@ -362,7 +345,7 @@ void TestUnmanagedCallersOnlyNonStatic()
{
.locals init ([0] native int ptr)
nop
ldftn int GetCallbacksType().CallbackNonStatic(int)
ldftn int typeof(Callbacks).CallbackNonStatic(int)
stloc.0

ldloc.0
Expand All @@ -379,7 +362,7 @@ ldftn int GetCallbacksType().CallbackNonStatic(int)
il.Emit(OpCodes.Nop);

// Get native function pointer of the callback
il.Emit(OpCodes.Ldftn, GetCallbacksType().GetMethod("CallbackNonStatic"));
il.Emit(OpCodes.Ldftn, typeof(Callbacks).GetMethod("CallbackNonStatic"));
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);

Expand Down Expand Up @@ -464,7 +447,7 @@ .locals init ([0] native int ptr)
il.Emit(OpCodes.Nop);

// Get native function pointer of the callback
il.Emit(OpCodes.Ldftn, GetCallbacksType().GetMethod("CallbackMethodGeneric"));
il.Emit(OpCodes.Ldftn, typeof(Callbacks).GetMethod("CallbackMethodGeneric"));
il.Emit(OpCodes.Stloc_0);

il.Emit(OpCodes.Ret);
Expand Down Expand Up @@ -500,7 +483,7 @@ ldftn void InvalidCSharp.Callbacks.CallbackMethodGeneric(int)
il.Emit(OpCodes.Nop);

// Get native function pointer of the instantiated generic callback
il.Emit(OpCodes.Ldftn, GetCallbacksType().GetMethod("CallbackMethodGeneric").MakeGenericMethod(new [] { typeof(int) }));
il.Emit(OpCodes.Ldftn, typeof(Callbacks).GetMethod("CallbackMethodGeneric").MakeGenericMethod(new [] { typeof(int) }));
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);

Expand Down Expand Up @@ -542,7 +525,7 @@ .locals init ([0] native int ptr)
il.Emit(OpCodes.Nop);

// Get native function pointer of the callback from the instantiated generic class.
il.Emit(OpCodes.Ldftn, GetGenericClassOfIntType().GetMethod("CallbackMethod"));
il.Emit(OpCodes.Ldftn, typeof(GenericClass<int>).GetMethod("CallbackMethod"));
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);

Expand Down Expand Up @@ -716,10 +699,10 @@ public static void TestPInvokeMarkedWithUnmanagedCallersOnly()
Console.WriteLine($"Running {nameof(TestPInvokeMarkedWithUnmanagedCallersOnly)}...");

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

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

// Call P/Invoke as a function pointer
Expand All @@ -728,7 +711,7 @@ void TestPInvokeMarkedWithUnmanagedCallersOnly_Throws()
{
.locals init (native int V_0)
IL_0000: nop
IL_0001: ldftn int UnmanagedCallersOnlyDll.PInvokeMarkedWithUnmanagedCallersOnly(int32)
IL_0001: ldftn int InvalidCSharp.CallingUnmanagedCallersOnlyDirectly::PInvokeMarkedWithUnmanagedCallersOnly(int32)
IL_0007: stloc.0

IL_0008: ldc.i4 1234
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="UnmanagedCallersOnlyTest.cs" />
Expand Down