blob: 42ef88c6bb7d928b9d63415750b2db8c1c32aad3 [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"
Alexey Samsonovc93d3e22012-08-22 13:31:37 +000015#include "asan_interface.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000016#include "asan_lock.h"
17#include "asan_stack.h"
18#include "asan_thread.h"
19#include "asan_thread_registry.h"
Alexey Samsonov6895adc2012-06-07 06:15:12 +000020#include "sanitizer_common/sanitizer_procmaps.h"
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000021#include "sanitizer_common/sanitizer_symbolizer.h"
22
Kostya Serebryany1e172b42011-11-30 01:07:02 +000023namespace __asan {
24
Alexey Samsonovc93d3e22012-08-22 13:31:37 +000025static __asan_symbolize_callback symbolize_callback;
26
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000027static 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
Alexander Potapenko1d483d42012-01-18 11:42:30 +000034// ----------------------- AsanStackTrace ----------------------------- {{{1
Alexey Samsonov08e80a42012-07-19 15:07:26 +000035// 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
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000046void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
Alexey Samsonov6895adc2012-06-07 06:15:12 +000047 ProcessMaps proc_maps;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000048 uptr frame_num = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000049 for (uptr i = 0; i < size && addr[i]; i++) {
Alexander Potapenko71d47ff2012-08-15 13:23:03 +000050 uptr pc = patch_pc(addr[i]);
Alexey Samsonovc93d3e22012-08-22 13:31:37 +000051 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 AsanPrintf(" #%zu 0x%zx %s\n", frame_num, pc, StripPathPrefix(buff));
58 frame_num++;
59 continue;
60 }
Alexey Samsonovfa82b082012-06-15 14:00:25 +000061 AddressInfo addr_frames[64];
62 uptr addr_frames_num = 0;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000063 if (flags()->symbolize) {
Alexey Samsonov08e80a42012-07-19 15:07:26 +000064 addr_frames_num = SymbolizeCode(pc, addr_frames,
Alexey Samsonovfa82b082012-06-15 14:00:25 +000065 ASAN_ARRAY_SIZE(addr_frames));
66 }
67 if (addr_frames_num > 0) {
68 for (uptr j = 0; j < addr_frames_num; j++) {
69 AddressInfo &info = addr_frames[j];
70 AsanPrintf(" #%zu 0x%zx", frame_num, pc);
Alexey Samsonova68633f2012-07-03 08:24:14 +000071 if (info.function) {
Alexey Samsonovc2018c12012-07-04 10:58:35 +000072 AsanPrintf(" in %s", info.function);
Alexey Samsonova68633f2012-07-03 08:24:14 +000073 }
74 if (info.file) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000075 AsanPrintf(" %s:%d:%d", StripPathPrefix(info.file), info.line,
76 info.column);
Alexey Samsonova68633f2012-07-03 08:24:14 +000077 } else if (info.module) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000078 AsanPrintf(" (%s+0x%zx)", StripPathPrefix(info.module),
79 info.module_offset);
Alexey Samsonovfa82b082012-06-15 14:00:25 +000080 }
81 AsanPrintf("\n");
82 info.Clear();
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000083 frame_num++;
84 }
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000085 } else {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000086 uptr offset;
87 char filename[4096];
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000088 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
89 filename, sizeof(filename))) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000090 AsanPrintf(" #%zu 0x%zx (%s+0x%zx)\n",
91 frame_num, pc, StripPathPrefix(filename), offset);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000092 } else {
Alexey Samsonove9541012012-06-06 13:11:29 +000093 AsanPrintf(" #%zu 0x%zx\n", frame_num, pc);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000094 }
95 frame_num++;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000096 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000097 }
98}
Kostya Serebryany1e172b42011-11-30 01:07:02 +000099
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000100uptr AsanStackTrace::GetCurrentPc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101 return GET_CALLER_PC();
102}
103
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000104void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000105 CHECK(size == 0 && trace[0] == pc);
106 size = 1;
107 if (!asan_inited) return;
108 AsanThread *t = asanThreadRegistry().GetCurrent();
109 if (!t) return;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000110 uptr *frame = (uptr*)bp;
111 uptr *prev_frame = frame;
112 uptr *top = (uptr*)t->stack_top();
113 uptr *bottom = (uptr*)t->stack_bottom();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000114 while (frame >= prev_frame &&
Kostya Serebryany40928f12012-03-08 22:25:08 +0000115 frame < top - 2 &&
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000116 frame > bottom &&
117 size < max_size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000118 uptr pc1 = frame[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119 if (pc1 != pc) {
120 trace[size++] = pc1;
121 }
122 prev_frame = frame;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000123 frame = (uptr*)frame[0];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000124 }
125}
126
127// On 32-bits we don't compress stack traces.
128// On 64-bits we compress stack traces: if a given pc differes slightly from
129// the previous one, we record a 31-bit offset instead of the full pc.
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000130SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000131uptr AsanStackTrace::CompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000132 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133#if __WORDSIZE == 32
134 // Don't compress, just copy.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000135 uptr res = 0;
136 for (uptr i = 0; i < stack->size && i < size; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000137 compressed[i] = stack->trace[i];
138 res++;
139 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000140 if (stack->size < size)
141 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000142#else // 64 bits, compress.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000143 uptr prev_pc = 0;
144 const uptr kMaxOffset = (1ULL << 30) - 1;
145 uptr c_index = 0;
146 uptr res = 0;
147 for (uptr i = 0, n = stack->size; i < n; i++) {
148 uptr pc = stack->trace[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000149 if (!pc) break;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000150 if ((s64)pc < 0) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000151 // Printf("C pc[%zu] %zx\n", i, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000153 uptr offset = (s64)(pc - prev_pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000154 offset |= (1U << 31);
155 if (c_index >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000156 // Printf("C co[%zu] offset %zx\n", i, offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000157 compressed[c_index++] = offset;
158 } else {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000159 uptr hi = pc >> 32;
160 uptr lo = (pc << 32) >> 32;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000161 CHECK((hi & (1 << 31)) == 0);
162 if (c_index + 1 >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000163 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164 compressed[c_index++] = hi;
165 compressed[c_index++] = lo;
166 }
167 res++;
168 prev_pc = pc;
169 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000170 if (c_index < size)
171 compressed[c_index] = 0;
172 if (c_index + 1 < size)
173 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174#endif // __WORDSIZE
175
176 // debug-only code
177#if 0
178 AsanStackTrace check_stack;
179 UncompressStack(&check_stack, compressed, size);
180 if (res < check_stack.size) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000181 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000182 check_stack.size, size);
183 }
184 // |res| may be greater than check_stack.size, because
185 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
186 CHECK(res >= check_stack.size);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000187 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000188 check_stack.size * sizeof(uptr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000189#endif
190
191 return res;
192}
193
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000194SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000195void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000196 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000197#if __WORDSIZE == 32
198 // Don't uncompress, just copy.
199 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000200 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000201 if (!compressed[i]) break;
202 stack->size++;
203 stack->trace[i] = compressed[i];
204 }
205#else // 64 bits, uncompress
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000206 uptr prev_pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000207 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000208 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000209 u32 x = compressed[i];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000210 uptr pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000211 if (x & (1U << 31)) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000212 // Printf("U co[%zu] offset: %x\n", i, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000213 // this is an offset
Kostya Serebryanyee392552012-05-31 15:02:07 +0000214 s32 offset = x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000215 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
216 pc = prev_pc + offset;
217 CHECK(pc);
218 } else {
219 // CHECK(i + 1 < size);
220 if (i + 1 >= size) break;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000221 uptr hi = x;
222 uptr lo = compressed[i+1];
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000223 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000224 i++;
225 pc = (hi << 32) | lo;
226 if (!pc) break;
227 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000228 // Printf("U pc[%zu] %zx\n", stack->size, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000229 stack->trace[stack->size++] = pc;
230 prev_pc = pc;
231 }
232#endif // __WORDSIZE
233}
234
235} // namespace __asan
Alexey Samsonovc93d3e22012-08-22 13:31:37 +0000236
237// ------------------ Interface -------------- {{{1
238using namespace __asan; // NOLINT
239
240void NOINLINE __asan_set_symbolize_callback(
241 __asan_symbolize_callback callback) {
242 symbolize_callback = callback;
243}