blob: d6103c2c98facb424c88340a11108dcfde96132d [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
Alexander Potapenko1d483d42012-01-18 11:42:30 +000029// ----------------------- AsanStackTrace ----------------------------- {{{1
Alexey Samsonov08e80a42012-07-19 15:07:26 +000030// PCs in stack traces are actually the return addresses, that is,
31// addresses of the next instructions after the call. That's why we
32// decrement them.
33static uptr patch_pc(uptr pc) {
34#ifdef __arm__
35 // Cancel Thumb bit.
36 pc = pc & (~1);
37#endif
38 return pc - 1;
39}
40
Alexander Potapenko1d483d42012-01-18 11:42:30 +000041#if defined(ASAN_USE_EXTERNAL_SYMBOLIZER)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000042void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
43 for (uptr i = 0; i < size && addr[i]; i++) {
44 uptr pc = addr[i];
Alexey Samsonov08e80a42012-07-19 15:07:26 +000045 if (i < size - 1 && addr[i + 1])
46 pc = patch_pc(pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000047 char buff[4096];
48 ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff));
Alexey Samsonove9541012012-06-06 13:11:29 +000049 AsanPrintf(" #%zu 0x%zx %s\n", i, pc, buff);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000050 }
51}
52
Alexander Potapenko1d483d42012-01-18 11:42:30 +000053#else // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000054void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
Alexey Samsonov6895adc2012-06-07 06:15:12 +000055 ProcessMaps proc_maps;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000056 uptr frame_num = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000057 for (uptr i = 0; i < size && addr[i]; i++) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000058 uptr pc = addr[i];
Alexey Samsonov08e80a42012-07-19 15:07:26 +000059 if (i < size - 1 && addr[i + 1])
60 pc = patch_pc(pc);
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) {
75 AsanPrintf(" %s:%d:%d", info.file, info.line, info.column);
76 } else if (info.module) {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000077 AsanPrintf(" (%s+0x%zx)", info.module, info.module_offset);
78 }
79 AsanPrintf("\n");
80 info.Clear();
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000081 frame_num++;
82 }
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000083 } else {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000084 uptr offset;
85 char filename[4096];
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000086 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
87 filename, sizeof(filename))) {
Alexey Samsonove9541012012-06-06 13:11:29 +000088 AsanPrintf(" #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc, filename,
89 offset);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000090 } else {
Alexey Samsonove9541012012-06-06 13:11:29 +000091 AsanPrintf(" #%zu 0x%zx\n", frame_num, pc);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000092 }
93 frame_num++;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000094 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000095 }
96}
Alexander Potapenko1d483d42012-01-18 11:42:30 +000097#endif // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany1e172b42011-11-30 01:07:02 +000098
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000099uptr AsanStackTrace::GetCurrentPc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000100 return GET_CALLER_PC();
101}
102
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000103void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000104 CHECK(size == 0 && trace[0] == pc);
105 size = 1;
106 if (!asan_inited) return;
107 AsanThread *t = asanThreadRegistry().GetCurrent();
108 if (!t) return;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000109 uptr *frame = (uptr*)bp;
110 uptr *prev_frame = frame;
111 uptr *top = (uptr*)t->stack_top();
112 uptr *bottom = (uptr*)t->stack_bottom();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000113 while (frame >= prev_frame &&
Kostya Serebryany40928f12012-03-08 22:25:08 +0000114 frame < top - 2 &&
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000115 frame > bottom &&
116 size < max_size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000117 uptr pc1 = frame[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000118 if (pc1 != pc) {
119 trace[size++] = pc1;
120 }
121 prev_frame = frame;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000122 frame = (uptr*)frame[0];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000123 }
124}
125
126// On 32-bits we don't compress stack traces.
127// On 64-bits we compress stack traces: if a given pc differes slightly from
128// the previous one, we record a 31-bit offset instead of the full pc.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000129uptr AsanStackTrace::CompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000130 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131#if __WORDSIZE == 32
132 // Don't compress, just copy.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000133 uptr res = 0;
134 for (uptr i = 0; i < stack->size && i < size; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000135 compressed[i] = stack->trace[i];
136 res++;
137 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000138 if (stack->size < size)
139 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140#else // 64 bits, compress.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000141 uptr prev_pc = 0;
142 const uptr kMaxOffset = (1ULL << 30) - 1;
143 uptr c_index = 0;
144 uptr res = 0;
145 for (uptr i = 0, n = stack->size; i < n; i++) {
146 uptr pc = stack->trace[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147 if (!pc) break;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000148 if ((s64)pc < 0) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000149 // Printf("C pc[%zu] %zx\n", i, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000150 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000151 uptr offset = (s64)(pc - prev_pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152 offset |= (1U << 31);
153 if (c_index >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000154 // Printf("C co[%zu] offset %zx\n", i, offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000155 compressed[c_index++] = offset;
156 } else {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000157 uptr hi = pc >> 32;
158 uptr lo = (pc << 32) >> 32;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000159 CHECK((hi & (1 << 31)) == 0);
160 if (c_index + 1 >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000161 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000162 compressed[c_index++] = hi;
163 compressed[c_index++] = lo;
164 }
165 res++;
166 prev_pc = pc;
167 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000168 if (c_index < size)
169 compressed[c_index] = 0;
170 if (c_index + 1 < size)
171 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000172#endif // __WORDSIZE
173
174 // debug-only code
175#if 0
176 AsanStackTrace check_stack;
177 UncompressStack(&check_stack, compressed, size);
178 if (res < check_stack.size) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000179 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000180 check_stack.size, size);
181 }
182 // |res| may be greater than check_stack.size, because
183 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
184 CHECK(res >= check_stack.size);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000185 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000186 check_stack.size * sizeof(uptr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000187#endif
188
189 return res;
190}
191
192void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000193 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000194#if __WORDSIZE == 32
195 // Don't uncompress, just copy.
196 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000197 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000198 if (!compressed[i]) break;
199 stack->size++;
200 stack->trace[i] = compressed[i];
201 }
202#else // 64 bits, uncompress
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000203 uptr prev_pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000204 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000205 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000206 u32 x = compressed[i];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000207 uptr pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000208 if (x & (1U << 31)) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000209 // Printf("U co[%zu] offset: %x\n", i, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000210 // this is an offset
Kostya Serebryanyee392552012-05-31 15:02:07 +0000211 s32 offset = x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000212 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
213 pc = prev_pc + offset;
214 CHECK(pc);
215 } else {
216 // CHECK(i + 1 < size);
217 if (i + 1 >= size) break;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000218 uptr hi = x;
219 uptr lo = compressed[i+1];
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000220 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000221 i++;
222 pc = (hi << 32) | lo;
223 if (!pc) break;
224 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000225 // Printf("U pc[%zu] %zx\n", stack->size, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000226 stack->trace[stack->size++] = pc;
227 prev_pc = pc;
228 }
229#endif // __WORDSIZE
230}
231
232} // namespace __asan