1 //===-- sanitizer_stacktrace_sparc.cpp ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
11 //
12 // Implementation of fast stack unwinding for Sparc.
13 //===----------------------------------------------------------------------===//
14 
15 #if defined(__sparc__)
16 
17 #if defined(__arch64__) || defined(__sparcv9)
18 #define STACK_BIAS 2047
19 #else
20 #define STACK_BIAS 0
21 #endif
22 
23 #include "sanitizer_common.h"
24 #include "sanitizer_stacktrace.h"
25 
26 namespace __sanitizer {
27 
28 void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
29                                     uptr stack_bottom, u32 max_depth) {
30   // TODO(yln): add arg sanity check for stack_top/stack_bottom
31   CHECK_GE(max_depth, 2);
32   const uptr kPageSize = GetPageSizeCached();
33   trace_buffer[0] = pc;
34   size = 1;
35   if (stack_top < 4096) return;  // Sanity check for stack top.
36   // Flush register windows to memory
37 #if defined(__sparc_v9__) || defined(__sparcv9__) || defined(__sparcv9)
38   asm volatile("flushw" ::: "memory");
39 #else
40   asm volatile("ta 3" ::: "memory");
41 #endif
42   // On the SPARC, the return address is not in the frame, it is in a
43   // register.  There is no way to access it off of the current frame
44   // pointer, but it can be accessed off the previous frame pointer by
45   // reading the value from the register window save area.
46   uptr prev_bp = GET_CURRENT_FRAME();
47   uptr next_bp = prev_bp;
48   unsigned int i = 0;
49   while (next_bp != bp && IsAligned(next_bp, sizeof(uhwptr)) && i++ < 8) {
50     prev_bp = next_bp;
51     next_bp = (uptr)((uhwptr *)next_bp)[14] + STACK_BIAS;
52   }
53   if (next_bp == bp)
54     bp = prev_bp;
55   // Lowest possible address that makes sense as the next frame pointer.
56   // Goes up as we walk the stack.
57   uptr bottom = stack_bottom;
58   // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
59   while (IsValidFrame(bp, stack_top, bottom) && IsAligned(bp, sizeof(uhwptr)) &&
60          size < max_depth) {
61     uhwptr pc1 = ((uhwptr *)bp)[15];
62     // Let's assume that any pointer in the 0th page is invalid and
63     // stop unwinding here.  If we're adding support for a platform
64     // where this isn't true, we need to reconsider this check.
65     if (pc1 < kPageSize)
66       break;
67     if (pc1 != pc) {
68       // %o7 contains the address of the call instruction and not the
69       // return address, so we need to compensate.
70       trace_buffer[size++] = GetNextInstructionPc((uptr)pc1);
71     }
72     bottom = bp;
73     bp = (uptr)((uhwptr *)bp)[14] + STACK_BIAS;
74   }
75 }
76 
77 }  // namespace __sanitizer
78 
79 #endif  // !defined(__sparc__)
80