From ef3b4dc6d38b7ef93da05e5bfd2d33bf69368722 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 30 Nov 2026 11:33:35 +0100 Subject: [PATCH 01/10] fix(native): Unwind the crashed thread with libunwindstack on Android The signal handler records a frame-pointer walk of the crashed thread because libunwindstack allocates, and `has_captured_stack()` preferred that walk over unwinding the saved fault context with libunwindstack. The walk skips the caller of a leaf function without a frame record and stops at frames without records such as ART's. In a React Native app, a JNI crash kept only the JNI function and a junk address. Unwind the saved context first and fall back to the walk if that fails. --- src/backends/native/sentry_crash_daemon.c | 23 ++++++++++++++++++++--- tests/test_integration_android.py | 5 +++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/backends/native/sentry_crash_daemon.c b/src/backends/native/sentry_crash_daemon.c index 317f3a5d7..ae90aa5bf 100644 --- a/src/backends/native/sentry_crash_daemon.c +++ b/src/backends/native/sentry_crash_daemon.c @@ -829,14 +829,15 @@ static void enrich_stacktrace( const sentry_crash_context_t *ctx, sentry_value_t stacktrace); /** - * Does the handler's capture hold the crashed thread's stack? The fault IP - * alone does not count with libunwindstack, which can unwind the saved context. + * Does the handler's capture hold the crashed thread's stack? Neither the + * fault IP alone nor a frame-pointer walk counts with libunwindstack, which can + * unwind the saved context. The walk only backs up the unwind. */ static bool has_captured_stack(const sentry_crash_context_t *ctx) { #ifdef SENTRY_WITH_UNWINDER_LIBUNWINDSTACK_REMOTE - return ctx->capture.count > 1; + return ctx->capture.count > 1 && !ctx->capture.frame_pointer; #else return ctx->capture.count > 1 || (ctx->capture.count == 1 && ctx->capture.ips[0] != 0); @@ -1221,6 +1222,22 @@ build_stacktrace_for_thread( sentry_free(remote_frames); } } + +# ifdef SENTRY_WITH_UNWINDER_LIBUNWINDSTACK_REMOTE + // Fall back to the handler's frame-pointer walk, e.g. when the + // crashed process is gone + if (thread_idx == SIZE_MAX && ctx->capture.count > 1) { + if (stack_buf) { + sentry_free(stack_buf); + } + sentry_value_decref(frames); + sentry_value_decref(stacktrace); + sentry_value_t captured = sentry__crash_context_build_stacktrace( + ctx, build_registers_from_ctx(ctx, SIZE_MAX)); + enrich_stacktrace(ctx, captured); + return captured; + } +# endif # endif } // Fall through to pre-captured backtrace or FP-walking if remote diff --git a/tests/test_integration_android.py b/tests/test_integration_android.py index e676aa8a4..01c1f1a01 100644 --- a/tests/test_integration_android.py +++ b/tests/test_integration_android.py @@ -77,5 +77,10 @@ def getprop(name): assert device["model"] == getprop("ro.product.model") assert device["model_id"] == getprop("ro.build.id") assert device["family"] == device["model"].split(" ")[0] + + # `sentry_crash()` is a leaf function without a frame record, so walking + # frame pointers would skip its caller + frames = event["exception"]["values"][0]["stacktrace"]["frames"] + assert any(frame.get("function") == "main" for frame in frames), frames finally: adb("shell", f"rm -rf {database_path}", check=False) From 8b0a937d8f4413ac2f25ae55b7be85295dc289d1 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 30 Nov 2026 13:12:15 +0100 Subject: [PATCH 02/10] fix(android): Leaf function callers in crash daemon events Drop the libunwindstack unwind from the previous commit. It replaced the stacktrace that `on_crash` had already seen and possibly edited with a new one. Fix the signal handler's capture instead. The capture walks frame records from `x29` on arm64. A leaf function without a frame record leaves its caller only in `x30`, so the walk skipped it. Record `x30` as the first caller. --- src/backends/native/sentry_crash_daemon.c | 23 +++------------------- src/backends/native/sentry_crash_handler.c | 11 ++++++++++- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/backends/native/sentry_crash_daemon.c b/src/backends/native/sentry_crash_daemon.c index ae90aa5bf..317f3a5d7 100644 --- a/src/backends/native/sentry_crash_daemon.c +++ b/src/backends/native/sentry_crash_daemon.c @@ -829,15 +829,14 @@ static void enrich_stacktrace( const sentry_crash_context_t *ctx, sentry_value_t stacktrace); /** - * Does the handler's capture hold the crashed thread's stack? Neither the - * fault IP alone nor a frame-pointer walk counts with libunwindstack, which can - * unwind the saved context. The walk only backs up the unwind. + * Does the handler's capture hold the crashed thread's stack? The fault IP + * alone does not count with libunwindstack, which can unwind the saved context. */ static bool has_captured_stack(const sentry_crash_context_t *ctx) { #ifdef SENTRY_WITH_UNWINDER_LIBUNWINDSTACK_REMOTE - return ctx->capture.count > 1 && !ctx->capture.frame_pointer; + return ctx->capture.count > 1; #else return ctx->capture.count > 1 || (ctx->capture.count == 1 && ctx->capture.ips[0] != 0); @@ -1222,22 +1221,6 @@ build_stacktrace_for_thread( sentry_free(remote_frames); } } - -# ifdef SENTRY_WITH_UNWINDER_LIBUNWINDSTACK_REMOTE - // Fall back to the handler's frame-pointer walk, e.g. when the - // crashed process is gone - if (thread_idx == SIZE_MAX && ctx->capture.count > 1) { - if (stack_buf) { - sentry_free(stack_buf); - } - sentry_value_decref(frames); - sentry_value_decref(stacktrace); - sentry_value_t captured = sentry__crash_context_build_stacktrace( - ctx, build_registers_from_ctx(ctx, SIZE_MAX)); - enrich_stacktrace(ctx, captured); - return captured; - } -# endif # endif } // Fall through to pre-captured backtrace or FP-walking if remote diff --git a/src/backends/native/sentry_crash_handler.c b/src/backends/native/sentry_crash_handler.c index a8551f76d..ec6880a08 100644 --- a/src/backends/native/sentry_crash_handler.c +++ b/src/backends/native/sentry_crash_handler.c @@ -329,16 +329,22 @@ capture_frame_pointer_stack( # if defined(SYS_process_vm_readv) && !defined(__arm__) uintptr_t sp = sentry__ucontext_get_sp(uctx); uintptr_t fp = 0; + uintptr_t lr = 0; # if defined(__x86_64__) fp = (uintptr_t)uctx->uc_mcontext.gregs[REG_RBP]; # elif defined(__i386__) fp = (uintptr_t)uctx->uc_mcontext.gregs[REG_EBP]; # elif defined(__aarch64__) fp = (uintptr_t)uctx->uc_mcontext.regs[29]; + lr = (uintptr_t)uctx->uc_mcontext.regs[30]; # elif defined(__riscv) && __riscv_xlen == 64 fp = (uintptr_t)uctx->uc_mcontext.__gregs[REG_S0]; # endif capture->frame_pointer = true; + // leaf functions without a frame record leave their caller only in lr + if (capture_valid_ip(lr)) { + capture->ips[capture->count++] = (uint64_t)lr; + } while (fp && fp >= sp && fp - sp <= SENTRY_CRASH_MAX_STACK_CAPTURE && fp % sizeof(uintptr_t) == 0 && capture->count < SENTRY_CRASH_MAX_BACKTRACE_FRAMES) { @@ -367,7 +373,10 @@ capture_frame_pointer_stack( || next - sp > SENTRY_CRASH_MAX_STACK_CAPTURE))) { break; } - capture->ips[capture->count++] = (uint64_t)ret; + if (ret != lr) { + capture->ips[capture->count++] = (uint64_t)ret; + } + lr = 0; if (!next) { break; } From 8d2c002f478f5237d3e62ddf98617c74f9cc74f1 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 30 Nov 2026 13:36:00 +0100 Subject: [PATCH 03/10] fix(android): Unwind unedited stacktraces in crash daemon events The frame-pointer capture stops at frames without records, such as ART's in a JNI crash. Replace it with the libunwindstack unwind unless `on_crash` or `before_send` edited it. `on_crash` still sees the capture. --- src/backends/native/sentry_crash_daemon.c | 43 +++++++++++++++++++++-- tests/test_integration_android.py | 4 +-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/backends/native/sentry_crash_daemon.c b/src/backends/native/sentry_crash_daemon.c index 317f3a5d7..2f26f2727 100644 --- a/src/backends/native/sentry_crash_daemon.c +++ b/src/backends/native/sentry_crash_daemon.c @@ -829,20 +829,40 @@ static void enrich_stacktrace( const sentry_crash_context_t *ctx, sentry_value_t stacktrace); /** - * Does the handler's capture hold the crashed thread's stack? The fault IP - * alone does not count with libunwindstack, which can unwind the saved context. + * Does the handler's capture hold the crashed thread's stack? Neither the + * fault IP alone nor a frame-pointer walk counts with libunwindstack, which can + * unwind the saved context. The walk only backs up the unwind. */ static bool has_captured_stack(const sentry_crash_context_t *ctx) { #ifdef SENTRY_WITH_UNWINDER_LIBUNWINDSTACK_REMOTE - return ctx->capture.count > 1; + return ctx->capture.count > 1 && !ctx->capture.frame_pointer; #else return ctx->capture.count > 1 || (ctx->capture.count == 1 && ctx->capture.ips[0] != 0); #endif } +/** + * Is the stacktrace still the one built from the handler's capture, without + * `on_crash` or `before_send` edits? + */ +static bool +is_captured_stacktrace( + const sentry_crash_context_t *ctx, sentry_value_t stacktrace) +{ + sentry_value_t captured = sentry__crash_context_build_stacktrace( + ctx, build_registers_from_ctx(ctx, SIZE_MAX)); + char *expected = sentry__value_to_json(captured, NULL); + char *actual = sentry__value_to_json(stacktrace, NULL); + bool equal = expected && actual && strcmp(expected, actual) == 0; + sentry_free(expected); + sentry_free(actual); + sentry_value_decref(captured); + return equal; +} + /** * Build stacktrace frames for a specific thread using frame pointer-based * unwinding. Reads the captured stack memory and walks the frame chain. @@ -1221,6 +1241,22 @@ build_stacktrace_for_thread( sentry_free(remote_frames); } } + +# ifdef SENTRY_WITH_UNWINDER_LIBUNWINDSTACK_REMOTE + // Fall back to the handler's frame-pointer walk, e.g. when the + // crashed process is gone + if (thread_idx == SIZE_MAX && ctx->capture.count > 1) { + if (stack_buf) { + sentry_free(stack_buf); + } + sentry_value_decref(frames); + sentry_value_decref(stacktrace); + sentry_value_t captured = sentry__crash_context_build_stacktrace( + ctx, build_registers_from_ctx(ctx, SIZE_MAX)); + enrich_stacktrace(ctx, captured); + return captured; + } +# endif # endif } // Fall through to pre-captured backtrace or FP-walking if remote @@ -3413,6 +3449,7 @@ build_native_event(const sentry_crash_context_t *ctx, sentry_value_t base, } else { sentry_value_t stacktrace = sentry_value_get_by_key(exc, "stacktrace"); sentry_value_t unwound = has_captured_stack(ctx) + || !is_captured_stacktrace(ctx, stacktrace) ? sentry_value_new_null() : build_stacktrace_from_ctx(ctx); if (!sentry_value_is_null(unwound)) { diff --git a/tests/test_integration_android.py b/tests/test_integration_android.py index 01c1f1a01..87ca4c795 100644 --- a/tests/test_integration_android.py +++ b/tests/test_integration_android.py @@ -78,9 +78,9 @@ def getprop(name): assert device["model_id"] == getprop("ro.build.id") assert device["family"] == device["model"].split(" ")[0] - # `sentry_crash()` is a leaf function without a frame record, so walking - # frame pointers would skip its caller + # the daemon unwinds the unedited frame-pointer capture with CFI frames = event["exception"]["values"][0]["stacktrace"]["frames"] + assert {frame["trust"] for frame in frames[:-1]} == {"cfi"}, frames assert any(frame.get("function") == "main" for frame in frames), frames finally: adb("shell", f"rm -rf {database_path}", check=False) From 6857e02a241ab800848798d56ed18442351ebac0 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 30 Nov 2026 15:17:56 +0100 Subject: [PATCH 04/10] docs: Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64068579e..3bbd14be6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Native: detect daemon startup failure to avoid a 10s stall in `sentry_init` on Windows and Linux. ([#2349](https://github.com/getsentry/sentry-native/pull/2349)) - Native/Windows: fix minidump failures on CPUs with AMX enabled. ([#2353](https://github.com/getsentry/sentry-native/pull/2353)) - Native/Android: fix the OS and device contexts in out-of-process crashes. ([#2354](https://github.com/getsentry/sentry-native/pull/2354)) +- Native/Android: fix truncated stacktraces in out-of-process crashes. ([#2357](https://github.com/getsentry/sentry-native/pull/2357)) ## 1.0.0-beta.2 From ab002731cdbb7f8b9ccd892d80090f8c8721f118 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 30 Nov 2026 17:02:28 +0100 Subject: [PATCH 05/10] perf(android): Compare stacktrace addresses in crash daemon events The crashed app waits for the daemon. Instead of rebuilding and serializing the captured stacktrace, compare the event's frame addresses with the capture. This still detects a stacktrace replaced by `on_crash` or `before_send`, but no longer in-place edits. --- src/backends/native/sentry_crash_daemon.c | 30 ++++++++++++++--------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/backends/native/sentry_crash_daemon.c b/src/backends/native/sentry_crash_daemon.c index 2f26f2727..90c20913a 100644 --- a/src/backends/native/sentry_crash_daemon.c +++ b/src/backends/native/sentry_crash_daemon.c @@ -845,22 +845,30 @@ has_captured_stack(const sentry_crash_context_t *ctx) } /** - * Is the stacktrace still the one built from the handler's capture, without - * `on_crash` or `before_send` edits? + * Does the stacktrace still hold the handler's capture, rather than one that + * `on_crash` or `before_send` replaced? */ static bool is_captured_stacktrace( const sentry_crash_context_t *ctx, sentry_value_t stacktrace) { - sentry_value_t captured = sentry__crash_context_build_stacktrace( - ctx, build_registers_from_ctx(ctx, SIZE_MAX)); - char *expected = sentry__value_to_json(captured, NULL); - char *actual = sentry__value_to_json(stacktrace, NULL); - bool equal = expected && actual && strcmp(expected, actual) == 0; - sentry_free(expected); - sentry_free(actual); - sentry_value_decref(captured); - return equal; + sentry_value_t frames = sentry_value_get_by_key(stacktrace, "frames"); + size_t count = sentry_value_get_length(frames); + if (count != ctx->capture.count + || count > SENTRY_CRASH_MAX_BACKTRACE_FRAMES) { + return false; + } + // frames list the outermost caller first + for (size_t i = 0; i < count; i++) { + const char *addr = sentry_value_as_string(sentry_value_get_by_key( + sentry_value_get_by_index(frames, i), "instruction_addr")); + char *end = NULL; + if (strtoull(addr, &end, 0) != ctx->capture.ips[count - 1 - i] + || end == addr || *end != '\0') { + return false; + } + } + return true; } /** From 4f17e83698c3b67ff4809269cd6b52075106c0a4 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 30 Nov 2026 17:14:48 +0100 Subject: [PATCH 06/10] ref(android): Decide on the crash daemon unwind in `unwind_stack()` Combining `has_captured_stack()` with a negated, similarly named predicate read poorly. --- src/backends/native/sentry_crash_daemon.c | 61 +++++++++++------------ 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/backends/native/sentry_crash_daemon.c b/src/backends/native/sentry_crash_daemon.c index 90c20913a..f13955242 100644 --- a/src/backends/native/sentry_crash_daemon.c +++ b/src/backends/native/sentry_crash_daemon.c @@ -844,33 +844,6 @@ has_captured_stack(const sentry_crash_context_t *ctx) #endif } -/** - * Does the stacktrace still hold the handler's capture, rather than one that - * `on_crash` or `before_send` replaced? - */ -static bool -is_captured_stacktrace( - const sentry_crash_context_t *ctx, sentry_value_t stacktrace) -{ - sentry_value_t frames = sentry_value_get_by_key(stacktrace, "frames"); - size_t count = sentry_value_get_length(frames); - if (count != ctx->capture.count - || count > SENTRY_CRASH_MAX_BACKTRACE_FRAMES) { - return false; - } - // frames list the outermost caller first - for (size_t i = 0; i < count; i++) { - const char *addr = sentry_value_as_string(sentry_value_get_by_key( - sentry_value_get_by_index(frames, i), "instruction_addr")); - char *end = NULL; - if (strtoull(addr, &end, 0) != ctx->capture.ips[count - 1 - i] - || end == addr || *end != '\0') { - return false; - } - } - return true; -} - /** * Build stacktrace frames for a specific thread using frame pointer-based * unwinding. Reads the captured stack memory and walks the frame chain. @@ -3330,6 +3303,35 @@ build_stacktrace_from_ctx(const sentry_crash_context_t *ctx) return build_stacktrace_for_thread(ctx, SIZE_MAX); } +/** + * Unwind the crashed thread, unless the event's stacktrace is a complete + * capture or `on_crash` or `before_send` changed its frames. + */ +static sentry_value_t +unwind_stack(const sentry_crash_context_t *ctx, sentry_value_t stacktrace) +{ + if (has_captured_stack(ctx)) { + return sentry_value_new_null(); + } + sentry_value_t frames = sentry_value_get_by_key(stacktrace, "frames"); + size_t count = sentry_value_get_length(frames); + if (count != ctx->capture.count + || count > SENTRY_CRASH_MAX_BACKTRACE_FRAMES) { + return sentry_value_new_null(); + } + // frames list the outermost caller first + for (size_t i = 0; i < count; i++) { + const char *addr = sentry_value_as_string(sentry_value_get_by_key( + sentry_value_get_by_index(frames, i), "instruction_addr")); + char *end = NULL; + if (strtoull(addr, &end, 0) != ctx->capture.ips[count - 1 - i] + || end == addr || *end != '\0') { + return sentry_value_new_null(); + } + } + return build_stacktrace_from_ctx(ctx); +} + #if defined(SENTRY_PLATFORM_WINDOWS) static bool is_wer_done(const sentry_crash_context_t *ctx) @@ -3456,10 +3458,7 @@ build_native_event(const sentry_crash_context_t *ctx, sentry_value_t base, sentry_event_add_exception(event, exc); } else { sentry_value_t stacktrace = sentry_value_get_by_key(exc, "stacktrace"); - sentry_value_t unwound = has_captured_stack(ctx) - || !is_captured_stacktrace(ctx, stacktrace) - ? sentry_value_new_null() - : build_stacktrace_from_ctx(ctx); + sentry_value_t unwound = unwind_stack(ctx, stacktrace); if (!sentry_value_is_null(unwound)) { sentry_value_set_by_key(exc, "stacktrace", unwound); stacktrace = unwound; From 75086c6fc6c3491d188660be71dd4bd017465800 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 1 Dec 2026 09:32:31 +0100 Subject: [PATCH 07/10] docs(android): Explain the lr check in the frame-pointer walk --- src/backends/native/sentry_crash_handler.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backends/native/sentry_crash_handler.c b/src/backends/native/sentry_crash_handler.c index ec6880a08..f354230d2 100644 --- a/src/backends/native/sentry_crash_handler.c +++ b/src/backends/native/sentry_crash_handler.c @@ -373,6 +373,7 @@ capture_frame_pointer_stack( || next - sp > SENTRY_CRASH_MAX_STACK_CAPTURE))) { break; } + // the first frame record may hold the same caller as lr if (ret != lr) { capture->ips[capture->count++] = (uint64_t)ret; } From 173a8c85ab44a3a0ba708e0d07630215a0285ba5 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 1 Dec 2026 10:15:53 +0100 Subject: [PATCH 08/10] revert(android): Leaf function callers in crash daemon events Revert the `x30` capture from 9908ec97e. Its daemon changes were already undone by 522169e94. Without unwind info, the signal handler cannot tell a leaf function's caller in `x30` from a stale return address. A function with a frame record that crashes after a call leaves `x30` pointing into itself, so the capture listed the crashing function twice. On Android, the daemon's libunwindstack unwind already finds leaf callers. --- src/backends/native/sentry_crash_handler.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/backends/native/sentry_crash_handler.c b/src/backends/native/sentry_crash_handler.c index f354230d2..a8551f76d 100644 --- a/src/backends/native/sentry_crash_handler.c +++ b/src/backends/native/sentry_crash_handler.c @@ -329,22 +329,16 @@ capture_frame_pointer_stack( # if defined(SYS_process_vm_readv) && !defined(__arm__) uintptr_t sp = sentry__ucontext_get_sp(uctx); uintptr_t fp = 0; - uintptr_t lr = 0; # if defined(__x86_64__) fp = (uintptr_t)uctx->uc_mcontext.gregs[REG_RBP]; # elif defined(__i386__) fp = (uintptr_t)uctx->uc_mcontext.gregs[REG_EBP]; # elif defined(__aarch64__) fp = (uintptr_t)uctx->uc_mcontext.regs[29]; - lr = (uintptr_t)uctx->uc_mcontext.regs[30]; # elif defined(__riscv) && __riscv_xlen == 64 fp = (uintptr_t)uctx->uc_mcontext.__gregs[REG_S0]; # endif capture->frame_pointer = true; - // leaf functions without a frame record leave their caller only in lr - if (capture_valid_ip(lr)) { - capture->ips[capture->count++] = (uint64_t)lr; - } while (fp && fp >= sp && fp - sp <= SENTRY_CRASH_MAX_STACK_CAPTURE && fp % sizeof(uintptr_t) == 0 && capture->count < SENTRY_CRASH_MAX_BACKTRACE_FRAMES) { @@ -373,11 +367,7 @@ capture_frame_pointer_stack( || next - sp > SENTRY_CRASH_MAX_STACK_CAPTURE))) { break; } - // the first frame record may hold the same caller as lr - if (ret != lr) { - capture->ips[capture->count++] = (uint64_t)ret; - } - lr = 0; + capture->ips[capture->count++] = (uint64_t)ret; if (!next) { break; } From 9bea4d93f9f4f467fff9782b650ec39551ee973b Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 1 Dec 2026 10:37:49 +0100 Subject: [PATCH 09/10] fix(native): Strip PAC bits from frame-pointer walks On arm64 with pointer authentication, frame records hold signed return addresses. The handler's capture recorded them as junk, and the daemon's own walk would too, because `SENTRY__STRIP_PAC` only stripped on macOS. Replace it with a shared `sentry__strip_pac()`, which keeps the macOS mask and uses `xpaclri` on Linux and Android, a NOP on CPUs without PAC. A fixed mask does not fit there because the PAC bits start at the virtual address size, such as bit 39 on Android. --- src/backends/native/sentry_crash_context.h | 22 ++++++++++++++++++++++ src/backends/native/sentry_crash_daemon.c | 22 ++++++++-------------- src/backends/native/sentry_crash_handler.c | 2 +- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/backends/native/sentry_crash_context.h b/src/backends/native/sentry_crash_context.h index 4c827f731..f96c889b7 100644 --- a/src/backends/native/sentry_crash_context.h +++ b/src/backends/native/sentry_crash_context.h @@ -131,6 +131,28 @@ typedef struct { # endif #endif +/** + * Strips pointer authentication bits from an address on arm64. + */ +static inline uint64_t +sentry__strip_pac(uint64_t addr) +{ +#if defined(SENTRY_PLATFORM_MACOS) && defined(__aarch64__) + return addr & 0x00007FFFFFFFFFFFULL; +#elif (defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)) \ + && defined(__aarch64__) + // xpaclri strips PAC bits from x30 and is a NOP without PAC + uint64_t stripped; + __asm__("mov x30, %1\n\thint #7\n\tmov %0, x30" + : "=r"(stripped) + : "r"(addr) + : "x30"); + return stripped; +#else + return addr; +#endif +} + /** * Crash state machine for atomic coordination between app and daemon */ diff --git a/src/backends/native/sentry_crash_daemon.c b/src/backends/native/sentry_crash_daemon.c index f13955242..afb152d9e 100644 --- a/src/backends/native/sentry_crash_daemon.c +++ b/src/backends/native/sentry_crash_daemon.c @@ -726,12 +726,6 @@ arm_frame_record_plausible( } #endif -#if defined(SENTRY_PLATFORM_MACOS) && defined(__aarch64__) -# define SENTRY__STRIP_PAC(addr) ((addr) & 0x00007FFFFFFFFFFFULL) -#else -# define SENTRY__STRIP_PAC(addr) (addr) -#endif - /** * Check if an address looks like a valid code pointer. * Basic sanity check to avoid garbage in the stacktrace. @@ -910,9 +904,9 @@ build_stacktrace_for_thread( fp = ctx->platform.mcontext.__ss.__rbp; sp = ctx->platform.mcontext.__ss.__rsp; # elif defined(__aarch64__) - ip = SENTRY__STRIP_PAC(SENTRY__ARM64_GET_PC(ctx->platform.mcontext.__ss)); - fp = SENTRY__STRIP_PAC(SENTRY__ARM64_GET_FP(ctx->platform.mcontext.__ss)); - sp = SENTRY__STRIP_PAC(SENTRY__ARM64_GET_SP(ctx->platform.mcontext.__ss)); + ip = sentry__strip_pac(SENTRY__ARM64_GET_PC(ctx->platform.mcontext.__ss)); + fp = sentry__strip_pac(SENTRY__ARM64_GET_FP(ctx->platform.mcontext.__ss)); + sp = sentry__strip_pac(SENTRY__ARM64_GET_SP(ctx->platform.mcontext.__ss)); # endif #elif defined(SENTRY_PLATFORM_WINDOWS) // Use thread-specific context, defaulting to crashed thread @@ -979,9 +973,9 @@ build_stacktrace_for_thread( fp = thread->state.__ss.__rbp; sp = thread->state.__ss.__rsp; # elif defined(__aarch64__) - ip = SENTRY__STRIP_PAC(SENTRY__ARM64_GET_PC(thread->state.__ss)); - fp = SENTRY__STRIP_PAC(SENTRY__ARM64_GET_FP(thread->state.__ss)); - sp = SENTRY__STRIP_PAC(SENTRY__ARM64_GET_SP(thread->state.__ss)); + ip = sentry__strip_pac(SENTRY__ARM64_GET_PC(thread->state.__ss)); + fp = sentry__strip_pac(SENTRY__ARM64_GET_FP(thread->state.__ss)); + sp = sentry__strip_pac(SENTRY__ARM64_GET_SP(thread->state.__ss)); # endif SENTRY_TRACEF("Thread %zu: IP=0x%llx FP=0x%llx SP=0x%llx", idx, @@ -1358,8 +1352,8 @@ build_stacktrace_for_thread( break; } #endif - saved_fp = SENTRY__STRIP_PAC(saved_fp); - return_addr = SENTRY__STRIP_PAC(return_addr); + saved_fp = sentry__strip_pac(saved_fp); + return_addr = sentry__strip_pac(return_addr); SENTRY_TRACEF("Frame %d: FP=0x%llx saved_fp=0x%llx ret=0x%llx", walk_count, (unsigned long long)current_fp, diff --git a/src/backends/native/sentry_crash_handler.c b/src/backends/native/sentry_crash_handler.c index a8551f76d..a5c266c9f 100644 --- a/src/backends/native/sentry_crash_handler.c +++ b/src/backends/native/sentry_crash_handler.c @@ -357,7 +357,7 @@ capture_frame_pointer_stack( break; } uintptr_t next = pair[0]; - uintptr_t ret = pair[1]; + uintptr_t ret = sentry__strip_pac(pair[1]); if (!ret) { break; } From 3b075689d71c5f3162ae3a0fe496e6e9880dc333 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 1 Dec 2026 11:11:53 +0100 Subject: [PATCH 10/10] fix(native): Poll ptrace attach every 1 ms Each thread waited a full 100 ms after `PTRACE_ATTACH`, because the first `waitpid()` rarely finds the thread stopped yet. Walking the 71 threads of a React Native app kept the crashed app frozen for 8 s. Poll every 1 ms with the same 2 s timeout. --- CHANGELOG.md | 1 + src/unwinder/sentry_unwinder.c | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bbd14be6..3db346cd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Native/Windows: fix minidump failures on CPUs with AMX enabled. ([#2353](https://github.com/getsentry/sentry-native/pull/2353)) - Native/Android: fix the OS and device contexts in out-of-process crashes. ([#2354](https://github.com/getsentry/sentry-native/pull/2354)) - Native/Android: fix truncated stacktraces in out-of-process crashes. ([#2357](https://github.com/getsentry/sentry-native/pull/2357)) +- Native: avoid a 100ms stall per thread when walking the threads of a crashed process on Linux and Android. ([#2357](https://github.com/getsentry/sentry-native/pull/2357)) ## 1.0.0-beta.2 diff --git a/src/unwinder/sentry_unwinder.c b/src/unwinder/sentry_unwinder.c index 1d1e5dca5..09f970d1a 100644 --- a/src/unwinder/sentry_unwinder.c +++ b/src/unwinder/sentry_unwinder.c @@ -131,7 +131,7 @@ sentry__ptrace_attach_thread(pid_t tid) // sends SIGSTOP but the thread won't stop until it leaves uninterruptible // sleep, which may never happen. int status = 0; - int retries = 20; + int retries = 2000; for (;;) { pid_t waited = waitpid(tid, &status, __WALL | WNOHANG); if (waited == tid) { @@ -148,7 +148,7 @@ sentry__ptrace_attach_thread(pid_t tid) ptrace(PTRACE_DETACH, tid, NULL, NULL); return false; } - usleep(100000); + usleep(1000); } if (!WIFSTOPPED(status)) {