stacktrace_aarch64: Strip PAC bits before sigreturn check When PAC-RET (`-mbranch-protection=pac-ret` or `standard`) is active on PAC-capable AArch64 hardware, the signal handler prologue signs `x30` before saving it to `old_frame_pointer[1]`. Comparing the raw signed return address directly against the unsigned VDSO address returned by `GetKernelRtSigreturnAddress()` fails to detect `__kernel_rt_sigreturn`. `StripPointerMetadata()` executes `xpaclri` under `#if defined(__aarch64__)` and is a no-op (`return ptr;`) on all other architectures. It is already called when recording return addresses and frame pointers in `stacktrace_aarch64-inl.inc` and on frame/stack pointers in the x86, PowerPC, ARM, and RISC-V unwinders (`stacktrace_x86-inl.inc`, `stacktrace_powerpc-inl.inc`, `stacktrace_arm-inl.inc`, and `stacktrace_riscv-inl.inc`). Strip pointer metadata with `StripPointerMetadata()` before comparing against `GetKernelRtSigreturnAddress()`, consistent with those call sites, and guard against matching a null return address when no VDSO is present (matching `stacktrace_x86-inl.inc` and `stacktrace_powerpc-inl.inc`). Add `StackTrace.SignalHandlerWithContext` to `stacktrace_test.cc`, which triggers a signal inline so `x30` matches `fp[1]` and verifies the caller's return address is not duplicated by falling through to the kernel's synthetic signal frame record. PiperOrigin-RevId: 1033240464
diff --git a/absl/debugging/internal/address_is_readable.cc b/absl/debugging/internal/address_is_readable.cc index 88aa516..18c0170 100644 --- a/absl/debugging/internal/address_is_readable.cc +++ b/absl/debugging/internal/address_is_readable.cc
@@ -21,7 +21,7 @@ #include "absl/base/config.h" -#if !defined(__linux__) || defined(__ANDROID__) +#if !defined(__linux__) namespace absl { ABSL_NAMESPACE_BEGIN @@ -34,7 +34,7 @@ ABSL_NAMESPACE_END } // namespace absl -#else // __linux__ && !__ANDROID__ +#else // __linux__ #include <stdint.h> #include <syscall.h> @@ -99,4 +99,4 @@ ABSL_NAMESPACE_END } // namespace absl -#endif // __linux__ && !__ANDROID__ +#endif // __linux__
diff --git a/absl/debugging/internal/stacktrace_aarch64-inl.inc b/absl/debugging/internal/stacktrace_aarch64-inl.inc index b5bf60e..c34a5dc 100644 --- a/absl/debugging/internal/stacktrace_aarch64-inl.inc +++ b/absl/debugging/internal/stacktrace_aarch64-inl.inc
@@ -112,7 +112,15 @@ #if defined(__linux__) if (WITH_CONTEXT && uc != nullptr) { // Check to see if next frame's return address is __kernel_rt_sigreturn. - if (old_frame_pointer[1] == GetKernelRtSigreturnAddress()) { + // Strip pointer metadata (e.g. PAC-RET signature bits) before comparing + // against the unsigned VDSO address. + const unsigned char *const sigreturn_address = + GetKernelRtSigreturnAddress(); + const unsigned char *const return_address = + reinterpret_cast<const unsigned char *>( + absl::debugging_internal::StripPointerMetadata( + old_frame_pointer[1])); + if (sigreturn_address != nullptr && return_address == sigreturn_address) { const ucontext_t *ucv = static_cast<const ucontext_t *>(uc); // old_frame_pointer[0] is not suitable for unwinding, look at // ucontext to discover frame pointer before signal.
diff --git a/absl/debugging/internal/vdso_support.cc b/absl/debugging/internal/vdso_support.cc index fb9d359..74fd9ee 100644 --- a/absl/debugging/internal/vdso_support.cc +++ b/absl/debugging/internal/vdso_support.cc
@@ -35,8 +35,11 @@ #endif #include <unistd.h> -#if !defined(__UCLIBC__) && defined(__GLIBC__) && \ - (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16)) +#if !defined(__UCLIBC__) && \ + ((defined(__GLIBC__) && \ + (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))) || \ + (defined(__ANDROID__) && defined(__ANDROID_API__) && \ + __ANDROID_API__ >= 18)) #define ABSL_HAVE_GETAUXVAL #endif
diff --git a/absl/debugging/stacktrace_test.cc b/absl/debugging/stacktrace_test.cc index 02950bc..f76659f 100644 --- a/absl/debugging/stacktrace_test.cc +++ b/absl/debugging/stacktrace_test.cc
@@ -30,6 +30,11 @@ #include "absl/base/optimization.h" #include "absl/types/span.h" +#if defined(__aarch64__) && defined(__linux__) +#include <sys/syscall.h> +#include <unistd.h> +#endif + static int g_should_fixup_calls = 0; static int g_fixup_calls = 0; static bool g_enable_fixup = false; @@ -380,6 +385,79 @@ } #endif +#if defined(__aarch64__) && defined(__linux__) && defined(__GNUC__) && \ + !defined(ABSL_HAVE_THREAD_SANITIZER) +// State for SignalHandlerWithContext: exercises unwinding through a signal +// handler whose prologue signs its saved return address (__kernel_rt_sigreturn) +// under PAC-RET. +struct DirectSignalState { + static constexpr int kMaxStackDepth = 64; + bool handled = false; + int depth = 0; + const void* expected_ra = nullptr; + void* result[kMaxStackDepth] = {}; +}; + +DirectSignalState g_direct_signal; + +ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS void DirectSignalHandler(int, siginfo_t*, + void* uc) { + absl::base_internal::ErrnoSaver errno_saver; + g_direct_signal.depth = absl::GetStackTraceWithContext( + g_direct_signal.result, DirectSignalState::kMaxStackDepth, 0, uc, + nullptr); + g_direct_signal.handled = true; +} + +ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS ABSL_ATTRIBUTE_NOINLINE void +TriggerDirectSignal(intptr_t pid, intptr_t tid) { + g_direct_signal.expected_ra = __builtin_return_address(0); + void* fp = __builtin_frame_address(0); + register intptr_t x8 __asm__("x8") = SYS_tgkill; + register intptr_t x0 __asm__("x0") = pid; + register intptr_t x1 __asm__("x1") = tid; + register intptr_t x2 __asm__("x2") = SIGUSR1; + // Invoke tgkill inline rather than via raise() so x30 still holds our return + // address (matching fp[1]) when the signal arrives. Passing fp and clobbering + // x30 forces a full frame record (and PAC signing under PAC-RET) before + // svc #0. If NextStackFrame fails to recognize __kernel_rt_sigreturn (for + // example because PAC bits were not stripped), it walks the kernel's + // synthetic signal frame_record in addition to our frame, duplicating + // expected_ra in the trace. + asm volatile("svc #0" + : "+r"(x0) + : "r"(x8), "r"(x1), "r"(x2), "r"(fp) + : "memory", "x30"); + static_cast<void>(x0); + ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); +} + +TEST(StackTrace, SignalHandlerWithContext) { + if (!absl::debugging_internal::StackTraceWorksForTest()) { + GTEST_SKIP() << "Stack traces are not expected to work in this config"; + } + + g_direct_signal = DirectSignalState{}; + struct sigaction act{}; + struct sigaction old_act{}; + act.sa_sigaction = DirectSignalHandler; + act.sa_flags = SA_SIGINFO; + sigemptyset(&act.sa_mask); + ASSERT_EQ(sigaction(SIGUSR1, &act, &old_act), 0) << strerror(errno); + const Cleanup restore_sigaction([&old_act]() { + EXPECT_EQ(sigaction(SIGUSR1, &old_act, nullptr), 0) << strerror(errno); + }); + + TriggerDirectSignal(getpid(), syscall(SYS_gettid)); + + ASSERT_TRUE(g_direct_signal.handled); + EXPECT_THAT( + absl::MakeSpan(g_direct_signal.result, + static_cast<size_t>(g_direct_signal.depth)), + Contains(g_direct_signal.expected_ra).Times(1)); +} +#endif + TEST(StackTrace, NoNullptrInPopulatedRange) { constexpr int kMaxDepth = 1024; void* results[kMaxDepth];