Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/absl/debugging/internal/stacktrace_x86-inl.inc is written in an unsupported language. File is not indexed.

0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // Produce stack trace
0016 
0017 #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_X86_INL_INC_
0018 #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_X86_INL_INC_
0019 
0020 #if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
0021 #include <ucontext.h>  // for ucontext_t
0022 #endif
0023 
0024 #if !defined(_WIN32)
0025 #include <unistd.h>
0026 #endif
0027 
0028 #include <cassert>
0029 #include <cstdint>
0030 #include <limits>
0031 
0032 #include "absl/base/attributes.h"
0033 #include "absl/base/macros.h"
0034 #include "absl/base/port.h"
0035 #include "absl/debugging/internal/address_is_readable.h"
0036 #include "absl/debugging/internal/vdso_support.h"  // a no-op on non-elf or non-glibc systems
0037 #include "absl/debugging/stacktrace.h"
0038 
0039 using absl::debugging_internal::AddressIsReadable;
0040 
0041 #if defined(__linux__) && defined(__i386__)
0042 // Count "push %reg" instructions in VDSO __kernel_vsyscall(),
0043 // preceding "syscall" or "sysenter".
0044 // If __kernel_vsyscall uses frame pointer, answer 0.
0045 //
0046 // kMaxBytes tells how many instruction bytes of __kernel_vsyscall
0047 // to analyze before giving up. Up to kMaxBytes+1 bytes of
0048 // instructions could be accessed.
0049 //
0050 // Here are known __kernel_vsyscall instruction sequences:
0051 //
0052 // SYSENTER (linux-2.6.26/arch/x86/vdso/vdso32/sysenter.S).
0053 // Used on Intel.
0054 //  0xffffe400 <__kernel_vsyscall+0>:       push   %ecx
0055 //  0xffffe401 <__kernel_vsyscall+1>:       push   %edx
0056 //  0xffffe402 <__kernel_vsyscall+2>:       push   %ebp
0057 //  0xffffe403 <__kernel_vsyscall+3>:       mov    %esp,%ebp
0058 //  0xffffe405 <__kernel_vsyscall+5>:       sysenter
0059 //
0060 // SYSCALL (see linux-2.6.26/arch/x86/vdso/vdso32/syscall.S).
0061 // Used on AMD.
0062 //  0xffffe400 <__kernel_vsyscall+0>:       push   %ebp
0063 //  0xffffe401 <__kernel_vsyscall+1>:       mov    %ecx,%ebp
0064 //  0xffffe403 <__kernel_vsyscall+3>:       syscall
0065 //
0066 
0067 // The sequence below isn't actually expected in Google fleet,
0068 // here only for completeness. Remove this comment from OSS release.
0069 
0070 // i386 (see linux-2.6.26/arch/x86/vdso/vdso32/int80.S)
0071 //  0xffffe400 <__kernel_vsyscall+0>:       int $0x80
0072 //  0xffffe401 <__kernel_vsyscall+1>:       ret
0073 //
0074 static const int kMaxBytes = 10;
0075 
0076 // We use assert()s instead of DCHECK()s -- this is too low level
0077 // for DCHECK().
0078 
0079 static int CountPushInstructions(const unsigned char *const addr) {
0080   int result = 0;
0081   for (int i = 0; i < kMaxBytes; ++i) {
0082     if (addr[i] == 0x89) {
0083       // "mov reg,reg"
0084       if (addr[i + 1] == 0xE5) {
0085         // Found "mov %esp,%ebp".
0086         return 0;
0087       }
0088       ++i;  // Skip register encoding byte.
0089     } else if (addr[i] == 0x0F &&
0090                (addr[i + 1] == 0x34 || addr[i + 1] == 0x05)) {
0091       // Found "sysenter" or "syscall".
0092       return result;
0093     } else if ((addr[i] & 0xF0) == 0x50) {
0094       // Found "push %reg".
0095       ++result;
0096     } else if (addr[i] == 0xCD && addr[i + 1] == 0x80) {
0097       // Found "int $0x80"
0098       assert(result == 0);
0099       return 0;
0100     } else {
0101       // Unexpected instruction.
0102       assert(false && "unexpected instruction in __kernel_vsyscall");
0103       return 0;
0104     }
0105   }
0106   // Unexpected: didn't find SYSENTER or SYSCALL in
0107   // [__kernel_vsyscall, __kernel_vsyscall + kMaxBytes) interval.
0108   assert(false && "did not find SYSENTER or SYSCALL in __kernel_vsyscall");
0109   return 0;
0110 }
0111 #endif
0112 
0113 // Assume stack frames larger than 100,000 bytes are bogus.
0114 static const int kMaxFrameBytes = 100000;
0115 // Stack end to use when we don't know the actual stack end
0116 // (effectively just the end of address space).
0117 constexpr uintptr_t kUnknownStackEnd =
0118     std::numeric_limits<size_t>::max() - sizeof(void *);
0119 
0120 // Returns the stack frame pointer from signal context, 0 if unknown.
0121 // vuc is a ucontext_t *.  We use void* to avoid the use
0122 // of ucontext_t on non-POSIX systems.
0123 static uintptr_t GetFP(const void *vuc) {
0124 #if !defined(__linux__)
0125   static_cast<void>(vuc);  // Avoid an unused argument compiler warning.
0126 #else
0127   if (vuc != nullptr) {
0128     auto *uc = reinterpret_cast<const ucontext_t *>(vuc);
0129 #if defined(__i386__)
0130     const auto bp = uc->uc_mcontext.gregs[REG_EBP];
0131     const auto sp = uc->uc_mcontext.gregs[REG_ESP];
0132 #elif defined(__x86_64__)
0133     const auto bp = uc->uc_mcontext.gregs[REG_RBP];
0134     const auto sp = uc->uc_mcontext.gregs[REG_RSP];
0135 #else
0136     const uintptr_t bp = 0;
0137     const uintptr_t sp = 0;
0138 #endif
0139     // Sanity-check that the base pointer is valid. It's possible that some
0140     // code in the process is compiled with --copt=-fomit-frame-pointer or
0141     // --copt=-momit-leaf-frame-pointer.
0142     //
0143     // TODO(bcmills): -momit-leaf-frame-pointer is currently the default
0144     // behavior when building with clang.  Talk to the C++ toolchain team about
0145     // fixing that.
0146     if (bp >= sp && bp - sp <= kMaxFrameBytes)
0147       return static_cast<uintptr_t>(bp);
0148 
0149     // If bp isn't a plausible frame pointer, return the stack pointer instead.
0150     // If we're lucky, it points to the start of a stack frame; otherwise, we'll
0151     // get one frame of garbage in the stack trace and fail the sanity check on
0152     // the next iteration.
0153     return static_cast<uintptr_t>(sp);
0154   }
0155 #endif
0156   return 0;
0157 }
0158 
0159 // Given a pointer to a stack frame, locate and return the calling
0160 // stackframe, or return null if no stackframe can be found. Perform sanity
0161 // checks (the strictness of which is controlled by the boolean parameter
0162 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
0163 template <bool STRICT_UNWINDING, bool WITH_CONTEXT>
0164 ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS  // May read random elements from stack.
0165 ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY   // May read random elements from stack.
0166 static void **NextStackFrame(void **old_fp, const void *uc,
0167                              size_t stack_low, size_t stack_high) {
0168   void **new_fp = (void **)*old_fp;
0169 
0170 #if defined(__linux__) && defined(__i386__)
0171   if (WITH_CONTEXT && uc != nullptr) {
0172     // How many "push %reg" instructions are there at __kernel_vsyscall?
0173     // This is constant for a given kernel and processor, so compute
0174     // it only once.
0175     static int num_push_instructions = -1;  // Sentinel: not computed yet.
0176     // Initialize with sentinel value: __kernel_rt_sigreturn can not possibly
0177     // be there.
0178     static const unsigned char *kernel_rt_sigreturn_address = nullptr;
0179     static const unsigned char *kernel_vsyscall_address = nullptr;
0180     if (num_push_instructions == -1) {
0181 #ifdef ABSL_HAVE_VDSO_SUPPORT
0182       absl::debugging_internal::VDSOSupport vdso;
0183       if (vdso.IsPresent()) {
0184         absl::debugging_internal::VDSOSupport::SymbolInfo
0185             rt_sigreturn_symbol_info;
0186         absl::debugging_internal::VDSOSupport::SymbolInfo vsyscall_symbol_info;
0187         if (!vdso.LookupSymbol("__kernel_rt_sigreturn", "LINUX_2.5", STT_FUNC,
0188                                &rt_sigreturn_symbol_info) ||
0189             !vdso.LookupSymbol("__kernel_vsyscall", "LINUX_2.5", STT_FUNC,
0190                                &vsyscall_symbol_info) ||
0191             rt_sigreturn_symbol_info.address == nullptr ||
0192             vsyscall_symbol_info.address == nullptr) {
0193           // Unexpected: 32-bit VDSO is present, yet one of the expected
0194           // symbols is missing or null.
0195           assert(false && "VDSO is present, but doesn't have expected symbols");
0196           num_push_instructions = 0;
0197         } else {
0198           kernel_rt_sigreturn_address =
0199               reinterpret_cast<const unsigned char *>(
0200                   rt_sigreturn_symbol_info.address);
0201           kernel_vsyscall_address =
0202               reinterpret_cast<const unsigned char *>(
0203                   vsyscall_symbol_info.address);
0204           num_push_instructions =
0205               CountPushInstructions(kernel_vsyscall_address);
0206         }
0207       } else {
0208         num_push_instructions = 0;
0209       }
0210 #else  // ABSL_HAVE_VDSO_SUPPORT
0211       num_push_instructions = 0;
0212 #endif  // ABSL_HAVE_VDSO_SUPPORT
0213     }
0214     if (num_push_instructions != 0 && kernel_rt_sigreturn_address != nullptr &&
0215         old_fp[1] == kernel_rt_sigreturn_address) {
0216       const ucontext_t *ucv = static_cast<const ucontext_t *>(uc);
0217       // This kernel does not use frame pointer in its VDSO code,
0218       // and so %ebp is not suitable for unwinding.
0219       void **const reg_ebp =
0220           reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_EBP]);
0221       const unsigned char *const reg_eip =
0222           reinterpret_cast<unsigned char *>(ucv->uc_mcontext.gregs[REG_EIP]);
0223       if (new_fp == reg_ebp && kernel_vsyscall_address <= reg_eip &&
0224           reg_eip - kernel_vsyscall_address < kMaxBytes) {
0225         // We "stepped up" to __kernel_vsyscall, but %ebp is not usable.
0226         // Restore from 'ucv' instead.
0227         void **const reg_esp =
0228             reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_ESP]);
0229         // Check that alleged %esp is not null and is reasonably aligned.
0230         if (reg_esp &&
0231             ((uintptr_t)reg_esp & (sizeof(reg_esp) - 1)) == 0) {
0232           // Check that alleged %esp is actually readable. This is to prevent
0233           // "double fault" in case we hit the first fault due to e.g. stack
0234           // corruption.
0235           void *const reg_esp2 = reg_esp[num_push_instructions - 1];
0236           if (AddressIsReadable(reg_esp2)) {
0237             // Alleged %esp is readable, use it for further unwinding.
0238             new_fp = reinterpret_cast<void **>(reg_esp2);
0239           }
0240         }
0241       }
0242     }
0243   }
0244 #endif
0245 
0246   const uintptr_t old_fp_u = reinterpret_cast<uintptr_t>(old_fp);
0247   const uintptr_t new_fp_u = reinterpret_cast<uintptr_t>(new_fp);
0248 
0249   // Check that the transition from frame pointer old_fp to frame
0250   // pointer new_fp isn't clearly bogus.  Skip the checks if new_fp
0251   // matches the signal context, so that we don't skip out early when
0252   // using an alternate signal stack.
0253   //
0254   // TODO(bcmills): The GetFP call should be completely unnecessary when
0255   // ENABLE_COMBINED_UNWINDER is set (because we should be back in the thread's
0256   // stack by this point), but it is empirically still needed (e.g. when the
0257   // stack includes a call to abort).  unw_get_reg returns UNW_EBADREG for some
0258   // frames.  Figure out why GetValidFrameAddr and/or libunwind isn't doing what
0259   // it's supposed to.
0260   if (STRICT_UNWINDING &&
0261       (!WITH_CONTEXT || uc == nullptr || new_fp_u != GetFP(uc))) {
0262     // With the stack growing downwards, older stack frame must be
0263     // at a greater address that the current one.
0264     if (new_fp_u <= old_fp_u) return nullptr;
0265 
0266     // If we get a very large frame size, it may be an indication that we
0267     // guessed frame pointers incorrectly and now risk a paging fault
0268     // dereferencing a wrong frame pointer. Or maybe not because large frames
0269     // are possible as well. The main stack is assumed to be readable,
0270     // so we assume the large frame is legit if we know the real stack bounds
0271     // and are within the stack.
0272     if (new_fp_u - old_fp_u > kMaxFrameBytes) {
0273       if (stack_high < kUnknownStackEnd &&
0274           static_cast<size_t>(getpagesize()) < stack_low) {
0275         // Stack bounds are known.
0276         if (!(stack_low < new_fp_u && new_fp_u <= stack_high)) {
0277           // new_fp_u is not within the known stack.
0278           return nullptr;
0279         }
0280       } else {
0281         // Stack bounds are unknown, prefer truncated stack to possible crash.
0282         return nullptr;
0283       }
0284     }
0285     if (stack_low < old_fp_u && old_fp_u <= stack_high) {
0286       // Old BP was in the expected stack region...
0287       if (!(stack_low < new_fp_u && new_fp_u <= stack_high)) {
0288         // ... but new BP is outside of expected stack region.
0289         // It is most likely bogus.
0290         return nullptr;
0291       }
0292     } else {
0293       // We may be here if we are executing in a co-routine with a
0294       // separate stack. We can't do safety checks in this case.
0295     }
0296   } else {
0297     if (new_fp == nullptr) return nullptr;  // skip AddressIsReadable() below
0298     // In the non-strict mode, allow discontiguous stack frames.
0299     // (alternate-signal-stacks for example).
0300     if (new_fp == old_fp) return nullptr;
0301   }
0302 
0303   if (new_fp_u & (sizeof(void *) - 1)) return nullptr;
0304 #ifdef __i386__
0305   // On 32-bit machines, the stack pointer can be very close to
0306   // 0xffffffff, so we explicitly check for a pointer into the
0307   // last two pages in the address space
0308   if (new_fp_u >= 0xffffe000) return nullptr;
0309 #endif
0310 #if !defined(_WIN32)
0311   if (!STRICT_UNWINDING) {
0312     // Lax sanity checks cause a crash in 32-bit tcmalloc/crash_reason_test
0313     // on AMD-based machines with VDSO-enabled kernels.
0314     // Make an extra sanity check to insure new_fp is readable.
0315     // Note: NextStackFrame<false>() is only called while the program
0316     //       is already on its last leg, so it's ok to be slow here.
0317 
0318     if (!AddressIsReadable(new_fp)) {
0319       return nullptr;
0320     }
0321   }
0322 #endif
0323   return new_fp;
0324 }
0325 
0326 template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
0327 ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS  // May read random elements from stack.
0328 ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY   // May read random elements from stack.
0329 ABSL_ATTRIBUTE_NOINLINE
0330 static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count,
0331                       const void *ucp, int *min_dropped_frames) {
0332   int n = 0;
0333   void **fp = reinterpret_cast<void **>(__builtin_frame_address(0));
0334 
0335   // Assume that the first page is not stack.
0336   size_t stack_low = static_cast<size_t>(getpagesize());
0337   size_t stack_high = kUnknownStackEnd;
0338 
0339   while (fp && n < max_depth) {
0340     if (*(fp + 1) == reinterpret_cast<void *>(0)) {
0341       // In 64-bit code, we often see a frame that
0342       // points to itself and has a return address of 0.
0343       break;
0344     }
0345     void **next_fp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(
0346         fp, ucp, stack_low, stack_high);
0347     if (skip_count > 0) {
0348       skip_count--;
0349     } else {
0350       result[n] = *(fp + 1);
0351       if (IS_STACK_FRAMES) {
0352         if (next_fp > fp) {
0353           sizes[n] = static_cast<int>(
0354               reinterpret_cast<uintptr_t>(next_fp) -
0355               reinterpret_cast<uintptr_t>(fp));
0356         } else {
0357           // A frame-size of 0 is used to indicate unknown frame size.
0358           sizes[n] = 0;
0359         }
0360       }
0361       n++;
0362     }
0363     fp = next_fp;
0364   }
0365   if (min_dropped_frames != nullptr) {
0366     // Implementation detail: we clamp the max of frames we are willing to
0367     // count, so as not to spend too much time in the loop below.
0368     const int kMaxUnwind = 1000;
0369     int num_dropped_frames = 0;
0370     for (int j = 0; fp != nullptr && j < kMaxUnwind; j++) {
0371       if (skip_count > 0) {
0372         skip_count--;
0373       } else {
0374         num_dropped_frames++;
0375       }
0376       fp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(fp, ucp, stack_low,
0377                                                              stack_high);
0378     }
0379     *min_dropped_frames = num_dropped_frames;
0380   }
0381   return n;
0382 }
0383 
0384 namespace absl {
0385 ABSL_NAMESPACE_BEGIN
0386 namespace debugging_internal {
0387 bool StackTraceWorksForTest() {
0388   return true;
0389 }
0390 }  // namespace debugging_internal
0391 ABSL_NAMESPACE_END
0392 }  // namespace absl
0393 
0394 #endif  // ABSL_DEBUGGING_INTERNAL_STACKTRACE_X86_INL_INC_