From f2dd73d7a801e5c7387722b46ec067d6ae4aac50 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 17 Jul 2025 12:07:36 -0700 Subject: [PATCH] Allocate memory for frames and sizes during stack trace fix-up when no memory is provided PiperOrigin-RevId: 784266735 Change-Id: I537a6472ce284e97734d56ccff1863108ed3eb39 --- absl/debugging/stacktrace.cc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/absl/debugging/stacktrace.cc b/absl/debugging/stacktrace.cc index f71e80cf..30d032a4 100644 --- a/absl/debugging/stacktrace.cc +++ b/absl/debugging/stacktrace.cc @@ -124,6 +124,19 @@ ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int internal_stacktrace::GetStackFrames(void** result, uintptr_t* frames, int* sizes, int max_depth, int skip_count) { if (internal_stacktrace::ShouldFixUpStack()) { + if constexpr (kHaveAlloca) { + // Some implementations of FixUpStack may need to be passed frame + // information from Unwind, even if the caller doesn't need that + // information. We allocate the necessary buffers for such implementations + // here. + const size_t nmax = static_cast(max_depth); + if (frames == nullptr) { + frames = static_cast(alloca(nmax * sizeof(*frames))); + } + if (sizes == nullptr) { + sizes = static_cast(alloca(nmax * sizeof(*sizes))); + } + } size_t depth = static_cast(Unwind( result, frames, sizes, max_depth, skip_count, nullptr, nullptr)); internal_stacktrace::FixUpStack(result, frames, sizes, @@ -141,6 +154,19 @@ internal_stacktrace::GetStackFramesWithContext(void** result, uintptr_t* frames, int skip_count, const void* uc, int* min_dropped_frames) { if (internal_stacktrace::ShouldFixUpStack()) { + if constexpr (kHaveAlloca) { + // Some implementations of FixUpStack may need to be passed frame + // information from Unwind, even if the caller doesn't need that + // information. We allocate the necessary buffers for such implementations + // here. + const size_t nmax = static_cast(max_depth); + if (frames == nullptr) { + frames = static_cast(alloca(nmax * sizeof(*frames))); + } + if (sizes == nullptr) { + sizes = static_cast(alloca(nmax * sizeof(*sizes))); + } + } size_t depth = static_cast(Unwind( result, frames, sizes, max_depth, skip_count, uc, min_dropped_frames)); internal_stacktrace::FixUpStack(result, frames, sizes,