diff --git a/CHANGELOG.md b/CHANGELOG.md index 64068579e1..3db346cd71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ - 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)) +- 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/backends/native/sentry_crash_context.h b/src/backends/native/sentry_crash_context.h index 4c827f7313..f96c889b7f 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 317f3a5d7e..afb152d9ef 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. @@ -829,14 +823,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); @@ -909,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 @@ -978,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, @@ -1221,6 +1216,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 @@ -1341,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, @@ -3286,6 +3297,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) @@ -3412,9 +3452,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) - ? 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; diff --git a/src/backends/native/sentry_crash_handler.c b/src/backends/native/sentry_crash_handler.c index a8551f76d5..a5c266c9fb 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; } diff --git a/src/unwinder/sentry_unwinder.c b/src/unwinder/sentry_unwinder.c index 1d1e5dca51..09f970d1a9 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)) { diff --git a/tests/test_integration_android.py b/tests/test_integration_android.py index e676aa8a4a..87ca4c795b 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] + + # 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)