blob: d8611333ade59076123222c791ae93f1ddf010f8 [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
Kostya Serebryanycc347222012-08-28 13:49:49 +000023static const char *StripPathPrefix(const char *filepath,
24 const char *strip_file_prefix) {
25 if (filepath == internal_strstr(filepath, strip_file_prefix))
26 return filepath + internal_strlen(strip_file_prefix);
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000027 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 Serebryanycc347222012-08-28 13:49:49 +000042void StackTrace::PrintStack(uptr *addr, uptr size,
43 bool symbolize, const char *strip_file_prefix,
44 SymbolizeCallback symbolize_callback ) {
Alexey Samsonove1f5dac2012-08-27 13:48:48 +000045 MemoryMappingLayout proc_maps;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000046 uptr frame_num = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000047 for (uptr i = 0; i < size && addr[i]; i++) {
Alexander Potapenko71d47ff2012-08-15 13:23:03 +000048 uptr pc = patch_pc(addr[i]);
Alexey Samsonovc93d3e22012-08-22 13:31:37 +000049 if (symbolize_callback) {
50 char buff[4096];
51 symbolize_callback((void*)pc, buff, sizeof(buff));
52 // We can't know anything about the string returned by external
53 // symbolizer, but if it starts with filename, try to strip path prefix
54 // from it.
Kostya Serebryanycc347222012-08-28 13:49:49 +000055 Printf(" #%zu 0x%zx %s\n", frame_num, pc,
56 StripPathPrefix(buff, strip_file_prefix));
Alexey Samsonovc93d3e22012-08-22 13:31:37 +000057 frame_num++;
58 continue;
59 }
Alexey Samsonovfa82b082012-06-15 14:00:25 +000060 AddressInfo addr_frames[64];
Kostya Serebryanycc347222012-08-28 13:49:49 +000061 uptr addr_frames_num =
62 symbolize ? SymbolizeCode(pc, addr_frames, ARRAY_SIZE(addr_frames)) : 0;
Alexey Samsonovfa82b082012-06-15 14:00:25 +000063 if (addr_frames_num > 0) {
64 for (uptr j = 0; j < addr_frames_num; j++) {
65 AddressInfo &info = addr_frames[j];
Kostya Serebryany283c2962012-08-28 11:34:40 +000066 Printf(" #%zu 0x%zx", frame_num, pc);
Alexey Samsonova68633f2012-07-03 08:24:14 +000067 if (info.function) {
Kostya Serebryany283c2962012-08-28 11:34:40 +000068 Printf(" in %s", info.function);
Alexey Samsonova68633f2012-07-03 08:24:14 +000069 }
70 if (info.file) {
Kostya Serebryanycc347222012-08-28 13:49:49 +000071 Printf(" %s:%d:%d", StripPathPrefix(info.file, strip_file_prefix),
72 info.line, info.column);
Alexey Samsonova68633f2012-07-03 08:24:14 +000073 } else if (info.module) {
Kostya Serebryanycc347222012-08-28 13:49:49 +000074 Printf(" (%s+0x%zx)", StripPathPrefix(info.module, strip_file_prefix),
75 info.module_offset);
Alexey Samsonovfa82b082012-06-15 14:00:25 +000076 }
Kostya Serebryany283c2962012-08-28 11:34:40 +000077 Printf("\n");
Alexey Samsonovfa82b082012-06-15 14:00:25 +000078 info.Clear();
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000079 frame_num++;
80 }
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000081 } else {
Alexey Samsonovfa82b082012-06-15 14:00:25 +000082 uptr offset;
83 char filename[4096];
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000084 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
85 filename, sizeof(filename))) {
Kostya Serebryanycc347222012-08-28 13:49:49 +000086 Printf(" #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc,
87 StripPathPrefix(filename, strip_file_prefix), offset);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000088 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +000089 Printf(" #%zu 0x%zx\n", frame_num, pc);
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000090 }
91 frame_num++;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000092 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000093 }
94}
Kostya Serebryany1e172b42011-11-30 01:07:02 +000095
Kostya Serebryanyc3390df2012-08-28 11:54:30 +000096uptr StackTrace::GetCurrentPc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000097 return GET_CALLER_PC();
98}
99
Kostya Serebryany2b939c32012-08-28 13:25:55 +0000100void StackTrace::FastUnwindStack(uptr pc, uptr bp,
101 uptr stack_top, uptr stack_bottom) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000102 CHECK(size == 0 && trace[0] == pc);
103 size = 1;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000104 uptr *frame = (uptr*)bp;
105 uptr *prev_frame = frame;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000106 while (frame >= prev_frame &&
Kostya Serebryany2b939c32012-08-28 13:25:55 +0000107 frame < (uptr*)stack_top - 2 &&
108 frame > (uptr*)stack_bottom &&
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000109 size < max_size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000110 uptr pc1 = frame[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000111 if (pc1 != pc) {
112 trace[size++] = pc1;
113 }
114 prev_frame = frame;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000115 frame = (uptr*)frame[0];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000116 }
117}
118
119// On 32-bits we don't compress stack traces.
120// On 64-bits we compress stack traces: if a given pc differes slightly from
121// the previous one, we record a 31-bit offset instead of the full pc.
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000122SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000123uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000124#if __WORDSIZE == 32
125 // Don't compress, just copy.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000126 uptr res = 0;
127 for (uptr i = 0; i < stack->size && i < size; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000128 compressed[i] = stack->trace[i];
129 res++;
130 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000131 if (stack->size < size)
132 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133#else // 64 bits, compress.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000134 uptr prev_pc = 0;
135 const uptr kMaxOffset = (1ULL << 30) - 1;
136 uptr c_index = 0;
137 uptr res = 0;
138 for (uptr i = 0, n = stack->size; i < n; i++) {
139 uptr pc = stack->trace[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140 if (!pc) break;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000141 if ((s64)pc < 0) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000142 // Printf("C pc[%zu] %zx\n", i, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000143 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000144 uptr offset = (s64)(pc - prev_pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000145 offset |= (1U << 31);
146 if (c_index >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000147 // Printf("C co[%zu] offset %zx\n", i, offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000148 compressed[c_index++] = offset;
149 } else {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000150 uptr hi = pc >> 32;
151 uptr lo = (pc << 32) >> 32;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152 CHECK((hi & (1 << 31)) == 0);
153 if (c_index + 1 >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000154 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000155 compressed[c_index++] = hi;
156 compressed[c_index++] = lo;
157 }
158 res++;
159 prev_pc = pc;
160 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000161 if (c_index < size)
162 compressed[c_index] = 0;
163 if (c_index + 1 < size)
164 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000165#endif // __WORDSIZE
166
167 // debug-only code
168#if 0
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000169 StackTrace check_stack;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170 UncompressStack(&check_stack, compressed, size);
171 if (res < check_stack.size) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000172 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000173 check_stack.size, size);
174 }
175 // |res| may be greater than check_stack.size, because
176 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
177 CHECK(res >= check_stack.size);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000178 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000179 check_stack.size * sizeof(uptr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000180#endif
181
182 return res;
183}
184
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000185SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000186void StackTrace::UncompressStack(StackTrace *stack,
187 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000188#if __WORDSIZE == 32
189 // Don't uncompress, just copy.
190 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000191 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000192 if (!compressed[i]) break;
193 stack->size++;
194 stack->trace[i] = compressed[i];
195 }
196#else // 64 bits, uncompress
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000197 uptr prev_pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000198 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000199 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000200 u32 x = compressed[i];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000201 uptr pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000202 if (x & (1U << 31)) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000203 // Printf("U co[%zu] offset: %x\n", i, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000204 // this is an offset
Kostya Serebryanyee392552012-05-31 15:02:07 +0000205 s32 offset = x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000206 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
207 pc = prev_pc + offset;
208 CHECK(pc);
209 } else {
210 // CHECK(i + 1 < size);
211 if (i + 1 >= size) break;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000212 uptr hi = x;
213 uptr lo = compressed[i+1];
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000214 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000215 i++;
216 pc = (hi << 32) | lo;
217 if (!pc) break;
218 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000219 // Printf("U pc[%zu] %zx\n", stack->size, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000220 stack->trace[stack->size++] = pc;
221 prev_pc = pc;
222 }
223#endif // __WORDSIZE
224}
225
Kostya Serebryanycc347222012-08-28 13:49:49 +0000226void PrintStack(StackTrace *stack) {
227 stack->PrintStack(stack->trace, stack->size, flags()->symbolize,
228 flags()->strip_path_prefix, symbolize_callback);
229}
230
231
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000232} // namespace __asan
Alexey Samsonovc93d3e22012-08-22 13:31:37 +0000233
234// ------------------ Interface -------------- {{{1
235using namespace __asan; // NOLINT
236
237void NOINLINE __asan_set_symbolize_callback(
238 __asan_symbolize_callback callback) {
239 symbolize_callback = callback;
240}