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 all 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
10 changes: 8 additions & 2 deletions src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// 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 )
}
.assembly extern System.Runtime.InteropServices
{
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
}

.assembly InvalidCSharp { }

Expand Down
38 changes: 9 additions & 29 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 @@ -31,26 +32,6 @@ public static class UnmanagedCallersOnlyDll
public static extern int CallManagedProcCatchException(IntPtr callbackProc, 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));
}

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

private delegate int IntNativeMethodInvoker();
private delegate void NativeMethodInvoker();

Expand Down Expand Up @@ -349,7 +330,7 @@ public static void NegativeTest_ViaDelegate()
// Local function to delay exception thrown during JIT
void CallAsDelegate()
{
Func<int, int> invoker = (Func<int, int>)GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("GetDoubleDelegate").Invoke(null, BindingFlags.DoNotWrapExceptions, null, null, null);
Func<int, int> invoker = CallingUnmanagedCallersOnlyDirectly.GetDoubleDelegate();
invoker(0);
}
}
Expand All @@ -364,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 @@ -381,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 @@ -466,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 @@ -502,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 @@ -544,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 @@ -718,11 +699,10 @@ public static void TestPInvokeMarkedWithUnmanagedCallersOnly()
Console.WriteLine($"Running {nameof(TestPInvokeMarkedWithUnmanagedCallersOnly)}...");

// Call P/Invoke directly
var pInvokeWrapperMethod = GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("CallPInvokeMarkedWithUnmanagedCallersOnly");
Assert.Throws<NotSupportedException>(() => pInvokeWrapperMethod.Invoke(null, BindingFlags.DoNotWrapExceptions, null, new[] { (object)0 }, null));
Assert.Throws<NotSupportedException>(() => CallingUnmanagedCallersOnlyDirectly.CallPInvokeMarkedWithUnmanagedCallersOnly(0));

// Call P/Invoke via reflection
var method = GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("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 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