-
-
Notifications
You must be signed in to change notification settings - Fork 194
feat: Add before crash hook to allow modifying other callbacks before crash #1366
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
Open
mujacica
wants to merge
12
commits into
master
Choose a base branch
from
before_crash_hook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4bb7111
feat: Add before crash hook to allow modifying other callbacks before…
mujacica b878cb0
Update changelog
mujacica 9ed2bea
Add unit test
mujacica 6e92d72
Simplify the test
mujacica 09aaa2f
Refactor the test, update CMakeLists
mujacica 13749ed
Refactor vol 3
mujacica 5af77a2
Fix user_data
mujacica 38f71ca
Fix type conversion
mujacica b0ed4fb
Fix includes
mujacica 4a99e82
Revert test changes
mujacica bb43805
Revert "Fix type conversion"
mujacica f53c146
Update pointers
mujacica 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,110 @@ | ||
#include "sentry_core.h" | ||
#include "sentry_options.h" | ||
#include "sentry_testsupport.h" | ||
#include <stdbool.h> | ||
|
||
static volatile bool g_before_crash_called = false; | ||
static volatile int g_before_crash_call_count = 0; | ||
static volatile void *g_before_crash_user_data = NULL; | ||
|
||
static void | ||
test_before_crash_func(void *user_data) | ||
{ | ||
g_before_crash_called = true; | ||
g_before_crash_call_count++; | ||
g_before_crash_user_data = user_data; | ||
} | ||
|
||
static void | ||
reset_before_crash_state(void) | ||
{ | ||
g_before_crash_called = false; | ||
g_before_crash_call_count = 0; | ||
g_before_crash_user_data = NULL; | ||
} | ||
|
||
SENTRY_TEST(before_crash_func_call) | ||
{ | ||
void *user_data = (void *)1; | ||
reset_before_crash_state(); | ||
|
||
SENTRY_TEST_OPTIONS_NEW(options); | ||
sentry_options_set_dsn(options, "https://[email protected]/42"); | ||
sentry_options_set_before_crash(options, test_before_crash_func, user_data); | ||
sentry_options_set_auto_session_tracking(options, false); | ||
|
||
TEST_CHECK_INT_EQUAL(sentry_init(options), 0); | ||
|
||
// Verify before_crash_func is set correctly in options | ||
TEST_CHECK(options->before_crash_func == test_before_crash_func); | ||
TEST_CHECK(options->before_crash_data == user_data); | ||
|
||
// Call the before_crash_func directly to test it | ||
if (options->before_crash_func) { | ||
options->before_crash_func(options->before_crash_data); | ||
} | ||
|
||
sentry_close(); | ||
|
||
// Verify the before_crash_func was called correctly | ||
TEST_CHECK(g_before_crash_called); | ||
TEST_CHECK_INT_EQUAL(g_before_crash_call_count, 1); | ||
TEST_CHECK(g_before_crash_user_data == user_data); | ||
} | ||
|
||
SENTRY_TEST(before_crash_func_not_set) | ||
{ | ||
reset_before_crash_state(); | ||
|
||
SENTRY_TEST_OPTIONS_NEW(options); | ||
sentry_options_set_dsn(options, "https://[email protected]/42"); | ||
// Do not set before_crash_func | ||
sentry_options_set_auto_session_tracking(options, false); | ||
|
||
TEST_CHECK_INT_EQUAL(sentry_init(options), 0); | ||
|
||
// Verify before_crash_func is not set | ||
TEST_CHECK(options->before_crash_func == NULL); | ||
|
||
sentry_close(); | ||
|
||
// Verify the before_crash_func was not called | ||
TEST_CHECK(!g_before_crash_called); | ||
TEST_CHECK_INT_EQUAL(g_before_crash_call_count, 0); | ||
} | ||
|
||
SENTRY_TEST(before_crash_func_change) | ||
{ | ||
reset_before_crash_state(); | ||
|
||
SENTRY_TEST_OPTIONS_NEW(options); | ||
sentry_options_set_dsn(options, "https://[email protected]/42"); | ||
|
||
// Set initial before_crash_func | ||
void *user_data1 = (void *)2; | ||
sentry_options_set_before_crash( | ||
options, test_before_crash_func, user_data1); | ||
|
||
// Change to different user data | ||
void *user_data2 = (void *)3; | ||
sentry_options_set_before_crash( | ||
options, test_before_crash_func, user_data2); | ||
|
||
sentry_options_set_auto_session_tracking(options, false); | ||
TEST_CHECK_INT_EQUAL(sentry_init(options), 0); | ||
|
||
// Verify the updated user_data is set | ||
TEST_CHECK(options->before_crash_func == test_before_crash_func); | ||
TEST_CHECK(options->before_crash_data == user_data2); | ||
|
||
// Call the before_crash_func to test it | ||
if (options->before_crash_func) { | ||
options->before_crash_func(options->before_crash_data); | ||
} | ||
|
||
sentry_close(); | ||
|
||
// Verify the updated user_data was used | ||
TEST_CHECK(g_before_crash_called); | ||
TEST_CHECK(g_before_crash_user_data == user_data2); | ||
} |
This file contains hidden or 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
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.
Potential bug: The
before_crash_func
is called from a signal handler before the SDK's signal-safety mechanisms are enabled viasentry__enter_signal_handler()
, bypassing existing protections.Description: The new
before_crash_func
is invoked from within a signal handler context in backends likeinproc
,breakpad
, andcrashpad
. However, it is called before the SDK's own signal-safety mechanisms are enabled viasentry__enter_signal_handler()
. This function is responsible for setting up spinlocks and a special page allocator to ensure subsequent operations are async-signal-safe. By calling the user-provided function before this setup, the SDK bypasses its own protections. This is inconsistent with existing callbacks likeon_crash_func
, which are called after the safety mechanisms are active. If a user'sbefore_crash_func
performs any non-async-signal-safe operations, it will likely lead to a deadlock or memory corruption, preventing the original crash from being reported.Suggested fix: Move the invocation of
before_crash_func
to be after the call tosentry__enter_signal_handler()
in all relevant backends (sentry_backend_inproc.c
,sentry_backend_breakpad.cpp
, andsentry_backend_crashpad.cpp
). This ensures the SDK's signal-safety infrastructure is active before executing user-provided code, consistent with other crash callbacks.severity: 0.85, confidence: 0.9
Did we get this right? 👍 / 👎 to inform future reviews.