blob: 91d7b13a448d971a815741adc19906dd83358b7d [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- asan_stack.cc -------------------------------------------*- C++ -*-===//
2//
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"
Kostya Serebryanydf499b42012-01-05 00:44:33 +000016#include "asan_procmaps.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000017#include "asan_stack.h"
18#include "asan_thread.h"
19#include "asan_thread_registry.h"
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000020#include "sanitizer_common/sanitizer_symbolizer.h"
21
22using namespace __sanitizer; // NOLINT
Kostya Serebryany1e172b42011-11-30 01:07:02 +000023
Kostya Serebryany1e172b42011-11-30 01:07:02 +000024#ifdef ASAN_USE_EXTERNAL_SYMBOLIZER
25extern bool
26ASAN_USE_EXTERNAL_SYMBOLIZER(const void *pc, char *out, int out_size);
27#endif
28
29namespace __asan {
30
Alexander Potapenko1d483d42012-01-18 11:42:30 +000031// ----------------------- AsanStackTrace ----------------------------- {{{1
32#if defined(ASAN_USE_EXTERNAL_SYMBOLIZER)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000033void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
34 for (uptr i = 0; i < size && addr[i]; i++) {
35 uptr pc = addr[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +000036 char buff[4096];
37 ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff));
Evgeniy Stepanov739eb792012-03-21 11:32:46 +000038 Printf(" #%zu 0x%zx %s\n", i, pc, buff);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000039 }
40}
41
Alexander Potapenko1d483d42012-01-18 11:42:30 +000042#else // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000043void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
Kostya Serebryanydf499b42012-01-05 00:44:33 +000044 AsanProcMaps proc_maps;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000045 uptr frame_num = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000046 for (uptr i = 0; i < size && addr[i]; i++) {
Kostya Serebryanydf499b42012-01-05 00:44:33 +000047 proc_maps.Reset();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000048 uptr pc = addr[i];
49 uptr offset;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000050 char filename[4096];
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000051 if (FLAG_symbolize) {
52 AddressInfoList *address_info_list = SymbolizeCode(pc);
53 for (AddressInfoList *entry = address_info_list; entry;
54 entry = entry->next) {
55 AddressInfo info = entry->info;
56 Printf(" #%zu 0x%zx %s:%d:%d\n", frame_num, pc,
57 (info.file) ? info.file : "",
58 info.line, info.column);
59 frame_num++;
60 }
61 address_info_list->Clear();
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000062 } else {
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000063 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
64 filename, sizeof(filename))) {
65 Printf(" #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc, filename,
66 offset);
67 } else {
68 Printf(" #%zu 0x%zx\n", frame_num, pc);
69 }
70 frame_num++;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000071 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000072 }
73}
Alexander Potapenko1d483d42012-01-18 11:42:30 +000074#endif // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany1e172b42011-11-30 01:07:02 +000075
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000076uptr AsanStackTrace::GetCurrentPc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000077 return GET_CALLER_PC();
78}
79
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000080void AsanStackTrace::FastUnwindStack(uptr pc, uptr bp) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081 CHECK(size == 0 && trace[0] == pc);
82 size = 1;
83 if (!asan_inited) return;
84 AsanThread *t = asanThreadRegistry().GetCurrent();
85 if (!t) return;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000086 uptr *frame = (uptr*)bp;
87 uptr *prev_frame = frame;
88 uptr *top = (uptr*)t->stack_top();
89 uptr *bottom = (uptr*)t->stack_bottom();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000090 while (frame >= prev_frame &&
Kostya Serebryany40928f12012-03-08 22:25:08 +000091 frame < top - 2 &&
Kostya Serebryany1e172b42011-11-30 01:07:02 +000092 frame > bottom &&
93 size < max_size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000094 uptr pc1 = frame[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +000095 if (pc1 != pc) {
96 trace[size++] = pc1;
97 }
98 prev_frame = frame;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000099 frame = (uptr*)frame[0];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000100 }
101}
102
103// On 32-bits we don't compress stack traces.
104// On 64-bits we compress stack traces: if a given pc differes slightly from
105// the previous one, we record a 31-bit offset instead of the full pc.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000106uptr AsanStackTrace::CompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000107 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000108#if __WORDSIZE == 32
109 // Don't compress, just copy.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000110 uptr res = 0;
111 for (uptr i = 0; i < stack->size && i < size; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000112 compressed[i] = stack->trace[i];
113 res++;
114 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000115 if (stack->size < size)
116 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000117#else // 64 bits, compress.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000118 uptr prev_pc = 0;
119 const uptr kMaxOffset = (1ULL << 30) - 1;
120 uptr c_index = 0;
121 uptr res = 0;
122 for (uptr i = 0, n = stack->size; i < n; i++) {
123 uptr pc = stack->trace[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000124 if (!pc) break;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000125 if ((s64)pc < 0) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000126 // Printf("C pc[%zu] %zx\n", i, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000128 uptr offset = (s64)(pc - prev_pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000129 offset |= (1U << 31);
130 if (c_index >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000131 // Printf("C co[%zu] offset %zx\n", i, offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000132 compressed[c_index++] = offset;
133 } else {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000134 uptr hi = pc >> 32;
135 uptr lo = (pc << 32) >> 32;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136 CHECK((hi & (1 << 31)) == 0);
137 if (c_index + 1 >= size) break;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000138 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139 compressed[c_index++] = hi;
140 compressed[c_index++] = lo;
141 }
142 res++;
143 prev_pc = pc;
144 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000145 if (c_index < size)
146 compressed[c_index] = 0;
147 if (c_index + 1 < size)
148 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000149#endif // __WORDSIZE
150
151 // debug-only code
152#if 0
153 AsanStackTrace check_stack;
154 UncompressStack(&check_stack, compressed, size);
155 if (res < check_stack.size) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000156 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000157 check_stack.size, size);
158 }
159 // |res| may be greater than check_stack.size, because
160 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
161 CHECK(res >= check_stack.size);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000162 CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000163 check_stack.size * sizeof(uptr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164#endif
165
166 return res;
167}
168
169void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000170 u32 *compressed, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171#if __WORDSIZE == 32
172 // Don't uncompress, just copy.
173 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000174 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000175 if (!compressed[i]) break;
176 stack->size++;
177 stack->trace[i] = compressed[i];
178 }
179#else // 64 bits, uncompress
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000180 uptr prev_pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000181 stack->size = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000182 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000183 u32 x = compressed[i];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000184 uptr pc = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000185 if (x & (1U << 31)) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000186 // Printf("U co[%zu] offset: %x\n", i, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000187 // this is an offset
Kostya Serebryanyee392552012-05-31 15:02:07 +0000188 s32 offset = x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000189 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
190 pc = prev_pc + offset;
191 CHECK(pc);
192 } else {
193 // CHECK(i + 1 < size);
194 if (i + 1 >= size) break;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000195 uptr hi = x;
196 uptr lo = compressed[i+1];
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000197 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000198 i++;
199 pc = (hi << 32) | lo;
200 if (!pc) break;
201 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000202 // Printf("U pc[%zu] %zx\n", stack->size, pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000203 stack->trace[stack->size++] = pc;
204 prev_pc = pc;
205 }
206#endif // __WORDSIZE
207}
208
209} // namespace __asan