blob: 568a79f5d6eb358d557502ad9cfd068c2c83e3db [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"
Kostya Serebryanydf499b42012-01-05 00:44:33 +000020#include "asan_procmaps.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021#include "asan_stack.h"
22#include "asan_stats.h"
23#include "asan_thread.h"
24#include "asan_thread_registry.h"
25
Kostya Serebryany1e172b42011-11-30 01:07:02 +000026namespace __asan {
27
28// -------------------------- Flags ------------------------- {{{1
29static const size_t kMallocContextSize = 30;
30static int FLAG_atexit;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000031
32size_t FLAG_redzone; // power of two, >= 32
Kostya Serebryany1e172b42011-11-30 01:07:02 +000033size_t FLAG_quarantine_size;
34int FLAG_demangle;
35bool FLAG_symbolize;
36int FLAG_v;
37int FLAG_debug;
38bool FLAG_poison_shadow;
39int FLAG_report_globals;
40size_t FLAG_malloc_context_size = kMallocContextSize;
41uintptr_t FLAG_large_malloc;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000042bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000043bool FLAG_replace_str;
44bool FLAG_replace_intrin;
45bool FLAG_replace_cfallocator; // Used on Mac only.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000046size_t FLAG_max_malloc_fill_size = 0;
47bool FLAG_use_fake_stack;
48int FLAG_exitcode = EXIT_FAILURE;
49bool FLAG_allow_user_poisoning;
Kostya Serebryanycb00d132012-01-31 00:52:18 +000050int FLAG_sleep_before_dying;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000051
52// -------------------------- Globals --------------------- {{{1
53int asan_inited;
54bool asan_init_is_running;
55
Kostya Serebryany1e172b42011-11-30 01:07:02 +000056// -------------------------- Misc ---------------- {{{1
57void ShowStatsAndAbort() {
58 __asan_print_accumulated_stats();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000059 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000060}
61
62static void PrintBytes(const char *before, uintptr_t *a) {
63 uint8_t *bytes = (uint8_t*)a;
64 size_t byte_num = (__WORDSIZE) / 8;
65 Printf("%s%p:", before, (uintptr_t)a);
66 for (size_t i = 0; i < byte_num; i++) {
67 Printf(" %lx%lx", bytes[i] >> 4, bytes[i] & 15);
68 }
69 Printf("\n");
70}
71
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000072size_t ReadFileToBuffer(const char *file_name, char **buff,
Kostya Serebryanydf499b42012-01-05 00:44:33 +000073 size_t *buff_size, size_t max_len) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +000074 const size_t kMinFileLen = kPageSize;
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000075 size_t read_len = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000076 *buff = 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000077 *buff_size = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000078 // The files we usually open are not seekable, so try different buffer sizes.
79 for (size_t size = kMinFileLen; size <= max_len; size *= 2) {
80 int fd = AsanOpenReadonly(file_name);
81 if (fd < 0) return -1;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000082 AsanUnmapOrDie(*buff, *buff_size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000083 *buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
Kostya Serebryanydf499b42012-01-05 00:44:33 +000084 *buff_size = size;
Kostya Serebryany454a0642012-01-17 18:00:07 +000085 // Read up to one page at a time.
86 read_len = 0;
87 bool reached_eof = false;
88 while (read_len + kPageSize <= size) {
89 size_t just_read = AsanRead(fd, *buff + read_len, kPageSize);
90 if (just_read == 0) {
91 reached_eof = true;
92 break;
93 }
94 read_len += just_read;
95 }
Kostya Serebryanyde496f42011-12-28 22:58:01 +000096 AsanClose(fd);
Kostya Serebryany454a0642012-01-17 18:00:07 +000097 if (reached_eof) // We've read the whole file.
Kostya Serebryanyde496f42011-12-28 22:58:01 +000098 break;
99 }
100 return read_len;
101}
102
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103// ---------------------- mmap -------------------- {{{1
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000104void OutOfMemoryMessageAndDie(const char *mem_type, size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000105 Report("ERROR: AddressSanitizer failed to allocate "
106 "0x%lx (%ld) bytes of %s\n",
107 size, size, mem_type);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000108 PRINT_CURRENT_STACK();
109 ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000110}
111
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000112// Reserve memory range [beg, end].
113static void ReserveShadowMemoryRange(uintptr_t beg, uintptr_t end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000114 CHECK((beg % kPageSize) == 0);
115 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000116 size_t size = end - beg + 1;
117 void *res = AsanMmapFixedNoReserve(beg, size);
118 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119}
120
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000121inline bool IntervalsAreSeparate(uintptr_t start1, uintptr_t end1,
122 uintptr_t start2, uintptr_t end2) {
123 CHECK(start1 <= end1);
124 CHECK(start2 <= end2);
125 if (start1 == start2) {
126 return false;
127 } else {
128 if (start1 < start2) {
129 return (end1 < start2);
130 } else {
131 return (end2 < start1);
132 }
133 }
134 return false;
135}
136
137// FIXME: this is thread-unsafe, but should not cause problems most of the time.
138// When the shadow is mapped only a single thread usually exists (plus maybe
139// several worker threads on Mac, which aren't expected to map big chunks of
140// memory.
141bool AsanShadowRangeIsAvailable() {
142 AsanProcMaps procmaps;
143 uintptr_t start, end;
144 bool available = true;
145 while (procmaps.Next(&start, &end,
146 /*offset*/NULL, /*filename*/NULL, /*size*/NULL)) {
147 if (!IntervalsAreSeparate(start, end,
148 kLowShadowBeg - kMmapGranularity,
149 kHighShadowEnd)) {
150 available = false;
151 break;
152 }
153 }
154 return available;
155}
156
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000157// ---------------------- LowLevelAllocator ------------- {{{1
158void *LowLevelAllocator::Allocate(size_t size) {
159 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
160 if (allocated_end_ - allocated_current_ < size) {
161 size_t size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000162 allocated_current_ =
163 (char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000164 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000165 PoisonShadow((uintptr_t)allocated_current_, size_to_allocate,
166 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000167 }
168 CHECK(allocated_end_ - allocated_current_ >= size);
169 void *res = allocated_current_;
170 allocated_current_ += size;
171 return res;
172}
173
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174// ---------------------- DescribeAddress -------------------- {{{1
175static bool DescribeStackAddress(uintptr_t addr, uintptr_t access_size) {
176 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
177 if (!t) return false;
178 const intptr_t kBufSize = 4095;
179 char buf[kBufSize];
180 uintptr_t offset = 0;
181 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
182 // This string is created by the compiler and has the following form:
183 // "FunctioName n alloc_1 alloc_2 ... alloc_n"
184 // where alloc_i looks like "offset size len ObjectName ".
185 CHECK(frame_descr);
186 // Report the function name and the offset.
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000187 const char *name_end = REAL(strchr)(frame_descr, ' ');
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000188 CHECK(name_end);
189 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000190 internal_strncat(buf, frame_descr,
191 Min(kBufSize,
192 static_cast<intptr_t>(name_end - frame_descr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000193 Printf("Address %p is located at offset %ld "
194 "in frame <%s> of T%d's stack:\n",
195 addr, offset, buf, t->tid());
196 // Report the number of stack objects.
197 char *p;
198 size_t n_objects = strtol(name_end, &p, 10);
199 CHECK(n_objects > 0);
200 Printf(" This frame has %ld object(s):\n", n_objects);
201 // Report all objects in this frame.
202 for (size_t i = 0; i < n_objects; i++) {
203 size_t beg, size;
204 intptr_t len;
205 beg = strtol(p, &p, 10);
206 size = strtol(p, &p, 10);
207 len = strtol(p, &p, 10);
208 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
209 Printf("AddressSanitizer can't parse the stack frame descriptor: |%s|\n",
210 frame_descr);
211 break;
212 }
213 p++;
214 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000215 internal_strncat(buf, p, Min(kBufSize, len));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000216 p += len;
217 Printf(" [%ld, %ld) '%s'\n", beg, beg + size, buf);
218 }
219 Printf("HINT: this may be a false positive if your program uses "
220 "some custom stack unwind mechanism\n"
221 " (longjmp and C++ exceptions *are* supported)\n");
222 t->summary()->Announce();
223 return true;
224}
225
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000226static NOINLINE void DescribeAddress(uintptr_t addr, uintptr_t access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000227 // Check if this is a global.
228 if (DescribeAddrIfGlobal(addr))
229 return;
230
231 if (DescribeStackAddress(addr, access_size))
232 return;
233
234 // finally, check if this is a heap.
235 DescribeHeapAddress(addr, access_size);
236}
237
238// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000239// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000240#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000241NOINLINE ASAN_INTERFACE_ATTRIBUTE \
242extern "C" void __asan_report_ ## type ## size(uintptr_t addr); \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000243extern "C" void __asan_report_ ## type ## size(uintptr_t addr) { \
244 GET_BP_PC_SP; \
245 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000246}
247
248ASAN_REPORT_ERROR(load, false, 1)
249ASAN_REPORT_ERROR(load, false, 2)
250ASAN_REPORT_ERROR(load, false, 4)
251ASAN_REPORT_ERROR(load, false, 8)
252ASAN_REPORT_ERROR(load, false, 16)
253ASAN_REPORT_ERROR(store, true, 1)
254ASAN_REPORT_ERROR(store, true, 2)
255ASAN_REPORT_ERROR(store, true, 4)
256ASAN_REPORT_ERROR(store, true, 8)
257ASAN_REPORT_ERROR(store, true, 16)
258
259// Force the linker to keep the symbols for various ASan interface functions.
260// We want to keep those in the executable in order to let the instrumented
261// dynamic libraries access the symbol even if it is not used by the executable
262// itself. This should help if the build system is removing dead code at link
263// time.
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000264static void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000265 volatile int fake_condition = 0; // prevent dead condition elimination.
266 if (fake_condition) {
267 __asan_report_load1(NULL);
268 __asan_report_load2(NULL);
269 __asan_report_load4(NULL);
270 __asan_report_load8(NULL);
271 __asan_report_load16(NULL);
272 __asan_report_store1(NULL);
273 __asan_report_store2(NULL);
274 __asan_report_store4(NULL);
275 __asan_report_store8(NULL);
276 __asan_report_store16(NULL);
277 __asan_register_global(0, 0, NULL);
278 __asan_register_globals(NULL, 0);
Kostya Serebryany45581682011-12-28 23:35:46 +0000279 __asan_unregister_globals(NULL, 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000280 }
281}
282
283// -------------------------- Init ------------------- {{{1
Alexander Potapenko6f045292012-01-27 15:15:04 +0000284#if defined(_WIN32)
285// atoll is not defined on Windows.
286int64_t atoll(const char *str) {
287 UNIMPLEMENTED();
288 return -1;
289}
290#endif
291
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000292static int64_t IntFlagValue(const char *flags, const char *flag,
293 int64_t default_val) {
294 if (!flags) return default_val;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000295 const char *str = internal_strstr(flags, flag);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000296 if (!str) return default_val;
297 return atoll(str + internal_strlen(flag));
298}
299
300static void asan_atexit() {
301 Printf("AddressSanitizer exit stats:\n");
302 __asan_print_accumulated_stats();
303}
304
305void CheckFailed(const char *cond, const char *file, int line) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000306 Report("CHECK failed: %s at %s:%d\n", cond, file, line);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000307 PRINT_CURRENT_STACK();
308 ShowStatsAndAbort();
309}
310
311} // namespace __asan
312
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000313// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000314using namespace __asan; // NOLINT
315
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000316int __asan_set_error_exit_code(int exit_code) {
317 int old = FLAG_exitcode;
318 FLAG_exitcode = exit_code;
319 return old;
320}
321
Evgeniy Stepanov5ffafd82012-02-13 11:55:24 +0000322NOINLINE ASAN_INTERFACE_ATTRIBUTE
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000323void __asan_handle_no_return() {
324 int local_stack;
325 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
326 CHECK(curr_thread);
327 uintptr_t top = curr_thread->stack_top();
328 uintptr_t bottom = ((uintptr_t)&local_stack - kPageSize) & ~(kPageSize-1);
329 PoisonShadow(bottom, top - bottom, 0);
330}
331
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000332void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
333 uintptr_t addr, bool is_write, size_t access_size) {
334 // Do not print more than one report, otherwise they will mix up.
335 static int num_calls = 0;
336 if (AtomicInc(&num_calls) > 1) return;
337
338 Printf("=================================================================\n");
339 const char *bug_descr = "unknown-crash";
340 if (AddrIsInMem(addr)) {
341 uint8_t *shadow_addr = (uint8_t*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000342 // If we are accessing 16 bytes, look at the second shadow byte.
343 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
344 shadow_addr++;
345 // If we are in the partial right redzone, look at the next shadow byte.
346 if (*shadow_addr > 0 && *shadow_addr < 128)
347 shadow_addr++;
348 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000349 case kAsanHeapLeftRedzoneMagic:
350 case kAsanHeapRightRedzoneMagic:
351 bug_descr = "heap-buffer-overflow";
352 break;
353 case kAsanHeapFreeMagic:
354 bug_descr = "heap-use-after-free";
355 break;
356 case kAsanStackLeftRedzoneMagic:
357 bug_descr = "stack-buffer-underflow";
358 break;
359 case kAsanStackMidRedzoneMagic:
360 case kAsanStackRightRedzoneMagic:
361 case kAsanStackPartialRedzoneMagic:
362 bug_descr = "stack-buffer-overflow";
363 break;
364 case kAsanStackAfterReturnMagic:
365 bug_descr = "stack-use-after-return";
366 break;
367 case kAsanUserPoisonedMemoryMagic:
368 bug_descr = "use-after-poison";
369 break;
370 case kAsanGlobalRedzoneMagic:
371 bug_descr = "global-buffer-overflow";
372 break;
373 }
374 }
375
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000376 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
377 int curr_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
378
379 if (curr_thread) {
380 // We started reporting an error message. Stop using the fake stack
381 // in case we will call an instrumented function from a symbolizer.
382 curr_thread->fake_stack().StopUsingFakeStack();
383 }
384
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000385 Report("ERROR: AddressSanitizer %s on address "
386 "%p at pc 0x%lx bp 0x%lx sp 0x%lx\n",
387 bug_descr, addr, pc, bp, sp);
388
389 Printf("%s of size %d at %p thread T%d\n",
390 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000391 access_size, addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000392
393 if (FLAG_debug) {
394 PrintBytes("PC: ", (uintptr_t*)pc);
395 }
396
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000397 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000398 stack.PrintStack();
399
400 CHECK(AddrIsInMem(addr));
401
402 DescribeAddress(addr, access_size);
403
404 uintptr_t shadow_addr = MemToShadow(addr);
405 Report("ABORTING\n");
406 __asan_print_accumulated_stats();
407 Printf("Shadow byte and word:\n");
408 Printf(" %p: %x\n", shadow_addr, *(unsigned char*)shadow_addr);
409 uintptr_t aligned_shadow = shadow_addr & ~(kWordSize - 1);
410 PrintBytes(" ", (uintptr_t*)(aligned_shadow));
411 Printf("More shadow bytes:\n");
412 PrintBytes(" ", (uintptr_t*)(aligned_shadow-4*kWordSize));
413 PrintBytes(" ", (uintptr_t*)(aligned_shadow-3*kWordSize));
414 PrintBytes(" ", (uintptr_t*)(aligned_shadow-2*kWordSize));
415 PrintBytes(" ", (uintptr_t*)(aligned_shadow-1*kWordSize));
416 PrintBytes("=>", (uintptr_t*)(aligned_shadow+0*kWordSize));
417 PrintBytes(" ", (uintptr_t*)(aligned_shadow+1*kWordSize));
418 PrintBytes(" ", (uintptr_t*)(aligned_shadow+2*kWordSize));
419 PrintBytes(" ", (uintptr_t*)(aligned_shadow+3*kWordSize));
420 PrintBytes(" ", (uintptr_t*)(aligned_shadow+4*kWordSize));
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000421 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000422}
423
424void __asan_init() {
425 if (asan_inited) return;
426 asan_init_is_running = true;
427
428 // Make sure we are not statically linked.
429 AsanDoesNotSupportStaticLinkage();
430
431 // flags
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000432 const char *options = AsanGetEnv("ASAN_OPTIONS");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000433 FLAG_malloc_context_size =
434 IntFlagValue(options, "malloc_context_size=", kMallocContextSize);
435 CHECK(FLAG_malloc_context_size <= kMallocContextSize);
436
437 FLAG_max_malloc_fill_size =
438 IntFlagValue(options, "max_malloc_fill_size=", 0);
439
440 FLAG_v = IntFlagValue(options, "verbosity=", 0);
441
442 FLAG_redzone = IntFlagValue(options, "redzone=", 128);
443 CHECK(FLAG_redzone >= 32);
444 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
445
446 FLAG_atexit = IntFlagValue(options, "atexit=", 0);
447 FLAG_poison_shadow = IntFlagValue(options, "poison_shadow=", 1);
448 FLAG_report_globals = IntFlagValue(options, "report_globals=", 1);
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000449 FLAG_handle_segv = IntFlagValue(options, "handle_segv=", ASAN_NEEDS_SEGV);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000450 FLAG_symbolize = IntFlagValue(options, "symbolize=", 1);
451 FLAG_demangle = IntFlagValue(options, "demangle=", 1);
452 FLAG_debug = IntFlagValue(options, "debug=", 0);
453 FLAG_replace_cfallocator = IntFlagValue(options, "replace_cfallocator=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000454 FLAG_replace_str = IntFlagValue(options, "replace_str=", 1);
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000455 FLAG_replace_intrin = IntFlagValue(options, "replace_intrin=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000456 FLAG_use_fake_stack = IntFlagValue(options, "use_fake_stack=", 1);
457 FLAG_exitcode = IntFlagValue(options, "exitcode=", EXIT_FAILURE);
458 FLAG_allow_user_poisoning = IntFlagValue(options,
459 "allow_user_poisoning=", 1);
Kostya Serebryanycb00d132012-01-31 00:52:18 +0000460 FLAG_sleep_before_dying = IntFlagValue(options, "sleep_before_dying=", 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000461
462 if (FLAG_atexit) {
463 atexit(asan_atexit);
464 }
465
466 FLAG_quarantine_size =
467 IntFlagValue(options, "quarantine_size=", 1UL << 28);
468
469 // 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");
516 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000517 }
518
519 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
520 // should be set to 1 prior to initializing the threads.
521 asan_inited = 1;
522 asan_init_is_running = false;
523
524 asanThreadRegistry().Init();
525 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000526 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000527
528 if (FLAG_v) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000529 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000530 }
531}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000532
533#if defined(ASAN_USE_PREINIT_ARRAY)
534// On Linux, we force __asan_init to be called before anyone else
535// by placing it into .preinit_array section.
536// FIXME: do we have anything like this on Mac?
537__attribute__((section(".preinit_array")))
538 typeof(__asan_init) *__asan_preinit =__asan_init;
539#endif