blob: 618fc32d5aa49a6409307e417d8e8e3cf5bf8895 [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//===----------------------------------------------------------------------===//
Alexey Samsonovc93d3e22012-08-22 13:31:37 +000014#include "asan_interface.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000015#include "asan_stack.h"
Alexey Samsonov6895adc2012-06-07 06:15:12 +000016#include "sanitizer_common/sanitizer_procmaps.h"
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000017#include "sanitizer_common/sanitizer_symbolizer.h"
18
Kostya Serebryany1e172b42011-11-30 01:07:02 +000019namespace __asan {
20
Alexey Samsonovc93d3e22012-08-22 13:31:37 +000021static __asan_symbolize_callback symbolize_callback;
22
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000023static const char *StripPathPrefix(const char *filepath) {
24 const char *path_prefix = flags()->strip_path_prefix;
25 if (filepath == internal_strstr(filepath, path_prefix))
26 return filepath + internal_strlen(path_prefix);
27 return filepath;
28}
29
Kostya Serebryanyc3390df2012-08-28 11:54:30 +000030// ----------------------- StackTrace ----------------------------- {{{1
Alexey Samsonov08e80a42012-07-19 15:07:26 +000031// PCs in stack traces are actually the return addresses, that is,
32// addresses of the next instructions after the call. That's why we
33// decrement them.
34static uptr patch_pc(uptr pc) {
35#ifdef __arm__
36 // Cancel Thumb bit.
37 pc = pc & (~1);
38#endif
39 return pc - 1;
40}
41
Kostya Serebryanyc3390df2012-08-28 11:54:30 +000042void StackTrace::PrintStack(uptr *addr, uptr size) {
Alexey Samsonove1f5dac2012-08-27 13:48:48 +000043 MemoryMappingLayout proc_maps;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000044 uptr frame_num = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000045 for (uptr i = 0; i < size && addr[i]; i++) {
Alexander Potapenko71d47ff2012-08-15 13:23:03 +000046 uptr pc = patch_pc(addr[i]);
Alexey Samsonovc93d3e22012-08-22 13:31:37 +000047 if (symbolize_callback) {
48 char buff[4096];
49 symbolize_callback((void*)pc, buff, sizeof(buff));
50 // We can't know anything about the string returned by external
51 // symbolizer, but if it starts with filename, try to strip path prefix
52 // from it.
Kostya Serebryany283c2962012-08-28 11:34:40 +000053 Printf(" #%zu 0x%zx %s\n", frame_num, pc, StripPathPrefix(buff));
Alexey Samsonovc93d3e22012-08-22 13:31:37 +000054 frame_num++;
55 continue;
56 }
Alexey Samsonovfa82b082012-06-15 14:00:25 +000057 AddressInfo addr_frames[64];
58 uptr addr_frames_num = 0;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000059 if (flags()->symbolize) {
Kostya Serebryanyc3390df2012-08-28 11:54:30 +000060 addr_frames_num = SymbolizeCode(pc, addr_frames, ARRAY_SIZE(addr_frames));
Alexey Samsonovfa82b082012-06-15 14:00:25 +000061 }
62 if (addr_frames_num > 0) {
63 for (uptr j = 0; j < addr_frames_num; j++) {
64 AddressInfo &info = addr_frames[j];
Kostya Serebryany283c2962012-08-28 11:34:40 +000065 Printf(" #%zu 0x%zx", frame_num, pc);
Alexey Samsonova68633f2012-07-03 08:24:14 +000066 if (info.function) {
Kostya Serebryany283c2962012-08-28 11:34:40 +000067 Printf(" in %s", info.function);
Alexey Samsonova68633f2012-07-03 08:24:14 +000068 }
69 if (info.file) {
Kostya Serebryany283c2962012-08-28 11:34:40 +000070 Printf(" %s:%d:%d", StripPathPrefix(info.file), info.line,
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000071 info.column);
Alexey Samsonova68633f2012-07-03 08:24:14 +000072 } else if (info.module) {
Kostya Serebryany283c2962012-08-28 11:34:40 +000073 Printf(" (%s+0x%zx)", StripPathPrefix(info.module),
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000074 info.module_offset);
Alexey Samsonovfa82b082012-06-15 14:00:25 +000075 }
Kostya Serebryany283c2962012-08-28 11:34:40 +000076 Printf("\n");
Alexey Samsonovfa82b082012-06-15 14:00:25 +000077 info.Clear();
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000078 frame_num++;
79 }
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000080 } else {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000081 uptr offset;
82 char filename[4096];
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000083 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
84 filename, sizeof(filename))) {
Kostya Serebryany283c2962012-08-28 11:34:40 +000085 Printf(" #%zu 0x%zx (%s+0x%zx)\n",
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000086 frame_num, pc, StripPathPrefix(filename), offset);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000087 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +000088 Printf(" #%zu 0x%zx\n", frame_num, pc);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000089 }
90 frame_num++;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000091 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000092 }
93}
Kostya Serebryany1e172b42011-11-30 01:07:02 +000094
Kostya Serebryanyc3390df2012-08-28 11:54:30 +000095uptr StackTrace::GetCurrentPc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000096 return GET_CALLER_PC();
97}
98
Kostya Serebryany2b939c32012-08-28 13:25:55 +000099void StackTrace::FastUnwindStack(uptr pc, uptr bp,
100 uptr stack_top, uptr stack_bottom) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101 CHECK(size == 0 && trace[0] == pc);
102 size = 1;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000103 uptr *frame = (uptr*)bp;
104 uptr *prev_frame = frame;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000105 while (frame >= prev_frame &&
Kostya Serebryany2b939c32012-08-28 13:25:55 +0000106 frame < (uptr*)stack_top - 2 &&
107 frame > (uptr*)stack_bottom &&
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000108 size < max_size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000109 uptr pc1 = frame[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000110 if (pc1 != pc) {
111 trace[size++] = pc1;
112 }
113 prev_frame = frame;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000114 frame = (uptr*)frame[0];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000115 }
116}
117
118// On 32-bits we don't compress stack traces.
119// On 64-bits we compress stack traces: if a given pc differes slightly from
120// the previous one, we record a 31-bit offset instead of the full pc.
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000121SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000122uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000123#if __WORDSIZE == 32
124 // Don't compress, just copy.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000125 uptr res = 0;
126 for (uptr i = 0; i < stack->size && i < size; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127 compressed[i] = stack->trace[i];
128 res++;
129 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000130 if (stack->size < size)
131 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000132#else // 64 bits, compress.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000133 uptr prev_pc = 0;
134 const uptr kMaxOffset = (1ULL << 30) - 1;
135 uptr c_index = 0;
136 uptr res = 0;
137 for (uptr i = 0, n = stack->size; i < n; i++) {
138 uptr pc = stack->trace[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139 if (!pc) break;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000140 if ((s64)pc < 0) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000141 // Printf("C pc[%zu] %zx\n", i, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000142 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000143 uptr offset = (s64)(pc - prev_pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144 offset |= (1U << 31);
145 if (c_index >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000146 // Printf("C co[%zu] offset %zx\n", i, offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147 compressed[c_index++] = offset;
148 } else {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000149 uptr hi = pc >> 32;
150 uptr lo = (pc << 32) >> 32;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000151 CHECK((hi & (1 << 31)) == 0);
152 if (c_index + 1 >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000153 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000154 compressed[c_index++] = hi;
155 compressed[c_index++] = lo;
156 }
157 res++;
158 prev_pc = pc;
159 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000160 if (c_index < size)
161 compressed[c_index] = 0;
162 if (c_index + 1 < size)
163 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164#endif // __WORDSIZE
165
166 // debug-only code
167#if 0
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000168 StackTrace check_stack;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000169 UncompressStack(&check_stack, compressed, size);
170 if (res < check_stack.size) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000171 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000172 check_stack.size, size);
173 }
174 // |res| may be greater than check_stack.size, because
175 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
176 CHECK(res >= check_stack.size);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000177 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000178 check_stack.size * sizeof(uptr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000179#endif
180
181 return res;
182}
183
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000184SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000185void StackTrace::UncompressStack(StackTrace *stack,
186 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000187#if __WORDSIZE == 32
188 // Don't uncompress, just copy.
189 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000190 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000191 if (!compressed[i]) break;
192 stack->size++;
193 stack->trace[i] = compressed[i];
194 }
195#else // 64 bits, uncompress
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000196 uptr prev_pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000197 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000198 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000199 u32 x = compressed[i];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000200 uptr pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000201 if (x & (1U << 31)) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000202 // Printf("U co[%zu] offset: %x\n", i, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000203 // this is an offset
Kostya Serebryanyee392552012-05-31 15:02:07 +0000204 s32 offset = x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000205 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
206 pc = prev_pc + offset;
207 CHECK(pc);
208 } else {
209 // CHECK(i + 1 < size);
210 if (i + 1 >= size) break;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000211 uptr hi = x;
212 uptr lo = compressed[i+1];
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000213 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000214 i++;
215 pc = (hi << 32) | lo;
216 if (!pc) break;
217 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000218 // Printf("U pc[%zu] %zx\n", stack->size, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000219 stack->trace[stack->size++] = pc;
220 prev_pc = pc;
221 }
222#endif // __WORDSIZE
223}
224
225} // namespace __asan
Alexey Samsonovc93d3e22012-08-22 13:31:37 +0000226
227// ------------------ Interface -------------- {{{1
228using namespace __asan; // NOLINT
229
230void NOINLINE __asan_set_symbolize_callback(
231 __asan_symbolize_callback callback) {
232 symbolize_callback = callback;
233}