blob: 60e4f92282e05d597c40e1ac1331312be6877d32 [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
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021#ifdef ASAN_USE_EXTERNAL_SYMBOLIZER
22extern bool
23ASAN_USE_EXTERNAL_SYMBOLIZER(const void *pc, char *out, int out_size);
24#endif
25
26namespace __asan {
27
Alexander Potapenko1d483d42012-01-18 11:42:30 +000028// ----------------------- AsanStackTrace ----------------------------- {{{1
29#if defined(ASAN_USE_EXTERNAL_SYMBOLIZER)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000030void AsanStackTrace::PrintStack(uintptr_t *addr, size_t size) {
31 for (size_t i = 0; i < size && addr[i]; i++) {
32 uintptr_t pc = addr[i];
33 char buff[4096];
34 ASAN_USE_EXTERNAL_SYMBOLIZER((void*)pc, buff, sizeof(buff));
35 Printf(" #%ld 0x%lx %s\n", i, pc, buff);
36 }
37}
38
Alexander Potapenko1d483d42012-01-18 11:42:30 +000039#else // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany1e172b42011-11-30 01:07:02 +000040void AsanStackTrace::PrintStack(uintptr_t *addr, size_t size) {
Kostya Serebryanydf499b42012-01-05 00:44:33 +000041 AsanProcMaps proc_maps;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000042 for (size_t i = 0; i < size && addr[i]; i++) {
Kostya Serebryanydf499b42012-01-05 00:44:33 +000043 proc_maps.Reset();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000044 uintptr_t pc = addr[i];
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000045 uintptr_t offset;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000046 char filename[4096];
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000047 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
48 filename, sizeof(filename))) {
49 Printf(" #%ld 0x%lx (%s+0x%lx)\n", i, pc, filename, offset);
50 } else {
Kostya Serebryanydf499b42012-01-05 00:44:33 +000051 Printf(" #%ld 0x%lx\n", i, pc);
52 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000053 }
54}
Alexander Potapenko1d483d42012-01-18 11:42:30 +000055#endif // ASAN_USE_EXTERNAL_SYMBOLIZER
Kostya Serebryany1e172b42011-11-30 01:07:02 +000056
57#ifdef __arm__
58#define UNWIND_STOP _URC_END_OF_STACK
59#define UNWIND_CONTINUE _URC_OK
60#else
61#define UNWIND_STOP _URC_NORMAL_STOP
62#define UNWIND_CONTINUE _URC_NO_REASON
63#endif
64
Kostya Serebryany1e172b42011-11-30 01:07:02 +000065uintptr_t AsanStackTrace::GetCurrentPc() {
66 return GET_CALLER_PC();
67}
68
69void AsanStackTrace::FastUnwindStack(uintptr_t pc, uintptr_t bp) {
70 CHECK(size == 0 && trace[0] == pc);
71 size = 1;
72 if (!asan_inited) return;
73 AsanThread *t = asanThreadRegistry().GetCurrent();
74 if (!t) return;
75 uintptr_t *frame = (uintptr_t*)bp;
76 uintptr_t *prev_frame = frame;
77 uintptr_t *top = (uintptr_t*)t->stack_top();
78 uintptr_t *bottom = (uintptr_t*)t->stack_bottom();
79 while (frame >= prev_frame &&
80 frame < top &&
81 frame > bottom &&
82 size < max_size) {
83 uintptr_t pc1 = frame[1];
84 if (pc1 != pc) {
85 trace[size++] = pc1;
86 }
87 prev_frame = frame;
88 frame = (uintptr_t*)frame[0];
89 }
90}
91
92// On 32-bits we don't compress stack traces.
93// On 64-bits we compress stack traces: if a given pc differes slightly from
94// the previous one, we record a 31-bit offset instead of the full pc.
95size_t AsanStackTrace::CompressStack(AsanStackTrace *stack,
96 uint32_t *compressed, size_t size) {
97#if __WORDSIZE == 32
98 // Don't compress, just copy.
99 size_t res = 0;
100 for (size_t i = 0; i < stack->size && i < size; i++) {
101 compressed[i] = stack->trace[i];
102 res++;
103 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000104 if (stack->size < size)
105 compressed[stack->size] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000106#else // 64 bits, compress.
107 uintptr_t prev_pc = 0;
108 const uintptr_t kMaxOffset = (1ULL << 30) - 1;
109 uintptr_t c_index = 0;
110 size_t res = 0;
111 for (size_t i = 0, n = stack->size; i < n; i++) {
112 uintptr_t pc = stack->trace[i];
113 if (!pc) break;
114 if ((int64_t)pc < 0) break;
115 // Printf("C pc[%ld] %lx\n", i, pc);
116 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
117 uintptr_t offset = (int64_t)(pc - prev_pc);
118 offset |= (1U << 31);
119 if (c_index >= size) break;
120 // Printf("C co[%ld] offset %lx\n", i, offset);
121 compressed[c_index++] = offset;
122 } else {
123 uintptr_t hi = pc >> 32;
124 uintptr_t lo = (pc << 32) >> 32;
125 CHECK((hi & (1 << 31)) == 0);
126 if (c_index + 1 >= size) break;
127 // Printf("C co[%ld] hi/lo: %lx %lx\n", c_index, hi, lo);
128 compressed[c_index++] = hi;
129 compressed[c_index++] = lo;
130 }
131 res++;
132 prev_pc = pc;
133 }
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000134 if (c_index < size)
135 compressed[c_index] = 0;
136 if (c_index + 1 < size)
137 compressed[c_index + 1] = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000138#endif // __WORDSIZE
139
140 // debug-only code
141#if 0
142 AsanStackTrace check_stack;
143 UncompressStack(&check_stack, compressed, size);
144 if (res < check_stack.size) {
145 Printf("res %ld check_stack.size %ld; c_size %ld\n", res,
146 check_stack.size, size);
147 }
148 // |res| may be greater than check_stack.size, because
149 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
150 CHECK(res >= check_stack.size);
Kostya Serebryany52fb2382011-12-28 18:56:42 +0000151 CHECK(0 == real_memcmp(check_stack.trace, stack->trace,
152 check_stack.size * sizeof(uintptr_t)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000153#endif
154
155 return res;
156}
157
158void AsanStackTrace::UncompressStack(AsanStackTrace *stack,
159 uint32_t *compressed, size_t size) {
160#if __WORDSIZE == 32
161 // Don't uncompress, just copy.
162 stack->size = 0;
163 for (size_t i = 0; i < size && i < kStackTraceMax; i++) {
164 if (!compressed[i]) break;
165 stack->size++;
166 stack->trace[i] = compressed[i];
167 }
168#else // 64 bits, uncompress
169 uintptr_t prev_pc = 0;
170 stack->size = 0;
171 for (size_t i = 0; i < size && stack->size < kStackTraceMax; i++) {
172 uint32_t x = compressed[i];
173 uintptr_t pc = 0;
174 if (x & (1U << 31)) {
175 // Printf("U co[%ld] offset: %x\n", i, x);
176 // this is an offset
177 int32_t offset = x;
178 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
179 pc = prev_pc + offset;
180 CHECK(pc);
181 } else {
182 // CHECK(i + 1 < size);
183 if (i + 1 >= size) break;
184 uintptr_t hi = x;
185 uintptr_t lo = compressed[i+1];
186 // Printf("U co[%ld] hi/lo: %lx %lx\n", i, hi, lo);
187 i++;
188 pc = (hi << 32) | lo;
189 if (!pc) break;
190 }
191 // Printf("U pc[%ld] %lx\n", stack->size, pc);
192 stack->trace[stack->size++] = pc;
193 prev_pc = pc;
194 }
195#endif // __WORDSIZE
196}
197
198} // namespace __asan