asan_stack.cc revision c3390df6670cb166119b961eb27a033fb9073496
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// ----------------------- StackTrace ----------------------------- {{{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 StackTrace::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, ARRAY_SIZE(addr_frames));
65    }
66    if (addr_frames_num > 0) {
67      for (uptr j = 0; j < addr_frames_num; j++) {
68        AddressInfo &info = addr_frames[j];
69        Printf("    #%zu 0x%zx", frame_num, pc);
70        if (info.function) {
71          Printf(" in %s", info.function);
72        }
73        if (info.file) {
74          Printf(" %s:%d:%d", StripPathPrefix(info.file), info.line,
75                                  info.column);
76        } else if (info.module) {
77          Printf(" (%s+0x%zx)", StripPathPrefix(info.module),
78                                    info.module_offset);
79        }
80        Printf("\n");
81        info.Clear();
82        frame_num++;
83      }
84    } else {
85      uptr offset;
86      char filename[4096];
87      if (proc_maps.GetObjectNameAndOffset(pc, &offset,
88                                           filename, sizeof(filename))) {
89        Printf("    #%zu 0x%zx (%s+0x%zx)\n",
90                   frame_num, pc, StripPathPrefix(filename), offset);
91      } else {
92        Printf("    #%zu 0x%zx\n", frame_num, pc);
93      }
94      frame_num++;
95    }
96  }
97}
98
99uptr StackTrace::GetCurrentPc() {
100  return GET_CALLER_PC();
101}
102
103void StackTrace::FastUnwindStack(uptr pc, uptr bp) {
104  CHECK(size == 0 && trace[0] == pc);
105  size = 1;
106  if (!asan_inited) return;
107  AsanThread *t = asanThreadRegistry().GetCurrent();
108  if (!t) return;
109  uptr *frame = (uptr*)bp;
110  uptr *prev_frame = frame;
111  uptr *top = (uptr*)t->stack_top();
112  uptr *bottom = (uptr*)t->stack_bottom();
113  while (frame >= prev_frame &&
114         frame < top - 2 &&
115         frame > bottom &&
116         size < max_size) {
117    uptr pc1 = frame[1];
118    if (pc1 != pc) {
119      trace[size++] = pc1;
120    }
121    prev_frame = frame;
122    frame = (uptr*)frame[0];
123  }
124}
125
126// On 32-bits we don't compress stack traces.
127// On 64-bits we compress stack traces: if a given pc differes slightly from
128// the previous one, we record a 31-bit offset instead of the full pc.
129SANITIZER_INTERFACE_ATTRIBUTE
130uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) {
131#if __WORDSIZE == 32
132  // Don't compress, just copy.
133  uptr res = 0;
134  for (uptr i = 0; i < stack->size && i < size; i++) {
135    compressed[i] = stack->trace[i];
136    res++;
137  }
138  if (stack->size < size)
139    compressed[stack->size] = 0;
140#else  // 64 bits, compress.
141  uptr prev_pc = 0;
142  const uptr kMaxOffset = (1ULL << 30) - 1;
143  uptr c_index = 0;
144  uptr res = 0;
145  for (uptr i = 0, n = stack->size; i < n; i++) {
146    uptr pc = stack->trace[i];
147    if (!pc) break;
148    if ((s64)pc < 0) break;
149    // Printf("C pc[%zu] %zx\n", i, pc);
150    if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
151      uptr offset = (s64)(pc - prev_pc);
152      offset |= (1U << 31);
153      if (c_index >= size) break;
154      // Printf("C co[%zu] offset %zx\n", i, offset);
155      compressed[c_index++] = offset;
156    } else {
157      uptr hi = pc >> 32;
158      uptr lo = (pc << 32) >> 32;
159      CHECK((hi & (1 << 31)) == 0);
160      if (c_index + 1 >= size) break;
161      // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
162      compressed[c_index++] = hi;
163      compressed[c_index++] = lo;
164    }
165    res++;
166    prev_pc = pc;
167  }
168  if (c_index < size)
169    compressed[c_index] = 0;
170  if (c_index + 1 < size)
171    compressed[c_index + 1] = 0;
172#endif  // __WORDSIZE
173
174  // debug-only code
175#if 0
176  StackTrace check_stack;
177  UncompressStack(&check_stack, compressed, size);
178  if (res < check_stack.size) {
179    Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
180           check_stack.size, size);
181  }
182  // |res| may be greater than check_stack.size, because
183  // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
184  CHECK(res >= check_stack.size);
185  CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
186                          check_stack.size * sizeof(uptr)));
187#endif
188
189  return res;
190}
191
192SANITIZER_INTERFACE_ATTRIBUTE
193void StackTrace::UncompressStack(StackTrace *stack,
194                                 u32 *compressed, uptr size) {
195#if __WORDSIZE == 32
196  // Don't uncompress, just copy.
197  stack->size = 0;
198  for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
199    if (!compressed[i]) break;
200    stack->size++;
201    stack->trace[i] = compressed[i];
202  }
203#else  // 64 bits, uncompress
204  uptr prev_pc = 0;
205  stack->size = 0;
206  for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
207    u32 x = compressed[i];
208    uptr pc = 0;
209    if (x & (1U << 31)) {
210      // Printf("U co[%zu] offset: %x\n", i, x);
211      // this is an offset
212      s32 offset = x;
213      offset = (offset << 1) >> 1;  // remove the 31-byte and sign-extend.
214      pc = prev_pc + offset;
215      CHECK(pc);
216    } else {
217      // CHECK(i + 1 < size);
218      if (i + 1 >= size) break;
219      uptr hi = x;
220      uptr lo = compressed[i+1];
221      // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
222      i++;
223      pc = (hi << 32) | lo;
224      if (!pc) break;
225    }
226    // Printf("U pc[%zu] %zx\n", stack->size, pc);
227    stack->trace[stack->size++] = pc;
228    prev_pc = pc;
229  }
230#endif  // __WORDSIZE
231}
232
233}  // namespace __asan
234
235// ------------------ Interface -------------- {{{1
236using namespace __asan;  // NOLINT
237
238void NOINLINE __asan_set_symbolize_callback(
239    __asan_symbolize_callback callback) {
240  symbolize_callback = callback;
241}
242