blob: 20cfddbcee8449a63b362253dbc68948c4c93f17 [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 {
26
27// -------------------------- Flags ------------------------- {{{1
28static const size_t kMallocContextSize = 30;
29static int FLAG_atexit;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000030
31size_t FLAG_redzone; // power of two, >= 32
Kostya Serebryany1e172b42011-11-30 01:07:02 +000032size_t FLAG_quarantine_size;
33int FLAG_demangle;
34bool FLAG_symbolize;
35int FLAG_v;
36int FLAG_debug;
37bool FLAG_poison_shadow;
38int FLAG_report_globals;
39size_t FLAG_malloc_context_size = kMallocContextSize;
40uintptr_t FLAG_large_malloc;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000041bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000042bool FLAG_replace_str;
43bool FLAG_replace_intrin;
44bool FLAG_replace_cfallocator; // Used on Mac only.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000045size_t FLAG_max_malloc_fill_size = 0;
46bool FLAG_use_fake_stack;
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000047int FLAG_exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000048bool FLAG_allow_user_poisoning;
Kostya Serebryanycb00d132012-01-31 00:52:18 +000049int FLAG_sleep_before_dying;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000050
51// -------------------------- Globals --------------------- {{{1
52int asan_inited;
53bool asan_init_is_running;
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +000054static void (*death_callback)(void);
Alexander Potapenko3fe91352012-02-27 14:06:48 +000055static void (*error_report_callback)(const char*);
56char *error_message_buffer = NULL;
57size_t error_message_buffer_pos = 0;
58size_t error_message_buffer_size = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059
Kostya Serebryany1e172b42011-11-30 01:07:02 +000060// -------------------------- Misc ---------------- {{{1
61void ShowStatsAndAbort() {
62 __asan_print_accumulated_stats();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000063 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064}
65
66static void PrintBytes(const char *before, uintptr_t *a) {
67 uint8_t *bytes = (uint8_t*)a;
68 size_t byte_num = (__WORDSIZE) / 8;
69 Printf("%s%p:", before, (uintptr_t)a);
70 for (size_t i = 0; i < byte_num; i++) {
71 Printf(" %lx%lx", bytes[i] >> 4, bytes[i] & 15);
72 }
73 Printf("\n");
74}
75
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000076size_t ReadFileToBuffer(const char *file_name, char **buff,
Kostya Serebryanydf499b42012-01-05 00:44:33 +000077 size_t *buff_size, size_t max_len) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +000078 const size_t kMinFileLen = kPageSize;
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000079 size_t read_len = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000080 *buff = 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000081 *buff_size = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000082 // The files we usually open are not seekable, so try different buffer sizes.
83 for (size_t size = kMinFileLen; size <= max_len; size *= 2) {
84 int fd = AsanOpenReadonly(file_name);
85 if (fd < 0) return -1;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000086 AsanUnmapOrDie(*buff, *buff_size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000087 *buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
Kostya Serebryanydf499b42012-01-05 00:44:33 +000088 *buff_size = size;
Kostya Serebryany454a0642012-01-17 18:00:07 +000089 // Read up to one page at a time.
90 read_len = 0;
91 bool reached_eof = false;
92 while (read_len + kPageSize <= size) {
93 size_t just_read = AsanRead(fd, *buff + read_len, kPageSize);
94 if (just_read == 0) {
95 reached_eof = true;
96 break;
97 }
98 read_len += just_read;
99 }
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000100 AsanClose(fd);
Kostya Serebryany454a0642012-01-17 18:00:07 +0000101 if (reached_eof) // We've read the whole file.
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000102 break;
103 }
104 return read_len;
105}
106
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000107void AsanDie() {
Kostya Serebryany5e4e91c2012-02-16 00:40:18 +0000108 static int num_calls = 0;
Alexey Samsonov95d6b332012-03-20 10:14:55 +0000109 if (AtomicInc(&num_calls) > 1) {
110 // Don't die twice - run a busy loop.
111 while (1) { }
112 }
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000113 if (FLAG_sleep_before_dying) {
114 Report("Sleeping for %d second(s)\n", FLAG_sleep_before_dying);
115 SleepForSeconds(FLAG_sleep_before_dying);
116 }
117 if (death_callback)
118 death_callback();
119 Exit(FLAG_exitcode);
120}
121
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122// ---------------------- mmap -------------------- {{{1
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000123void OutOfMemoryMessageAndDie(const char *mem_type, size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000124 Report("ERROR: AddressSanitizer failed to allocate "
125 "0x%lx (%ld) bytes of %s\n",
126 size, size, mem_type);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000127 PRINT_CURRENT_STACK();
128 ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000129}
130
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000131// Reserve memory range [beg, end].
132static void ReserveShadowMemoryRange(uintptr_t beg, uintptr_t end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133 CHECK((beg % kPageSize) == 0);
134 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000135 size_t size = end - beg + 1;
136 void *res = AsanMmapFixedNoReserve(beg, size);
137 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000138}
139
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000140// ---------------------- LowLevelAllocator ------------- {{{1
141void *LowLevelAllocator::Allocate(size_t size) {
142 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
143 if (allocated_end_ - allocated_current_ < size) {
144 size_t size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000145 allocated_current_ =
146 (char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000147 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000148 PoisonShadow((uintptr_t)allocated_current_, size_to_allocate,
149 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000150 }
151 CHECK(allocated_end_ - allocated_current_ >= size);
152 void *res = allocated_current_;
153 allocated_current_ += size;
154 return res;
155}
156
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000157// ---------------------- DescribeAddress -------------------- {{{1
158static bool DescribeStackAddress(uintptr_t addr, uintptr_t access_size) {
159 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
160 if (!t) return false;
161 const intptr_t kBufSize = 4095;
162 char buf[kBufSize];
163 uintptr_t offset = 0;
164 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
165 // This string is created by the compiler and has the following form:
166 // "FunctioName n alloc_1 alloc_2 ... alloc_n"
167 // where alloc_i looks like "offset size len ObjectName ".
168 CHECK(frame_descr);
169 // Report the function name and the offset.
Timur Iskhodzhanov53627172012-02-14 19:33:04 +0000170 const char *name_end = internal_strchr(frame_descr, ' ');
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171 CHECK(name_end);
172 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000173 internal_strncat(buf, frame_descr,
174 Min(kBufSize,
175 static_cast<intptr_t>(name_end - frame_descr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000176 Printf("Address %p is located at offset %ld "
177 "in frame <%s> of T%d's stack:\n",
178 addr, offset, buf, t->tid());
179 // Report the number of stack objects.
180 char *p;
Alexey Samsonov88981022012-02-17 16:15:09 +0000181 size_t n_objects = internal_simple_strtoll(name_end, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000182 CHECK(n_objects > 0);
183 Printf(" This frame has %ld object(s):\n", n_objects);
184 // Report all objects in this frame.
185 for (size_t i = 0; i < n_objects; i++) {
186 size_t beg, size;
187 intptr_t len;
Alexey Samsonov88981022012-02-17 16:15:09 +0000188 beg = internal_simple_strtoll(p, &p, 10);
189 size = internal_simple_strtoll(p, &p, 10);
190 len = internal_simple_strtoll(p, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000191 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
192 Printf("AddressSanitizer can't parse the stack frame descriptor: |%s|\n",
193 frame_descr);
194 break;
195 }
196 p++;
197 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000198 internal_strncat(buf, p, Min(kBufSize, len));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000199 p += len;
200 Printf(" [%ld, %ld) '%s'\n", beg, beg + size, buf);
201 }
202 Printf("HINT: this may be a false positive if your program uses "
203 "some custom stack unwind mechanism\n"
204 " (longjmp and C++ exceptions *are* supported)\n");
205 t->summary()->Announce();
206 return true;
207}
208
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000209static NOINLINE void DescribeAddress(uintptr_t addr, uintptr_t access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000210 // Check if this is a global.
211 if (DescribeAddrIfGlobal(addr))
212 return;
213
214 if (DescribeStackAddress(addr, access_size))
215 return;
216
217 // finally, check if this is a heap.
218 DescribeHeapAddress(addr, access_size);
219}
220
221// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000222// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000223#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000224NOINLINE ASAN_INTERFACE_ATTRIBUTE \
225extern "C" void __asan_report_ ## type ## size(uintptr_t addr); \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000226extern "C" void __asan_report_ ## type ## size(uintptr_t addr) { \
Kostya Serebryany9f311bb2012-03-15 01:36:00 +0000227 GET_CALLER_PC_BP_SP; \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000228 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000229}
230
231ASAN_REPORT_ERROR(load, false, 1)
232ASAN_REPORT_ERROR(load, false, 2)
233ASAN_REPORT_ERROR(load, false, 4)
234ASAN_REPORT_ERROR(load, false, 8)
235ASAN_REPORT_ERROR(load, false, 16)
236ASAN_REPORT_ERROR(store, true, 1)
237ASAN_REPORT_ERROR(store, true, 2)
238ASAN_REPORT_ERROR(store, true, 4)
239ASAN_REPORT_ERROR(store, true, 8)
240ASAN_REPORT_ERROR(store, true, 16)
241
242// Force the linker to keep the symbols for various ASan interface functions.
243// We want to keep those in the executable in order to let the instrumented
244// dynamic libraries access the symbol even if it is not used by the executable
245// itself. This should help if the build system is removing dead code at link
246// time.
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000247static NOINLINE void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000248 volatile int fake_condition = 0; // prevent dead condition elimination.
249 if (fake_condition) {
Kostya Serebryanyf0977db2012-03-14 22:48:09 +0000250 __asan_report_load1(0);
251 __asan_report_load2(0);
252 __asan_report_load4(0);
253 __asan_report_load8(0);
254 __asan_report_load16(0);
255 __asan_report_store1(0);
256 __asan_report_store2(0);
257 __asan_report_store4(0);
258 __asan_report_store8(0);
259 __asan_report_store16(0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000260 __asan_register_global(0, 0, NULL);
261 __asan_register_globals(NULL, 0);
Kostya Serebryany45581682011-12-28 23:35:46 +0000262 __asan_unregister_globals(NULL, 0);
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000263 __asan_set_death_callback(NULL);
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000264 __asan_set_error_report_callback(NULL);
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000265 __asan_handle_no_return();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000266 }
267}
268
269// -------------------------- Init ------------------- {{{1
270static int64_t IntFlagValue(const char *flags, const char *flag,
271 int64_t default_val) {
272 if (!flags) return default_val;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000273 const char *str = internal_strstr(flags, flag);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000274 if (!str) return default_val;
Alexey Samsonov88981022012-02-17 16:15:09 +0000275 return internal_atoll(str + internal_strlen(flag));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000276}
277
278static void asan_atexit() {
279 Printf("AddressSanitizer exit stats:\n");
280 __asan_print_accumulated_stats();
281}
282
283void CheckFailed(const char *cond, const char *file, int line) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000284 Report("CHECK failed: %s at %s:%d\n", cond, file, line);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000285 PRINT_CURRENT_STACK();
286 ShowStatsAndAbort();
287}
288
289} // namespace __asan
290
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000291// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000292using namespace __asan; // NOLINT
293
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000294int __asan_set_error_exit_code(int exit_code) {
295 int old = FLAG_exitcode;
296 FLAG_exitcode = exit_code;
297 return old;
298}
299
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000300void NOINLINE __asan_handle_no_return() {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000301 int local_stack;
302 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
303 CHECK(curr_thread);
304 uintptr_t top = curr_thread->stack_top();
305 uintptr_t bottom = ((uintptr_t)&local_stack - kPageSize) & ~(kPageSize-1);
306 PoisonShadow(bottom, top - bottom, 0);
307}
308
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000309void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000310 death_callback = callback;
311}
312
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000313void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
314 error_report_callback = callback;
315 if (callback) {
316 error_message_buffer_size = 1 << 14;
317 error_message_buffer =
318 (char*)AsanMmapSomewhereOrDie(error_message_buffer_size, __FUNCTION__);
319 error_message_buffer_pos = 0;
320 }
321}
322
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000323void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
324 uintptr_t addr, bool is_write, size_t access_size) {
325 // Do not print more than one report, otherwise they will mix up.
326 static int num_calls = 0;
327 if (AtomicInc(&num_calls) > 1) return;
328
329 Printf("=================================================================\n");
330 const char *bug_descr = "unknown-crash";
331 if (AddrIsInMem(addr)) {
332 uint8_t *shadow_addr = (uint8_t*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000333 // If we are accessing 16 bytes, look at the second shadow byte.
334 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
335 shadow_addr++;
336 // If we are in the partial right redzone, look at the next shadow byte.
337 if (*shadow_addr > 0 && *shadow_addr < 128)
338 shadow_addr++;
339 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000340 case kAsanHeapLeftRedzoneMagic:
341 case kAsanHeapRightRedzoneMagic:
342 bug_descr = "heap-buffer-overflow";
343 break;
344 case kAsanHeapFreeMagic:
345 bug_descr = "heap-use-after-free";
346 break;
347 case kAsanStackLeftRedzoneMagic:
348 bug_descr = "stack-buffer-underflow";
349 break;
350 case kAsanStackMidRedzoneMagic:
351 case kAsanStackRightRedzoneMagic:
352 case kAsanStackPartialRedzoneMagic:
353 bug_descr = "stack-buffer-overflow";
354 break;
355 case kAsanStackAfterReturnMagic:
356 bug_descr = "stack-use-after-return";
357 break;
358 case kAsanUserPoisonedMemoryMagic:
359 bug_descr = "use-after-poison";
360 break;
361 case kAsanGlobalRedzoneMagic:
362 bug_descr = "global-buffer-overflow";
363 break;
364 }
365 }
366
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000367 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
368 int curr_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
369
370 if (curr_thread) {
371 // We started reporting an error message. Stop using the fake stack
372 // in case we will call an instrumented function from a symbolizer.
373 curr_thread->fake_stack().StopUsingFakeStack();
374 }
375
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000376 Report("ERROR: AddressSanitizer %s on address "
377 "%p at pc 0x%lx bp 0x%lx sp 0x%lx\n",
378 bug_descr, addr, pc, bp, sp);
379
380 Printf("%s of size %d at %p thread T%d\n",
381 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000382 access_size, addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000383
384 if (FLAG_debug) {
385 PrintBytes("PC: ", (uintptr_t*)pc);
386 }
387
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000388 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000389 stack.PrintStack();
390
391 CHECK(AddrIsInMem(addr));
392
393 DescribeAddress(addr, access_size);
394
395 uintptr_t shadow_addr = MemToShadow(addr);
396 Report("ABORTING\n");
397 __asan_print_accumulated_stats();
398 Printf("Shadow byte and word:\n");
399 Printf(" %p: %x\n", shadow_addr, *(unsigned char*)shadow_addr);
400 uintptr_t aligned_shadow = shadow_addr & ~(kWordSize - 1);
401 PrintBytes(" ", (uintptr_t*)(aligned_shadow));
402 Printf("More shadow bytes:\n");
403 PrintBytes(" ", (uintptr_t*)(aligned_shadow-4*kWordSize));
404 PrintBytes(" ", (uintptr_t*)(aligned_shadow-3*kWordSize));
405 PrintBytes(" ", (uintptr_t*)(aligned_shadow-2*kWordSize));
406 PrintBytes(" ", (uintptr_t*)(aligned_shadow-1*kWordSize));
407 PrintBytes("=>", (uintptr_t*)(aligned_shadow+0*kWordSize));
408 PrintBytes(" ", (uintptr_t*)(aligned_shadow+1*kWordSize));
409 PrintBytes(" ", (uintptr_t*)(aligned_shadow+2*kWordSize));
410 PrintBytes(" ", (uintptr_t*)(aligned_shadow+3*kWordSize));
411 PrintBytes(" ", (uintptr_t*)(aligned_shadow+4*kWordSize));
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000412 if (error_report_callback) {
413 error_report_callback(error_message_buffer);
414 }
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000415 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000416}
417
418void __asan_init() {
419 if (asan_inited) return;
420 asan_init_is_running = true;
421
422 // Make sure we are not statically linked.
423 AsanDoesNotSupportStaticLinkage();
424
425 // flags
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000426 const char *options = AsanGetEnv("ASAN_OPTIONS");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000427 FLAG_malloc_context_size =
428 IntFlagValue(options, "malloc_context_size=", kMallocContextSize);
429 CHECK(FLAG_malloc_context_size <= kMallocContextSize);
430
431 FLAG_max_malloc_fill_size =
432 IntFlagValue(options, "max_malloc_fill_size=", 0);
433
434 FLAG_v = IntFlagValue(options, "verbosity=", 0);
435
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +0000436 FLAG_redzone = IntFlagValue(options, "redzone=",
437 (ASAN_LOW_MEMORY) ? 64 : 128);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000438 CHECK(FLAG_redzone >= 32);
439 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
440
441 FLAG_atexit = IntFlagValue(options, "atexit=", 0);
442 FLAG_poison_shadow = IntFlagValue(options, "poison_shadow=", 1);
443 FLAG_report_globals = IntFlagValue(options, "report_globals=", 1);
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000444 FLAG_handle_segv = IntFlagValue(options, "handle_segv=", ASAN_NEEDS_SEGV);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000445 FLAG_symbolize = IntFlagValue(options, "symbolize=", 1);
446 FLAG_demangle = IntFlagValue(options, "demangle=", 1);
447 FLAG_debug = IntFlagValue(options, "debug=", 0);
448 FLAG_replace_cfallocator = IntFlagValue(options, "replace_cfallocator=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000449 FLAG_replace_str = IntFlagValue(options, "replace_str=", 1);
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000450 FLAG_replace_intrin = IntFlagValue(options, "replace_intrin=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000451 FLAG_use_fake_stack = IntFlagValue(options, "use_fake_stack=", 1);
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000452 FLAG_exitcode = IntFlagValue(options, "exitcode=",
453 ASAN_DEFAULT_FAILURE_EXITCODE);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000454 FLAG_allow_user_poisoning = IntFlagValue(options,
455 "allow_user_poisoning=", 1);
Kostya Serebryanycb00d132012-01-31 00:52:18 +0000456 FLAG_sleep_before_dying = IntFlagValue(options, "sleep_before_dying=", 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000457
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000458 FLAG_quarantine_size = IntFlagValue(options, "quarantine_size=",
459 (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28);
460
461 if (FLAG_v) {
462 Report("Parsed ASAN_OPTIONS: %s\n", options);
463 }
464
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000465 if (FLAG_atexit) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000466 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000467 }
468
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000469 // interceptors
470 InitializeAsanInterceptors();
471
472 ReplaceSystemMalloc();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000473 InstallSignalHandlers();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000474
475 if (FLAG_v) {
476 Printf("|| `[%p, %p]` || HighMem ||\n", kHighMemBeg, kHighMemEnd);
477 Printf("|| `[%p, %p]` || HighShadow ||\n",
478 kHighShadowBeg, kHighShadowEnd);
479 Printf("|| `[%p, %p]` || ShadowGap ||\n",
480 kShadowGapBeg, kShadowGapEnd);
481 Printf("|| `[%p, %p]` || LowShadow ||\n",
482 kLowShadowBeg, kLowShadowEnd);
483 Printf("|| `[%p, %p]` || LowMem ||\n", kLowMemBeg, kLowMemEnd);
484 Printf("MemToShadow(shadow): %p %p %p %p\n",
485 MEM_TO_SHADOW(kLowShadowBeg),
486 MEM_TO_SHADOW(kLowShadowEnd),
487 MEM_TO_SHADOW(kHighShadowBeg),
488 MEM_TO_SHADOW(kHighShadowEnd));
489 Printf("red_zone=%ld\n", FLAG_redzone);
490 Printf("malloc_context_size=%ld\n", (int)FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000491
492 Printf("SHADOW_SCALE: %lx\n", SHADOW_SCALE);
493 Printf("SHADOW_GRANULARITY: %lx\n", SHADOW_GRANULARITY);
494 Printf("SHADOW_OFFSET: %lx\n", SHADOW_OFFSET);
495 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
496 }
497
498 if (__WORDSIZE == 64) {
499 // Disable core dumper -- it makes little sense to dump 16T+ core.
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000500 AsanDisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000501 }
502
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000503 if (AsanShadowRangeIsAvailable()) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000504 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000505 // mmap the low shadow plus at least one page.
506 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000507 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000508 // mmap the high shadow.
509 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000510 // protect the gap
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000511 void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
512 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000513 } else {
514 Report("Shadow memory range interleaves with an existing memory mapping. "
515 "ASan cannot proceed correctly. ABORTING.\n");
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000516 AsanDumpProcessMap();
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000517 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000518 }
519
520 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
521 // should be set to 1 prior to initializing the threads.
522 asan_inited = 1;
523 asan_init_is_running = false;
524
525 asanThreadRegistry().Init();
526 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000527 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000528
529 if (FLAG_v) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000530 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000531 }
532}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000533
534#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000535 // On Linux, we force __asan_init to be called before anyone else
536 // by placing it into .preinit_array section.
537 // FIXME: do we have anything like this on Mac?
538 __attribute__((section(".preinit_array")))
539 typeof(__asan_init) *__asan_preinit =__asan_init;
540#elif defined(_WIN32) && defined(_DLL)
541 // On Windows, when using dynamic CRT (/MD), we can put a pointer
542 // to __asan_init into the global list of C initializers.
543 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000544 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000545 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000546#endif