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