blob: 19e1d9934e3032ae0ee8d764f5884190b20ce36f [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- asan_rtl.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// Main file of the ASan run-time library.
13//===----------------------------------------------------------------------===//
14#include "asan_allocator.h"
15#include "asan_interceptors.h"
16#include "asan_interface.h"
17#include "asan_internal.h"
18#include "asan_lock.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000019#include "asan_mapping.h"
20#include "asan_stack.h"
21#include "asan_stats.h"
22#include "asan_thread.h"
23#include "asan_thread_registry.h"
24
Kostya Serebryany1e172b42011-11-30 01:07:02 +000025namespace __asan {
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000026using namespace __sanitizer;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000027
28// -------------------------- Flags ------------------------- {{{1
29static const size_t kMallocContextSize = 30;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000030
Alexander Potapenko62f10e72012-05-28 16:21:19 +000031size_t FLAG_malloc_context_size = kMallocContextSize;
32size_t FLAG_max_malloc_fill_size = 0;
33int64_t FLAG_v = 0;
34size_t FLAG_redzone = (ASAN_LOW_MEMORY) ? 64 : 128; // power of two, >= 32
35size_t FLAG_quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28;
36static int64_t FLAG_atexit = 0;
37bool FLAG_poison_shadow = 1;
38int64_t FLAG_report_globals = 1;
39bool FLAG_handle_segv = ASAN_NEEDS_SEGV;
40bool FLAG_use_sigaltstack = 0;
41bool FLAG_symbolize = 1;
42int64_t FLAG_demangle = 1;
43int64_t FLAG_debug = 0;
44bool FLAG_replace_cfallocator = 1; // Used on Mac only.
45bool FLAG_replace_str = 1;
46bool FLAG_replace_intrin = 1;
47bool FLAG_use_fake_stack = 1;
48int64_t FLAG_exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
49bool FLAG_allow_user_poisoning = 1;
50int64_t FLAG_sleep_before_dying = 0;
51bool FLAG_abort_on_error = 0;
52bool FLAG_unmap_shadow_on_exit = 0;
53bool FLAG_disable_core = __WORDSIZE == 64;
54bool FLAG_check_malloc_usable_size = 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000055
56// -------------------------- Globals --------------------- {{{1
57int asan_inited;
58bool asan_init_is_running;
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +000059static void (*death_callback)(void);
Alexander Potapenko3fe91352012-02-27 14:06:48 +000060static void (*error_report_callback)(const char*);
61char *error_message_buffer = NULL;
62size_t error_message_buffer_pos = 0;
63size_t error_message_buffer_size = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064
Kostya Serebryany1e172b42011-11-30 01:07:02 +000065// -------------------------- Misc ---------------- {{{1
66void ShowStatsAndAbort() {
67 __asan_print_accumulated_stats();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000068 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000069}
70
71static void PrintBytes(const char *before, uintptr_t *a) {
72 uint8_t *bytes = (uint8_t*)a;
73 size_t byte_num = (__WORDSIZE) / 8;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +000074 Printf("%s%p:", before, (void*)a);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000075 for (size_t i = 0; i < byte_num; i++) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +000076 Printf(" %x%x", bytes[i] >> 4, bytes[i] & 15);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000077 }
78 Printf("\n");
79}
80
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000081size_t ReadFileToBuffer(const char *file_name, char **buff,
Kostya Serebryanydf499b42012-01-05 00:44:33 +000082 size_t *buff_size, size_t max_len) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +000083 const size_t kMinFileLen = kPageSize;
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000084 size_t read_len = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000085 *buff = 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000086 *buff_size = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000087 // The files we usually open are not seekable, so try different buffer sizes.
88 for (size_t size = kMinFileLen; size <= max_len; size *= 2) {
89 int fd = AsanOpenReadonly(file_name);
Alexander Potapenkof2b1df72012-05-10 12:03:09 +000090 if (fd < 0) return 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000091 AsanUnmapOrDie(*buff, *buff_size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000092 *buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
Kostya Serebryanydf499b42012-01-05 00:44:33 +000093 *buff_size = size;
Kostya Serebryany454a0642012-01-17 18:00:07 +000094 // Read up to one page at a time.
95 read_len = 0;
96 bool reached_eof = false;
97 while (read_len + kPageSize <= size) {
98 size_t just_read = AsanRead(fd, *buff + read_len, kPageSize);
99 if (just_read == 0) {
100 reached_eof = true;
101 break;
102 }
103 read_len += just_read;
104 }
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000105 AsanClose(fd);
Kostya Serebryany454a0642012-01-17 18:00:07 +0000106 if (reached_eof) // We've read the whole file.
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000107 break;
108 }
109 return read_len;
110}
111
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000112void AsanDie() {
Kostya Serebryany5e4e91c2012-02-16 00:40:18 +0000113 static int num_calls = 0;
Alexey Samsonov95d6b332012-03-20 10:14:55 +0000114 if (AtomicInc(&num_calls) > 1) {
115 // Don't die twice - run a busy loop.
116 while (1) { }
117 }
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000118 if (FLAG_sleep_before_dying) {
119 Report("Sleeping for %d second(s)\n", FLAG_sleep_before_dying);
120 SleepForSeconds(FLAG_sleep_before_dying);
121 }
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000122 if (FLAG_unmap_shadow_on_exit)
123 AsanUnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000124 if (death_callback)
125 death_callback();
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000126 if (FLAG_abort_on_error)
127 Abort();
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000128 Exit(FLAG_exitcode);
129}
130
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131// ---------------------- mmap -------------------- {{{1
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000132void OutOfMemoryMessageAndDie(const char *mem_type, size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133 Report("ERROR: AddressSanitizer failed to allocate "
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000134 "0x%zx (%zd) bytes of %s\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000135 size, size, mem_type);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000136 PRINT_CURRENT_STACK();
137 ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000138}
139
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000140// Reserve memory range [beg, end].
141static void ReserveShadowMemoryRange(uintptr_t beg, uintptr_t end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000142 CHECK((beg % kPageSize) == 0);
143 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000144 size_t size = end - beg + 1;
145 void *res = AsanMmapFixedNoReserve(beg, size);
146 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147}
148
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000149// ---------------------- LowLevelAllocator ------------- {{{1
150void *LowLevelAllocator::Allocate(size_t size) {
151 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
152 if (allocated_end_ - allocated_current_ < size) {
153 size_t size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000154 allocated_current_ =
155 (char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000156 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000157 PoisonShadow((uintptr_t)allocated_current_, size_to_allocate,
158 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000159 }
160 CHECK(allocated_end_ - allocated_current_ >= size);
161 void *res = allocated_current_;
162 allocated_current_ += size;
163 return res;
164}
165
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166// ---------------------- DescribeAddress -------------------- {{{1
167static bool DescribeStackAddress(uintptr_t addr, uintptr_t access_size) {
168 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
169 if (!t) return false;
170 const intptr_t kBufSize = 4095;
171 char buf[kBufSize];
172 uintptr_t offset = 0;
173 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
174 // This string is created by the compiler and has the following form:
175 // "FunctioName n alloc_1 alloc_2 ... alloc_n"
176 // where alloc_i looks like "offset size len ObjectName ".
177 CHECK(frame_descr);
178 // Report the function name and the offset.
Timur Iskhodzhanov53627172012-02-14 19:33:04 +0000179 const char *name_end = internal_strchr(frame_descr, ' ');
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000180 CHECK(name_end);
181 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000182 internal_strncat(buf, frame_descr,
183 Min(kBufSize,
184 static_cast<intptr_t>(name_end - frame_descr)));
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000185 Printf("Address %p is located at offset %zu "
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000186 "in frame <%s> of T%d's stack:\n",
187 addr, offset, buf, t->tid());
188 // Report the number of stack objects.
189 char *p;
Alexey Samsonov88981022012-02-17 16:15:09 +0000190 size_t n_objects = internal_simple_strtoll(name_end, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000191 CHECK(n_objects > 0);
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000192 Printf(" This frame has %zu object(s):\n", n_objects);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000193 // Report all objects in this frame.
194 for (size_t i = 0; i < n_objects; i++) {
195 size_t beg, size;
196 intptr_t len;
Alexey Samsonov88981022012-02-17 16:15:09 +0000197 beg = internal_simple_strtoll(p, &p, 10);
198 size = internal_simple_strtoll(p, &p, 10);
199 len = internal_simple_strtoll(p, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000200 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
201 Printf("AddressSanitizer can't parse the stack frame descriptor: |%s|\n",
202 frame_descr);
203 break;
204 }
205 p++;
206 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000207 internal_strncat(buf, p, Min(kBufSize, len));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000208 p += len;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000209 Printf(" [%zu, %zu) '%s'\n", beg, beg + size, buf);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000210 }
211 Printf("HINT: this may be a false positive if your program uses "
212 "some custom stack unwind mechanism\n"
213 " (longjmp and C++ exceptions *are* supported)\n");
214 t->summary()->Announce();
215 return true;
216}
217
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000218static NOINLINE void DescribeAddress(uintptr_t addr, uintptr_t access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000219 // Check if this is a global.
220 if (DescribeAddrIfGlobal(addr))
221 return;
222
223 if (DescribeStackAddress(addr, access_size))
224 return;
225
226 // finally, check if this is a heap.
227 DescribeHeapAddress(addr, access_size);
228}
229
230// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000231// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000232#define ASAN_REPORT_ERROR(type, is_write, size) \
Kostya Serebryany4d9a9572012-04-06 20:19:59 +0000233extern "C" NOINLINE ASAN_INTERFACE_ATTRIBUTE \
234void __asan_report_ ## type ## size(uintptr_t addr); \
235void __asan_report_ ## type ## size(uintptr_t addr) { \
Kostya Serebryany9f311bb2012-03-15 01:36:00 +0000236 GET_CALLER_PC_BP_SP; \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000237 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000238}
239
240ASAN_REPORT_ERROR(load, false, 1)
241ASAN_REPORT_ERROR(load, false, 2)
242ASAN_REPORT_ERROR(load, false, 4)
243ASAN_REPORT_ERROR(load, false, 8)
244ASAN_REPORT_ERROR(load, false, 16)
245ASAN_REPORT_ERROR(store, true, 1)
246ASAN_REPORT_ERROR(store, true, 2)
247ASAN_REPORT_ERROR(store, true, 4)
248ASAN_REPORT_ERROR(store, true, 8)
249ASAN_REPORT_ERROR(store, true, 16)
250
251// Force the linker to keep the symbols for various ASan interface functions.
252// We want to keep those in the executable in order to let the instrumented
253// dynamic libraries access the symbol even if it is not used by the executable
254// itself. This should help if the build system is removing dead code at link
255// time.
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000256static NOINLINE void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000257 volatile int fake_condition = 0; // prevent dead condition elimination.
258 if (fake_condition) {
Kostya Serebryanyf0977db2012-03-14 22:48:09 +0000259 __asan_report_load1(0);
260 __asan_report_load2(0);
261 __asan_report_load4(0);
262 __asan_report_load8(0);
263 __asan_report_load16(0);
264 __asan_report_store1(0);
265 __asan_report_store2(0);
266 __asan_report_store4(0);
267 __asan_report_store8(0);
268 __asan_report_store16(0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000269 __asan_register_global(0, 0, NULL);
270 __asan_register_globals(NULL, 0);
Kostya Serebryany45581682011-12-28 23:35:46 +0000271 __asan_unregister_globals(NULL, 0);
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000272 __asan_set_death_callback(NULL);
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000273 __asan_set_error_report_callback(NULL);
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000274 __asan_handle_no_return();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000275 }
276}
277
278// -------------------------- Init ------------------- {{{1
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000279static void IntFlagValue(const char *flags, const char *flag,
280 int64_t *out_val) {
281 if (!flags) return;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000282 const char *str = internal_strstr(flags, flag);
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000283 if (!str) return;
284 *out_val = internal_atoll(str + internal_strlen(flag));
285}
286
287static void BoolFlagValue(const char *flags, const char *flag,
288 bool *out_val) {
289 if (!flags) return;
290 const char *str = internal_strstr(flags, flag);
291 if (!str) return;
292 if (!internal_atoll(str + internal_strlen(flag))) {
293 if (str[0] == '0') {
294 *out_val = false;
295 return;
296 }
297 } else {
298 *out_val = true;
299 return;
300 }
301 switch (str[0]) {
302 case 'y':
303 case 't': {
304 *out_val = true;
305 break;
306 }
307 case 'n':
308 case 'f': {
309 *out_val = false;
310 break;
311 }
312 default: return;
313 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000314}
315
316static void asan_atexit() {
317 Printf("AddressSanitizer exit stats:\n");
318 __asan_print_accumulated_stats();
319}
320
321void CheckFailed(const char *cond, const char *file, int line) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000322 Report("CHECK failed: %s at %s:%d\n", cond, file, line);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000323 PRINT_CURRENT_STACK();
324 ShowStatsAndAbort();
325}
326
327} // namespace __asan
328
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000329// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000330using namespace __asan; // NOLINT
331
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000332int __asan_set_error_exit_code(int exit_code) {
333 int old = FLAG_exitcode;
334 FLAG_exitcode = exit_code;
335 return old;
336}
337
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000338void NOINLINE __asan_handle_no_return() {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000339 int local_stack;
340 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
341 CHECK(curr_thread);
342 uintptr_t top = curr_thread->stack_top();
343 uintptr_t bottom = ((uintptr_t)&local_stack - kPageSize) & ~(kPageSize-1);
344 PoisonShadow(bottom, top - bottom, 0);
345}
346
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000347void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000348 death_callback = callback;
349}
350
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000351void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
352 error_report_callback = callback;
353 if (callback) {
Alexander Potapenko8ca02782012-05-12 12:33:41 +0000354 error_message_buffer_size = 1 << 16;
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000355 error_message_buffer =
356 (char*)AsanMmapSomewhereOrDie(error_message_buffer_size, __FUNCTION__);
357 error_message_buffer_pos = 0;
358 }
359}
360
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000361void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
362 uintptr_t addr, bool is_write, size_t access_size) {
363 // Do not print more than one report, otherwise they will mix up.
364 static int num_calls = 0;
365 if (AtomicInc(&num_calls) > 1) return;
366
367 Printf("=================================================================\n");
368 const char *bug_descr = "unknown-crash";
369 if (AddrIsInMem(addr)) {
370 uint8_t *shadow_addr = (uint8_t*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000371 // If we are accessing 16 bytes, look at the second shadow byte.
372 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
373 shadow_addr++;
374 // If we are in the partial right redzone, look at the next shadow byte.
375 if (*shadow_addr > 0 && *shadow_addr < 128)
376 shadow_addr++;
377 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000378 case kAsanHeapLeftRedzoneMagic:
379 case kAsanHeapRightRedzoneMagic:
380 bug_descr = "heap-buffer-overflow";
381 break;
382 case kAsanHeapFreeMagic:
383 bug_descr = "heap-use-after-free";
384 break;
385 case kAsanStackLeftRedzoneMagic:
386 bug_descr = "stack-buffer-underflow";
387 break;
388 case kAsanStackMidRedzoneMagic:
389 case kAsanStackRightRedzoneMagic:
390 case kAsanStackPartialRedzoneMagic:
391 bug_descr = "stack-buffer-overflow";
392 break;
393 case kAsanStackAfterReturnMagic:
394 bug_descr = "stack-use-after-return";
395 break;
396 case kAsanUserPoisonedMemoryMagic:
397 bug_descr = "use-after-poison";
398 break;
399 case kAsanGlobalRedzoneMagic:
400 bug_descr = "global-buffer-overflow";
401 break;
402 }
403 }
404
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000405 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
406 int curr_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
407
408 if (curr_thread) {
409 // We started reporting an error message. Stop using the fake stack
410 // in case we will call an instrumented function from a symbolizer.
411 curr_thread->fake_stack().StopUsingFakeStack();
412 }
413
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000414 Report("ERROR: AddressSanitizer %s on address "
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000415 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000416 bug_descr, addr, pc, bp, sp);
417
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000418 Printf("%s of size %zu at %p thread T%d\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000419 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000420 access_size, addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000421
422 if (FLAG_debug) {
423 PrintBytes("PC: ", (uintptr_t*)pc);
424 }
425
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000426 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000427 stack.PrintStack();
428
429 CHECK(AddrIsInMem(addr));
430
431 DescribeAddress(addr, access_size);
432
433 uintptr_t shadow_addr = MemToShadow(addr);
434 Report("ABORTING\n");
435 __asan_print_accumulated_stats();
436 Printf("Shadow byte and word:\n");
437 Printf(" %p: %x\n", shadow_addr, *(unsigned char*)shadow_addr);
438 uintptr_t aligned_shadow = shadow_addr & ~(kWordSize - 1);
439 PrintBytes(" ", (uintptr_t*)(aligned_shadow));
440 Printf("More shadow bytes:\n");
441 PrintBytes(" ", (uintptr_t*)(aligned_shadow-4*kWordSize));
442 PrintBytes(" ", (uintptr_t*)(aligned_shadow-3*kWordSize));
443 PrintBytes(" ", (uintptr_t*)(aligned_shadow-2*kWordSize));
444 PrintBytes(" ", (uintptr_t*)(aligned_shadow-1*kWordSize));
445 PrintBytes("=>", (uintptr_t*)(aligned_shadow+0*kWordSize));
446 PrintBytes(" ", (uintptr_t*)(aligned_shadow+1*kWordSize));
447 PrintBytes(" ", (uintptr_t*)(aligned_shadow+2*kWordSize));
448 PrintBytes(" ", (uintptr_t*)(aligned_shadow+3*kWordSize));
449 PrintBytes(" ", (uintptr_t*)(aligned_shadow+4*kWordSize));
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000450 if (error_report_callback) {
451 error_report_callback(error_message_buffer);
452 }
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000453 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000454}
455
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000456static void ParseAsanOptions(const char *options) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000457 IntFlagValue(options, "malloc_context_size=",
458 (int64_t*)&FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000459 CHECK(FLAG_malloc_context_size <= kMallocContextSize);
460
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000461 IntFlagValue(options, "max_malloc_fill_size=",
462 (int64_t*)&FLAG_max_malloc_fill_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000463
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000464 IntFlagValue(options, "verbosity=", &FLAG_v);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000465
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000466 IntFlagValue(options, "redzone=", (int64_t*)&FLAG_redzone);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000467 CHECK(FLAG_redzone >= 32);
468 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000469 IntFlagValue(options, "quarantine_size=", (int64_t*)&FLAG_quarantine_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000470
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000471 IntFlagValue(options, "atexit=", &FLAG_atexit);
472 BoolFlagValue(options, "poison_shadow=", &FLAG_poison_shadow);
473 IntFlagValue(options, "report_globals=", &FLAG_report_globals);
474 BoolFlagValue(options, "handle_segv=", &FLAG_handle_segv);
475 BoolFlagValue(options, "use_sigaltstack=", &FLAG_use_sigaltstack);
476 BoolFlagValue(options, "symbolize=", &FLAG_symbolize);
477 IntFlagValue(options, "demangle=", &FLAG_demangle);
478 IntFlagValue(options, "debug=", &FLAG_debug);
479 BoolFlagValue(options, "replace_cfallocator=", &FLAG_replace_cfallocator);
480 BoolFlagValue(options, "replace_str=", &FLAG_replace_str);
481 BoolFlagValue(options, "replace_intrin=", &FLAG_replace_intrin);
482 BoolFlagValue(options, "use_fake_stack=", &FLAG_use_fake_stack);
483 IntFlagValue(options, "exitcode=", &FLAG_exitcode);
484 BoolFlagValue(options, "allow_user_poisoning=", &FLAG_allow_user_poisoning);
485 IntFlagValue(options, "sleep_before_dying=", &FLAG_sleep_before_dying);
486 BoolFlagValue(options, "abort_on_error=", &FLAG_abort_on_error);
487 BoolFlagValue(options, "unmap_shadow_on_exit=", &FLAG_unmap_shadow_on_exit);
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000488 // By default, disable core dumper on 64-bit --
489 // it makes little sense to dump 16T+ core.
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000490 BoolFlagValue(options, "disable_core=", &FLAG_disable_core);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000491
Alexander Potapenko37931232012-05-25 15:20:13 +0000492 // Allow the users to work around the bug in Nvidia drivers prior to 295.*.
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000493 BoolFlagValue(options, "check_malloc_usable_size=",
494 &FLAG_check_malloc_usable_size);
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000495}
496
497void __asan_init() {
498 if (asan_inited) return;
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +0000499 MiniLibcStub(); // FIXME: remove me once mini libc build is tested properly.
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000500 asan_init_is_running = true;
501
502 // Make sure we are not statically linked.
503 AsanDoesNotSupportStaticLinkage();
504
Alexey Samsonovff20f172012-05-29 09:39:01 +0000505#if !defined(_WIN32)
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000506 if (__asan_default_options) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000507 ParseAsanOptions(__asan_default_options);
Alexander Potapenkoe4781f02012-05-30 14:12:20 +0000508 if (FLAG_v) {
509 Report("Using the defaults from __asan_default_options: %s\n",
510 __asan_default_options);
511 }
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000512 }
Alexey Samsonovff20f172012-05-29 09:39:01 +0000513#endif
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000514 // flags
515 const char *options = AsanGetEnv("ASAN_OPTIONS");
Alexander Potapenkof2981f32012-05-25 15:56:40 +0000516 ParseAsanOptions(options);
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000517
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000518 if (FLAG_v && options) {
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000519 Report("Parsed ASAN_OPTIONS: %s\n", options);
520 }
521
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000522 if (FLAG_atexit) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000523 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000524 }
525
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000526 // interceptors
527 InitializeAsanInterceptors();
528
529 ReplaceSystemMalloc();
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000530 ReplaceOperatorsNewAndDelete();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000531
532 if (FLAG_v) {
533 Printf("|| `[%p, %p]` || HighMem ||\n", kHighMemBeg, kHighMemEnd);
534 Printf("|| `[%p, %p]` || HighShadow ||\n",
535 kHighShadowBeg, kHighShadowEnd);
536 Printf("|| `[%p, %p]` || ShadowGap ||\n",
537 kShadowGapBeg, kShadowGapEnd);
538 Printf("|| `[%p, %p]` || LowShadow ||\n",
539 kLowShadowBeg, kLowShadowEnd);
540 Printf("|| `[%p, %p]` || LowMem ||\n", kLowMemBeg, kLowMemEnd);
541 Printf("MemToShadow(shadow): %p %p %p %p\n",
542 MEM_TO_SHADOW(kLowShadowBeg),
543 MEM_TO_SHADOW(kLowShadowEnd),
544 MEM_TO_SHADOW(kHighShadowBeg),
545 MEM_TO_SHADOW(kHighShadowEnd));
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000546 Printf("red_zone=%zu\n", (size_t)FLAG_redzone);
547 Printf("malloc_context_size=%zu\n", (size_t)FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000548
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000549 Printf("SHADOW_SCALE: %zx\n", (size_t)SHADOW_SCALE);
550 Printf("SHADOW_GRANULARITY: %zx\n", (size_t)SHADOW_GRANULARITY);
551 Printf("SHADOW_OFFSET: %zx\n", (size_t)SHADOW_OFFSET);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000552 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
553 }
554
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000555 if (FLAG_disable_core) {
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000556 AsanDisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000557 }
558
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000559 if (AsanShadowRangeIsAvailable()) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000560 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000561 // mmap the low shadow plus at least one page.
562 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000563 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000564 // mmap the high shadow.
565 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000566 // protect the gap
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000567 void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
568 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000569 } else {
570 Report("Shadow memory range interleaves with an existing memory mapping. "
571 "ASan cannot proceed correctly. ABORTING.\n");
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000572 AsanDumpProcessMap();
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000573 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000574 }
575
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000576 InstallSignalHandlers();
577
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000578 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
579 // should be set to 1 prior to initializing the threads.
580 asan_inited = 1;
581 asan_init_is_running = false;
582
583 asanThreadRegistry().Init();
584 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000585 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000586
587 if (FLAG_v) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000588 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000589 }
590}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000591
592#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000593 // On Linux, we force __asan_init to be called before anyone else
594 // by placing it into .preinit_array section.
595 // FIXME: do we have anything like this on Mac?
596 __attribute__((section(".preinit_array")))
597 typeof(__asan_init) *__asan_preinit =__asan_init;
598#elif defined(_WIN32) && defined(_DLL)
599 // On Windows, when using dynamic CRT (/MD), we can put a pointer
600 // to __asan_init into the global list of C initializers.
601 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000602 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000603 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000604#endif