public static int Main(string[] args) { try { CheckGCMode.Initialize(&SuppressGCTransitionNative.SetIsInCooperativeModeFunction); int n = 1; n = Inline_NoGCTransition(n); n = Inline_GCTransition(n); n = NoInline_NoGCTransition(n); n = NoInline_GCTransition(n); n = Mixed(n); n = Mixed_TightLoop(n); n = Inline_NoGCTransition_FunctionPointer(n); n = Inline_GCTransition_FunctionPointer(n); n = NoInline_NoGCTransition_FunctionPointer(n); n = NoInline_GCTransition_FunctionPointer(n); n = CallAsFunctionPointer(n); n = ILStubCache_NoGCTransition_GCTransition(n); if (args.Length != 0 && args[0].Equals("ILStubCache", StringComparison.OrdinalIgnoreCase)) { // This test intentionally results in a fatal error, so only run when manually specified n = ILStubCache_GCTransition_NoGCTransition(n); } } catch (Exception e) { Console.WriteLine(e.ToString()); return(101); } return(100); }
public static int Main(string[] args) { try { DefaultDllImport_Blittable(); DefaultDllImport_NotBlittable(); WinapiDllImport_Blittable(); WinapiDllImport_NotBlittable(); UnsetPInvokeImpl_Blittable(); UnsetPInvokeImpl_NotBlittable(); // Following tests explicitly check GC mode when possible CheckGCMode.Initialize(&PInvokesCS.SetIsInCooperativeModeFunction); SuppressGCTransition_Blittable(); SuppressGCTransition_NotBlittable(); MatchingDllImport_Blittable(); MatchingDllImport_NotBlittable(); } catch (Exception e) { Console.WriteLine($"Test Failure: {e}"); return(101); } return(100); }
private static int NoInline_GCTransition_FunctionPointer(int expected) { Console.WriteLine($"{nameof(NoInline_GCTransition)} ({expected}) ..."); int n; bool ret = SuppressGCTransitionNative.GetNextUIntFunctionPointer_NoInline_GCTransition()(&n); Assert.Equal(expected, n); CheckGCMode.Validate(transitionSuppressed: false, ret); return(n + 1); }
private static int NoInline_NoGCTransition(int expected) { Console.WriteLine($"{nameof(NoInline_NoGCTransition)} ({expected}) ..."); int n; bool ret = SuppressGCTransitionNative.NextUInt_NoInline_NoGCTransition(&n); Assert.Equal(expected, n); CheckGCMode.Validate(transitionSuppressed: true, ret); return(n + 1); }
private static int Inline_GCTransition(int expected) { Console.WriteLine($"{nameof(Inline_GCTransition)} ({expected}) ..."); int n; int ret = SuppressGCTransitionNative.NextUInt_Inline_GCTransition(&n); Assert.AreEqual(expected, n); CheckGCMode.Validate(transitionSuppressed: false, ret); return(n + 1); }
private static void SuppressGCTransition_NotBlittable() { Console.WriteLine($"Running {nameof(SuppressGCTransition_NotBlittable)}..."); const int a = 11; const int expected = a * 2; { Console.WriteLine($" -- default: SuppressGCTransition, UnmanagedCallConv()"); int b; bool ret = PInvokesCS.SuppressGCTransition.Default.NotBlittable_Double_DefaultUnmanagedCallConv_SuppressGCAttr(a, &b); Assert.AreEqual(expected, b); CheckGCMode.Validate(transitionSuppressed: true, ret); } { Console.WriteLine($" -- default: UnmanagedCallConv(suppressgctransition)"); int b; bool ret = PInvokesCS.SuppressGCTransition.Default.NotBlittable_Double_DefaultUnmanagedCallConv_SuppressGC(a, &b); Assert.AreEqual(expected, b); CheckGCMode.Validate(transitionSuppressed: true, ret); } { Console.WriteLine($" -- cdecl: SuppressGCTransition, UnmanagedCallConv(cdecl)"); int b; bool ret = PInvokesCS.SuppressGCTransition.Cdecl.NotBlittable_Double_CdeclUnmanagedCallConv_SuppressGCAttr(a, &b); Assert.AreEqual(expected, b); CheckGCMode.Validate(transitionSuppressed: true, ret); } { Console.WriteLine($" -- cdecl: UnmanagedCallConv(cdecl, suppressgctransition)"); int b; bool ret = PInvokesCS.SuppressGCTransition.Cdecl.NotBlittable_Double_CdeclUnmanagedCallConv_SuppressGC(a, &b); Assert.AreEqual(expected, b); CheckGCMode.Validate(transitionSuppressed: true, ret); } { Console.WriteLine($" -- stdcall: SuppressGCTransition, UnmanagedCallConv(stdcall)"); int b; bool ret = PInvokesCS.SuppressGCTransition.Stdcall.NotBlittable_Double_StdcallUnmanagedCallConv_SuppressGCAttr(a, &b); Assert.AreEqual(expected, b); CheckGCMode.Validate(transitionSuppressed: true, ret); } { Console.WriteLine($" -- stdcall: UnmanagedCallConv(stdcall, suppressgctransition)"); int b; bool ret = PInvokesCS.SuppressGCTransition.Stdcall.NotBlittable_Double_StdcallUnmanagedCallConv_SuppressGC(a, &b); Assert.AreEqual(expected, b); CheckGCMode.Validate(transitionSuppressed: true, ret); } }
private static int CallAsFunctionPointer(int expected) { Console.WriteLine($"{nameof(CallAsFunctionPointer)} ({expected}) ..."); IntPtr fptr = SuppressGCTransitionNative.GetNextUIntFunctionPointer(); int n = 0; int * pn = &n; object boxedN = Pointer.Box(pn, typeof(int *)); MethodInfo callNextUInt = typeof(FunctionPointer).GetMethod("Call_NextUInt"); int ret = (int)callNextUInt.Invoke(null, new object[] { fptr, boxedN }); Assert.Equal(expected, n); CheckGCMode.Validate(transitionSuppressed: false, ret); return(n + 1); }
private static void MatchingDllImport_NotBlittable() { Console.WriteLine($"Running {nameof(MatchingDllImport_NotBlittable)}..."); // Calling convention is set in DllImport. UnmanagedCallConv should be ignored, const int a = 11; const int expected = a * 2; { // Should work despite the mismatched value in UnmanagedCallConv Console.WriteLine($" -- cdecl: UnmanagedCallConv(stdcall)"); int b; PInvokesCS.MatchingDllImport.Cdecl.NotBlittable_Double_StdcallUnmanagedCallConv(a, &b); Assert.AreEqual(expected, b); } { // Should not suppress GC transition Console.WriteLine($" -- cdecl: UnmanagedCallConv(suppressgctransition)"); int b; bool ret = PInvokesCS.MatchingDllImport.Cdecl.NotBlittable_Double_SuppressGCUnmanagedCallConv(a, &b); Assert.AreEqual(expected, b); CheckGCMode.Validate(transitionSuppressed: false, ret); } { // Should work despite the mismatched value in UnmanagedCallConv Console.WriteLine($" -- stdcall: UnmanagedCallConv(cdecl)"); int b; PInvokesCS.MatchingDllImport.Stdcall.NotBlittable_Double_CdeclUnmanagedCallConv(a, &b); Assert.AreEqual(expected, b); } { // Should not suppress GC transition Console.WriteLine($" -- stdcall: UnmanagedCallConv(suppressgctransition)"); int b; bool ret = PInvokesCS.MatchingDllImport.Stdcall.NotBlittable_Double_SuppressGCUnmanagedCallConv(a, &b); Assert.AreEqual(expected, b); CheckGCMode.Validate(transitionSuppressed: false, ret); } }
private static int Mixed(int expected) { Console.WriteLine($"{nameof(Mixed)} ({expected}) ..."); int n; { bool ret = SuppressGCTransitionNative.NextUInt_NoInline_GCTransition(&n); Assert.AreEqual(expected++, n); CheckGCMode.Validate(transitionSuppressed: false, ret); ret = SuppressGCTransitionNative.NextUInt_NoInline_NoGCTransition(&n); Assert.AreEqual(expected++, n); CheckGCMode.Validate(transitionSuppressed: true, ret); } { int ret = SuppressGCTransitionNative.NextUInt_Inline_GCTransition(&n); Assert.AreEqual(expected++, n); CheckGCMode.Validate(transitionSuppressed: false, ret); ret = SuppressGCTransitionNative.NextUInt_Inline_NoGCTransition(&n); Assert.AreEqual(expected++, n); CheckGCMode.Validate(transitionSuppressed: true, ret); } return(n + 1); }