xref: /openbsd-src/gnu/llvm/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace_sparc.cpp (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick //===-- sanitizer_stacktrace_sparc.cpp ------------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is shared between AddressSanitizer and ThreadSanitizer
103cab2bb3Spatrick // run-time libraries.
113cab2bb3Spatrick //
12*810390e3Srobert // Implementation of fast stack unwinding for Sparc.
133cab2bb3Spatrick //===----------------------------------------------------------------------===//
143cab2bb3Spatrick 
153cab2bb3Spatrick #if defined(__sparc__)
163cab2bb3Spatrick 
173cab2bb3Spatrick #if defined(__arch64__) || defined(__sparcv9)
183cab2bb3Spatrick #define STACK_BIAS 2047
193cab2bb3Spatrick #else
203cab2bb3Spatrick #define STACK_BIAS 0
213cab2bb3Spatrick #endif
223cab2bb3Spatrick 
233cab2bb3Spatrick #include "sanitizer_common.h"
243cab2bb3Spatrick #include "sanitizer_stacktrace.h"
253cab2bb3Spatrick 
263cab2bb3Spatrick namespace __sanitizer {
273cab2bb3Spatrick 
UnwindFast(uptr pc,uptr bp,uptr stack_top,uptr stack_bottom,u32 max_depth)283cab2bb3Spatrick void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
293cab2bb3Spatrick                                     uptr stack_bottom, u32 max_depth) {
303cab2bb3Spatrick   // TODO(yln): add arg sanity check for stack_top/stack_bottom
313cab2bb3Spatrick   CHECK_GE(max_depth, 2);
323cab2bb3Spatrick   const uptr kPageSize = GetPageSizeCached();
333cab2bb3Spatrick #if defined(__GNUC__)
343cab2bb3Spatrick   // __builtin_return_address returns the address of the call instruction
353cab2bb3Spatrick   // on the SPARC and not the return address, so we need to compensate.
363cab2bb3Spatrick   trace_buffer[0] = GetNextInstructionPc(pc);
373cab2bb3Spatrick #else
383cab2bb3Spatrick   trace_buffer[0] = pc;
393cab2bb3Spatrick #endif
403cab2bb3Spatrick   size = 1;
413cab2bb3Spatrick   if (stack_top < 4096) return;  // Sanity check for stack top.
423cab2bb3Spatrick   // Flush register windows to memory
433cab2bb3Spatrick #if defined(__sparc_v9__) || defined(__sparcv9__) || defined(__sparcv9)
443cab2bb3Spatrick   asm volatile("flushw" ::: "memory");
453cab2bb3Spatrick #else
463cab2bb3Spatrick   asm volatile("ta 3" ::: "memory");
473cab2bb3Spatrick #endif
483cab2bb3Spatrick   // On the SPARC, the return address is not in the frame, it is in a
493cab2bb3Spatrick   // register.  There is no way to access it off of the current frame
503cab2bb3Spatrick   // pointer, but it can be accessed off the previous frame pointer by
513cab2bb3Spatrick   // reading the value from the register window save area.
523cab2bb3Spatrick   uptr prev_bp = GET_CURRENT_FRAME();
533cab2bb3Spatrick   uptr next_bp = prev_bp;
543cab2bb3Spatrick   unsigned int i = 0;
553cab2bb3Spatrick   while (next_bp != bp && IsAligned(next_bp, sizeof(uhwptr)) && i++ < 8) {
563cab2bb3Spatrick     prev_bp = next_bp;
573cab2bb3Spatrick     next_bp = (uptr)((uhwptr *)next_bp)[14] + STACK_BIAS;
583cab2bb3Spatrick   }
593cab2bb3Spatrick   if (next_bp == bp)
603cab2bb3Spatrick     bp = prev_bp;
613cab2bb3Spatrick   // Lowest possible address that makes sense as the next frame pointer.
623cab2bb3Spatrick   // Goes up as we walk the stack.
633cab2bb3Spatrick   uptr bottom = stack_bottom;
643cab2bb3Spatrick   // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
653cab2bb3Spatrick   while (IsValidFrame(bp, stack_top, bottom) && IsAligned(bp, sizeof(uhwptr)) &&
663cab2bb3Spatrick          size < max_depth) {
673cab2bb3Spatrick     uhwptr pc1 = ((uhwptr *)bp)[15];
683cab2bb3Spatrick     // Let's assume that any pointer in the 0th page is invalid and
693cab2bb3Spatrick     // stop unwinding here.  If we're adding support for a platform
703cab2bb3Spatrick     // where this isn't true, we need to reconsider this check.
713cab2bb3Spatrick     if (pc1 < kPageSize)
723cab2bb3Spatrick       break;
733cab2bb3Spatrick     if (pc1 != pc) {
743cab2bb3Spatrick       // %o7 contains the address of the call instruction and not the
753cab2bb3Spatrick       // return address, so we need to compensate.
763cab2bb3Spatrick       trace_buffer[size++] = GetNextInstructionPc((uptr)pc1);
773cab2bb3Spatrick     }
783cab2bb3Spatrick     bottom = bp;
793cab2bb3Spatrick     bp = (uptr)((uhwptr *)bp)[14] + STACK_BIAS;
803cab2bb3Spatrick   }
813cab2bb3Spatrick }
823cab2bb3Spatrick 
833cab2bb3Spatrick }  // namespace __sanitizer
843cab2bb3Spatrick 
853cab2bb3Spatrick #endif  // !defined(__sparc__)
86