Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions src/backends/native/sentry_crash_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
78 changes: 58 additions & 20 deletions src/backends/native/sentry_crash_daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/backends/native/sentry_crash_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/unwinder/sentry_unwinder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/test_integration_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading