1//===-- sanitizer_stacktrace.cc -------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries.
12//===----------------------------------------------------------------------===//
13
14#include "sanitizer_common.h"
15#include "sanitizer_flags.h"
16#include "sanitizer_stacktrace.h"
17
18namespace __sanitizer {
19
20uptr StackTrace::GetNextInstructionPc(uptr pc) {
21#if defined(__mips__)
22  return pc + 8;
23#elif defined(__powerpc__) || defined(__sparc__) || defined(__arm__) || \
24    defined(__aarch64__)
25  return pc + 4;
26#else
27  return pc + 1;
28#endif
29}
30
31uptr StackTrace::GetCurrentPc() {
32  return GET_CALLER_PC();
33}
34
35void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
36  size = cnt + !!extra_top_pc;
37  CHECK_LE(size, kStackTraceMax);
38  internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
39  if (extra_top_pc)
40    trace_buffer[cnt] = extra_top_pc;
41  top_frame_bp = 0;
42}
43
44// Sparc implemention is in its own file.
45#if !defined(__sparc__)
46
47// In GCC on ARM bp points to saved lr, not fp, so we should check the next
48// cell in stack to be a saved frame pointer. GetCanonicFrame returns the
49// pointer to saved frame pointer in any case.
50static inline uhwptr *GetCanonicFrame(uptr bp,
51                                      uptr stack_top,
52                                      uptr stack_bottom) {
53#ifdef __arm__
54  if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
55  uhwptr *bp_prev = (uhwptr *)bp;
56  if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
57  // The next frame pointer does not look right. This could be a GCC frame, step
58  // back by 1 word and try again.
59  if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
60    return bp_prev - 1;
61  // Nope, this does not look right either. This means the frame after next does
62  // not have a valid frame pointer, but we can still extract the caller PC.
63  // Unfortunately, there is no way to decide between GCC and LLVM frame
64  // layouts. Assume LLVM.
65  return bp_prev;
66#else
67  return (uhwptr*)bp;
68#endif
69}
70
71void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
72                                         uptr stack_bottom, u32 max_depth) {
73  const uptr kPageSize = GetPageSizeCached();
74  CHECK_GE(max_depth, 2);
75  trace_buffer[0] = pc;
76  size = 1;
77  if (stack_top < 4096) return;  // Sanity check for stack top.
78  uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
79  // Lowest possible address that makes sense as the next frame pointer.
80  // Goes up as we walk the stack.
81  uptr bottom = stack_bottom;
82  // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
83  while (IsValidFrame((uptr)frame, stack_top, bottom) &&
84         IsAligned((uptr)frame, sizeof(*frame)) &&
85         size < max_depth) {
86#ifdef __powerpc__
87    // PowerPC ABIs specify that the return address is saved at offset
88    // 16 of the *caller's* stack frame.  Thus we must dereference the
89    // back chain to find the caller frame before extracting it.
90    uhwptr *caller_frame = (uhwptr*)frame[0];
91    if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
92        !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
93      break;
94    uhwptr pc1 = caller_frame[2];
95#elif defined(__s390__)
96    uhwptr pc1 = frame[14];
97#else
98    uhwptr pc1 = frame[1];
99#endif
100    // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
101    // x86_64) is invalid and stop unwinding here.  If we're adding support for
102    // a platform where this isn't true, we need to reconsider this check.
103    if (pc1 < kPageSize)
104      break;
105    if (pc1 != pc) {
106      trace_buffer[size++] = (uptr) pc1;
107    }
108    bottom = (uptr)frame;
109    frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
110  }
111}
112
113#endif  // !defined(__sparc__)
114
115void BufferedStackTrace::PopStackFrames(uptr count) {
116  CHECK_LT(count, size);
117  size -= count;
118  for (uptr i = 0; i < size; ++i) {
119    trace_buffer[i] = trace_buffer[i + count];
120  }
121}
122
123static uptr Distance(uptr a, uptr b) { return a < b ? b - a : a - b; }
124
125uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
126  uptr best = 0;
127  for (uptr i = 1; i < size; ++i) {
128    if (Distance(trace[i], pc) < Distance(trace[best], pc)) best = i;
129  }
130  return best;
131}
132
133}  // namespace __sanitizer
134