Coverage Report

Created: 2025-08-25 06:55

/src/abseil-cpp/absl/debugging/stacktrace.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2017 The Abseil Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
// Produce stack trace.
16
//
17
// There are three different ways we can try to get the stack trace:
18
//
19
// 1) Our hand-coded stack-unwinder.  This depends on a certain stack
20
//    layout, which is used by gcc (and those systems using a
21
//    gcc-compatible ABI) on x86 systems, at least since gcc 2.95.
22
//    It uses the frame pointer to do its work.
23
//
24
// 2) The libunwind library.  This is still in development, and as a
25
//    separate library adds a new dependency, but doesn't need a frame
26
//    pointer.  It also doesn't call malloc.
27
//
28
// 3) The gdb unwinder -- also the one used by the c++ exception code.
29
//    It's obviously well-tested, but has a fatal flaw: it can call
30
//    malloc() from the unwinder.  This is a problem because we're
31
//    trying to use the unwinder to instrument malloc().
32
//
33
// Note: if you add a new implementation here, make sure it works
34
// correctly when absl::GetStackTrace() is called with max_depth == 0.
35
// Some code may do that.
36
37
#include "absl/debugging/stacktrace.h"
38
39
#include <stddef.h>
40
#include <stdint.h>
41
#include <stdlib.h>
42
43
#include <algorithm>
44
#include <atomic>
45
#include <iterator>
46
47
#include "absl/base/attributes.h"
48
#include "absl/base/config.h"
49
#include "absl/base/internal/low_level_alloc.h"
50
#include "absl/base/optimization.h"
51
#include "absl/base/port.h"
52
#include "absl/debugging/internal/stacktrace_config.h"
53
54
#if defined(ABSL_STACKTRACE_INL_HEADER)
55
#include ABSL_STACKTRACE_INL_HEADER
56
#else
57
# error Cannot calculate stack trace: will need to write for your environment
58
59
# include "absl/debugging/internal/stacktrace_aarch64-inl.inc"
60
# include "absl/debugging/internal/stacktrace_arm-inl.inc"
61
# include "absl/debugging/internal/stacktrace_emscripten-inl.inc"
62
# include "absl/debugging/internal/stacktrace_generic-inl.inc"
63
# include "absl/debugging/internal/stacktrace_powerpc-inl.inc"
64
# include "absl/debugging/internal/stacktrace_riscv-inl.inc"
65
# include "absl/debugging/internal/stacktrace_unimplemented-inl.inc"
66
# include "absl/debugging/internal/stacktrace_win32-inl.inc"
67
# include "absl/debugging/internal/stacktrace_x86-inl.inc"
68
#endif
69
70
namespace absl {
71
ABSL_NAMESPACE_BEGIN
72
namespace {
73
74
typedef int (*Unwinder)(void**, int*, int, int, const void*, int*);
75
std::atomic<Unwinder> custom;
76
77
template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
78
ABSL_ATTRIBUTE_ALWAYS_INLINE inline int Unwind(void** result, uintptr_t* frames,
79
                                               int* sizes, size_t max_depth,
80
                                               int skip_count, const void* uc,
81
0
                                               int* min_dropped_frames) {
82
0
  static constexpr size_t kMinPageSize = 4096;
83
84
  // Allow up to ~half a page, leaving some slack space for local variables etc.
85
0
  static constexpr size_t kMaxStackElements =
86
0
      (kMinPageSize / 2) / (sizeof(*frames) + sizeof(*sizes));
87
88
  // Allocate a buffer dynamically, using the signal-safe allocator.
89
0
  static constexpr auto allocate = [](size_t num_bytes) -> void* {
90
0
    base_internal::InitSigSafeArena();
91
0
    return base_internal::LowLevelAlloc::AllocWithArena(
92
0
        num_bytes, base_internal::SigSafeArena());
93
0
  };
Unexecuted instantiation: stacktrace.cc:absl::(anonymous namespace)::Unwind<true, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)::{lambda(unsigned long)#1}::operator()(unsigned long) const
Unexecuted instantiation: stacktrace.cc:absl::(anonymous namespace)::Unwind<true, true>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)::{lambda(unsigned long)#1}::operator()(unsigned long) const
Unexecuted instantiation: stacktrace.cc:absl::(anonymous namespace)::Unwind<false, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)::{lambda(unsigned long)#1}::operator()(unsigned long) const
Unexecuted instantiation: stacktrace.cc:absl::(anonymous namespace)::Unwind<false, true>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)::{lambda(unsigned long)#1}::operator()(unsigned long) const
94
95
0
  uintptr_t frames_stackbuf[kMaxStackElements];
96
0
  int sizes_stackbuf[kMaxStackElements];
97
98
  // We only need to free the buffers if we allocated them with the signal-safe
99
  // allocator.
100
0
  bool must_free_frames = false;
101
0
  bool must_free_sizes = false;
102
103
0
  bool unwind_with_fixup = internal_stacktrace::ShouldFixUpStack();
104
105
#ifdef _WIN32
106
  if (unwind_with_fixup) {
107
    // TODO(b/434184677): Fixups are flaky and not supported on Windows
108
    unwind_with_fixup = false;
109
#ifndef NDEBUG
110
    abort();
111
#endif
112
  }
113
#endif
114
115
0
  if (unwind_with_fixup) {
116
    // Some implementations of FixUpStack may need to be passed frame
117
    // information from Unwind, even if the caller doesn't need that
118
    // information. We allocate the necessary buffers for such implementations
119
    // here.
120
121
0
    if (frames == nullptr) {
122
0
      if (max_depth <= std::size(frames_stackbuf)) {
123
0
        frames = frames_stackbuf;
124
0
      } else {
125
0
        frames = static_cast<uintptr_t*>(allocate(max_depth * sizeof(*frames)));
126
0
        must_free_frames = true;
127
0
      }
128
0
    }
129
130
0
    if (sizes == nullptr) {
131
0
      if (max_depth <= std::size(sizes_stackbuf)) {
132
0
        sizes = sizes_stackbuf;
133
0
      } else {
134
0
        sizes = static_cast<int*>(allocate(max_depth * sizeof(*sizes)));
135
0
        must_free_sizes = true;
136
0
      }
137
0
    }
138
0
  }
139
140
0
  Unwinder g = custom.load(std::memory_order_acquire);
141
0
  size_t size;
142
  // Add 1 to skip count for the unwinder function itself
143
0
  ++skip_count;
144
0
  if (g != nullptr) {
145
0
    size = static_cast<size_t>((*g)(result, sizes, static_cast<int>(max_depth),
146
0
                                    skip_count, uc, min_dropped_frames));
147
    // Frame pointers aren't returned by existing hooks, so clear them.
148
0
    if (frames != nullptr) {
149
0
      std::fill(frames, frames + size, uintptr_t());
150
0
    }
151
0
  } else {
152
0
    size = static_cast<size_t>(
153
0
        unwind_with_fixup
154
0
            ? UnwindImpl<true, IS_WITH_CONTEXT>(
155
0
                  result, frames, sizes, static_cast<int>(max_depth),
156
0
                  skip_count, uc, min_dropped_frames)
157
0
            : UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>(
158
0
                  result, frames, sizes, static_cast<int>(max_depth),
159
0
                  skip_count, uc, min_dropped_frames));
160
0
  }
161
0
  if (unwind_with_fixup) {
162
0
    internal_stacktrace::FixUpStack(result, frames, sizes, max_depth, size);
163
0
  }
164
165
0
  if (must_free_sizes) {
166
0
    base_internal::LowLevelAlloc::Free(sizes);
167
0
  }
168
169
0
  if (must_free_frames) {
170
0
    base_internal::LowLevelAlloc::Free(frames);
171
0
  }
172
173
0
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
174
0
  return static_cast<int>(size);
175
0
}
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<true, true>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, false>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)
Unexecuted instantiation: stacktrace.cc:int absl::(anonymous namespace)::Unwind<false, true>(void**, unsigned long*, int*, unsigned long, int, void const*, int*)
176
177
}  // anonymous namespace
178
179
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
180
internal_stacktrace::GetStackFrames(void** result, uintptr_t* frames,
181
0
                                    int* sizes, int max_depth, int skip_count) {
182
0
  return Unwind<true, false>(result, frames, sizes,
183
0
                             static_cast<size_t>(max_depth), skip_count,
184
0
                             nullptr, nullptr);
185
0
}
186
187
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
188
internal_stacktrace::GetStackFramesWithContext(void** result, uintptr_t* frames,
189
                                               int* sizes, int max_depth,
190
                                               int skip_count, const void* uc,
191
0
                                               int* min_dropped_frames) {
192
0
  return Unwind<true, true>(result, frames, sizes,
193
0
                            static_cast<size_t>(max_depth), skip_count, uc,
194
0
                            min_dropped_frames);
195
0
}
196
197
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackTrace(
198
0
    void** result, int max_depth, int skip_count) {
199
0
  return Unwind<false, false>(result, nullptr, nullptr,
200
0
                              static_cast<size_t>(max_depth), skip_count,
201
0
                              nullptr, nullptr);
202
0
}
203
204
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
205
GetStackTraceWithContext(void** result, int max_depth, int skip_count,
206
0
                         const void* uc, int* min_dropped_frames) {
207
0
  return Unwind<false, true>(result, nullptr, nullptr,
208
0
                             static_cast<size_t>(max_depth), skip_count, uc,
209
0
                             min_dropped_frames);
210
0
}
211
212
0
void SetStackUnwinder(Unwinder w) {
213
0
  custom.store(w, std::memory_order_release);
214
0
}
215
216
ABSL_ATTRIBUTE_ALWAYS_INLINE static inline int DefaultStackUnwinderImpl(
217
    void** pcs, uintptr_t* frames, int* sizes, int depth, int skip,
218
0
    const void* uc, int* min_dropped_frames) {
219
0
  skip++;  // For this function
220
0
  decltype(&UnwindImpl<false, false>) f;
221
0
  if (sizes == nullptr) {
222
0
    if (uc == nullptr) {
223
0
      f = &UnwindImpl<false, false>;
224
0
    } else {
225
0
      f = &UnwindImpl<false, true>;
226
0
    }
227
0
  } else {
228
0
    if (uc == nullptr) {
229
0
      f = &UnwindImpl<true, false>;
230
0
    } else {
231
0
      f = &UnwindImpl<true, true>;
232
0
    }
233
0
  }
234
0
  return (*f)(pcs, frames, sizes, depth, skip, uc, min_dropped_frames);
235
0
}
236
237
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int
238
internal_stacktrace::DefaultStackUnwinder(void** pcs, uintptr_t* frames,
239
                                          int* sizes, int depth, int skip,
240
                                          const void* uc,
241
0
                                          int* min_dropped_frames) {
242
0
  int n = DefaultStackUnwinderImpl(pcs, frames, sizes, depth, skip, uc,
243
0
                                   min_dropped_frames);
244
0
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
245
0
  return n;
246
0
}
247
248
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int DefaultStackUnwinder(
249
    void** pcs, int* sizes, int depth, int skip, const void* uc,
250
0
    int* min_dropped_frames) {
251
0
  int n = DefaultStackUnwinderImpl(pcs, nullptr, sizes, depth, skip, uc,
252
0
                                   min_dropped_frames);
253
0
  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
254
0
  return n;
255
0
}
256
257
0
ABSL_ATTRIBUTE_WEAK bool internal_stacktrace::ShouldFixUpStack() {
258
0
  return false;
259
0
}
260
261
// Fixes up the stack trace of the current thread, in the first `depth` frames
262
// of each buffer. The buffers need to be larger than `depth`, to accommodate
263
// any newly inserted elements. `depth` is updated to reflect the new number of
264
// elements valid across all the buffers. (It is therefore recommended that all
265
// buffer sizes be equal.)
266
//
267
// The `frames` and `sizes` parameters denote the bounds of the stack frame
268
// corresponding to each instruction pointer in the `pcs`.
269
// Any elements inside these buffers may be zero or null, in which case that
270
// information is assumed to be absent/unavailable.
271
ABSL_ATTRIBUTE_WEAK void internal_stacktrace::FixUpStack(void**, uintptr_t*,
272
                                                         int*, size_t,
273
0
                                                         size_t&) {}
274
275
ABSL_NAMESPACE_END
276
}  // namespace absl