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