blob: 70b180143b0e89de1c76d4ed3c58c5a500a78467 [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_rtl.cc -------------------------------------------------------===//
Kostya Serebryany1e172b42011-11-30 01:07:02 +00002//
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"
Alexey Samsonove218beb2012-08-09 09:06:52 +000020#include "asan_report.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"
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +000025#include "sanitizer_common/sanitizer_atomic.h"
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000026#include "sanitizer_common/sanitizer_flags.h"
Alexey Samsonov9552db72012-06-05 07:25:47 +000027#include "sanitizer_common/sanitizer_libc.h"
Alexey Samsonov9c6e5302012-08-23 07:32:06 +000028#include "sanitizer_common/sanitizer_symbolizer.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000029
Alexey Samsonov47657ce2012-06-06 07:02:44 +000030namespace __sanitizer {
31using namespace __asan;
32
33void Die() {
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +000034 static atomic_uint32_t num_calls;
35 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
Alexey Samsonov47657ce2012-06-06 07:02:44 +000036 // Don't die twice - run a busy loop.
37 while (1) { }
38 }
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000039 if (flags()->sleep_before_dying) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000040 Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000041 SleepForSeconds(flags()->sleep_before_dying);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000042 }
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000043 if (flags()->unmap_shadow_on_exit)
Alexey Samsonova25b3462012-06-06 16:15:07 +000044 UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000045 if (death_callback)
46 death_callback();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000047 if (flags()->abort_on_error)
Alexey Samsonov47657ce2012-06-06 07:02:44 +000048 Abort();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000049 Exit(flags()->exitcode);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000050}
51
Alexander Potapenkoec3b0732012-08-15 11:57:52 +000052SANITIZER_INTERFACE_ATTRIBUTE
Alexey Samsonov15a77612012-06-06 15:22:20 +000053void CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2) {
Kostya Serebryanyb134ffa2012-07-17 07:20:13 +000054 AsanReport("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n",
Alexey Samsonov15a77612012-06-06 15:22:20 +000055 file, line, cond, (uptr)v1, (uptr)v2);
56 PRINT_CURRENT_STACK();
57 ShowStatsAndAbort();
58}
59
Alexey Samsonov47657ce2012-06-06 07:02:44 +000060} // namespace __sanitizer
61
Kostya Serebryany1e172b42011-11-30 01:07:02 +000062namespace __asan {
63
64// -------------------------- Flags ------------------------- {{{1
Alexey Samsonov9b1b1012012-07-10 09:17:06 +000065static const int kMallocContextSize = 30;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000066
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000067static Flags asan_flags;
68
69Flags *flags() {
70 return &asan_flags;
71}
72
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000073static void ParseFlagsFromString(Flags *f, const char *str) {
74 ParseFlag(str, &f->quarantine_size, "quarantine_size");
75 ParseFlag(str, &f->symbolize, "symbolize");
76 ParseFlag(str, &f->verbosity, "verbosity");
77 ParseFlag(str, &f->redzone, "redzone");
78 CHECK(f->redzone >= 16);
79 CHECK(IsPowerOfTwo(f->redzone));
80
81 ParseFlag(str, &f->debug, "debug");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000082 ParseFlag(str, &f->report_globals, "report_globals");
Kostya Serebryany3945c582012-08-21 14:10:25 +000083 ParseFlag(str, &f->check_initialization_order, "initialization_order");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000084 ParseFlag(str, &f->malloc_context_size, "malloc_context_size");
85 CHECK(f->malloc_context_size <= kMallocContextSize);
86
87 ParseFlag(str, &f->replace_str, "replace_str");
88 ParseFlag(str, &f->replace_intrin, "replace_intrin");
89 ParseFlag(str, &f->replace_cfallocator, "replace_cfallocator");
90 ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free");
91 ParseFlag(str, &f->use_fake_stack, "use_fake_stack");
92 ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size");
93 ParseFlag(str, &f->exitcode, "exitcode");
94 ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning");
95 ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying");
96 ParseFlag(str, &f->handle_segv, "handle_segv");
97 ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000098 ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size");
99 ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit");
100 ParseFlag(str, &f->abort_on_error, "abort_on_error");
101 ParseFlag(str, &f->atexit, "atexit");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000102 ParseFlag(str, &f->disable_core, "disable_core");
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +0000103 ParseFlag(str, &f->strip_path_prefix, "strip_path_prefix");
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +0000104 ParseFlag(str, &f->allow_reexec, "allow_reexec");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000105}
106
Alexey Samsonovb750c4c2012-07-25 10:40:57 +0000107extern "C" {
Alexey Samsonovc6b87162012-08-14 13:54:28 +0000108SANITIZER_WEAK_ATTRIBUTE
109SANITIZER_INTERFACE_ATTRIBUTE
110const char* __asan_default_options() { return ""; }
Alexey Samsonovb750c4c2012-07-25 10:40:57 +0000111} // extern "C"
112
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000113void InitializeFlags(Flags *f, const char *env) {
114 internal_memset(f, 0, sizeof(*f));
115
116 f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28;
117 f->symbolize = false;
118 f->verbosity = 0;
Alexey Samsonov7ed1d2b2012-07-10 07:41:27 +0000119 f->redzone = (ASAN_LOW_MEMORY) ? 64 : 128;
120 f->debug = false;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000121 f->report_globals = 1;
Kostya Serebryany3945c582012-08-21 14:10:25 +0000122 f->check_initialization_order = true;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000123 f->malloc_context_size = kMallocContextSize;
124 f->replace_str = true;
125 f->replace_intrin = true;
Alexey Samsonov7ed1d2b2012-07-10 07:41:27 +0000126 f->replace_cfallocator = true;
127 f->mac_ignore_invalid_free = false;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000128 f->use_fake_stack = true;
129 f->max_malloc_fill_size = 0;
130 f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
131 f->allow_user_poisoning = true;
132 f->sleep_before_dying = 0;
133 f->handle_segv = ASAN_NEEDS_SEGV;
134 f->use_sigaltstack = false;
135 f->check_malloc_usable_size = true;
136 f->unmap_shadow_on_exit = false;
137 f->abort_on_error = false;
138 f->atexit = false;
139 f->disable_core = (__WORDSIZE == 64);
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +0000140 f->strip_path_prefix = "";
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +0000141 f->allow_reexec = true;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000142
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000143 // Override from user-specified string.
Alexey Samsonovb750c4c2012-07-25 10:40:57 +0000144 ParseFlagsFromString(f, __asan_default_options());
145 if (flags()->verbosity) {
146 Report("Using the defaults from __asan_default_options: %s\n",
147 __asan_default_options());
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000148 }
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000149
150 // Override from command line.
151 ParseFlagsFromString(f, env);
152}
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000153
154// -------------------------- Globals --------------------- {{{1
155int asan_inited;
156bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000157void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000158
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000159// -------------------------- Misc ---------------- {{{1
160void ShowStatsAndAbort() {
161 __asan_print_accumulated_stats();
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000162 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000163}
164
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000165// ---------------------- mmap -------------------- {{{1
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000166// Reserve memory range [beg, end].
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000167static void ReserveShadowMemoryRange(uptr beg, uptr end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000168 CHECK((beg % kPageSize) == 0);
169 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000170 uptr size = end - beg + 1;
Alexey Samsonovf607fc12012-06-14 14:42:58 +0000171 void *res = MmapFixedNoReserve(beg, size);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000172 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000173}
174
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000175// ---------------------- LowLevelAllocator ------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000176void *LowLevelAllocator::Allocate(uptr size) {
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000177 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
Kostya Serebryany94c54f12012-06-23 16:30:48 +0000178 if (allocated_end_ - allocated_current_ < (sptr)size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000179 uptr size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000180 allocated_current_ =
Alexey Samsonova25b3462012-06-06 16:15:07 +0000181 (char*)MmapOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000182 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000183 PoisonShadow((uptr)allocated_current_, size_to_allocate,
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000184 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000185 }
Kostya Serebryany94c54f12012-06-23 16:30:48 +0000186 CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000187 void *res = allocated_current_;
188 allocated_current_ += size;
189 return res;
190}
191
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000192// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000193// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000194#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000195extern "C" NOINLINE INTERFACE_ATTRIBUTE \
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000196void __asan_report_ ## type ## size(uptr addr); \
197void __asan_report_ ## type ## size(uptr addr) { \
Kostya Serebryany9f311bb2012-03-15 01:36:00 +0000198 GET_CALLER_PC_BP_SP; \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000199 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000200}
201
202ASAN_REPORT_ERROR(load, false, 1)
203ASAN_REPORT_ERROR(load, false, 2)
204ASAN_REPORT_ERROR(load, false, 4)
205ASAN_REPORT_ERROR(load, false, 8)
206ASAN_REPORT_ERROR(load, false, 16)
207ASAN_REPORT_ERROR(store, true, 1)
208ASAN_REPORT_ERROR(store, true, 2)
209ASAN_REPORT_ERROR(store, true, 4)
210ASAN_REPORT_ERROR(store, true, 8)
211ASAN_REPORT_ERROR(store, true, 16)
212
213// Force the linker to keep the symbols for various ASan interface functions.
214// We want to keep those in the executable in order to let the instrumented
215// dynamic libraries access the symbol even if it is not used by the executable
216// itself. This should help if the build system is removing dead code at link
217// time.
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000218static NOINLINE void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000219 volatile int fake_condition = 0; // prevent dead condition elimination.
Alexander Potapenko448fe9a2012-08-09 09:46:12 +0000220 // __asan_report_* functions are noreturn, so we need a switch to prevent
221 // the compiler from removing any of them.
222 switch (fake_condition) {
223 case 1: __asan_report_load1(0); break;
224 case 2: __asan_report_load2(0); break;
225 case 3: __asan_report_load4(0); break;
226 case 4: __asan_report_load8(0); break;
227 case 5: __asan_report_load16(0); break;
228 case 6: __asan_report_store1(0); break;
229 case 7: __asan_report_store2(0); break;
230 case 8: __asan_report_store4(0); break;
231 case 9: __asan_report_store8(0); break;
232 case 10: __asan_report_store16(0); break;
233 case 11: __asan_register_global(0, 0, 0); break;
234 case 12: __asan_register_globals(0, 0); break;
235 case 13: __asan_unregister_globals(0, 0); break;
236 case 14: __asan_set_death_callback(0); break;
237 case 15: __asan_set_error_report_callback(0); break;
238 case 16: __asan_handle_no_return(); break;
Alexander Potapenko5a9938d2012-08-09 16:05:17 +0000239 case 17: __asan_address_is_poisoned(0); break;
240 case 18: __asan_get_allocated_size(0); break;
241 case 19: __asan_get_current_allocated_bytes(); break;
242 case 20: __asan_get_estimated_allocated_size(0); break;
243 case 21: __asan_get_free_bytes(); break;
244 case 22: __asan_get_heap_size(); break;
245 case 23: __asan_get_ownership(0); break;
246 case 24: __asan_get_unmapped_bytes(); break;
247 case 25: __asan_poison_memory_region(0, 0); break;
248 case 26: __asan_unpoison_memory_region(0, 0); break;
249 case 27: __asan_set_error_exit_code(0); break;
250 case 28: __asan_stack_free(0, 0, 0); break;
251 case 29: __asan_stack_malloc(0, 0); break;
Alexey Samsonove2430d22012-08-13 14:05:00 +0000252 case 30: __asan_set_on_error_callback(0); break;
Alexey Samsonov08d97882012-08-14 15:03:24 +0000253 case 31: __asan_default_options(); break;
Kostya Serebryany128892c2012-08-21 14:43:36 +0000254 case 32: __asan_before_dynamic_init(0, 0); break;
255 case 33: __asan_after_dynamic_init(); break;
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000256 case 34: __asan_malloc_hook(0, 0); break;
257 case 35: __asan_free_hook(0); break;
Alexey Samsonovc93d3e22012-08-22 13:31:37 +0000258 case 36: __asan_set_symbolize_callback(0); break;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000259 }
260}
261
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000262static void asan_atexit() {
Alexey Samsonove9541012012-06-06 13:11:29 +0000263 AsanPrintf("AddressSanitizer exit stats:\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000264 __asan_print_accumulated_stats();
265}
266
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000267} // namespace __asan
268
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000269// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000270using namespace __asan; // NOLINT
271
Alexander Potapenko5a9938d2012-08-09 16:05:17 +0000272int NOINLINE __asan_set_error_exit_code(int exit_code) {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000273 int old = flags()->exitcode;
274 flags()->exitcode = exit_code;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000275 return old;
276}
277
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000278void NOINLINE __asan_handle_no_return() {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000279 int local_stack;
280 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
281 CHECK(curr_thread);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000282 uptr top = curr_thread->stack_top();
283 uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1);
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000284 PoisonShadow(bottom, top - bottom, 0);
285}
286
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000287void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000288 death_callback = callback;
289}
290
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000291void __asan_init() {
292 if (asan_inited) return;
293 asan_init_is_running = true;
294
295 // Make sure we are not statically linked.
296 AsanDoesNotSupportStaticLinkage();
297
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +0000298 // Initialize flags. This must be done early, because most of the
299 // initialization steps look at flags().
Alexey Samsonov3dbeabb2012-06-14 14:07:21 +0000300 const char *options = GetEnv("ASAN_OPTIONS");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000301 InitializeFlags(flags(), options);
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000302
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000303 if (flags()->verbosity && options) {
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000304 Report("Parsed ASAN_OPTIONS: %s\n", options);
305 }
306
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +0000307 // Re-exec ourselves if we need to set additional env or command line args.
308 MaybeReexec();
309
310
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000311 if (flags()->atexit) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000312 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000313 }
314
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000315 // interceptors
316 InitializeAsanInterceptors();
317
318 ReplaceSystemMalloc();
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000319 ReplaceOperatorsNewAndDelete();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000320
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000321 if (flags()->verbosity) {
Alexey Samsonove4309e82012-06-06 10:54:25 +0000322 Printf("|| `[%p, %p]` || HighMem ||\n",
323 (void*)kHighMemBeg, (void*)kHighMemEnd);
324 Printf("|| `[%p, %p]` || HighShadow ||\n",
325 (void*)kHighShadowBeg, (void*)kHighShadowEnd);
326 Printf("|| `[%p, %p]` || ShadowGap ||\n",
327 (void*)kShadowGapBeg, (void*)kShadowGapEnd);
328 Printf("|| `[%p, %p]` || LowShadow ||\n",
329 (void*)kLowShadowBeg, (void*)kLowShadowEnd);
330 Printf("|| `[%p, %p]` || LowMem ||\n",
331 (void*)kLowMemBeg, (void*)kLowMemEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000332 Printf("MemToShadow(shadow): %p %p %p %p\n",
Alexey Samsonove4309e82012-06-06 10:54:25 +0000333 (void*)MEM_TO_SHADOW(kLowShadowBeg),
334 (void*)MEM_TO_SHADOW(kLowShadowEnd),
335 (void*)MEM_TO_SHADOW(kHighShadowBeg),
336 (void*)MEM_TO_SHADOW(kHighShadowEnd));
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000337 Printf("red_zone=%zu\n", (uptr)flags()->redzone);
338 Printf("malloc_context_size=%zu\n", (uptr)flags()->malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000339
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000340 Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE);
341 Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY);
342 Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000343 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
344 }
345
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000346 if (flags()->disable_core) {
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000347 DisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000348 }
349
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000350 uptr shadow_start = kLowShadowBeg;
351 if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity;
352 uptr shadow_end = kHighShadowEnd;
353 if (MemoryRangeIsAvailable(shadow_start, shadow_end)) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000354 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000355 // mmap the low shadow plus at least one page.
356 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000357 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000358 // mmap the high shadow.
359 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000360 // protect the gap
Alexey Samsonovf607fc12012-06-14 14:42:58 +0000361 void *prot = Mprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000362 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000363 } else {
364 Report("Shadow memory range interleaves with an existing memory mapping. "
365 "ASan cannot proceed correctly. ABORTING.\n");
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000366 DumpProcessMap();
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000367 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000368 }
369
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000370 InstallSignalHandlers();
Alexey Samsonov9c6e5302012-08-23 07:32:06 +0000371 // Start symbolizer process if necessary.
372 if (flags()->symbolize) {
373 const char *external_symbolizer = GetEnv("ASAN_SYMBOLIZER_PATH");
374 if (external_symbolizer) {
375 InitializeExternalSymbolizer(external_symbolizer);
376 }
377 }
Alexey Samsonovc93d3e22012-08-22 13:31:37 +0000378#ifdef _WIN32
379 __asan_set_symbolize_callback(WinSymbolize);
380#endif // _WIN32
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000381
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000382 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
383 // should be set to 1 prior to initializing the threads.
384 asan_inited = 1;
385 asan_init_is_running = false;
386
387 asanThreadRegistry().Init();
388 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000389 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000390
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000391 if (flags()->verbosity) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000392 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000393 }
394}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000395
396#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000397 // On Linux, we force __asan_init to be called before anyone else
398 // by placing it into .preinit_array section.
399 // FIXME: do we have anything like this on Mac?
400 __attribute__((section(".preinit_array")))
401 typeof(__asan_init) *__asan_preinit =__asan_init;
402#elif defined(_WIN32) && defined(_DLL)
403 // On Windows, when using dynamic CRT (/MD), we can put a pointer
404 // to __asan_init into the global list of C initializers.
405 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000406 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000407 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000408#endif