Add test for unnmanaged delegates interop (#85903)
authorAndrii Kurdiumov <kant2002@gmail.com>
Mon, 8 May 2023 18:46:39 +0000 (00:46 +0600)
committerGitHub <noreply@github.com>
Mon, 8 May 2023 18:46:39 +0000 (11:46 -0700)
src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs
src/tests/nativeaot/SmokeTests/PInvoke/PInvokeNative.cpp

index 8ea60c0..92fc028 100644 (file)
@@ -266,6 +266,8 @@ namespace PInvokeTests
         [DllImport("PInvokeNative", CallingConvention = CallingConvention.StdCall)]
         internal static extern IntPtr GetFunctionPointer();
 
+        [DllImport("PInvokeNative", CallingConvention = CallingConvention.StdCall)]
+        internal static extern IntPtr GetNativeFuncFunctionPointer();
 
         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
         internal unsafe struct InlineString
@@ -344,6 +346,7 @@ namespace PInvokeTests
             TestForwardDelegateWithUnmanagedCallersOnly();
             TestDecimal();
             TestDifferentModopts();
+            TestFunctionPointers();
 
             return 100;
         }
@@ -677,6 +680,15 @@ namespace PInvokeTests
                 Marshal.GetDelegateForFunctionPointer<SetLastErrorFuncDelegate>(procAddress);
             funcDelegate(0x204);
             ThrowIfNotEquals(0x204, Marshal.GetLastWin32Error(), "Not match");
+               }
+               
+               private static unsafe void TestFunctionPointers()
+               {
+            IntPtr procAddress = GetNativeFuncFunctionPointer();
+            delegate* unmanaged[Cdecl] <int, int> unmanagedFuncDelegate =
+                (delegate* unmanaged[Cdecl] <int, int>)procAddress;
+            var result = unmanagedFuncDelegate(100);
+            ThrowIfNotEquals(1422, result, "Function pointer did not set expected error code");
         }
 
         static int Sum(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
@@ -1125,7 +1137,7 @@ namespace PInvokeTests
             Action a = Marshal.GetDelegateForFunctionPointer<Action>((IntPtr)(void*)(delegate* unmanaged<void>)&UnmanagedCallback);
             a();
         }
-        
+
         public static unsafe void TestDifferentModopts()
         {
             byte storage;
index 43f9db1..bd6bb32 100644 (file)
@@ -694,6 +694,15 @@ DLL_EXPORT void* __stdcall GetFunctionPointer()
     return (void*)&SetLastErrorFunc;
 }
 
+DLL_EXPORT int __cdecl NativeFunc(int errorCode)
+{
+    return errorCode + 1322;
+}
+DLL_EXPORT void* __stdcall GetNativeFuncFunctionPointer()
+{
+    return (void*)&NativeFunc;
+}
+
 typedef struct {
     int c;
     char inlineString[260];