asan_stack.cc revision 283c296b64bc55deec9698260b3427a9b050a925
1//===-- asan_stack.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 a part of AddressSanitizer, an address sanity checker.
11//
12// Code for ASan stack trace.
13//===----------------------------------------------------------------------===//
14#include "asan_interceptors.h"
15#include "asan_interface.h"
16#include "asan_lock.h"
17#include "asan_stack.h"
18#include "asan_thread.h"
19#include "asan_thread_registry.h"
20#include "sanitizer_common/sanitizer_procmaps.h"
21#include "sanitizer_common/sanitizer_symbolizer.h"
22
23namespace __asan {
24
25static __asan_symbolize_callback symbolize_callback;
26
27static const char *StripPathPrefix(const char *filepath) {
28  const char *path_prefix = flags()->strip_path_prefix;
29  if (filepath == internal_strstr(filepath, path_prefix))
30    return filepath + internal_strlen(path_prefix);
31  return filepath;
32}
33
34// ----------------------- AsanStackTrace ----------------------------- {{{1
35// PCs in stack traces are actually the return addresses, that is,
36// addresses of the next instructions after the call. That's why we
37// decrement them.
38static uptr patch_pc(uptr pc) {
39#ifdef __arm__
40  // Cancel Thumb bit.
41  pc = pc & (~1);
42#endif
43  return pc - 1;
44}
45
46void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
47  MemoryMappingLayout proc_maps;
48  uptr frame_num = 0;
49  for (uptr i = 0; i < size && addr[i]; i++) {
50    uptr pc = patch_pc(addr[i]);
51    if (symbolize_callback) {
52      char buff[4096];
53      symbolize_callback((void*)pc, buff, sizeof(buff));
54      // We can't know anything about the string returned by external
55      // symbolizer, but if it starts with filename, try to strip path prefix
56      // from it.
57      Printf("  #%zu 0x%zx %s\n", frame_num, pc, StripPathPrefix(buff));
58      frame_num++;
59      continue;
60    }
61    AddressInfo addr_frames[64];
62    uptr addr_frames_num = 0;
63    if (flags()->symbolize) {
64      addr_frames_num = SymbolizeCode(pc, addr_frames,
65                                      ASAN_ARRAY_SIZE(addr_frames));
66    }
67    if (addr_frames_num > 0) {
68      for (uptr j = 0; j < addr_frames_num; j++) {
69        AddressInfo &info = addr_frames[j];
70        Printf("    #%zu 0x%zx", frame_num, pc);
71        if (info.function) {
72          Printf(" in %s", info.function);
73        }
74        if (info.file) {
75          Printf(" %s:%d:%d", StripPathPrefix(info.file), info.line,
76                                  info.column);
77        } else if (info.module) {
78          Printf(" (%s+0x%zx)", StripPathPrefix(info.module),
79                                    info.module_offset);
80        }
81        Printf("\n");
82        info.Clear();
83        frame_num++;
84      }
85    } else {
86      uptr offset;
87      char filename[4096];
88      if (proc_maps.GetObjectNameAndOffset(pc, &offset,
89                                           filename, sizeof(filename))) {
90        Printf("    #%zu 0x%zx (%s+0x%zx)\n",
91                   frame_num, pc, StripPathPrefix(filename), offset);
92      } else {
93        Printf("    #%zu 0x%zx\n", frame_num, pc);
94      }
95      frame_num++;
96    }
97  }
98}
99
100uptr AsanStackTrace::GetCurrentPc() {
101  return GET_CALLER_PC();
102}
103
104void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) {
105  CHECK(size == 0 && trace[0] == pc);
106  size = 1;
107  if (!asan_inited) return;
108  AsanThread *t = asanThreadRegistry().GetCurrent();
109  if (!t) return;
110  uptr *frame = (uptr*)bp;
111  uptr *prev_frame = frame;
112  uptr *top = (uptr*)t->stack_top();
113  uptr *bottom = (uptr*)t->stack_bottom();
114  while (frame >= prev_frame &&
115         frame < top - 2 &&
116         frame > bottom &&
117         size < max_size) {
118    uptr pc1 = frame[1];
119    if (pc1 != pc) {
120      trace[size++] = pc1;
121    }
122    prev_frame = frame;
123    frame = (uptr*)frame[0];
124  }
125}
126
127// On 32-bits we don't compress stack traces.
128// On 64-bits we compress stack traces: if a given pc differes slightly from
129// the previous one, we record a 31-bit offset instead of the full pc.
130SANITIZER_INTERFACE_ATTRIBUTE
131uptr AsanStackTrace::CompressStack(AsanStackTrace *stack,
132                                   u32 *compressed, uptr size) {
133#if __WORDSIZE == 32
134  // Don't compress, just copy.
135  uptr res = 0;
136  for (uptr i = 0; i < stack->size && i < size; i++) {
137    compressed[i] = stack->trace[i];
138    res++;
139  }
140  if (stack->size < size)
141    compressed[stack->size] = 0;
142#else  // 64 bits, compress.
143  uptr prev_pc = 0;
144  const uptr kMaxOffset = (1ULL << 30) - 1;
145  uptr c_index = 0;
146  uptr res = 0;
147  for (uptr i = 0, n = stack->size; i < n; i++) {
148    uptr pc = stack->trace[i];
149    if (!pc) break;
150    if ((s64)pc < 0) break;
151    // Printf("C pc[%zu] %zx\n", i, pc);
152    if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
153      uptr offset = (s64)(pc - prev_pc);
154      offset |= (1U << 31);
155      if (c_index >= size) break;
156      // Printf("C co[%zu] offset %zx\n", i, offset);
157      compressed[c_index++] = offset;
158    } else {
159      uptr hi = pc >> 32;
160      uptr lo = (pc << 32) >> 32;
161      CHECK((hi & (1 << 31)) == 0);
162      if (c_index + 1 >= size) break;
163      // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
164      compressed[c_index++] = hi;
165      compressed[c_index++] = lo;
166    }
167    res++;
168    prev_pc = pc;
169  }
170  if (c_index < size)
171    compressed[c_index] = 0;
172  if (c_index + 1 < size)
173    compressed[c_index + 1] = 0;
174#endif  // __WORDSIZE
175
176  // debug-only code
177#if 0
178  AsanStackTrace check_stack;
179  UncompressStack(&check_stack, compressed, size);
180  if (res < check_stack.size) {
181    Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
182           check_stack.size, size);
183  }
184  // |res| may be greater than check_stack.size, because
185  // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
186  CHECK(res >= check_stack.size);
187  CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
188                          check_stack.size * sizeof(uptr)));
189#endif
190
191  return res;
192}
193
194SANITIZER_INTERFACE_ATTRIBUTE
195void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
196                                     u32 *compressed, uptr size) {
197#if __WORDSIZE == 32
198  // Don't uncompress, just copy.
199  stack->size = 0;
200  for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
201    if (!compressed[i]) break;
202    stack->size++;
203    stack->trace[i] = compressed[i];
204  }
205#else  // 64 bits, uncompress
206  uptr prev_pc = 0;
207  stack->size = 0;
208  for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
209    u32 x = compressed[i];
210    uptr pc = 0;
211    if (x & (1U << 31)) {
212      // Printf("U co[%zu] offset: %x\n", i, x);
213      // this is an offset
214      s32 offset = x;
215      offset = (offset << 1) >> 1;  // remove the 31-byte and sign-extend.
216      pc = prev_pc + offset;
217      CHECK(pc);
218    } else {
219      // CHECK(i + 1 < size);
220      if (i + 1 >= size) break;
221      uptr hi = x;
222      uptr lo = compressed[i+1];
223      // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
224      i++;
225      pc = (hi << 32) | lo;
226      if (!pc) break;
227    }
228    // Printf("U pc[%zu] %zx\n", stack->size, pc);
229    stack->trace[stack->size++] = pc;
230    prev_pc = pc;
231  }
232#endif  // __WORDSIZE
233}
234
235}  // namespace __asan
236
237// ------------------ Interface -------------- {{{1
238using namespace __asan;  // NOLINT
239
240void NOINLINE __asan_set_symbolize_callback(
241    __asan_symbolize_callback callback) {
242  symbolize_callback = callback;
243}
244