tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

stacktrace_powerpc-inl.inc (10824B)


      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.  I'm guessing (hoping!) the code is much like
     16 // for x86.  For apple machines, at least, it seems to be; see
     17 //    https://developer.apple.com/documentation/mac/runtimehtml/RTArch-59.html
     18 //    https://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK
     19 // Linux has similar code: http://patchwork.ozlabs.org/linuxppc/patch?id=8882
     20 
     21 #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_
     22 #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_
     23 
     24 #include "absl/debugging/internal/addresses.h"
     25 #if defined(__linux__)
     26 #include <asm/ptrace.h>   // for PT_NIP.
     27 #include <ucontext.h>     // for ucontext_t
     28 #endif
     29 
     30 #include <unistd.h>
     31 #include <cassert>
     32 #include <cstdint>
     33 #include <cstdio>
     34 
     35 #include "absl/base/attributes.h"
     36 #include "absl/base/optimization.h"
     37 #include "absl/base/port.h"
     38 #include "absl/debugging/stacktrace.h"
     39 #include "absl/debugging/internal/address_is_readable.h"
     40 #include "absl/debugging/internal/vdso_support.h"  // a no-op on non-elf or non-glibc systems
     41 
     42 // Given a stack pointer, return the saved link register value.
     43 // Note that this is the link register for a callee.
     44 static inline void **StacktracePowerPCGetLRPtr(void **sp) {
     45  // PowerPC has 3 main ABIs, which say where in the stack the
     46  // Link Register is.  For DARWIN and AIX (used by apple and
     47  // linux ppc64), it's in sp[2].  For SYSV (used by linux ppc),
     48  // it's in sp[1].
     49 #if defined(_CALL_AIX) || defined(_CALL_DARWIN)
     50  return (sp + 2);
     51 #elif defined(_CALL_SYSV)
     52  return (sp + 1);
     53 #elif defined(__APPLE__) || defined(__FreeBSD__) || \
     54    (defined(__linux__) && defined(__PPC64__))
     55  // This check is in case the compiler doesn't define _CALL_AIX/etc.
     56  return (sp + 2);
     57 #elif defined(__linux)
     58  // This check is in case the compiler doesn't define _CALL_SYSV.
     59  return (sp + 1);
     60 #else
     61 #error Need to specify the PPC ABI for your architecture.
     62 #endif
     63 }
     64 
     65 // Given a pointer to a stack frame, locate and return the calling
     66 // stackframe, or return null if no stackframe can be found. Perform sanity
     67 // checks (the strictness of which is controlled by the boolean parameter
     68 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
     69 template<bool STRICT_UNWINDING, bool IS_WITH_CONTEXT>
     70 ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS  // May read random elements from stack.
     71 ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY   // May read random elements from stack.
     72 static void **NextStackFrame(void **old_sp, const void *uc) {
     73  void **new_sp = (void **) *old_sp;
     74  enum { kStackAlignment = 16 };
     75 
     76  // Check that the transition from frame pointer old_sp to frame
     77  // pointer new_sp isn't clearly bogus
     78  if (STRICT_UNWINDING) {
     79    // With the stack growing downwards, older stack frame must be
     80    // at a greater address that the current one.
     81    if (new_sp <= old_sp) return nullptr;
     82    // Assume stack frames larger than 100,000 bytes are bogus.
     83    if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return nullptr;
     84  } else {
     85    // In the non-strict mode, allow discontiguous stack frames.
     86    // (alternate-signal-stacks for example).
     87    if (new_sp == old_sp) return nullptr;
     88    // And allow frames upto about 1MB.
     89    if ((new_sp > old_sp)
     90        && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return nullptr;
     91  }
     92  if ((uintptr_t)new_sp % kStackAlignment != 0) return nullptr;
     93 
     94 #if defined(__linux__)
     95  enum StackTraceKernelSymbolStatus {
     96      kNotInitialized = 0, kAddressValid, kAddressInvalid };
     97 
     98  if (IS_WITH_CONTEXT && uc != nullptr) {
     99    static StackTraceKernelSymbolStatus kernel_symbol_status =
    100        kNotInitialized;  // Sentinel: not computed yet.
    101    // Initialize with sentinel value: __kernel_rt_sigtramp_rt64 can not
    102    // possibly be there.
    103    static const unsigned char *kernel_sigtramp_rt64_address = nullptr;
    104    if (kernel_symbol_status == kNotInitialized) {
    105      absl::debugging_internal::VDSOSupport vdso;
    106      if (vdso.IsPresent()) {
    107        absl::debugging_internal::VDSOSupport::SymbolInfo
    108            sigtramp_rt64_symbol_info;
    109        if (!vdso.LookupSymbol(
    110                "__kernel_sigtramp_rt64", "LINUX_2.6.15",
    111                absl::debugging_internal::VDSOSupport::kVDSOSymbolType,
    112                &sigtramp_rt64_symbol_info) ||
    113            sigtramp_rt64_symbol_info.address == nullptr) {
    114          // Unexpected: VDSO is present, yet the expected symbol is missing
    115          // or null.
    116          assert(false && "VDSO is present, but doesn't have expected symbol");
    117          kernel_symbol_status = kAddressInvalid;
    118        } else {
    119          kernel_sigtramp_rt64_address =
    120              reinterpret_cast<const unsigned char *>(
    121                  sigtramp_rt64_symbol_info.address);
    122          kernel_symbol_status = kAddressValid;
    123        }
    124      } else {
    125        kernel_symbol_status = kAddressInvalid;
    126      }
    127    }
    128 
    129    if (new_sp != nullptr && kernel_symbol_status == kAddressValid &&
    130        *StacktracePowerPCGetLRPtr(new_sp) == kernel_sigtramp_rt64_address) {
    131      const ucontext_t* signal_context =
    132          reinterpret_cast<const ucontext_t*>(uc);
    133      void **const sp_before_signal =
    134 #if defined(__PPC64__)
    135          reinterpret_cast<void **>(signal_context->uc_mcontext.gp_regs[PT_R1]);
    136 #else
    137          reinterpret_cast<void **>(
    138              signal_context->uc_mcontext.uc_regs->gregs[PT_R1]);
    139 #endif
    140      // Check that alleged sp before signal is nonnull and is reasonably
    141      // aligned.
    142      if (sp_before_signal != nullptr &&
    143          ((uintptr_t)sp_before_signal % kStackAlignment) == 0) {
    144        // Check that alleged stack pointer is actually readable. This is to
    145        // prevent a "double fault" in case we hit the first fault due to e.g.
    146        // a stack corruption.
    147        if (absl::debugging_internal::AddressIsReadable(sp_before_signal)) {
    148          // Alleged stack pointer is readable, use it for further unwinding.
    149          new_sp = sp_before_signal;
    150        }
    151      }
    152    }
    153  }
    154 #endif
    155 
    156  return new_sp;
    157 }
    158 
    159 // This ensures that absl::GetStackTrace sets up the Link Register properly.
    160 ABSL_ATTRIBUTE_NOINLINE static void AbslStacktracePowerPCDummyFunction() {
    161  ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
    162 }
    163 
    164 template <bool IS_STACK_FRAMES, bool IS_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 static int UnwindImpl(void **result, uintptr_t *frames, int *sizes,
    168                      int max_depth, int skip_count, const void *ucp,
    169                      int *min_dropped_frames) {
    170  void **sp;
    171  // Apple macOS uses an old version of gnu as -- both Darwin 7.9.0 (Panther)
    172  // and Darwin 8.8.1 (Tiger) use as 1.38.  This means we have to use a
    173  // different asm syntax.  I don't know quite the best way to discriminate
    174  // systems using the old as from the new one; I've gone with __APPLE__.
    175 #ifdef __APPLE__
    176  __asm__ volatile ("mr %0,r1" : "=r" (sp));
    177 #else
    178  __asm__ volatile ("mr %0,1" : "=r" (sp));
    179 #endif
    180 
    181  // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack
    182  // entry that holds the return address of the subroutine call (what
    183  // instruction we run after our function finishes).  This is the
    184  // same as the stack-pointer of our parent routine, which is what we
    185  // want here.  While the compiler will always(?) set up LR for
    186  // subroutine calls, it may not for leaf functions (such as this one).
    187  // This routine forces the compiler (at least gcc) to push it anyway.
    188  AbslStacktracePowerPCDummyFunction();
    189 
    190  // The LR save area is used by the callee, so the top entry is bogus.
    191  skip_count++;
    192 
    193  int n = 0;
    194 
    195  // Unlike ABIs of X86 and ARM, PowerPC ABIs say that return address (in
    196  // the link register) of a function call is stored in the caller's stack
    197  // frame instead of the callee's.  When we look for the return address
    198  // associated with a stack frame, we need to make sure that there is a
    199  // caller frame before it.  So we call NextStackFrame before entering the
    200  // loop below and check next_sp instead of sp for loop termination.
    201  // The outermost frame is set up by runtimes and it does not have a
    202  // caller frame, so it is skipped.
    203 
    204  // The absl::GetStackFrames routine is called when we are in some
    205  // informational context (the failure signal handler for example).
    206  // Use the non-strict unwinding rules to produce a stack trace
    207  // that is as complete as possible (even if it contains a few
    208  // bogus entries in some rare cases).
    209  void **next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(sp, ucp);
    210 
    211  while (next_sp && n < max_depth) {
    212    if (skip_count > 0) {
    213      skip_count--;
    214    } else {
    215      void **lr = StacktracePowerPCGetLRPtr(sp);
    216      result[n] = *lr;
    217      if (IS_STACK_FRAMES) {
    218        if (frames != nullptr) {
    219          frames[n] = absl::debugging_internal::StripPointerMetadata(lr) +
    220                      1 * sizeof(void *) /* go past the return address */;
    221        }
    222        if (sizes != nullptr) {
    223          if (next_sp > sp) {
    224            sizes[n] = absl::debugging_internal::StripPointerMetadata(next_sp) -
    225                       absl::debugging_internal::StripPointerMetadata(sp);
    226          } else {
    227            // A frame-size of 0 is used to indicate unknown frame size.
    228            sizes[n] = 0;
    229          }
    230        }
    231      }
    232      n++;
    233    }
    234 
    235    sp = next_sp;
    236    next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(sp, ucp);
    237  }
    238 
    239  if (min_dropped_frames != nullptr) {
    240    // Implementation detail: we clamp the max of frames we are willing to
    241    // count, so as not to spend too much time in the loop below.
    242    const int kMaxUnwind = 1000;
    243    int num_dropped_frames = 0;
    244    for (int j = 0; next_sp != nullptr && j < kMaxUnwind; j++) {
    245      if (skip_count > 0) {
    246        skip_count--;
    247      } else {
    248        num_dropped_frames++;
    249      }
    250      next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(next_sp, ucp);
    251    }
    252    *min_dropped_frames = num_dropped_frames;
    253  }
    254  return n;
    255 }
    256 
    257 namespace absl {
    258 ABSL_NAMESPACE_BEGIN
    259 namespace debugging_internal {
    260 bool StackTraceWorksForTest() {
    261  return true;
    262 }
    263 }  // namespace debugging_internal
    264 ABSL_NAMESPACE_END
    265 }  // namespace absl
    266 
    267 #endif  // ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_