From 96073fd52739ecd8a97be800f23e4ebe5252fddd Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Mon, 9 Nov 2020 10:35:33 -0800 Subject: [PATCH] Move some more UnmanagedCallersOnly tests to IL now that they're invalid C# (#43366) --- .../UnmanagedCallersOnly/InvalidCallbacks.il | 10 ++++-- .../UnmanagedCallersOnlyTest.cs | 38 +++++----------------- .../UnmanagedCallersOnlyTest.csproj | 1 + 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il b/src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il index 9eac369..f85723a 100644 --- a/src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il +++ b/src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il @@ -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 { } diff --git a/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs b/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs index 1ebe513..93ab29a 100644 --- a/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs +++ b/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs @@ -9,6 +9,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using TestLibrary; +using InvalidCSharp; public class Program { @@ -31,26 +32,6 @@ public class Program 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(); @@ -349,7 +330,7 @@ public class Program // Local function to delay exception thrown during JIT void CallAsDelegate() { - Func invoker = (Func)GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("GetDoubleDelegate").Invoke(null, BindingFlags.DoNotWrapExceptions, null, null, null); + Func invoker = CallingUnmanagedCallersOnlyDirectly.GetDoubleDelegate(); invoker(0); } } @@ -364,7 +345,7 @@ public class Program { .locals init ([0] native int ptr) nop - ldftn int GetCallbacksType().CallbackNonStatic(int) + ldftn int typeof(Callbacks).CallbackNonStatic(int) stloc.0 ldloc.0 @@ -381,7 +362,7 @@ public class Program 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); @@ -466,7 +447,7 @@ public class Program 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); @@ -502,7 +483,7 @@ public class Program 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); @@ -544,7 +525,7 @@ public class Program 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).GetMethod("CallbackMethod")); il.Emit(OpCodes.Stloc_0); il.Emit(OpCodes.Ldloc_0); @@ -718,11 +699,10 @@ public class Program Console.WriteLine($"Running {nameof(TestPInvokeMarkedWithUnmanagedCallersOnly)}..."); // Call P/Invoke directly - var pInvokeWrapperMethod = GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("CallPInvokeMarkedWithUnmanagedCallersOnly"); - Assert.Throws(() => pInvokeWrapperMethod.Invoke(null, BindingFlags.DoNotWrapExceptions, null, new[] { (object)0 }, null)); + Assert.Throws(() => CallingUnmanagedCallersOnlyDirectly.CallPInvokeMarkedWithUnmanagedCallersOnly(0)); // Call P/Invoke via reflection - var method = GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("PInvokeMarkedWithUnmanagedCallersOnly"); + var method = typeof(CallingUnmanagedCallersOnlyDirectly).GetMethod(nameof(CallingUnmanagedCallersOnlyDirectly.PInvokeMarkedWithUnmanagedCallersOnly)); Assert.Throws(() => method.Invoke(null, BindingFlags.DoNotWrapExceptions, null, new[] { (object)0 }, null)); // Call P/Invoke as a function pointer diff --git a/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.csproj b/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.csproj index 170fac3..72c30ff 100644 --- a/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.csproj +++ b/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.csproj @@ -1,6 +1,7 @@ Exe + true -- 2.7.4