1 //===-- sanitizer_stacktrace_sparc.cpp ------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
12 // Implementation of fast stack unwinding for Sparc.
13 //===----------------------------------------------------------------------===//
15 #if defined(__sparc__)
17 #if defined(__arch64__) || defined(__sparcv9)
18 #define STACK_BIAS 2047
23 #include "sanitizer_common.h"
24 #include "sanitizer_stacktrace.h"
26 namespace __sanitizer
{
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();
34 // __builtin_return_address returns the address of the call instruction
35 // on the SPARC and not the return address, so we need to compensate.
36 trace_buffer
[0] = GetNextInstructionPc(pc
);
41 if (stack_top
< 4096) return; // Sanity check for stack top.
42 // Flush register windows to memory
43 #if defined(__sparc_v9__) || defined(__sparcv9__) || defined(__sparcv9)
44 asm volatile("flushw" ::: "memory");
46 asm volatile("ta 3" ::: "memory");
48 // On the SPARC, the return address is not in the frame, it is in a
49 // register. There is no way to access it off of the current frame
50 // pointer, but it can be accessed off the previous frame pointer by
51 // reading the value from the register window save area.
52 uptr prev_bp
= GET_CURRENT_FRAME();
53 uptr next_bp
= prev_bp
;
55 while (next_bp
!= bp
&& IsAligned(next_bp
, sizeof(uhwptr
)) && i
++ < 8) {
57 next_bp
= (uptr
)((uhwptr
*)next_bp
)[14] + STACK_BIAS
;
61 // Lowest possible address that makes sense as the next frame pointer.
62 // Goes up as we walk the stack.
63 uptr bottom
= stack_bottom
;
64 // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
65 while (IsValidFrame(bp
, stack_top
, bottom
) && IsAligned(bp
, sizeof(uhwptr
)) &&
67 uhwptr pc1
= ((uhwptr
*)bp
)[15];
68 // Let's assume that any pointer in the 0th page is invalid and
69 // stop unwinding here. If we're adding support for a platform
70 // where this isn't true, we need to reconsider this check.
74 // %o7 contains the address of the call instruction and not the
75 // return address, so we need to compensate.
76 trace_buffer
[size
++] = GetNextInstructionPc((uptr
)pc1
);
79 bp
= (uptr
)((uhwptr
*)bp
)[14] + STACK_BIAS
;
83 } // namespace __sanitizer
85 #endif // !defined(__sparc__)