//
// Thus, ICF::ExceptionUnwind should not do anything significant. If any of these assumptions
// break, then the next best thing will be to make the JIT link/unlink the frame dynamically.
+ //
+ // If the current method executing is from precompiled ReadyToRun code, then the above is no longer
+ // applicable because each PInvoke is wrapped by calls to the JIT_PInvokeBegin and JIT_PInvokeEnd
+ // helpers, which push and pop the ICF to the current thread. Unlike jitted code, the ICF is not
+ // linked during the method prolog, and unlinked at the epilog (it looks more like the X64 case).
+ // In that case, we need to unlink the ICF during unwinding here.
if (fTargetUnwind && (pFrame->GetVTablePtr() == InlinedCallFrame::GetMethodFrameVPtr()))
{
((UINT_PTR)pICF < uCallerSP))
{
pICFForUnwindTarget = pFrame;
+
+ // When unwinding an exception in ReadyToRun, the JIT_PInvokeEnd helper which unlinks the ICF from
+ // the thread will be skipped. This is because unlike jitted code, each pinvoke is wrapped by calls
+ // to the JIT_PInvokeBegin and JIT_PInvokeEnd helpers, which push and pop the ICF on the thread. The
+ // ICF is not linked at the method prolog and unlined at the epilog when running R2R code. Since the
+ // JIT_PInvokeEnd helper will be skipped, we need to unlink the ICF here. If the executing method
+ // has another pinovoke, it will re-link the ICF again when the JIT_PInvokeBegin helper is called
+
+ if (ExecutionManager::IsReadyToRunCode(((InlinedCallFrame*)pFrame)->m_pCallerReturnAddress))
+ {
+ pICFForUnwindTarget = pICFForUnwindTarget->Next();
+ }
}
}
-#endif // defined(_TARGET_ARM_)
+#endif // USE_PER_FRAME_PINVOKE_INIT
cfThisFrame.CheckGSCookies();
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+using System;
+using System.IO;
+using System.Reflection;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using System.Threading;
+
+using Console = Internal.Console;
+
+[assembly: DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
+public class CallbackStressTest
+{
+ static volatile bool s_RunGC = true;
+
+ static int s_LoopCounter = 25;
+ static int s_FinallyCalled = 0;
+ static int s_CatchCalled = 0;
+ static int s_OtherExceptionCatchCalled = 0;
+ static int s_SEHExceptionCatchCalled = 0;
+ static int s_WrongPInvokesExecuted = 0;
+ static int s_PInvokesExecuted = 0;
+
+ public static void Collector()
+ {
+ while(s_RunGC) GC.Collect();
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public static void SetResolve()
+ {
+ Console.WriteLine("Setting PInvoke Resolver");
+
+ DllImportResolver resolver =
+ (string libraryName, Assembly asm, DllImportSearchPath? dllImportSearchPath) =>
+ {
+ if (dllImportSearchPath != DllImportSearchPath.System32)
+ {
+ Console.WriteLine($"Unexpected dllImportSearchPath: {dllImportSearchPath.ToString()}");
+ throw new ArgumentException();
+ }
+
+ return NativeLibrary.Load("ResolveLib", asm, null);
+ };
+
+ NativeLibrary.SetDllImportResolver(
+ Assembly.GetExecutingAssembly(),
+ resolver);
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public static void DoCall()
+ {
+ NativeSum(10, 10);
+ s_WrongPInvokesExecuted++;
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public static void DoCallTryCatch(bool shouldThrow)
+ {
+ try
+ {
+ var a = NativeSum(10, 10);
+ if (shouldThrow)
+ s_WrongPInvokesExecuted++;
+ else
+ s_PInvokesExecuted += (a == 20 ? 1 : 0);
+ }
+ catch (DllNotFoundException) { s_CatchCalled++; }
+
+ throw new ArgumentException();
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public static void DoCallTryRethrowInCatch()
+ {
+ try
+ {
+ var a = NativeSum(10, 10);
+ s_WrongPInvokesExecuted++;
+ }
+ catch (DllNotFoundException) { s_CatchCalled++; throw; }
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public static void DoCallTryRethrowDifferentExceptionInCatch()
+ {
+ try
+ {
+ var a = NativeSum(10, 10);
+ s_WrongPInvokesExecuted++;
+ }
+ catch (DllNotFoundException) { s_CatchCalled++; throw new InvalidOperationException(); }
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public static void DoCallTryFinally()
+ {
+ try
+ {
+ NativeSum(10, 10);
+ s_WrongPInvokesExecuted++;
+ }
+ finally { s_FinallyCalled++; }
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public static void ManualRaiseException()
+ {
+#if WINDOWS
+ try
+ {
+ RaiseException(5, 0, 0, IntPtr.Zero);
+ }
+ catch(SEHException ex) { s_SEHExceptionCatchCalled++; }
+#else
+ // TODO: test on Unix when implementing pinvoke inlining
+ s_SEHExceptionCatchCalled++;
+#endif
+ }
+
+ public static int Main()
+ {
+ new Thread(Collector).Start();
+
+ for(int i = 0; i < s_LoopCounter; i++)
+ {
+ try
+ {
+ NativeSum(10, 10);
+ s_WrongPInvokesExecuted++;
+ }
+ catch (DllNotFoundException) { s_CatchCalled++; }
+
+ try { DoCall(); }
+ catch (DllNotFoundException) { s_CatchCalled++; }
+
+ try { DoCallTryFinally(); }
+ catch (DllNotFoundException) { s_CatchCalled++; }
+
+ try { DoCallTryCatch(true); }
+ catch (ArgumentException) { s_OtherExceptionCatchCalled++; }
+
+ try { DoCallTryRethrowInCatch(); }
+ catch (DllNotFoundException) { s_CatchCalled++; }
+
+ try { DoCallTryRethrowDifferentExceptionInCatch(); }
+ catch (InvalidOperationException) { s_OtherExceptionCatchCalled++; }
+
+ ManualRaiseException();
+ }
+
+ SetResolve();
+
+ for(int i = 0; i < s_LoopCounter; i++)
+ {
+ var a = NativeSum(10, 10);
+ var b = NativeSum(10, 10);
+ s_PInvokesExecuted += (a == b && a == 20)? 2 : 0;
+
+ try { DoCallTryCatch(false); }
+ catch (ArgumentException) { s_OtherExceptionCatchCalled++; }
+
+ ManualRaiseException();
+ }
+
+ s_RunGC = false;
+
+ if (s_FinallyCalled == s_LoopCounter &&
+ s_CatchCalled == (s_LoopCounter * 7) &&
+ s_OtherExceptionCatchCalled == (s_LoopCounter * 3) &&
+ s_WrongPInvokesExecuted == 0 &&
+ s_PInvokesExecuted == (s_LoopCounter * 3) &&
+ s_SEHExceptionCatchCalled == (s_LoopCounter * 2))
+ {
+ Console.WriteLine("PASS");
+ return 100;
+ }
+
+ Console.WriteLine("s_FinallyCalled = " + s_FinallyCalled);
+ Console.WriteLine("s_CatchCalled = " + s_CatchCalled);
+ Console.WriteLine("s_OtherExceptionCatchCalled = " + s_OtherExceptionCatchCalled);
+ Console.WriteLine("s_SEHExceptionCatchCalled = " + s_SEHExceptionCatchCalled);
+ Console.WriteLine("s_WrongPInvokesExecuted = " + s_WrongPInvokesExecuted);
+ Console.WriteLine("s_PInvokesExecuted = " + s_PInvokesExecuted);
+ return -1;
+ }
+
+ [DllImport("NativeLib")]
+ [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
+ static extern int NativeSum(int arg1, int arg2);
+
+#if WINDOWS
+ [DllImport("kernel32")]
+ static extern void RaiseException(uint dwExceptionCode, uint dwExceptionFlags, uint nNumberOfArguments, IntPtr lpArguments);
+#endif
+}
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <AssemblyName>CallbackStressTest</AssemblyName>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+ <ReferenceSystemPrivateCoreLib>true</ReferenceSystemPrivateCoreLib>
+ <DefineConstants Condition="$(OSGroup) == 'Windows_NT'">WINDOWS</DefineConstants>
+ </PropertyGroup>
+ <!-- Default configurations to help VS understand the configurations -->
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
+ </PropertyGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="CallbackStressTest.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="CMakeLists.txt" />
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>