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