blob: 703cc692bc1633e24c3eeb24b5c60b86248c6ca2 [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_stack.cc -----------------------------------------------------===//
Kostya Serebryany1e172b42011-11-30 01:07:02 +00002//
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"
Alexey Samsonov6895adc2012-06-07 06:15:12 +000019#include "sanitizer_common/sanitizer_procmaps.h"
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000020#include "sanitizer_common/sanitizer_symbolizer.h"
21
Kostya Serebryany1e172b42011-11-30 01:07:02 +000022#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
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000029static 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
Alexander Potapenko1d483d42012-01-18 11:42:30 +000036// ----------------------- AsanStackTrace ----------------------------- {{{1
Alexey Samsonov08e80a42012-07-19 15:07:26 +000037// 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
Alexander Potapenko1d483d42012-01-18 11:42:30 +000048#if defined(ASAN_USE_EXTERNAL_SYMBOLIZER)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000049void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
50 for (uptr i = 0; i < size && addr[i]; i++) {
51 uptr pc = addr[i];
Alexey Samsonov08e80a42012-07-19 15:07:26 +000052 if (i < size - 1 && addr[i + 1])
53 pc = patch_pc(pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054 char buff[4096];
55 ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff));
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000056 // We can't know anything about the string returned by external
57 // symbolizer, but if it starts with filename, try to strip path prefix
58 // from it.
59 AsanPrintf(" #%zu 0x%zx %s\n", i, pc, StripPathPrefix(buff));
Kostya Serebryany1e172b42011-11-30 01:07:02 +000060 }
61}
62
Alexander Potapenko1d483d42012-01-18 11:42:30 +000063#else // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000064void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
Alexey Samsonov6895adc2012-06-07 06:15:12 +000065 ProcessMaps proc_maps;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000066 uptr frame_num = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000067 for (uptr i = 0; i < size && addr[i]; i++) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000068 uptr pc = addr[i];
Alexey Samsonov08e80a42012-07-19 15:07:26 +000069 if (i < size - 1 && addr[i + 1])
70 pc = patch_pc(pc);
Alexey Samsonovfa82b082012-06-15 14:00:25 +000071 AddressInfo addr_frames[64];
72 uptr addr_frames_num = 0;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000073 if (flags()->symbolize) {
Alexey Samsonov08e80a42012-07-19 15:07:26 +000074 addr_frames_num = SymbolizeCode(pc, addr_frames,
Alexey Samsonovfa82b082012-06-15 14:00:25 +000075 ASAN_ARRAY_SIZE(addr_frames));
76 }
77 if (addr_frames_num > 0) {
78 for (uptr j = 0; j < addr_frames_num; j++) {
79 AddressInfo &info = addr_frames[j];
80 AsanPrintf(" #%zu 0x%zx", frame_num, pc);
Alexey Samsonova68633f2012-07-03 08:24:14 +000081 if (info.function) {
Alexey Samsonovc2018c12012-07-04 10:58:35 +000082 AsanPrintf(" in %s", info.function);
Alexey Samsonova68633f2012-07-03 08:24:14 +000083 }
84 if (info.file) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000085 AsanPrintf(" %s:%d:%d", StripPathPrefix(info.file), info.line,
86 info.column);
Alexey Samsonova68633f2012-07-03 08:24:14 +000087 } else if (info.module) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000088 AsanPrintf(" (%s+0x%zx)", StripPathPrefix(info.module),
89 info.module_offset);
Alexey Samsonovfa82b082012-06-15 14:00:25 +000090 }
91 AsanPrintf("\n");
92 info.Clear();
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000093 frame_num++;
94 }
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000095 } else {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000096 uptr offset;
97 char filename[4096];
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000098 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
99 filename, sizeof(filename))) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +0000100 AsanPrintf(" #%zu 0x%zx (%s+0x%zx)\n",
101 frame_num, pc, StripPathPrefix(filename), offset);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +0000102 } else {
Alexey Samsonove9541012012-06-06 13:11:29 +0000103 AsanPrintf(" #%zu 0x%zx\n", frame_num, pc);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +0000104 }
105 frame_num++;
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000106 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000107 }
108}
Alexander Potapenko1d483d42012-01-18 11:42:30 +0000109#endif // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000110
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000111uptr AsanStackTrace::GetCurrentPc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000112 return GET_CALLER_PC();
113}
114
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000115void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000116 CHECK(size == 0 && trace[0] == pc);
117 size = 1;
118 if (!asan_inited) return;
119 AsanThread *t = asanThreadRegistry().GetCurrent();
120 if (!t) return;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000121 uptr *frame = (uptr*)bp;
122 uptr *prev_frame = frame;
123 uptr *top = (uptr*)t->stack_top();
124 uptr *bottom = (uptr*)t->stack_bottom();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000125 while (frame >= prev_frame &&
Kostya Serebryany40928f12012-03-08 22:25:08 +0000126 frame < top - 2 &&
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127 frame > bottom &&
128 size < max_size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000129 uptr pc1 = frame[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000130 if (pc1 != pc) {
131 trace[size++] = pc1;
132 }
133 prev_frame = frame;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000134 frame = (uptr*)frame[0];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000135 }
136}
137
138// On 32-bits we don't compress stack traces.
139// On 64-bits we compress stack traces: if a given pc differes slightly from
140// the previous one, we record a 31-bit offset instead of the full pc.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000141uptr AsanStackTrace::CompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000142 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000143#if __WORDSIZE == 32
144 // Don't compress, just copy.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000145 uptr res = 0;
146 for (uptr i = 0; i < stack->size && i < size; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147 compressed[i] = stack->trace[i];
148 res++;
149 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000150 if (stack->size < size)
151 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152#else // 64 bits, compress.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000153 uptr prev_pc = 0;
154 const uptr kMaxOffset = (1ULL << 30) - 1;
155 uptr c_index = 0;
156 uptr res = 0;
157 for (uptr i = 0, n = stack->size; i < n; i++) {
158 uptr pc = stack->trace[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000159 if (!pc) break;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000160 if ((s64)pc < 0) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000161 // Printf("C pc[%zu] %zx\n", i, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000162 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000163 uptr offset = (s64)(pc - prev_pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164 offset |= (1U << 31);
165 if (c_index >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000166 // Printf("C co[%zu] offset %zx\n", i, offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000167 compressed[c_index++] = offset;
168 } else {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000169 uptr hi = pc >> 32;
170 uptr lo = (pc << 32) >> 32;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171 CHECK((hi & (1 << 31)) == 0);
172 if (c_index + 1 >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000173 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174 compressed[c_index++] = hi;
175 compressed[c_index++] = lo;
176 }
177 res++;
178 prev_pc = pc;
179 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000180 if (c_index < size)
181 compressed[c_index] = 0;
182 if (c_index + 1 < size)
183 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000184#endif // __WORDSIZE
185
186 // debug-only code
187#if 0
188 AsanStackTrace check_stack;
189 UncompressStack(&check_stack, compressed, size);
190 if (res < check_stack.size) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000191 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000192 check_stack.size, size);
193 }
194 // |res| may be greater than check_stack.size, because
195 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
196 CHECK(res >= check_stack.size);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000197 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000198 check_stack.size * sizeof(uptr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000199#endif
200
201 return res;
202}
203
204void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000205 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000206#if __WORDSIZE == 32
207 // Don't uncompress, just copy.
208 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000209 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000210 if (!compressed[i]) break;
211 stack->size++;
212 stack->trace[i] = compressed[i];
213 }
214#else // 64 bits, uncompress
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000215 uptr prev_pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000216 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000217 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000218 u32 x = compressed[i];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000219 uptr pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000220 if (x & (1U << 31)) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000221 // Printf("U co[%zu] offset: %x\n", i, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000222 // this is an offset
Kostya Serebryanyee392552012-05-31 15:02:07 +0000223 s32 offset = x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000224 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
225 pc = prev_pc + offset;
226 CHECK(pc);
227 } else {
228 // CHECK(i + 1 < size);
229 if (i + 1 >= size) break;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000230 uptr hi = x;
231 uptr lo = compressed[i+1];
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000232 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000233 i++;
234 pc = (hi << 32) | lo;
235 if (!pc) break;
236 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000237 // Printf("U pc[%zu] %zx\n", stack->size, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000238 stack->trace[stack->size++] = pc;
239 prev_pc = pc;
240 }
241#endif // __WORDSIZE
242}
243
244} // namespace __asan