Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_
0002 #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_
0003 
0004 // Generate stack tracer for aarch64
0005 
0006 #if defined(__linux__)
0007 #include <signal.h>
0008 #include <sys/mman.h>
0009 #include <ucontext.h>
0010 #include <unistd.h>
0011 #endif
0012 
0013 #include <atomic>
0014 #include <cassert>
0015 #include <cstdint>
0016 #include <iostream>
0017 #include <limits>
0018 
0019 #include "absl/base/attributes.h"
0020 #include "absl/debugging/internal/address_is_readable.h"
0021 #include "absl/debugging/internal/vdso_support.h"  // a no-op on non-elf or non-glibc systems
0022 #include "absl/debugging/stacktrace.h"
0023 
0024 static const size_t kUnknownFrameSize = 0;
0025 // Stack end to use when we don't know the actual stack end
0026 // (effectively just the end of address space).
0027 constexpr uintptr_t kUnknownStackEnd =
0028     std::numeric_limits<size_t>::max() - sizeof(void *);
0029 
0030 #if defined(__linux__)
0031 // Returns the address of the VDSO __kernel_rt_sigreturn function, if present.
0032 static const unsigned char* GetKernelRtSigreturnAddress() {
0033   constexpr uintptr_t kImpossibleAddress = 1;
0034   ABSL_CONST_INIT static std::atomic<uintptr_t> memoized{kImpossibleAddress};
0035   uintptr_t address = memoized.load(std::memory_order_relaxed);
0036   if (address != kImpossibleAddress) {
0037     return reinterpret_cast<const unsigned char*>(address);
0038   }
0039 
0040   address = reinterpret_cast<uintptr_t>(nullptr);
0041 
0042 #ifdef ABSL_HAVE_VDSO_SUPPORT
0043   absl::debugging_internal::VDSOSupport vdso;
0044   if (vdso.IsPresent()) {
0045     absl::debugging_internal::VDSOSupport::SymbolInfo symbol_info;
0046     auto lookup = [&](int type) {
0047       return vdso.LookupSymbol("__kernel_rt_sigreturn", "LINUX_2.6.39", type,
0048                                &symbol_info);
0049     };
0050     if ((!lookup(STT_FUNC) && !lookup(STT_NOTYPE)) ||
0051         symbol_info.address == nullptr) {
0052       // Unexpected: VDSO is present, yet the expected symbol is missing
0053       // or null.
0054       assert(false && "VDSO is present, but doesn't have expected symbol");
0055     } else {
0056       if (reinterpret_cast<uintptr_t>(symbol_info.address) !=
0057           kImpossibleAddress) {
0058         address = reinterpret_cast<uintptr_t>(symbol_info.address);
0059       } else {
0060         assert(false && "VDSO returned invalid address");
0061       }
0062     }
0063   }
0064 #endif
0065 
0066   memoized.store(address, std::memory_order_relaxed);
0067   return reinterpret_cast<const unsigned char*>(address);
0068 }
0069 #endif  // __linux__
0070 
0071 // Compute the size of a stack frame in [low..high).  We assume that
0072 // low < high.  Return size of kUnknownFrameSize.
0073 template<typename T>
0074 static size_t ComputeStackFrameSize(const T* low,
0075                                            const T* high) {
0076   const char* low_char_ptr = reinterpret_cast<const char *>(low);
0077   const char* high_char_ptr = reinterpret_cast<const char *>(high);
0078   return low < high ? static_cast<size_t>(high_char_ptr - low_char_ptr)
0079                     : kUnknownFrameSize;
0080 }
0081 
0082 // Saves stack info that is expensive to calculate to avoid recalculating per frame.
0083 struct StackInfo {
0084   uintptr_t stack_low;
0085   uintptr_t stack_high;
0086   uintptr_t sig_stack_low;
0087   uintptr_t sig_stack_high;
0088 };
0089 
0090 static bool InsideSignalStack(void** ptr, const StackInfo* stack_info) {
0091   uintptr_t comparable_ptr = reinterpret_cast<uintptr_t>(ptr);
0092   if (stack_info->sig_stack_high == kUnknownStackEnd)
0093     return false;
0094   return (comparable_ptr >= stack_info->sig_stack_low &&
0095           comparable_ptr < stack_info->sig_stack_high);
0096 }
0097 
0098 // Given a pointer to a stack frame, locate and return the calling
0099 // stackframe, or return null if no stackframe can be found. Perform sanity
0100 // checks (the strictness of which is controlled by the boolean parameter
0101 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
0102 template<bool STRICT_UNWINDING, bool WITH_CONTEXT>
0103 ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS  // May read random elements from stack.
0104 ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY  // May read random elements from stack.
0105 static void **NextStackFrame(void **old_frame_pointer, const void *uc,
0106                              const StackInfo *stack_info) {
0107   void **new_frame_pointer = reinterpret_cast<void**>(*old_frame_pointer);
0108 
0109 #if defined(__linux__)
0110   if (WITH_CONTEXT && uc != nullptr) {
0111     // Check to see if next frame's return address is __kernel_rt_sigreturn.
0112     if (old_frame_pointer[1] == GetKernelRtSigreturnAddress()) {
0113       const ucontext_t *ucv = static_cast<const ucontext_t *>(uc);
0114       // old_frame_pointer[0] is not suitable for unwinding, look at
0115       // ucontext to discover frame pointer before signal.
0116       void **const pre_signal_frame_pointer =
0117           reinterpret_cast<void **>(ucv->uc_mcontext.regs[29]);
0118 
0119       // The most recent signal always needs special handling to find the frame
0120       // pointer, but a nested signal does not.  If pre_signal_frame_pointer is
0121       // earlier in the stack than the old_frame_pointer, then use it. If it is
0122       // later, then we have already unwound through it and it needs no special
0123       // handling.
0124       if (pre_signal_frame_pointer >= old_frame_pointer) {
0125         new_frame_pointer = pre_signal_frame_pointer;
0126       }
0127   }
0128 #endif
0129 
0130   // The frame pointer should be 8-byte aligned.
0131   if ((reinterpret_cast<uintptr_t>(new_frame_pointer) & 7) != 0)
0132     return nullptr;
0133 
0134   // Check that alleged frame pointer is actually readable. This is to
0135   // prevent "double fault" in case we hit the first fault due to e.g.
0136   // stack corruption.
0137   if (!absl::debugging_internal::AddressIsReadable(
0138           new_frame_pointer))
0139     return nullptr;
0140   }
0141 
0142   // Only check the size if both frames are in the same stack.
0143   if (InsideSignalStack(new_frame_pointer, stack_info) ==
0144       InsideSignalStack(old_frame_pointer, stack_info)) {
0145     // Check frame size.  In strict mode, we assume frames to be under
0146     // 100,000 bytes.  In non-strict mode, we relax the limit to 1MB.
0147     const size_t max_size = STRICT_UNWINDING ? 100000 : 1000000;
0148     const size_t frame_size =
0149         ComputeStackFrameSize(old_frame_pointer, new_frame_pointer);
0150     if (frame_size == kUnknownFrameSize)
0151        return nullptr;
0152     // A very large frame may mean corrupt memory or an erroneous frame
0153     // pointer. But also maybe just a plain-old large frame.  Assume that if the
0154     // frame is within a known stack, then it is valid.
0155     if (frame_size > max_size) {
0156       size_t stack_low = stack_info->stack_low;
0157       size_t stack_high = stack_info->stack_high;
0158       if (InsideSignalStack(new_frame_pointer, stack_info)) {
0159         stack_low = stack_info->sig_stack_low;
0160         stack_high = stack_info->sig_stack_high;
0161       }
0162       if (stack_high < kUnknownStackEnd &&
0163           static_cast<size_t>(getpagesize()) < stack_low) {
0164         const uintptr_t new_fp_u =
0165             reinterpret_cast<uintptr_t>(new_frame_pointer);
0166         // Stack bounds are known.
0167         if (!(stack_low < new_fp_u && new_fp_u <= stack_high)) {
0168           // new_frame_pointer is not within a known stack.
0169           return nullptr;
0170         }
0171       } else {
0172         // Stack bounds are unknown, prefer truncated stack to possible crash.
0173         return nullptr;
0174       }
0175     }
0176   }
0177 
0178   return new_frame_pointer;
0179 }
0180 
0181 template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
0182 // We count on the bottom frame being this one. See the comment
0183 // at prev_return_address
0184 ABSL_ATTRIBUTE_NOINLINE
0185 ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS  // May read random elements from stack.
0186 ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY   // May read random elements from stack.
0187 static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
0188                       const void *ucp, int *min_dropped_frames) {
0189 #ifdef __GNUC__
0190   void **frame_pointer = reinterpret_cast<void**>(__builtin_frame_address(0));
0191 #else
0192 # error reading stack point not yet supported on this platform.
0193 #endif
0194   skip_count++;    // Skip the frame for this function.
0195   int n = 0;
0196 
0197   // Assume that the first page is not stack.
0198   StackInfo stack_info;
0199   stack_info.stack_low = static_cast<uintptr_t>(getpagesize());
0200   stack_info.stack_high = kUnknownStackEnd;
0201   stack_info.sig_stack_low = stack_info.stack_low;
0202   stack_info.sig_stack_high = kUnknownStackEnd;
0203 
0204   // The frame pointer points to low address of a frame.  The first 64-bit
0205   // word of a frame points to the next frame up the call chain, which normally
0206   // is just after the high address of the current frame.  The second word of
0207   // a frame contains return address of to the caller.   To find a pc value
0208   // associated with the current frame, we need to go down a level in the call
0209   // chain.  So we remember return the address of the last frame seen.  This
0210   // does not work for the first stack frame, which belongs to UnwindImp() but
0211   // we skip the frame for UnwindImp() anyway.
0212   void* prev_return_address = nullptr;
0213   // The nth frame size is the difference between the nth frame pointer and the
0214   // the frame pointer below it in the call chain. There is no frame below the
0215   // leaf frame, but this function is the leaf anyway, and we skip it.
0216   void** prev_frame_pointer = nullptr;
0217 
0218    while (frame_pointer && n < max_depth) {
0219     if (skip_count > 0) {
0220       skip_count--;
0221     } else {
0222       result[n] = prev_return_address;
0223       if (IS_STACK_FRAMES) {
0224         sizes[n] = static_cast<int>(
0225             ComputeStackFrameSize(prev_frame_pointer, frame_pointer));
0226       }
0227       n++;
0228     }
0229     prev_return_address = frame_pointer[1];
0230     prev_frame_pointer = frame_pointer;
0231     // The absl::GetStackFrames routine is called when we are in some
0232     // informational context (the failure signal handler for example).
0233     // Use the non-strict unwinding rules to produce a stack trace
0234     // that is as complete as possible (even if it contains a few bogus
0235     // entries in some rare cases).
0236     frame_pointer = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(
0237         frame_pointer, ucp, &stack_info);
0238   }
0239 
0240   if (min_dropped_frames != nullptr) {
0241     // Implementation detail: we clamp the max of frames we are willing to
0242     // count, so as not to spend too much time in the loop below.
0243     const int kMaxUnwind = 200;
0244     int num_dropped_frames = 0;
0245     for (int j = 0; frame_pointer != nullptr && j < kMaxUnwind; j++) {
0246       if (skip_count > 0) {
0247         skip_count--;
0248       } else {
0249         num_dropped_frames++;
0250       }
0251       frame_pointer = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(
0252           frame_pointer, ucp, &stack_info);
0253     }
0254     *min_dropped_frames = num_dropped_frames;
0255   }
0256   return n;
0257 }
0258 
0259 namespace absl {
0260 ABSL_NAMESPACE_BEGIN
0261 namespace debugging_internal {
0262 bool StackTraceWorksForTest() {
0263   return true;
0264 }
0265 }  // namespace debugging_internal
0266 ABSL_NAMESPACE_END
0267 }  // namespace absl
0268 
0269 #endif  // ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_