blob: b46c5f49cd83a4dc78f4b3af40b11a9ae3891d59 [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
30#if defined(ASAN_USE_EXTERNAL_SYMBOLIZER)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000031void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
32 for (uptr i = 0; i < size && addr[i]; i++) {
33 uptr pc = addr[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +000034 char buff[4096];
35 ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff));
Alexey Samsonove9541012012-06-06 13:11:29 +000036 AsanPrintf(" #%zu 0x%zx %s\n", i, pc, buff);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000037 }
38}
39
Alexander Potapenko1d483d42012-01-18 11:42:30 +000040#else // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000041void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
Alexey Samsonov6895adc2012-06-07 06:15:12 +000042 ProcessMaps proc_maps;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000043 uptr frame_num = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000044 for (uptr i = 0; i < size && addr[i]; i++) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000045 uptr pc = addr[i];
Alexey Samsonovfa82b082012-06-15 14:00:25 +000046 AddressInfo addr_frames[64];
47 uptr addr_frames_num = 0;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000048 if (FLAG_symbolize) {
Alexey Samsonova68633f2012-07-03 08:24:14 +000049 bool last_frame = (i == size - 1) || !addr[i + 1];
50 addr_frames_num = SymbolizeCode(pc - !last_frame, addr_frames,
Alexey Samsonovfa82b082012-06-15 14:00:25 +000051 ASAN_ARRAY_SIZE(addr_frames));
52 }
53 if (addr_frames_num > 0) {
54 for (uptr j = 0; j < addr_frames_num; j++) {
55 AddressInfo &info = addr_frames[j];
56 AsanPrintf(" #%zu 0x%zx", frame_num, pc);
Alexey Samsonova68633f2012-07-03 08:24:14 +000057 if (info.function) {
Alexey Samsonovc2018c12012-07-04 10:58:35 +000058 AsanPrintf(" in %s", info.function);
Alexey Samsonova68633f2012-07-03 08:24:14 +000059 }
60 if (info.file) {
61 AsanPrintf(" %s:%d:%d", info.file, info.line, info.column);
62 } else if (info.module) {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000063 AsanPrintf(" (%s+0x%zx)", info.module, info.module_offset);
64 }
65 AsanPrintf("\n");
66 info.Clear();
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000067 frame_num++;
68 }
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000069 } else {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000070 uptr offset;
71 char filename[4096];
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000072 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
73 filename, sizeof(filename))) {
Alexey Samsonove9541012012-06-06 13:11:29 +000074 AsanPrintf(" #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc, filename,
75 offset);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000076 } else {
Alexey Samsonove9541012012-06-06 13:11:29 +000077 AsanPrintf(" #%zu 0x%zx\n", frame_num, pc);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000078 }
79 frame_num++;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000080 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081 }
82}
Alexander Potapenko1d483d42012-01-18 11:42:30 +000083#endif // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000085uptr AsanStackTrace::GetCurrentPc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000086 return GET_CALLER_PC();
87}
88
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000089void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000090 CHECK(size == 0 && trace[0] == pc);
91 size = 1;
92 if (!asan_inited) return;
93 AsanThread *t = asanThreadRegistry().GetCurrent();
94 if (!t) return;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000095 uptr *frame = (uptr*)bp;
96 uptr *prev_frame = frame;
97 uptr *top = (uptr*)t->stack_top();
98 uptr *bottom = (uptr*)t->stack_bottom();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000099 while (frame >= prev_frame &&
Kostya Serebryany40928f12012-03-08 22:25:08 +0000100 frame < top - 2 &&
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101 frame > bottom &&
102 size < max_size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000103 uptr pc1 = frame[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000104 if (pc1 != pc) {
105 trace[size++] = pc1;
106 }
107 prev_frame = frame;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000108 frame = (uptr*)frame[0];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000109 }
110}
111
112// On 32-bits we don't compress stack traces.
113// On 64-bits we compress stack traces: if a given pc differes slightly from
114// the previous one, we record a 31-bit offset instead of the full pc.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000115uptr AsanStackTrace::CompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000116 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000117#if __WORDSIZE == 32
118 // Don't compress, just copy.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000119 uptr res = 0;
120 for (uptr i = 0; i < stack->size && i < size; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121 compressed[i] = stack->trace[i];
122 res++;
123 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000124 if (stack->size < size)
125 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126#else // 64 bits, compress.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000127 uptr prev_pc = 0;
128 const uptr kMaxOffset = (1ULL << 30) - 1;
129 uptr c_index = 0;
130 uptr res = 0;
131 for (uptr i = 0, n = stack->size; i < n; i++) {
132 uptr pc = stack->trace[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133 if (!pc) break;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000134 if ((s64)pc < 0) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000135 // Printf("C pc[%zu] %zx\n", i, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000137 uptr offset = (s64)(pc - prev_pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000138 offset |= (1U << 31);
139 if (c_index >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000140 // Printf("C co[%zu] offset %zx\n", i, offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000141 compressed[c_index++] = offset;
142 } else {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000143 uptr hi = pc >> 32;
144 uptr lo = (pc << 32) >> 32;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000145 CHECK((hi & (1 << 31)) == 0);
146 if (c_index + 1 >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000147 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000148 compressed[c_index++] = hi;
149 compressed[c_index++] = lo;
150 }
151 res++;
152 prev_pc = pc;
153 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000154 if (c_index < size)
155 compressed[c_index] = 0;
156 if (c_index + 1 < size)
157 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000158#endif // __WORDSIZE
159
160 // debug-only code
161#if 0
162 AsanStackTrace check_stack;
163 UncompressStack(&check_stack, compressed, size);
164 if (res < check_stack.size) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000165 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166 check_stack.size, size);
167 }
168 // |res| may be greater than check_stack.size, because
169 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
170 CHECK(res >= check_stack.size);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000171 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000172 check_stack.size * sizeof(uptr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000173#endif
174
175 return res;
176}
177
178void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000179 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000180#if __WORDSIZE == 32
181 // Don't uncompress, just copy.
182 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000183 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000184 if (!compressed[i]) break;
185 stack->size++;
186 stack->trace[i] = compressed[i];
187 }
188#else // 64 bits, uncompress
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000189 uptr prev_pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000190 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000191 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000192 u32 x = compressed[i];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000193 uptr pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000194 if (x & (1U << 31)) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000195 // Printf("U co[%zu] offset: %x\n", i, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000196 // this is an offset
Kostya Serebryanyee392552012-05-31 15:02:07 +0000197 s32 offset = x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000198 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
199 pc = prev_pc + offset;
200 CHECK(pc);
201 } else {
202 // CHECK(i + 1 < size);
203 if (i + 1 >= size) break;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000204 uptr hi = x;
205 uptr lo = compressed[i+1];
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000206 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000207 i++;
208 pc = (hi << 32) | lo;
209 if (!pc) break;
210 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000211 // Printf("U pc[%zu] %zx\n", stack->size, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000212 stack->trace[stack->size++] = pc;
213 prev_pc = pc;
214 }
215#endif // __WORDSIZE
216}
217
218} // namespace __asan