forked from getsentry/sentry-native
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce optional handler strategy (getsentry#1027)
* reintroduce #ifdef symmetry regarding the usage of the handler_strategy (which must work on all UNIXes) and the query towards the handler_strategy option which must only work on Linux. * ensure we "leave" the signal-handler when we invoke the CLR/Mono runtime handler * ensure the page_allocator is only enabled when we have an actual native crash, we don't allocate before * continuing the signal chain at the end is something we want for both strategies, because CHAIN_AT_START will reach this execution-path only if the runtime-handler decided that it was an actual native crash. * add trace logs to at-start chaining, so we can see the behavior in the field when debugging is enabled * ensure page-allocator is only referenced on UNIXes * add integration test for managed and native crash * ignore 32-bit Linux build in the integration test * extract skip_condition to check what provokes the invalid syntax * clean up sigaltstack initialization * overwrite it only when the flag `SS_DISABLED` is set and the query didn't result in an error * if the query was successful but the flag is anything but `SS_DISABLED`, only log the size and flags of the current stack * if the query failed then log the corresponding error * clean up run assertion on output, so we can actually see what's happening. * create a non-faulty `sigaltstack` for the GHA runner * disable the test on ASAN runs since that would require an instrumented runtime :-) dotnet/runtime#13458 * Add changelog.
- Loading branch information
1 parent
67cc95c
commit 7c1d428
Showing
9 changed files
with
308 additions
and
33 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
namespace dotnet_signal; | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
class Program | ||
{ | ||
[DllImport("crash", EntryPoint = "native_crash")] | ||
static extern void native_crash(); | ||
|
||
[DllImport("crash", EntryPoint = "enable_sigaltstack")] | ||
static extern void enable_sigaltstack(); | ||
|
||
[DllImport("sentry", EntryPoint = "sentry_options_new")] | ||
static extern IntPtr sentry_options_new(); | ||
|
||
[DllImport("sentry", EntryPoint = "sentry_options_set_handler_strategy")] | ||
static extern IntPtr sentry_options_set_handler_strategy(IntPtr options, int strategy); | ||
|
||
[DllImport("sentry", EntryPoint = "sentry_options_set_debug")] | ||
static extern IntPtr sentry_options_set_debug(IntPtr options, int debug); | ||
|
||
[DllImport("sentry", EntryPoint = "sentry_init")] | ||
static extern int sentry_init(IntPtr options); | ||
|
||
static void Main(string[] args) | ||
{ | ||
var githubActions = Environment.GetEnvironmentVariable("GITHUB_ACTIONS") ?? string.Empty; | ||
if (githubActions == "true") { | ||
// Set up our own `sigaltstack` for this thread if we're running on GHA because of a failure to run any | ||
// signal handler after the initial setup. This behavior is locally non-reproducible and likely runner-related. | ||
// I ran this against .net7/8/9 on at least 10 different Linux setups, and it worked on all, but on GHA | ||
// it only works if we __don't__ accept the already installed `sigaltstack`. | ||
enable_sigaltstack(); | ||
} | ||
|
||
// setup minimal sentry-native | ||
var options = sentry_options_new(); | ||
sentry_options_set_handler_strategy(options, 1); | ||
sentry_options_set_debug(options, 1); | ||
sentry_init(options); | ||
|
||
var doNativeCrash = args is ["native-crash"]; | ||
if (doNativeCrash) | ||
{ | ||
native_crash(); | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
Console.WriteLine("dereference a NULL object from managed code"); | ||
var s = default(string); | ||
var c = s.Length; | ||
} | ||
catch (NullReferenceException exception) | ||
{ | ||
Console.WriteLine("dereference another NULL object from managed code"); | ||
var s = default(string); | ||
var c = s.Length; | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
#include <signal.h> | ||
#include <stdlib.h> | ||
void native_crash(void) | ||
{ | ||
*(int *)10 = 100; | ||
} | ||
|
||
void enable_sigaltstack(void) | ||
{ | ||
const size_t signal_stack_size = 16384; | ||
stack_t signal_stack; | ||
signal_stack.ss_sp = malloc(signal_stack_size); | ||
if (!signal_stack.ss_sp) { | ||
return; | ||
} | ||
signal_stack.ss_size = signal_stack_size; | ||
signal_stack.ss_flags = 0; | ||
sigaltstack(&signal_stack, 0); | ||
} |
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,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
</Project> |
Oops, something went wrong.