blob: 8c6320b2410658d8b4e9387bc3716d48c2de6914 [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"
20
21#include <string.h>
22
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000023#if ASAN_USE_SYSINFO == 1
Kostya Serebryany1e172b42011-11-30 01:07:02 +000024#include "sysinfo/sysinfo.h"
25#endif
26
27#ifdef ASAN_USE_EXTERNAL_SYMBOLIZER
28extern bool
29ASAN_USE_EXTERNAL_SYMBOLIZER(const void *pc, char *out, int out_size);
30#endif
31
32namespace __asan {
33
34// ----------------------- ProcSelfMaps ----------------------------- {{{1
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000035#if ASAN_USE_SYSINFO == 1
Kostya Serebryany1e172b42011-11-30 01:07:02 +000036class ProcSelfMaps {
37 public:
38 void Init() {
39 ScopedLock lock(&mu_);
40 if (map_size_ != 0) return; // already inited
41 if (FLAG_v >= 2) {
42 Printf("ProcSelfMaps::Init()\n");
43 }
44 ProcMapsIterator it(0, &proc_self_maps_); // 0 means "current pid"
45
46 uint64 start, end, offset;
47 int64 inode;
48 char *flags, *filename;
49 CHECK(map_size_ == 0);
50 while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) {
51 CHECK(map_size_ < kMaxProcSelfMapsSize);
52 Mapping &mapping = memory_map[map_size_];
53 mapping.beg = start;
54 mapping.end = end;
55 mapping.offset = offset;
56 real_strncpy(mapping.name,
57 filename, ASAN_ARRAY_SIZE(mapping.name));
58 mapping.name[ASAN_ARRAY_SIZE(mapping.name) - 1] = 0;
59 if (FLAG_v >= 2) {
60 Printf("[%ld] [%p,%p] off %p %s\n", map_size_,
61 mapping.beg, mapping.end, mapping.offset, mapping.name);
62 }
63 map_size_++;
64 }
65 }
66
67 void Print() {
68 Printf("%s\n", proc_self_maps_);
69 }
70
71 void PrintPc(uintptr_t pc, int idx) {
72 for (size_t i = 0; i < map_size_; i++) {
73 Mapping &m = memory_map[i];
74 if (pc >= m.beg && pc < m.end) {
75 uintptr_t offset = pc - m.beg;
76 if (i == 0) offset = pc;
77 Printf(" #%d 0x%lx (%s+0x%lx)\n", idx, pc, m.name, offset);
78 return;
79 }
80 }
81 Printf(" #%d 0x%lx\n", idx, pc);
82 }
83
84 private:
85 void copy_until_new_line(const char *str, char *dest, size_t max_size) {
86 size_t i = 0;
87 for (; str[i] && str[i] != '\n' && i < max_size - 1; i++) {
88 dest[i] = str[i];
89 }
90 dest[i] = 0;
91 }
92
93
94 struct Mapping {
95 uintptr_t beg, end, offset;
96 char name[1000];
97 };
98 static const size_t kMaxNumMapEntries = 4096;
99 static const size_t kMaxProcSelfMapsSize = 1 << 20;
100 ProcMapsIterator::Buffer proc_self_maps_;
101 size_t map_size_;
102 Mapping memory_map[kMaxNumMapEntries];
103
104 static AsanLock mu_;
105};
106
107static ProcSelfMaps proc_self_maps;
108AsanLock ProcSelfMaps::mu_(LINKER_INITIALIZED);
109
110
111void AsanStackTrace::PrintStack(uintptr_t *addr, size_t size) {
112 proc_self_maps.Init();
113 for (size_t i = 0; i < size && addr[i]; i++) {
114 uintptr_t pc = addr[i];
115 // int line;
116 proc_self_maps.PrintPc(pc, i);
117 // Printf(" #%ld 0x%lx %s\n", i, pc, rtn.c_str());
118 }
119}
120#elif defined(ASAN_USE_EXTERNAL_SYMBOLIZER)
121void AsanStackTrace::PrintStack(uintptr_t *addr, size_t size) {
122 for (size_t i = 0; i < size && addr[i]; i++) {
123 uintptr_t pc = addr[i];
124 char buff[4096];
125 ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff));
126 Printf(" #%ld 0x%lx %s\n", i, pc, buff);
127 }
128}
129
130#else // ASAN_USE_SYSINFO
131void AsanStackTrace::PrintStack(uintptr_t *addr, size_t size) {
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000132 AsanProcMaps proc_maps;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133 for (size_t i = 0; i < size && addr[i]; i++) {
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000134 proc_maps.Reset();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000135 uintptr_t pc = addr[i];
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +0000136 uintptr_t offset;
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000137 char filename[4096];
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +0000138 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
139 filename, sizeof(filename))) {
140 Printf(" #%ld 0x%lx (%s+0x%lx)\n", i, pc, filename, offset);
141 } else {
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000142 Printf(" #%ld 0x%lx\n", i, pc);
143 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144 }
145}
146#endif // ASAN_USE_SYSINFO
147
148#ifdef __arm__
149#define UNWIND_STOP _URC_END_OF_STACK
150#define UNWIND_CONTINUE _URC_OK
151#else
152#define UNWIND_STOP _URC_NORMAL_STOP
153#define UNWIND_CONTINUE _URC_NO_REASON
154#endif
155
156// ----------------------- AsanStackTrace ----------------------------- {{{1
157uintptr_t AsanStackTrace::GetCurrentPc() {
158 return GET_CALLER_PC();
159}
160
161void AsanStackTrace::FastUnwindStack(uintptr_t pc, uintptr_t bp) {
162 CHECK(size == 0 && trace[0] == pc);
163 size = 1;
164 if (!asan_inited) return;
165 AsanThread *t = asanThreadRegistry().GetCurrent();
166 if (!t) return;
167 uintptr_t *frame = (uintptr_t*)bp;
168 uintptr_t *prev_frame = frame;
169 uintptr_t *top = (uintptr_t*)t->stack_top();
170 uintptr_t *bottom = (uintptr_t*)t->stack_bottom();
171 while (frame >= prev_frame &&
172 frame < top &&
173 frame > bottom &&
174 size < max_size) {
175 uintptr_t pc1 = frame[1];
176 if (pc1 != pc) {
177 trace[size++] = pc1;
178 }
179 prev_frame = frame;
180 frame = (uintptr_t*)frame[0];
181 }
182}
183
184// On 32-bits we don't compress stack traces.
185// On 64-bits we compress stack traces: if a given pc differes slightly from
186// the previous one, we record a 31-bit offset instead of the full pc.
187size_t AsanStackTrace::CompressStack(AsanStackTrace *stack,
188 uint32_t *compressed, size_t size) {
189#if __WORDSIZE == 32
190 // Don't compress, just copy.
191 size_t res = 0;
192 for (size_t i = 0; i < stack->size && i < size; i++) {
193 compressed[i] = stack->trace[i];
194 res++;
195 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000196 if (stack->size < size)
197 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000198#else // 64 bits, compress.
199 uintptr_t prev_pc = 0;
200 const uintptr_t kMaxOffset = (1ULL << 30) - 1;
201 uintptr_t c_index = 0;
202 size_t res = 0;
203 for (size_t i = 0, n = stack->size; i < n; i++) {
204 uintptr_t pc = stack->trace[i];
205 if (!pc) break;
206 if ((int64_t)pc < 0) break;
207 // Printf("C pc[%ld] %lx\n", i, pc);
208 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
209 uintptr_t offset = (int64_t)(pc - prev_pc);
210 offset |= (1U << 31);
211 if (c_index >= size) break;
212 // Printf("C co[%ld] offset %lx\n", i, offset);
213 compressed[c_index++] = offset;
214 } else {
215 uintptr_t hi = pc >> 32;
216 uintptr_t lo = (pc << 32) >> 32;
217 CHECK((hi & (1 << 31)) == 0);
218 if (c_index + 1 >= size) break;
219 // Printf("C co[%ld] hi/lo: %lx %lx\n", c_index, hi, lo);
220 compressed[c_index++] = hi;
221 compressed[c_index++] = lo;
222 }
223 res++;
224 prev_pc = pc;
225 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000226 if (c_index < size)
227 compressed[c_index] = 0;
228 if (c_index + 1 < size)
229 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000230#endif // __WORDSIZE
231
232 // debug-only code
233#if 0
234 AsanStackTrace check_stack;
235 UncompressStack(&check_stack, compressed, size);
236 if (res < check_stack.size) {
237 Printf("res %ld check_stack.size %ld; c_size %ld\n", res,
238 check_stack.size, size);
239 }
240 // |res| may be greater than check_stack.size, because
241 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
242 CHECK(res >= check_stack.size);
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000243 CHECK(0 == real_memcmp(check_stack.trace, stack->trace,
244 check_stack.size * sizeof(uintptr_t)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000245#endif
246
247 return res;
248}
249
250void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
251 uint32_t *compressed, size_t size) {
252#if __WORDSIZE == 32
253 // Don't uncompress, just copy.
254 stack->size = 0;
255 for (size_t i = 0; i < size && i < kStackTraceMax; i++) {
256 if (!compressed[i]) break;
257 stack->size++;
258 stack->trace[i] = compressed[i];
259 }
260#else // 64 bits, uncompress
261 uintptr_t prev_pc = 0;
262 stack->size = 0;
263 for (size_t i = 0; i < size && stack->size < kStackTraceMax; i++) {
264 uint32_t x = compressed[i];
265 uintptr_t pc = 0;
266 if (x & (1U << 31)) {
267 // Printf("U co[%ld] offset: %x\n", i, x);
268 // this is an offset
269 int32_t offset = x;
270 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
271 pc = prev_pc + offset;
272 CHECK(pc);
273 } else {
274 // CHECK(i + 1 < size);
275 if (i + 1 >= size) break;
276 uintptr_t hi = x;
277 uintptr_t lo = compressed[i+1];
278 // Printf("U co[%ld] hi/lo: %lx %lx\n", i, hi, lo);
279 i++;
280 pc = (hi << 32) | lo;
281 if (!pc) break;
282 }
283 // Printf("U pc[%ld] %lx\n", stack->size, pc);
284 stack->trace[stack->size++] = pc;
285 prev_pc = pc;
286 }
287#endif // __WORDSIZE
288}
289
290} // namespace __asan