blob: 1c60565e85ab46f8196fa33af13c33bbce11809b [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 Samsonovfa82b082012-06-15 14:00:25 +000049 addr_frames_num = SymbolizeCode(pc, addr_frames,
50 ASAN_ARRAY_SIZE(addr_frames));
51 }
52 if (addr_frames_num > 0) {
53 for (uptr j = 0; j < addr_frames_num; j++) {
54 AddressInfo &info = addr_frames[j];
55 AsanPrintf(" #%zu 0x%zx", frame_num, pc);
56 if (info.module) {
57 AsanPrintf(" (%s+0x%zx)", info.module, info.module_offset);
58 }
59 AsanPrintf("\n");
60 info.Clear();
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000061 frame_num++;
62 }
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000063 } else {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000064 uptr offset;
65 char filename[4096];
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000066 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
67 filename, sizeof(filename))) {
Alexey Samsonove9541012012-06-06 13:11:29 +000068 AsanPrintf(" #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc, filename,
69 offset);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000070 } else {
Alexey Samsonove9541012012-06-06 13:11:29 +000071 AsanPrintf(" #%zu 0x%zx\n", frame_num, pc);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000072 }
73 frame_num++;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000074 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000075 }
76}
Alexander Potapenko1d483d42012-01-18 11:42:30 +000077#endif // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany1e172b42011-11-30 01:07:02 +000078
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000079uptr AsanStackTrace::GetCurrentPc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000080 return GET_CALLER_PC();
81}
82
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000083void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084 CHECK(size == 0 && trace[0] == pc);
85 size = 1;
86 if (!asan_inited) return;
87 AsanThread *t = asanThreadRegistry().GetCurrent();
88 if (!t) return;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000089 uptr *frame = (uptr*)bp;
90 uptr *prev_frame = frame;
91 uptr *top = (uptr*)t->stack_top();
92 uptr *bottom = (uptr*)t->stack_bottom();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000093 while (frame >= prev_frame &&
Kostya Serebryany40928f12012-03-08 22:25:08 +000094 frame < top - 2 &&
Kostya Serebryany1e172b42011-11-30 01:07:02 +000095 frame > bottom &&
96 size < max_size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000097 uptr pc1 = frame[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +000098 if (pc1 != pc) {
99 trace[size++] = pc1;
100 }
101 prev_frame = frame;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000102 frame = (uptr*)frame[0];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103 }
104}
105
106// On 32-bits we don't compress stack traces.
107// On 64-bits we compress stack traces: if a given pc differes slightly from
108// the previous one, we record a 31-bit offset instead of the full pc.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000109uptr AsanStackTrace::CompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000110 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000111#if __WORDSIZE == 32
112 // Don't compress, just copy.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000113 uptr res = 0;
114 for (uptr i = 0; i < stack->size && i < size; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000115 compressed[i] = stack->trace[i];
116 res++;
117 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000118 if (stack->size < size)
119 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000120#else // 64 bits, compress.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000121 uptr prev_pc = 0;
122 const uptr kMaxOffset = (1ULL << 30) - 1;
123 uptr c_index = 0;
124 uptr res = 0;
125 for (uptr i = 0, n = stack->size; i < n; i++) {
126 uptr pc = stack->trace[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127 if (!pc) break;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000128 if ((s64)pc < 0) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000129 // Printf("C pc[%zu] %zx\n", i, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000130 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000131 uptr offset = (s64)(pc - prev_pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000132 offset |= (1U << 31);
133 if (c_index >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000134 // Printf("C co[%zu] offset %zx\n", i, offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000135 compressed[c_index++] = offset;
136 } else {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000137 uptr hi = pc >> 32;
138 uptr lo = (pc << 32) >> 32;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139 CHECK((hi & (1 << 31)) == 0);
140 if (c_index + 1 >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000141 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000142 compressed[c_index++] = hi;
143 compressed[c_index++] = lo;
144 }
145 res++;
146 prev_pc = pc;
147 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000148 if (c_index < size)
149 compressed[c_index] = 0;
150 if (c_index + 1 < size)
151 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152#endif // __WORDSIZE
153
154 // debug-only code
155#if 0
156 AsanStackTrace check_stack;
157 UncompressStack(&check_stack, compressed, size);
158 if (res < check_stack.size) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000159 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000160 check_stack.size, size);
161 }
162 // |res| may be greater than check_stack.size, because
163 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
164 CHECK(res >= check_stack.size);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000165 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000166 check_stack.size * sizeof(uptr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000167#endif
168
169 return res;
170}
171
172void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000173 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174#if __WORDSIZE == 32
175 // Don't uncompress, just copy.
176 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000177 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000178 if (!compressed[i]) break;
179 stack->size++;
180 stack->trace[i] = compressed[i];
181 }
182#else // 64 bits, uncompress
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000183 uptr prev_pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000184 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000185 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000186 u32 x = compressed[i];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000187 uptr pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000188 if (x & (1U << 31)) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000189 // Printf("U co[%zu] offset: %x\n", i, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000190 // this is an offset
Kostya Serebryanyee392552012-05-31 15:02:07 +0000191 s32 offset = x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000192 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
193 pc = prev_pc + offset;
194 CHECK(pc);
195 } else {
196 // CHECK(i + 1 < size);
197 if (i + 1 >= size) break;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000198 uptr hi = x;
199 uptr lo = compressed[i+1];
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000200 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000201 i++;
202 pc = (hi << 32) | lo;
203 if (!pc) break;
204 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000205 // Printf("U pc[%zu] %zx\n", stack->size, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000206 stack->trace[stack->size++] = pc;
207 prev_pc = pc;
208 }
209#endif // __WORDSIZE
210}
211
212} // namespace __asan