tor-browser

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

stacktrace_arm-inl.inc (5594B)


      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 // This is inspired by Craig Silverstein's PowerPC stacktrace code.
     16 
     17 #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
     18 #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
     19 
     20 #include <cstdint>
     21 
     22 #include "absl/debugging/internal/addresses.h"
     23 #include "absl/debugging/stacktrace.h"
     24 
     25 // WARNING:
     26 // This only works if all your code is in either ARM or THUMB mode.  With
     27 // interworking, the frame pointer of the caller can either be in r11 (ARM
     28 // mode) or r7 (THUMB mode).  A callee only saves the frame pointer of its
     29 // mode in a fixed location on its stack frame.  If the caller is a different
     30 // mode, there is no easy way to find the frame pointer.  It can either be
     31 // still in the designated register or saved on stack along with other callee
     32 // saved registers.
     33 
     34 // Given a pointer to a stack frame, locate and return the calling
     35 // stackframe, or return nullptr if no stackframe can be found. Perform sanity
     36 // checks (the strictness of which is controlled by the boolean parameter
     37 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
     38 template<bool STRICT_UNWINDING>
     39 static void **NextStackFrame(void **old_sp) {
     40  void **new_sp = (void**) old_sp[-1];
     41 
     42  // Check that the transition from frame pointer old_sp to frame
     43  // pointer new_sp isn't clearly bogus
     44  if (STRICT_UNWINDING) {
     45    // With the stack growing downwards, older stack frame must be
     46    // at a greater address that the current one.
     47    if (new_sp <= old_sp) return nullptr;
     48    // Assume stack frames larger than 100,000 bytes are bogus.
     49    if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return nullptr;
     50  } else {
     51    // In the non-strict mode, allow discontiguous stack frames.
     52    // (alternate-signal-stacks for example).
     53    if (new_sp == old_sp) return nullptr;
     54    // And allow frames upto about 1MB.
     55    if ((new_sp > old_sp)
     56        && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return nullptr;
     57  }
     58  if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return nullptr;
     59  return new_sp;
     60 }
     61 
     62 // This ensures that absl::GetStackTrace sets up the Link Register properly.
     63 #ifdef __GNUC__
     64 void StacktraceArmDummyFunction() __attribute__((noinline));
     65 void StacktraceArmDummyFunction() { __asm__ volatile(""); }
     66 #else
     67 # error StacktraceArmDummyFunction() needs to be ported to this platform.
     68 #endif
     69 
     70 template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
     71 static int UnwindImpl(void **result, uintptr_t *frames, int *sizes,
     72                      int max_depth, int skip_count, const void * /* ucp */,
     73                      int *min_dropped_frames) {
     74 #ifdef __GNUC__
     75  void **sp = reinterpret_cast<void**>(__builtin_frame_address(0));
     76 #else
     77 # error reading stack point not yet supported on this platform.
     78 #endif
     79 
     80  // On ARM, the return address is stored in the link register (r14).
     81  // This is not saved on the stack frame of a leaf function.  To
     82  // simplify code that reads return addresses, we call a dummy
     83  // function so that the return address of this function is also
     84  // stored in the stack frame.  This works at least for gcc.
     85  StacktraceArmDummyFunction();
     86 
     87  int n = 0;
     88  while (sp && n < max_depth) {
     89    // The absl::GetStackFrames routine is called when we are in some
     90    // informational context (the failure signal handler for example).
     91    // Use the non-strict unwinding rules to produce a stack trace
     92    // that is as complete as possible (even if it contains a few bogus
     93    // entries in some rare cases).
     94    void **next_sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
     95 
     96    if (skip_count > 0) {
     97      skip_count--;
     98    } else {
     99      result[n] = *sp;
    100 
    101      if (IS_STACK_FRAMES) {
    102        if (frames != nullptr) {
    103          frames[n] = absl::debugging_internal::StripPointerMetadata(sp) +
    104                      1 * sizeof(void *) /* go past the return address */;
    105        }
    106        if (sizes != nullptr) {
    107          if (next_sp > sp) {
    108            sizes[n] = absl::debugging_internal::StripPointerMetadata(next_sp) -
    109                       absl::debugging_internal::StripPointerMetadata(sp);
    110          } else {
    111            // A frame-size of 0 is used to indicate unknown frame size.
    112            sizes[n] = 0;
    113          }
    114        }
    115      }
    116      n++;
    117    }
    118    sp = next_sp;
    119  }
    120  if (min_dropped_frames != nullptr) {
    121    // Implementation detail: we clamp the max of frames we are willing to
    122    // count, so as not to spend too much time in the loop below.
    123    const int kMaxUnwind = 200;
    124    int num_dropped_frames = 0;
    125    for (int j = 0; sp != nullptr && j < kMaxUnwind; j++) {
    126      if (skip_count > 0) {
    127        skip_count--;
    128      } else {
    129        num_dropped_frames++;
    130      }
    131      sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
    132    }
    133    *min_dropped_frames = num_dropped_frames;
    134  }
    135  return n;
    136 }
    137 
    138 namespace absl {
    139 ABSL_NAMESPACE_BEGIN
    140 namespace debugging_internal {
    141 bool StackTraceWorksForTest() {
    142  return false;
    143 }
    144 }  // namespace debugging_internal
    145 ABSL_NAMESPACE_END
    146 }  // namespace absl
    147 
    148 #endif  // ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_