asan_stack.cc revision 6895adc39c4e09371154c8037366ad4464163ed0
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_lock.h"
16#include "asan_procmaps.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
23#ifdef ASAN_USE_EXTERNAL_SYMBOLIZER
24extern bool
25ASAN_USE_EXTERNAL_SYMBOLIZER(const void *pc, char *out, int out_size);
26#endif
27
28namespace __asan {
29
30// ----------------------- AsanStackTrace ----------------------------- {{{1
31#if defined(ASAN_USE_EXTERNAL_SYMBOLIZER)
32void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
33  for (uptr i = 0; i < size && addr[i]; i++) {
34    uptr pc = addr[i];
35    char buff[4096];
36    ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff));
37    AsanPrintf("  #%zu 0x%zx %s\n", i, pc, buff);
38  }
39}
40
41#else  // ASAN_USE_EXTERNAL_SYMBOLIZER
42void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
43  ProcessMaps proc_maps;
44  uptr frame_num = 0;
45  for (uptr i = 0; i < size && addr[i]; i++) {
46    proc_maps.Reset();
47    uptr pc = addr[i];
48    uptr offset;
49    char filename[4096];
50    if (FLAG_symbolize) {
51      AddressInfoList *address_info_list = SymbolizeCode(pc);
52      for (AddressInfoList *entry = address_info_list; entry;
53           entry = entry->next) {
54        AddressInfo info = entry->info;
55        AsanPrintf("    #%zu 0x%zx %s:%d:%d\n", frame_num, pc,
56                                                (info.file) ? info.file : "",
57                                                info.line, info.column);
58        frame_num++;
59      }
60      address_info_list->Clear();
61    } else {
62      if (proc_maps.GetObjectNameAndOffset(pc, &offset,
63                                           filename, sizeof(filename))) {
64        AsanPrintf("    #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc, filename,
65                                                  offset);
66      } else {
67        AsanPrintf("    #%zu 0x%zx\n", frame_num, pc);
68      }
69      frame_num++;
70    }
71  }
72}
73#endif  // ASAN_USE_EXTERNAL_SYMBOLIZER
74
75uptr AsanStackTrace::GetCurrentPc() {
76  return GET_CALLER_PC();
77}
78
79void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) {
80  CHECK(size == 0 && trace[0] == pc);
81  size = 1;
82  if (!asan_inited) return;
83  AsanThread *t = asanThreadRegistry().GetCurrent();
84  if (!t) return;
85  uptr *frame = (uptr*)bp;
86  uptr *prev_frame = frame;
87  uptr *top = (uptr*)t->stack_top();
88  uptr *bottom = (uptr*)t->stack_bottom();
89  while (frame >= prev_frame &&
90         frame < top - 2 &&
91         frame > bottom &&
92         size < max_size) {
93    uptr pc1 = frame[1];
94    if (pc1 != pc) {
95      trace[size++] = pc1;
96    }
97    prev_frame = frame;
98    frame = (uptr*)frame[0];
99  }
100}
101
102// On 32-bits we don't compress stack traces.
103// On 64-bits we compress stack traces: if a given pc differes slightly from
104// the previous one, we record a 31-bit offset instead of the full pc.
105uptr AsanStackTrace::CompressStack(AsanStackTrace *stack,
106                                   u32 *compressed, uptr size) {
107#if __WORDSIZE == 32
108  // Don't compress, just copy.
109  uptr res = 0;
110  for (uptr i = 0; i < stack->size && i < size; i++) {
111    compressed[i] = stack->trace[i];
112    res++;
113  }
114  if (stack->size < size)
115    compressed[stack->size] = 0;
116#else  // 64 bits, compress.
117  uptr prev_pc = 0;
118  const uptr kMaxOffset = (1ULL << 30) - 1;
119  uptr c_index = 0;
120  uptr res = 0;
121  for (uptr i = 0, n = stack->size; i < n; i++) {
122    uptr pc = stack->trace[i];
123    if (!pc) break;
124    if ((s64)pc < 0) break;
125    // Printf("C pc[%zu] %zx\n", i, pc);
126    if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
127      uptr offset = (s64)(pc - prev_pc);
128      offset |= (1U << 31);
129      if (c_index >= size) break;
130      // Printf("C co[%zu] offset %zx\n", i, offset);
131      compressed[c_index++] = offset;
132    } else {
133      uptr hi = pc >> 32;
134      uptr lo = (pc << 32) >> 32;
135      CHECK((hi & (1 << 31)) == 0);
136      if (c_index + 1 >= size) break;
137      // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
138      compressed[c_index++] = hi;
139      compressed[c_index++] = lo;
140    }
141    res++;
142    prev_pc = pc;
143  }
144  if (c_index < size)
145    compressed[c_index] = 0;
146  if (c_index + 1 < size)
147    compressed[c_index + 1] = 0;
148#endif  // __WORDSIZE
149
150  // debug-only code
151#if 0
152  AsanStackTrace check_stack;
153  UncompressStack(&check_stack, compressed, size);
154  if (res < check_stack.size) {
155    Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
156           check_stack.size, size);
157  }
158  // |res| may be greater than check_stack.size, because
159  // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
160  CHECK(res >= check_stack.size);
161  CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
162                          check_stack.size * sizeof(uptr)));
163#endif
164
165  return res;
166}
167
168void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
169                                     u32 *compressed, uptr size) {
170#if __WORDSIZE == 32
171  // Don't uncompress, just copy.
172  stack->size = 0;
173  for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
174    if (!compressed[i]) break;
175    stack->size++;
176    stack->trace[i] = compressed[i];
177  }
178#else  // 64 bits, uncompress
179  uptr prev_pc = 0;
180  stack->size = 0;
181  for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
182    u32 x = compressed[i];
183    uptr pc = 0;
184    if (x & (1U << 31)) {
185      // Printf("U co[%zu] offset: %x\n", i, x);
186      // this is an offset
187      s32 offset = x;
188      offset = (offset << 1) >> 1;  // remove the 31-byte and sign-extend.
189      pc = prev_pc + offset;
190      CHECK(pc);
191    } else {
192      // CHECK(i + 1 < size);
193      if (i + 1 >= size) break;
194      uptr hi = x;
195      uptr lo = compressed[i+1];
196      // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
197      i++;
198      pc = (hi << 32) | lo;
199      if (!pc) break;
200    }
201    // Printf("U pc[%zu] %zx\n", stack->size, pc);
202    stack->trace[stack->size++] = pc;
203    prev_pc = pc;
204  }
205#endif  // __WORDSIZE
206}
207
208}  // namespace __asan
209