@@ -969,6 +969,11 @@ public static void RunInterpreterTests()
969969
970970 Console . WriteLine ( "IntPtr.Zero: {0}, UIntPtr.Zero: {1}" , IntPtr . Zero , UIntPtr . Zero ) ;
971971
972+ Console . WriteLine ( "TestPInvoke" ) ;
973+ if ( ! TestPInvoke ( ) )
974+ Environment . FailFast ( null ) ;
975+
976+ // For stackwalking validation
972977 System . GC . Collect ( ) ;
973978
974979 Console . WriteLine ( "All tests passed successfully!" ) ;
@@ -2383,6 +2388,99 @@ static object BoxedSubtraction(object lhs, object rhs)
23832388 return ( int ) lhs - ( int ) rhs ;
23842389 }
23852390
2391+ [ DllImport ( "pinvoke" , CallingConvention = CallingConvention . Cdecl ) ]
2392+ public static extern int sumTwoInts ( int x , int y ) ;
2393+ [ DllImport ( "pinvoke" , CallingConvention = CallingConvention . Cdecl ) ]
2394+ public static extern double sumTwoDoubles ( double x , double y ) ;
2395+ [ DllImport ( "pinvoke" , CallingConvention = CallingConvention . Cdecl , CharSet = CharSet . Ansi ) ]
2396+ public static extern int writeToStdout ( string s ) ;
2397+ [ DllImport ( "missingLibrary" , CallingConvention = CallingConvention . Cdecl ) ]
2398+ public static extern void missingPInvoke ( ) ;
2399+ [ DllImport ( "missingLibrary" , CallingConvention = CallingConvention . Cdecl ) ]
2400+ public static extern void missingPInvokeWithMarshaling ( string s ) ;
2401+
2402+ public static bool TestPInvoke ( )
2403+ {
2404+ if ( sumTwoInts ( 1 , 2 ) != 3 )
2405+ return false ;
2406+
2407+ double summed = sumTwoDoubles ( 1 , 2 ) ;
2408+ if ( summed != 3 )
2409+ return false ;
2410+
2411+ // Test marshaling wrappers
2412+ writeToStdout ( "Hello world from pinvoke.dll!writeToStdout\n " ) ;
2413+
2414+ /* fails, with output:
2415+ Assert failure(PID 32748 [0x00007fec], Thread: 24256 [0x5ec0]): pMD == codeInfo.GetMethodDesc()
2416+
2417+ CORECLR! AppendExceptionStackFrame + 0x331 (0x00007ff9`85879b71)
2418+ SYSTEM.PRIVATE.CORELIB! <no symbol> + 0x0 (0x00007ff9`80d91f30)
2419+ SYSTEM.PRIVATE.CORELIB! <no symbol> + 0x0 (0x00007ff9`80d926b7)
2420+ SYSTEM.PRIVATE.CORELIB! <no symbol> + 0x0 (0x00007ff9`80d92289)
2421+ CORECLR! CallDescrWorkerInternal + 0x83 (0x00007ff9`859811c3)
2422+ CORECLR! CallDescrWorkerWithHandler + 0x130 (0x00007ff9`854755c0)
2423+ CORECLR! DispatchCallSimple + 0x26C (0x00007ff9`8547655c)
2424+ CORECLR! DispatchManagedException + 0x388 (0x00007ff9`85872998)
2425+ CORECLR! DispatchManagedException + 0x67 (0x00007ff9`858725a7)
2426+ CORECLR! UnwindAndContinueRethrowHelperAfterCatch + 0x1F8 (0x00007ff9`851be5e8)
2427+ File: Z:\runtime\src\coreclr\vm\exceptionhandling.cpp:3032
2428+ Image: Z:\runtime\artifacts\tests\coreclr\windows.x64.Checked\Tests\Core_Root\corerun.exe
2429+
2430+ pMD is TestPInvoke (correct) and codeInfo.GetMethodDesc() is Main (wrong)
2431+ */
2432+ /*
2433+ bool caught = false;
2434+ try {
2435+ Console.WriteLine("calling missingPInvoke");
2436+ missingPInvoke();
2437+ return false;
2438+ } catch (DllNotFoundException) {
2439+ Console.WriteLine("caught #1");
2440+ caught = true;
2441+ }
2442+
2443+ if (!caught)
2444+ return false;
2445+ */
2446+
2447+ /* fails, with output:
2448+ calling missingPInvokeWithMarshaling
2449+ caught #2
2450+
2451+ Assert failure(PID 59772 [0x0000e97c], Thread: 24864 [0x6120]): ohThrowable
2452+
2453+ CORECLR! PreStubWorker$catch$10 + 0x93 (0x00007ff9`580972b3)
2454+ CORECLR! CallSettingFrame_LookupContinuationIndex + 0x20 (0x00007ff9`57f32e70)
2455+ CORECLR! _FrameHandler4::CxxCallCatchBlock + 0x1DE (0x00007ff9`57f1e83e)
2456+ NTDLL! RtlCaptureContext2 + 0x4A6 (0x00007ffa`b7e46606)
2457+ CORECLR! PreStubWorker + 0x4F8 (0x00007ff9`5789dd78)
2458+ CORECLR! ThePreStub + 0x55 (0x00007ff9`57ec29c5)
2459+ CORECLR! CallJittedMethodRetVoid + 0x14 (0x00007ff9`57ec0f34)
2460+ CORECLR! InvokeCompiledMethod + 0x5D7 (0x00007ff9`57afaf67)
2461+ CORECLR! InterpExecMethod + 0x84BB (0x00007ff9`57af68cb)
2462+ CORECLR! ExecuteInterpretedMethod + 0x11B (0x00007ff9`5789c77b)
2463+ File: Z:\runtime\src\coreclr\vm\prestub.cpp:1965
2464+ Image: Z:\runtime\artifacts\tests\coreclr\windows.x64.Checked\Tests\Core_Root\corerun.exe
2465+ */
2466+ /*
2467+ bool caught2 = false;
2468+ try {
2469+ Console.WriteLine("calling missingPInvokeWithMarshaling");
2470+ missingPInvokeWithMarshaling("test");
2471+ return false;
2472+ } catch (DllNotFoundException) {
2473+ Console.WriteLine("caught #2");
2474+ caught2 = true;
2475+ }
2476+
2477+ if (!caught2)
2478+ return false;
2479+ */
2480+
2481+ return true ;
2482+ }
2483+
23862484 public static bool TestArray ( )
23872485 {
23882486 // sbyte
0 commit comments