blob: 1099f32fa13c1f88c30af2c78cc8cf5ca81e28af [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++) {
Alexander Potapenko71d47ff2012-08-15 13:23:03 +000051 uptr pc = patch_pc(addr[i]);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000052 char buff[4096];
53 ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff));
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000054 // 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));
Kostya Serebryany1e172b42011-11-30 01:07:02 +000058 }
59}
60
Alexander Potapenko1d483d42012-01-18 11:42:30 +000061#else // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000062void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
Alexey Samsonov6895adc2012-06-07 06:15:12 +000063 ProcessMaps proc_maps;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000064 uptr frame_num = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000065 for (uptr i = 0; i < size && addr[i]; i++) {
Alexander Potapenko71d47ff2012-08-15 13:23:03 +000066 uptr pc = patch_pc(addr[i]);
Alexey Samsonovfa82b082012-06-15 14:00:25 +000067 AddressInfo addr_frames[64];
68 uptr addr_frames_num = 0;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000069 if (flags()->symbolize) {
Alexey Samsonov08e80a42012-07-19 15:07:26 +000070 addr_frames_num = SymbolizeCode(pc, addr_frames,
Alexey Samsonovfa82b082012-06-15 14:00:25 +000071 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);
Alexey Samsonova68633f2012-07-03 08:24:14 +000077 if (info.function) {
Alexey Samsonovc2018c12012-07-04 10:58:35 +000078 AsanPrintf(" in %s", info.function);
Alexey Samsonova68633f2012-07-03 08:24:14 +000079 }
80 if (info.file) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000081 AsanPrintf(" %s:%d:%d", StripPathPrefix(info.file), info.line,
82 info.column);
Alexey Samsonova68633f2012-07-03 08:24:14 +000083 } else if (info.module) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000084 AsanPrintf(" (%s+0x%zx)", StripPathPrefix(info.module),
85 info.module_offset);
Alexey Samsonovfa82b082012-06-15 14:00:25 +000086 }
87 AsanPrintf("\n");
88 info.Clear();
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000089 frame_num++;
90 }
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000091 } else {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000092 uptr offset;
93 char filename[4096];
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000094 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
95 filename, sizeof(filename))) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000096 AsanPrintf(" #%zu 0x%zx (%s+0x%zx)\n",
97 frame_num, pc, StripPathPrefix(filename), offset);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000098 } else {
Alexey Samsonove9541012012-06-06 13:11:29 +000099 AsanPrintf(" #%zu 0x%zx\n", frame_num, pc);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +0000100 }
101 frame_num++;
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000102 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103 }
104}
Alexander Potapenko1d483d42012-01-18 11:42:30 +0000105#endif // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000106
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000107uptr AsanStackTrace::GetCurrentPc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000108 return GET_CALLER_PC();
109}
110
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000111void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000112 CHECK(size == 0 && trace[0] == pc);
113 size = 1;
114 if (!asan_inited) return;
115 AsanThread *t = asanThreadRegistry().GetCurrent();
116 if (!t) return;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000117 uptr *frame = (uptr*)bp;
118 uptr *prev_frame = frame;
119 uptr *top = (uptr*)t->stack_top();
120 uptr *bottom = (uptr*)t->stack_bottom();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121 while (frame >= prev_frame &&
Kostya Serebryany40928f12012-03-08 22:25:08 +0000122 frame < top - 2 &&
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000123 frame > bottom &&
124 size < max_size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000125 uptr pc1 = frame[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126 if (pc1 != pc) {
127 trace[size++] = pc1;
128 }
129 prev_frame = frame;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000130 frame = (uptr*)frame[0];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131 }
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.
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000137SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000138uptr AsanStackTrace::CompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000139 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140#if __WORDSIZE == 32
141 // Don't compress, just copy.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000142 uptr res = 0;
143 for (uptr i = 0; i < stack->size && i < size; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144 compressed[i] = stack->trace[i];
145 res++;
146 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000147 if (stack->size < size)
148 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000149#else // 64 bits, compress.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000150 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];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000156 if (!pc) break;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000157 if ((s64)pc < 0) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000158 // Printf("C pc[%zu] %zx\n", i, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000159 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000160 uptr offset = (s64)(pc - prev_pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000161 offset |= (1U << 31);
162 if (c_index >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000163 // Printf("C co[%zu] offset %zx\n", i, offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164 compressed[c_index++] = offset;
165 } else {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000166 uptr hi = pc >> 32;
167 uptr lo = (pc << 32) >> 32;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000168 CHECK((hi & (1 << 31)) == 0);
169 if (c_index + 1 >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000170 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171 compressed[c_index++] = hi;
172 compressed[c_index++] = lo;
173 }
174 res++;
175 prev_pc = pc;
176 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000177 if (c_index < size)
178 compressed[c_index] = 0;
179 if (c_index + 1 < size)
180 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000181#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) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000188 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000189 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);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000194 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000195 check_stack.size * sizeof(uptr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000196#endif
197
198 return res;
199}
200
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000201SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000202void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000203 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000204#if __WORDSIZE == 32
205 // Don't uncompress, just copy.
206 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000207 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000208 if (!compressed[i]) break;
209 stack->size++;
210 stack->trace[i] = compressed[i];
211 }
212#else // 64 bits, uncompress
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000213 uptr prev_pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000214 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000215 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000216 u32 x = compressed[i];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000217 uptr pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000218 if (x & (1U << 31)) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000219 // Printf("U co[%zu] offset: %x\n", i, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000220 // this is an offset
Kostya Serebryanyee392552012-05-31 15:02:07 +0000221 s32 offset = x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000222 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;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000228 uptr hi = x;
229 uptr lo = compressed[i+1];
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000230 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000231 i++;
232 pc = (hi << 32) | lo;
233 if (!pc) break;
234 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000235 // Printf("U pc[%zu] %zx\n", stack->size, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000236 stack->trace[stack->size++] = pc;
237 prev_pc = pc;
238 }
239#endif // __WORDSIZE
240}
241
242} // namespace __asan