-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Make longjmp over managed frames work #111259
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
Merged
janvorli
merged 8 commits into
dotnet:main
from
janvorli:fix-longjmp-over-managed-frames
Jan 21, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
82b67ac
Make longjmp over managed frames work
janvorli 3a019a2
Fully simulate legacy EH for longjmp on Windows
janvorli ec133dc
Fix Unix build
janvorli a7366aa
Fix bad merge
janvorli 19b02b1
Fix regression issue id in the test path / name
janvorli 7fe2b47
Add comment explaining this is best effort for back compat
janvorli 2e11229
Update src/coreclr/vm/exceptionhandling.cpp
jkotas e0a9b38
Update src/coreclr/vm/exceptionhandling.cpp
jkotas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/tests/Regressions/coreclr/GitHub_111242/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
project (Test111242Lib) | ||
include_directories(${INC_PLATFORM_DIR}) | ||
|
||
if(CLR_CMAKE_HOST_WIN32) | ||
set_source_files_properties(Test111242.c PROPERTIES COMPILE_OPTIONS /TC) # compile as C | ||
else() | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden") | ||
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -fvisibility=hidden -Wno-return-type-c-linkage") | ||
endif() | ||
|
||
# add the executable | ||
add_library (Test111242Lib SHARED Test111242.c) | ||
|
||
# add the install targets | ||
install (TARGETS Test111242Lib DESTINATION bin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <setjmp.h> | ||
|
||
|
||
#ifdef _MSC_VER | ||
#define DLLEXPORT __declspec(dllexport) | ||
#else | ||
#define DLLEXPORT __attribute__((visibility("default"))) | ||
#endif // _MSC_VER | ||
|
||
DLLEXPORT void TestSetJmp(void (*managedCallback)(void *)) | ||
{ | ||
jmp_buf jmpBuf; | ||
if (!setjmp(jmpBuf)) | ||
{ | ||
managedCallback(&jmpBuf); | ||
} | ||
else | ||
{ | ||
printf("longjmp called\n"); | ||
} | ||
} | ||
|
||
DLLEXPORT void TestLongJmp(void *jmpBuf) | ||
{ | ||
longjmp(*(jmp_buf*)jmpBuf, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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.InteropServices; | ||
using System.Runtime.CompilerServices; | ||
using Xunit; | ||
|
||
public static class Test111242 | ||
{ | ||
[DllImport("Test111242Lib")] | ||
static extern unsafe void TestSetJmp(delegate* unmanaged<void*,void> managedCallback); | ||
|
||
[DllImport("Test111242Lib")] | ||
static extern unsafe void TestLongJmp(void *jmpBuf); | ||
|
||
static bool ExceptionFilter(Exception ex) | ||
{ | ||
Assert.Fail("Should not call filter for longjmp SEH exception"); | ||
return true; | ||
} | ||
|
||
static bool wasFinallyInvoked = false; | ||
|
||
[UnmanagedCallersOnly] | ||
static unsafe void ManagedCallback(void *jmpBuf) | ||
{ | ||
try | ||
{ | ||
TestLongJmp(jmpBuf); | ||
} | ||
catch (Exception ex) when (ExceptionFilter(ex)) | ||
{ | ||
Assert.Fail("Should not catch longjmp SEH exception via filter"); | ||
} | ||
catch | ||
{ | ||
Assert.Fail("Should not catch longjmp SEH exception via catch-all"); | ||
} | ||
finally | ||
{ | ||
Console.WriteLine("Finally block executed"); | ||
wasFinallyInvoked = true; | ||
} | ||
Assert.Fail("Should not reach here"); | ||
} | ||
|
||
[Fact] | ||
public static unsafe void TestEntryPoint() | ||
{ | ||
TestSetJmp(&ManagedCallback); | ||
Assert.True(wasFinallyInvoked); | ||
Console.WriteLine("Test passed"); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/tests/Regressions/coreclr/GitHub_111242/Test111242.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<!-- Needed for CMakeProjectReference --> | ||
<RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<CLRTestPriority>1</CLRTestPriority> | ||
<CLRTestTargetUnsupported Condition="'$(TargetOS)' != 'windows'">true</CLRTestTargetUnsupported> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<DebugType>PdbOnly</DebugType> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="Test111242.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<CMakeProjectReference Include="CMakeLists.txt" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is one of the reasons for not supporting setjmp/longjmp. setjmp/longjmp behaves like uncatchable exception - managed code is not generally robust against uncatchable exceptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely agree. The intent is really just to fix the regression on Windows w.r.t. the legacy EH. It is a best effort and not something people should use in new code. I hope that the fact that it doesn't work on Linux is enough reason for trying to stay away from using longjmp over managed code.