Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception interop test for validating native exception interop on Windows + CoreCLR. #70110

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

include_directories(${INC_PLATFORM_DIR})
add_library(ExceptionInteropNative SHARED ExceptionInteropNative.cpp)
target_link_libraries(ExceptionInteropNative PRIVATE platformdefines)
125 changes: 125 additions & 0 deletions src/tests/baseservices/exceptions/exceptioninterop/ExceptionInterop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;
using static ExceptionInteropNative;

internal unsafe static class ExceptionInteropNative
{
[DllImport(nameof(ExceptionInteropNative))]
public static extern void ThrowException();

[DllImport(nameof(ExceptionInteropNative))]
public static extern void NativeFunction();

[DllImport(nameof(ExceptionInteropNative))]
public static extern void CallCallback(delegate* unmanaged<void> cb);
}

public unsafe static class ExceptionInterop
{
[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[SkipOnMono("Exception interop not supported on Mono.")]
public static void ThrowNativeExceptionAndCatchInFrame()
{
bool caughtException = false;
try
{
ThrowException();
}
catch
{
caughtException = true;
// Try calling another P/Invoke in the catch block to make sure we have everything set up
// to recover from the exceptional control flow.
NativeFunction();
}
Assert.True(caughtException);
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[SkipOnMono("Exception interop not supported on Mono.")]
public static void ThrowManagedExceptionThroughNativeAndCatchInFrame()
{
bool caughtException = false;
try
{
CallCallback(&ThrowManagedException);
}
catch
{
caughtException = true;
// Try calling another P/Invoke in the catch block to make sure we have everything set up
// to recover from the exceptional control flow.
NativeFunction();
}
Assert.True(caughtException);

[UnmanagedCallersOnly]
static void ThrowManagedException()
{
throw new Exception();
}
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[SkipOnMono("Exception interop not supported on Mono.")]
public static void ThrowNativeExceptionAndCatchInFrameWithFilter()
{
bool caughtException = false;
try
{
ThrowException();
}
catch (Exception) when (Filter())
{
caughtException = true;
// Try calling another P/Invoke in the catch block to make sure we have everything set up
// to recover from the exceptional control flow.
NativeFunction();
}
Assert.True(caughtException);

// Aggresively inline to make sure the call to NativeFunction is in the filter clause
[MethodImpl(MethodImplOptions.AggressiveInlining)]
bool Filter()
{
NativeFunction();
return true;
}
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[SkipOnMono("Exception interop not supported on Mono.")]
public static void ThrowNativeExceptionAndCatchInFrameWithFinally()
{
bool caughtException = false;
try
{
try
{
ThrowException();
}
finally
{
// Try calling another P/Invoke in the finally block before the catch
// to make sure we have everything set up
// to recover from the exceptional control flow.
NativeFunction();
}
}
catch
{
caughtException = true;
}

Assert.True(caughtException);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<CMakeProjectReference Include="CMakeLists.txt" />
<Compile Include="ExceptionInterop.cs" />
</ItemGroup>
</Project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <exception>
#include <platformdefines.h>

extern "C" DLL_EXPORT void STDMETHODCALLTYPE ThrowException()
{
throw std::exception{};
}

extern "C" DLL_EXPORT void STDMETHODCALLTYPE NativeFunction()
{
}

extern "C" DLL_EXPORT void STDMETHODCALLTYPE CallCallback(void (*cb)())
{
cb();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<CMakeProjectReference Include="CMakeLists.txt" />
<Compile Include="ExceptionInterop.cs" />
</ItemGroup>
</Project>