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