/src/abseil-cpp/absl/debugging/internal/stacktrace_x86-inl.inc
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 | | #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_X86_INL_INC_ |
18 | | #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_X86_INL_INC_ |
19 | | |
20 | | #if defined(__linux__) && (defined(__i386__) || defined(__x86_64__)) |
21 | | #include <ucontext.h> // for ucontext_t |
22 | | #endif |
23 | | |
24 | | #if !defined(_WIN32) |
25 | | #include <unistd.h> |
26 | | #endif |
27 | | |
28 | | #include <cassert> |
29 | | #include <cstdint> |
30 | | #include <limits> |
31 | | |
32 | | #include "absl/base/attributes.h" |
33 | | #include "absl/base/macros.h" |
34 | | #include "absl/base/port.h" |
35 | | #include "absl/debugging/internal/address_is_readable.h" |
36 | | #include "absl/debugging/internal/addresses.h" |
37 | | #include "absl/debugging/internal/vdso_support.h" // a no-op on non-elf or non-glibc systems |
38 | | #include "absl/debugging/stacktrace.h" |
39 | | |
40 | | using absl::debugging_internal::AddressIsReadable; |
41 | | |
42 | | #if defined(__linux__) && defined(__i386__) |
43 | | // Count "push %reg" instructions in VDSO __kernel_vsyscall(), |
44 | | // preceding "syscall" or "sysenter". |
45 | | // If __kernel_vsyscall uses frame pointer, answer 0. |
46 | | // |
47 | | // kMaxBytes tells how many instruction bytes of __kernel_vsyscall |
48 | | // to analyze before giving up. Up to kMaxBytes+1 bytes of |
49 | | // instructions could be accessed. |
50 | | // |
51 | | // Here are known __kernel_vsyscall instruction sequences: |
52 | | // |
53 | | // SYSENTER (linux-2.6.26/arch/x86/vdso/vdso32/sysenter.S). |
54 | | // Used on Intel. |
55 | | // 0xffffe400 <__kernel_vsyscall+0>: push %ecx |
56 | | // 0xffffe401 <__kernel_vsyscall+1>: push %edx |
57 | | // 0xffffe402 <__kernel_vsyscall+2>: push %ebp |
58 | | // 0xffffe403 <__kernel_vsyscall+3>: mov %esp,%ebp |
59 | | // 0xffffe405 <__kernel_vsyscall+5>: sysenter |
60 | | // |
61 | | // SYSCALL (see linux-2.6.26/arch/x86/vdso/vdso32/syscall.S). |
62 | | // Used on AMD. |
63 | | // 0xffffe400 <__kernel_vsyscall+0>: push %ebp |
64 | | // 0xffffe401 <__kernel_vsyscall+1>: mov %ecx,%ebp |
65 | | // 0xffffe403 <__kernel_vsyscall+3>: syscall |
66 | | // |
67 | | |
68 | | // The sequence below isn't actually expected in Google fleet, |
69 | | // here only for completeness. Remove this comment from OSS release. |
70 | | |
71 | | // i386 (see linux-2.6.26/arch/x86/vdso/vdso32/int80.S) |
72 | | // 0xffffe400 <__kernel_vsyscall+0>: int $0x80 |
73 | | // 0xffffe401 <__kernel_vsyscall+1>: ret |
74 | | // |
75 | | static const int kMaxBytes = 10; |
76 | | |
77 | | // We use assert()s instead of DCHECK()s -- this is too low level |
78 | | // for DCHECK(). |
79 | | |
80 | | static int CountPushInstructions(const unsigned char *const addr) { |
81 | | int result = 0; |
82 | | for (int i = 0; i < kMaxBytes; ++i) { |
83 | | if (addr[i] == 0x89) { |
84 | | // "mov reg,reg" |
85 | | if (addr[i + 1] == 0xE5) { |
86 | | // Found "mov %esp,%ebp". |
87 | | return 0; |
88 | | } |
89 | | ++i; // Skip register encoding byte. |
90 | | } else if (addr[i] == 0x0F && |
91 | | (addr[i + 1] == 0x34 || addr[i + 1] == 0x05)) { |
92 | | // Found "sysenter" or "syscall". |
93 | | return result; |
94 | | } else if ((addr[i] & 0xF0) == 0x50) { |
95 | | // Found "push %reg". |
96 | | ++result; |
97 | | } else if (addr[i] == 0xCD && addr[i + 1] == 0x80) { |
98 | | // Found "int $0x80" |
99 | | assert(result == 0); |
100 | | return 0; |
101 | | } else { |
102 | | // Unexpected instruction. |
103 | | assert(false && "unexpected instruction in __kernel_vsyscall"); |
104 | | return 0; |
105 | | } |
106 | | } |
107 | | // Unexpected: didn't find SYSENTER or SYSCALL in |
108 | | // [__kernel_vsyscall, __kernel_vsyscall + kMaxBytes) interval. |
109 | | assert(false && "did not find SYSENTER or SYSCALL in __kernel_vsyscall"); |
110 | | return 0; |
111 | | } |
112 | | #endif |
113 | | |
114 | | // Assume stack frames larger than 100,000 bytes are bogus. |
115 | | static const int kMaxFrameBytes = 100000; |
116 | | // Stack end to use when we don't know the actual stack end |
117 | | // (effectively just the end of address space). |
118 | | constexpr uintptr_t kUnknownStackEnd = |
119 | | std::numeric_limits<size_t>::max() - sizeof(void *); |
120 | | |
121 | | // Returns the stack frame pointer from signal context, 0 if unknown. |
122 | | // vuc is a ucontext_t *. We use void* to avoid the use |
123 | | // of ucontext_t on non-POSIX systems. |
124 | 0 | static uintptr_t GetFP(const void *vuc) { |
125 | | #if !defined(__linux__) |
126 | | static_cast<void>(vuc); // Avoid an unused argument compiler warning. |
127 | | #else |
128 | 0 | if (vuc != nullptr) { |
129 | 0 | auto *uc = reinterpret_cast<const ucontext_t *>(vuc); |
130 | | #if defined(__i386__) |
131 | | const auto bp = uc->uc_mcontext.gregs[REG_EBP]; |
132 | | const auto sp = uc->uc_mcontext.gregs[REG_ESP]; |
133 | | #elif defined(__x86_64__) |
134 | | const auto bp = uc->uc_mcontext.gregs[REG_RBP]; |
135 | 0 | const auto sp = uc->uc_mcontext.gregs[REG_RSP]; |
136 | | #else |
137 | | const uintptr_t bp = 0; |
138 | | const uintptr_t sp = 0; |
139 | | #endif |
140 | | // Sanity-check that the base pointer is valid. It's possible that some |
141 | | // code in the process is compiled with --copt=-fomit-frame-pointer or |
142 | | // --copt=-momit-leaf-frame-pointer. |
143 | | // |
144 | | // TODO(bcmills): -momit-leaf-frame-pointer is currently the default |
145 | | // behavior when building with clang. Talk to the C++ toolchain team about |
146 | | // fixing that. |
147 | 0 | if (bp >= sp && bp - sp <= kMaxFrameBytes) |
148 | 0 | return static_cast<uintptr_t>(bp); |
149 | | |
150 | | // If bp isn't a plausible frame pointer, return the stack pointer instead. |
151 | | // If we're lucky, it points to the start of a stack frame; otherwise, we'll |
152 | | // get one frame of garbage in the stack trace and fail the sanity check on |
153 | | // the next iteration. |
154 | 0 | return static_cast<uintptr_t>(sp); |
155 | 0 | } |
156 | 0 | #endif |
157 | 0 | return 0; |
158 | 0 | } |
159 | | |
160 | | // Given a pointer to a stack frame, locate and return the calling |
161 | | // stackframe, or return null if no stackframe can be found. Perform sanity |
162 | | // checks (the strictness of which is controlled by the boolean parameter |
163 | | // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. |
164 | | template <bool STRICT_UNWINDING, bool WITH_CONTEXT> |
165 | | ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack. |
166 | | ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack. |
167 | | ABSL_ATTRIBUTE_NO_SANITIZE_THREAD // May read random elements from stack. |
168 | | static void **NextStackFrame(void **old_fp, const void *uc, |
169 | 21.4k | size_t stack_low, size_t stack_high) { |
170 | 21.4k | void **new_fp = (void **)*old_fp; |
171 | | |
172 | | #if defined(__linux__) && defined(__i386__) |
173 | | if (WITH_CONTEXT && uc != nullptr) { |
174 | | // How many "push %reg" instructions are there at __kernel_vsyscall? |
175 | | // This is constant for a given kernel and processor, so compute |
176 | | // it only once. |
177 | | static int num_push_instructions = -1; // Sentinel: not computed yet. |
178 | | // Initialize with sentinel value: __kernel_rt_sigreturn can not possibly |
179 | | // be there. |
180 | | static const unsigned char *kernel_rt_sigreturn_address = nullptr; |
181 | | static const unsigned char *kernel_vsyscall_address = nullptr; |
182 | | if (num_push_instructions == -1) { |
183 | | #ifdef ABSL_HAVE_VDSO_SUPPORT |
184 | | absl::debugging_internal::VDSOSupport vdso; |
185 | | if (vdso.IsPresent()) { |
186 | | absl::debugging_internal::VDSOSupport::SymbolInfo |
187 | | rt_sigreturn_symbol_info; |
188 | | absl::debugging_internal::VDSOSupport::SymbolInfo vsyscall_symbol_info; |
189 | | if (!vdso.LookupSymbol("__kernel_rt_sigreturn", "LINUX_2.5", STT_FUNC, |
190 | | &rt_sigreturn_symbol_info) || |
191 | | !vdso.LookupSymbol("__kernel_vsyscall", "LINUX_2.5", STT_FUNC, |
192 | | &vsyscall_symbol_info) || |
193 | | rt_sigreturn_symbol_info.address == nullptr || |
194 | | vsyscall_symbol_info.address == nullptr) { |
195 | | // Unexpected: 32-bit VDSO is present, yet one of the expected |
196 | | // symbols is missing or null. |
197 | | assert(false && "VDSO is present, but doesn't have expected symbols"); |
198 | | num_push_instructions = 0; |
199 | | } else { |
200 | | kernel_rt_sigreturn_address = |
201 | | reinterpret_cast<const unsigned char *>( |
202 | | rt_sigreturn_symbol_info.address); |
203 | | kernel_vsyscall_address = |
204 | | reinterpret_cast<const unsigned char *>( |
205 | | vsyscall_symbol_info.address); |
206 | | num_push_instructions = |
207 | | CountPushInstructions(kernel_vsyscall_address); |
208 | | } |
209 | | } else { |
210 | | num_push_instructions = 0; |
211 | | } |
212 | | #else // ABSL_HAVE_VDSO_SUPPORT |
213 | | num_push_instructions = 0; |
214 | | #endif // ABSL_HAVE_VDSO_SUPPORT |
215 | | } |
216 | | if (num_push_instructions != 0 && kernel_rt_sigreturn_address != nullptr && |
217 | | old_fp[1] == kernel_rt_sigreturn_address) { |
218 | | const ucontext_t *ucv = static_cast<const ucontext_t *>(uc); |
219 | | // This kernel does not use frame pointer in its VDSO code, |
220 | | // and so %ebp is not suitable for unwinding. |
221 | | void **const reg_ebp = |
222 | | reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_EBP]); |
223 | | const unsigned char *const reg_eip = |
224 | | reinterpret_cast<unsigned char *>(ucv->uc_mcontext.gregs[REG_EIP]); |
225 | | if (new_fp == reg_ebp && kernel_vsyscall_address <= reg_eip && |
226 | | reg_eip - kernel_vsyscall_address < kMaxBytes) { |
227 | | // We "stepped up" to __kernel_vsyscall, but %ebp is not usable. |
228 | | // Restore from 'ucv' instead. |
229 | | void **const reg_esp = |
230 | | reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_ESP]); |
231 | | // Check that alleged %esp is not null and is reasonably aligned. |
232 | | if (reg_esp && |
233 | | ((uintptr_t)reg_esp & (sizeof(reg_esp) - 1)) == 0) { |
234 | | // Check that alleged %esp is actually readable. This is to prevent |
235 | | // "double fault" in case we hit the first fault due to e.g. stack |
236 | | // corruption. |
237 | | void *const reg_esp2 = reg_esp[num_push_instructions - 1]; |
238 | | if (AddressIsReadable(reg_esp2)) { |
239 | | // Alleged %esp is readable, use it for further unwinding. |
240 | | new_fp = reinterpret_cast<void **>(reg_esp2); |
241 | | } |
242 | | } |
243 | | } |
244 | | } |
245 | | } |
246 | | #endif |
247 | | |
248 | 21.4k | const uintptr_t old_fp_u = reinterpret_cast<uintptr_t>(old_fp); |
249 | 21.4k | const uintptr_t new_fp_u = reinterpret_cast<uintptr_t>(new_fp); |
250 | | |
251 | | // Check that the transition from frame pointer old_fp to frame |
252 | | // pointer new_fp isn't clearly bogus. Skip the checks if new_fp |
253 | | // matches the signal context, so that we don't skip out early when |
254 | | // using an alternate signal stack. |
255 | | // |
256 | | // TODO(bcmills): The GetFP call should be completely unnecessary when |
257 | | // ENABLE_COMBINED_UNWINDER is set (because we should be back in the thread's |
258 | | // stack by this point), but it is empirically still needed (e.g. when the |
259 | | // stack includes a call to abort). unw_get_reg returns UNW_EBADREG for some |
260 | | // frames. Figure out why GetValidFrameAddr and/or libunwind isn't doing what |
261 | | // it's supposed to. |
262 | 21.4k | if (STRICT_UNWINDING && |
263 | 21.4k | (!WITH_CONTEXT || uc == nullptr || new_fp_u != GetFP(uc))) { |
264 | | // With the stack growing downwards, older stack frame should be |
265 | | // at a greater address that the current one. However if we get multiple |
266 | | // signals handled on altstack the new frame pointer might return to the |
267 | | // main stack, but be different than the value from the most recent |
268 | | // ucontext. |
269 | | // If we get a very large frame size, it may be an indication that we |
270 | | // guessed frame pointers incorrectly and now risk a paging fault |
271 | | // dereferencing a wrong frame pointer. Or maybe not because large frames |
272 | | // are possible as well. The main stack is assumed to be readable, |
273 | | // so we assume the large frame is legit if we know the real stack bounds |
274 | | // and are within the stack. |
275 | 21.4k | if (new_fp_u <= old_fp_u || new_fp_u - old_fp_u > kMaxFrameBytes) { |
276 | 1.18k | if (stack_high < kUnknownStackEnd && |
277 | 1.18k | static_cast<size_t>(getpagesize()) < stack_low) { |
278 | | // Stack bounds are known. |
279 | 0 | if (!(stack_low < new_fp_u && new_fp_u <= stack_high)) { |
280 | | // new_fp_u is not within the known stack. |
281 | 0 | return nullptr; |
282 | 0 | } |
283 | 1.18k | } else { |
284 | | // Stack bounds are unknown, prefer truncated stack to possible crash. |
285 | 1.18k | return nullptr; |
286 | 1.18k | } |
287 | 1.18k | } |
288 | 20.2k | if (stack_low < old_fp_u && old_fp_u <= stack_high) { |
289 | | // Old BP was in the expected stack region... |
290 | 20.2k | if (!(stack_low < new_fp_u && new_fp_u <= stack_high)) { |
291 | | // ... but new BP is outside of expected stack region. |
292 | | // It is most likely bogus. |
293 | 0 | return nullptr; |
294 | 0 | } |
295 | 20.2k | } else { |
296 | | // We may be here if we are executing in a co-routine with a |
297 | | // separate stack. We can't do safety checks in this case. |
298 | 0 | } |
299 | 20.2k | } else { |
300 | 0 | if (new_fp == nullptr) return nullptr; // skip AddressIsReadable() below |
301 | | // In the non-strict mode, allow discontiguous stack frames. |
302 | | // (alternate-signal-stacks for example). |
303 | 0 | if (new_fp == old_fp) return nullptr; |
304 | 0 | } |
305 | | |
306 | 20.2k | if (new_fp_u & (sizeof(void *) - 1)) return nullptr; |
307 | | #ifdef __i386__ |
308 | | // On 32-bit machines, the stack pointer can be very close to |
309 | | // 0xffffffff, so we explicitly check for a pointer into the |
310 | | // last two pages in the address space |
311 | | if (new_fp_u >= 0xffffe000) return nullptr; |
312 | | #endif |
313 | 20.2k | #if !defined(_WIN32) |
314 | 20.2k | if (!STRICT_UNWINDING) { |
315 | | // Lax sanity checks cause a crash in 32-bit tcmalloc/crash_reason_test |
316 | | // on AMD-based machines with VDSO-enabled kernels. |
317 | | // Make an extra sanity check to insure new_fp is readable. |
318 | | // Note: NextStackFrame<false>() is only called while the program |
319 | | // is already on its last leg, so it's ok to be slow here. |
320 | |
|
321 | 0 | if (!AddressIsReadable(new_fp)) { |
322 | 0 | return nullptr; |
323 | 0 | } |
324 | 0 | } |
325 | 20.2k | #endif |
326 | 20.2k | return new_fp; |
327 | 20.2k | } stacktrace.cc:void** NextStackFrame<true, false>(void**, void const*, unsigned long, unsigned long) Line | Count | Source | 169 | 21.4k | size_t stack_low, size_t stack_high) { | 170 | 21.4k | void **new_fp = (void **)*old_fp; | 171 | | | 172 | | #if defined(__linux__) && defined(__i386__) | 173 | | if (WITH_CONTEXT && uc != nullptr) { | 174 | | // How many "push %reg" instructions are there at __kernel_vsyscall? | 175 | | // This is constant for a given kernel and processor, so compute | 176 | | // it only once. | 177 | | static int num_push_instructions = -1; // Sentinel: not computed yet. | 178 | | // Initialize with sentinel value: __kernel_rt_sigreturn can not possibly | 179 | | // be there. | 180 | | static const unsigned char *kernel_rt_sigreturn_address = nullptr; | 181 | | static const unsigned char *kernel_vsyscall_address = nullptr; | 182 | | if (num_push_instructions == -1) { | 183 | | #ifdef ABSL_HAVE_VDSO_SUPPORT | 184 | | absl::debugging_internal::VDSOSupport vdso; | 185 | | if (vdso.IsPresent()) { | 186 | | absl::debugging_internal::VDSOSupport::SymbolInfo | 187 | | rt_sigreturn_symbol_info; | 188 | | absl::debugging_internal::VDSOSupport::SymbolInfo vsyscall_symbol_info; | 189 | | if (!vdso.LookupSymbol("__kernel_rt_sigreturn", "LINUX_2.5", STT_FUNC, | 190 | | &rt_sigreturn_symbol_info) || | 191 | | !vdso.LookupSymbol("__kernel_vsyscall", "LINUX_2.5", STT_FUNC, | 192 | | &vsyscall_symbol_info) || | 193 | | rt_sigreturn_symbol_info.address == nullptr || | 194 | | vsyscall_symbol_info.address == nullptr) { | 195 | | // Unexpected: 32-bit VDSO is present, yet one of the expected | 196 | | // symbols is missing or null. | 197 | | assert(false && "VDSO is present, but doesn't have expected symbols"); | 198 | | num_push_instructions = 0; | 199 | | } else { | 200 | | kernel_rt_sigreturn_address = | 201 | | reinterpret_cast<const unsigned char *>( | 202 | | rt_sigreturn_symbol_info.address); | 203 | | kernel_vsyscall_address = | 204 | | reinterpret_cast<const unsigned char *>( | 205 | | vsyscall_symbol_info.address); | 206 | | num_push_instructions = | 207 | | CountPushInstructions(kernel_vsyscall_address); | 208 | | } | 209 | | } else { | 210 | | num_push_instructions = 0; | 211 | | } | 212 | | #else // ABSL_HAVE_VDSO_SUPPORT | 213 | | num_push_instructions = 0; | 214 | | #endif // ABSL_HAVE_VDSO_SUPPORT | 215 | | } | 216 | | if (num_push_instructions != 0 && kernel_rt_sigreturn_address != nullptr && | 217 | | old_fp[1] == kernel_rt_sigreturn_address) { | 218 | | const ucontext_t *ucv = static_cast<const ucontext_t *>(uc); | 219 | | // This kernel does not use frame pointer in its VDSO code, | 220 | | // and so %ebp is not suitable for unwinding. | 221 | | void **const reg_ebp = | 222 | | reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_EBP]); | 223 | | const unsigned char *const reg_eip = | 224 | | reinterpret_cast<unsigned char *>(ucv->uc_mcontext.gregs[REG_EIP]); | 225 | | if (new_fp == reg_ebp && kernel_vsyscall_address <= reg_eip && | 226 | | reg_eip - kernel_vsyscall_address < kMaxBytes) { | 227 | | // We "stepped up" to __kernel_vsyscall, but %ebp is not usable. | 228 | | // Restore from 'ucv' instead. | 229 | | void **const reg_esp = | 230 | | reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_ESP]); | 231 | | // Check that alleged %esp is not null and is reasonably aligned. | 232 | | if (reg_esp && | 233 | | ((uintptr_t)reg_esp & (sizeof(reg_esp) - 1)) == 0) { | 234 | | // Check that alleged %esp is actually readable. This is to prevent | 235 | | // "double fault" in case we hit the first fault due to e.g. stack | 236 | | // corruption. | 237 | | void *const reg_esp2 = reg_esp[num_push_instructions - 1]; | 238 | | if (AddressIsReadable(reg_esp2)) { | 239 | | // Alleged %esp is readable, use it for further unwinding. | 240 | | new_fp = reinterpret_cast<void **>(reg_esp2); | 241 | | } | 242 | | } | 243 | | } | 244 | | } | 245 | | } | 246 | | #endif | 247 | | | 248 | 21.4k | const uintptr_t old_fp_u = reinterpret_cast<uintptr_t>(old_fp); | 249 | 21.4k | const uintptr_t new_fp_u = reinterpret_cast<uintptr_t>(new_fp); | 250 | | | 251 | | // Check that the transition from frame pointer old_fp to frame | 252 | | // pointer new_fp isn't clearly bogus. Skip the checks if new_fp | 253 | | // matches the signal context, so that we don't skip out early when | 254 | | // using an alternate signal stack. | 255 | | // | 256 | | // TODO(bcmills): The GetFP call should be completely unnecessary when | 257 | | // ENABLE_COMBINED_UNWINDER is set (because we should be back in the thread's | 258 | | // stack by this point), but it is empirically still needed (e.g. when the | 259 | | // stack includes a call to abort). unw_get_reg returns UNW_EBADREG for some | 260 | | // frames. Figure out why GetValidFrameAddr and/or libunwind isn't doing what | 261 | | // it's supposed to. | 262 | 21.4k | if (STRICT_UNWINDING && | 263 | 21.4k | (!WITH_CONTEXT || uc == nullptr || new_fp_u != GetFP(uc))) { | 264 | | // With the stack growing downwards, older stack frame should be | 265 | | // at a greater address that the current one. However if we get multiple | 266 | | // signals handled on altstack the new frame pointer might return to the | 267 | | // main stack, but be different than the value from the most recent | 268 | | // ucontext. | 269 | | // If we get a very large frame size, it may be an indication that we | 270 | | // guessed frame pointers incorrectly and now risk a paging fault | 271 | | // dereferencing a wrong frame pointer. Or maybe not because large frames | 272 | | // are possible as well. The main stack is assumed to be readable, | 273 | | // so we assume the large frame is legit if we know the real stack bounds | 274 | | // and are within the stack. | 275 | 21.4k | if (new_fp_u <= old_fp_u || new_fp_u - old_fp_u > kMaxFrameBytes) { | 276 | 1.18k | if (stack_high < kUnknownStackEnd && | 277 | 1.18k | static_cast<size_t>(getpagesize()) < stack_low) { | 278 | | // Stack bounds are known. | 279 | 0 | if (!(stack_low < new_fp_u && new_fp_u <= stack_high)) { | 280 | | // new_fp_u is not within the known stack. | 281 | 0 | return nullptr; | 282 | 0 | } | 283 | 1.18k | } else { | 284 | | // Stack bounds are unknown, prefer truncated stack to possible crash. | 285 | 1.18k | return nullptr; | 286 | 1.18k | } | 287 | 1.18k | } | 288 | 20.2k | if (stack_low < old_fp_u && old_fp_u <= stack_high) { | 289 | | // Old BP was in the expected stack region... | 290 | 20.2k | if (!(stack_low < new_fp_u && new_fp_u <= stack_high)) { | 291 | | // ... but new BP is outside of expected stack region. | 292 | | // It is most likely bogus. | 293 | 0 | return nullptr; | 294 | 0 | } | 295 | 20.2k | } else { | 296 | | // We may be here if we are executing in a co-routine with a | 297 | | // separate stack. We can't do safety checks in this case. | 298 | 0 | } | 299 | 20.2k | } else { | 300 | 0 | if (new_fp == nullptr) return nullptr; // skip AddressIsReadable() below | 301 | | // In the non-strict mode, allow discontiguous stack frames. | 302 | | // (alternate-signal-stacks for example). | 303 | 0 | if (new_fp == old_fp) return nullptr; | 304 | 0 | } | 305 | | | 306 | 20.2k | if (new_fp_u & (sizeof(void *) - 1)) return nullptr; | 307 | | #ifdef __i386__ | 308 | | // On 32-bit machines, the stack pointer can be very close to | 309 | | // 0xffffffff, so we explicitly check for a pointer into the | 310 | | // last two pages in the address space | 311 | | if (new_fp_u >= 0xffffe000) return nullptr; | 312 | | #endif | 313 | 20.2k | #if !defined(_WIN32) | 314 | 20.2k | if (!STRICT_UNWINDING) { | 315 | | // Lax sanity checks cause a crash in 32-bit tcmalloc/crash_reason_test | 316 | | // on AMD-based machines with VDSO-enabled kernels. | 317 | | // Make an extra sanity check to insure new_fp is readable. | 318 | | // Note: NextStackFrame<false>() is only called while the program | 319 | | // is already on its last leg, so it's ok to be slow here. | 320 | |
| 321 | 0 | if (!AddressIsReadable(new_fp)) { | 322 | 0 | return nullptr; | 323 | 0 | } | 324 | 0 | } | 325 | 20.2k | #endif | 326 | 20.2k | return new_fp; | 327 | 20.2k | } |
Unexecuted instantiation: stacktrace.cc:void** NextStackFrame<true, true>(void**, void const*, unsigned long, unsigned long) Unexecuted instantiation: stacktrace.cc:void** NextStackFrame<false, false>(void**, void const*, unsigned long, unsigned long) Unexecuted instantiation: stacktrace.cc:void** NextStackFrame<false, true>(void**, void const*, unsigned long, unsigned long) |
328 | | |
329 | | template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT> |
330 | | ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack. |
331 | | ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack. |
332 | | ABSL_ATTRIBUTE_NO_SANITIZE_THREAD // May read random elements from stack. |
333 | | ABSL_ATTRIBUTE_NOINLINE |
334 | | static int UnwindImpl(void **result, uintptr_t *frames, int *sizes, |
335 | | int max_depth, int skip_count, const void *ucp, |
336 | 1.18k | int *min_dropped_frames) { |
337 | 1.18k | int n = 0; |
338 | 1.18k | void **fp = reinterpret_cast<void **>(__builtin_frame_address(0)); |
339 | | |
340 | | // Assume that the first page is not stack. |
341 | 1.18k | size_t stack_low = static_cast<size_t>(getpagesize()); |
342 | 1.18k | size_t stack_high = kUnknownStackEnd; |
343 | | |
344 | 22.6k | while (fp && n < max_depth) { |
345 | 21.4k | if (*(fp + 1) == reinterpret_cast<void *>(0)) { |
346 | | // In 64-bit code, we often see a frame that |
347 | | // points to itself and has a return address of 0. |
348 | 0 | break; |
349 | 0 | } |
350 | 21.4k | void **next_fp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>( |
351 | 21.4k | fp, ucp, stack_low, stack_high); |
352 | 21.4k | if (skip_count > 0) { |
353 | 4.74k | skip_count--; |
354 | 16.7k | } else { |
355 | 16.7k | result[n] = *(fp + 1); |
356 | 16.7k | if (IS_STACK_FRAMES) { |
357 | 0 | if (frames) { |
358 | 0 | frames[n] = absl::debugging_internal::StripPointerMetadata(fp) + |
359 | 0 | 2 * sizeof(void *) /* go past the return address */; |
360 | 0 | } |
361 | 0 | if (sizes) { |
362 | 0 | if (next_fp > fp) { |
363 | 0 | sizes[n] = static_cast<int>( |
364 | 0 | absl::debugging_internal::StripPointerMetadata(next_fp) - |
365 | 0 | absl::debugging_internal::StripPointerMetadata(fp)); |
366 | 0 | } else { |
367 | | // A frame-size of 0 is used to indicate unknown frame size. |
368 | 0 | sizes[n] = 0; |
369 | 0 | } |
370 | 0 | } |
371 | 0 | } |
372 | 16.7k | n++; |
373 | 16.7k | } |
374 | 21.4k | fp = next_fp; |
375 | 21.4k | } |
376 | 1.18k | if (min_dropped_frames != nullptr) { |
377 | | // Implementation detail: we clamp the max of frames we are willing to |
378 | | // count, so as not to spend too much time in the loop below. |
379 | 0 | const int kMaxUnwind = 1000; |
380 | 0 | int num_dropped_frames = 0; |
381 | 0 | for (int j = 0; fp != nullptr && j < kMaxUnwind; j++) { |
382 | 0 | if (skip_count > 0) { |
383 | 0 | skip_count--; |
384 | 0 | } else { |
385 | 0 | num_dropped_frames++; |
386 | 0 | } |
387 | 0 | fp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(fp, ucp, stack_low, |
388 | 0 | stack_high); |
389 | 0 | } |
390 | 0 | *min_dropped_frames = num_dropped_frames; |
391 | 0 | } |
392 | 1.18k | return n; |
393 | 1.18k | } stacktrace.cc:int UnwindImpl<false, false>(void**, unsigned long*, int*, int, int, void const*, int*) Line | Count | Source | 336 | 1.18k | int *min_dropped_frames) { | 337 | 1.18k | int n = 0; | 338 | 1.18k | void **fp = reinterpret_cast<void **>(__builtin_frame_address(0)); | 339 | | | 340 | | // Assume that the first page is not stack. | 341 | 1.18k | size_t stack_low = static_cast<size_t>(getpagesize()); | 342 | 1.18k | size_t stack_high = kUnknownStackEnd; | 343 | | | 344 | 22.6k | while (fp && n < max_depth) { | 345 | 21.4k | if (*(fp + 1) == reinterpret_cast<void *>(0)) { | 346 | | // In 64-bit code, we often see a frame that | 347 | | // points to itself and has a return address of 0. | 348 | 0 | break; | 349 | 0 | } | 350 | 21.4k | void **next_fp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>( | 351 | 21.4k | fp, ucp, stack_low, stack_high); | 352 | 21.4k | if (skip_count > 0) { | 353 | 4.74k | skip_count--; | 354 | 16.7k | } else { | 355 | 16.7k | result[n] = *(fp + 1); | 356 | 16.7k | if (IS_STACK_FRAMES) { | 357 | 0 | if (frames) { | 358 | 0 | frames[n] = absl::debugging_internal::StripPointerMetadata(fp) + | 359 | 0 | 2 * sizeof(void *) /* go past the return address */; | 360 | 0 | } | 361 | 0 | if (sizes) { | 362 | 0 | if (next_fp > fp) { | 363 | 0 | sizes[n] = static_cast<int>( | 364 | 0 | absl::debugging_internal::StripPointerMetadata(next_fp) - | 365 | 0 | absl::debugging_internal::StripPointerMetadata(fp)); | 366 | 0 | } else { | 367 | | // A frame-size of 0 is used to indicate unknown frame size. | 368 | 0 | sizes[n] = 0; | 369 | 0 | } | 370 | 0 | } | 371 | 0 | } | 372 | 16.7k | n++; | 373 | 16.7k | } | 374 | 21.4k | fp = next_fp; | 375 | 21.4k | } | 376 | 1.18k | if (min_dropped_frames != nullptr) { | 377 | | // Implementation detail: we clamp the max of frames we are willing to | 378 | | // count, so as not to spend too much time in the loop below. | 379 | 0 | const int kMaxUnwind = 1000; | 380 | 0 | int num_dropped_frames = 0; | 381 | 0 | for (int j = 0; fp != nullptr && j < kMaxUnwind; j++) { | 382 | 0 | if (skip_count > 0) { | 383 | 0 | skip_count--; | 384 | 0 | } else { | 385 | 0 | num_dropped_frames++; | 386 | 0 | } | 387 | 0 | fp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(fp, ucp, stack_low, | 388 | 0 | stack_high); | 389 | 0 | } | 390 | 0 | *min_dropped_frames = num_dropped_frames; | 391 | 0 | } | 392 | 1.18k | return n; | 393 | 1.18k | } |
Unexecuted instantiation: stacktrace.cc:int UnwindImpl<false, true>(void**, unsigned long*, int*, int, int, void const*, int*) Unexecuted instantiation: stacktrace.cc:int UnwindImpl<true, false>(void**, unsigned long*, int*, int, int, void const*, int*) Unexecuted instantiation: stacktrace.cc:int UnwindImpl<true, true>(void**, unsigned long*, int*, int, int, void const*, int*) |
394 | | |
395 | | namespace absl { |
396 | | ABSL_NAMESPACE_BEGIN |
397 | | namespace debugging_internal { |
398 | 0 | bool StackTraceWorksForTest() { |
399 | 0 | return true; |
400 | 0 | } |
401 | | } // namespace debugging_internal |
402 | | ABSL_NAMESPACE_END |
403 | | } // namespace absl |
404 | | |
405 | | #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_X86_INL_INC_ |