blob: f0c51c9fc02a98a9c276284d7c5bf4b76328b8bb [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"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000028
Alexey Samsonov47657ce2012-06-06 07:02:44 +000029namespace __sanitizer {
30using namespace __asan;
31
32void Die() {
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +000033 static atomic_uint32_t num_calls;
34 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
Alexey Samsonov47657ce2012-06-06 07:02:44 +000035 // Don't die twice - run a busy loop.
36 while (1) { }
37 }
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000038 if (flags()->sleep_before_dying) {
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +000039 Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000040 SleepForSeconds(flags()->sleep_before_dying);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000041 }
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000042 if (flags()->unmap_shadow_on_exit)
Alexey Samsonova25b3462012-06-06 16:15:07 +000043 UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000044 if (death_callback)
45 death_callback();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000046 if (flags()->abort_on_error)
Alexey Samsonov47657ce2012-06-06 07:02:44 +000047 Abort();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000048 Exit(flags()->exitcode);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000049}
50
Alexey Samsonov15a77612012-06-06 15:22:20 +000051void CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2) {
Kostya Serebryanyb134ffa2012-07-17 07:20:13 +000052 AsanReport("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n",
Alexey Samsonov15a77612012-06-06 15:22:20 +000053 file, line, cond, (uptr)v1, (uptr)v2);
54 PRINT_CURRENT_STACK();
55 ShowStatsAndAbort();
56}
57
Alexey Samsonov47657ce2012-06-06 07:02:44 +000058} // namespace __sanitizer
59
Kostya Serebryany1e172b42011-11-30 01:07:02 +000060namespace __asan {
61
62// -------------------------- Flags ------------------------- {{{1
Alexey Samsonov9b1b1012012-07-10 09:17:06 +000063static const int kMallocContextSize = 30;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000065static Flags asan_flags;
66
67Flags *flags() {
68 return &asan_flags;
69}
70
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000071static void ParseFlagsFromString(Flags *f, const char *str) {
72 ParseFlag(str, &f->quarantine_size, "quarantine_size");
73 ParseFlag(str, &f->symbolize, "symbolize");
74 ParseFlag(str, &f->verbosity, "verbosity");
75 ParseFlag(str, &f->redzone, "redzone");
76 CHECK(f->redzone >= 16);
77 CHECK(IsPowerOfTwo(f->redzone));
78
79 ParseFlag(str, &f->debug, "debug");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000080 ParseFlag(str, &f->report_globals, "report_globals");
81 ParseFlag(str, &f->malloc_context_size, "malloc_context_size");
82 CHECK(f->malloc_context_size <= kMallocContextSize);
83
84 ParseFlag(str, &f->replace_str, "replace_str");
85 ParseFlag(str, &f->replace_intrin, "replace_intrin");
86 ParseFlag(str, &f->replace_cfallocator, "replace_cfallocator");
87 ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free");
88 ParseFlag(str, &f->use_fake_stack, "use_fake_stack");
89 ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size");
90 ParseFlag(str, &f->exitcode, "exitcode");
91 ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning");
92 ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying");
93 ParseFlag(str, &f->handle_segv, "handle_segv");
94 ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000095 ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size");
96 ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit");
97 ParseFlag(str, &f->abort_on_error, "abort_on_error");
98 ParseFlag(str, &f->atexit, "atexit");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000099 ParseFlag(str, &f->disable_core, "disable_core");
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +0000100 ParseFlag(str, &f->strip_path_prefix, "strip_path_prefix");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000101}
102
Alexey Samsonovb750c4c2012-07-25 10:40:57 +0000103extern "C" {
104const char* WEAK __asan_default_options() { return ""; }
105} // extern "C"
106
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000107void InitializeFlags(Flags *f, const char *env) {
108 internal_memset(f, 0, sizeof(*f));
109
110 f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28;
111 f->symbolize = false;
112 f->verbosity = 0;
Alexey Samsonov7ed1d2b2012-07-10 07:41:27 +0000113 f->redzone = (ASAN_LOW_MEMORY) ? 64 : 128;
114 f->debug = false;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000115 f->report_globals = 1;
116 f->malloc_context_size = kMallocContextSize;
117 f->replace_str = true;
118 f->replace_intrin = true;
Alexey Samsonov7ed1d2b2012-07-10 07:41:27 +0000119 f->replace_cfallocator = true;
120 f->mac_ignore_invalid_free = false;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000121 f->use_fake_stack = true;
122 f->max_malloc_fill_size = 0;
123 f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
124 f->allow_user_poisoning = true;
125 f->sleep_before_dying = 0;
126 f->handle_segv = ASAN_NEEDS_SEGV;
127 f->use_sigaltstack = false;
128 f->check_malloc_usable_size = true;
129 f->unmap_shadow_on_exit = false;
130 f->abort_on_error = false;
131 f->atexit = false;
132 f->disable_core = (__WORDSIZE == 64);
Alexey Samsonov4e21c6b2012-08-06 13:00:21 +0000133 f->strip_path_prefix = "";
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000134
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000135 // Override from user-specified string.
Alexey Samsonovb750c4c2012-07-25 10:40:57 +0000136 ParseFlagsFromString(f, __asan_default_options());
137 if (flags()->verbosity) {
138 Report("Using the defaults from __asan_default_options: %s\n",
139 __asan_default_options());
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000140 }
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000141
142 // Override from command line.
143 ParseFlagsFromString(f, env);
144}
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000145
146// -------------------------- Globals --------------------- {{{1
147int asan_inited;
148bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000149void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000150
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000151// -------------------------- Misc ---------------- {{{1
152void ShowStatsAndAbort() {
153 __asan_print_accumulated_stats();
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000154 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000155}
156
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000157// ---------------------- mmap -------------------- {{{1
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000158// Reserve memory range [beg, end].
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000159static void ReserveShadowMemoryRange(uptr beg, uptr end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000160 CHECK((beg % kPageSize) == 0);
161 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000162 uptr size = end - beg + 1;
Alexey Samsonovf607fc12012-06-14 14:42:58 +0000163 void *res = MmapFixedNoReserve(beg, size);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000164 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000165}
166
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000167// ---------------------- LowLevelAllocator ------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000168void *LowLevelAllocator::Allocate(uptr size) {
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000169 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
Kostya Serebryany94c54f12012-06-23 16:30:48 +0000170 if (allocated_end_ - allocated_current_ < (sptr)size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000171 uptr size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000172 allocated_current_ =
Alexey Samsonova25b3462012-06-06 16:15:07 +0000173 (char*)MmapOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000174 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000175 PoisonShadow((uptr)allocated_current_, size_to_allocate,
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000176 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000177 }
Kostya Serebryany94c54f12012-06-23 16:30:48 +0000178 CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000179 void *res = allocated_current_;
180 allocated_current_ += size;
181 return res;
182}
183
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000184// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000185// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000186#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000187extern "C" NOINLINE INTERFACE_ATTRIBUTE \
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000188void __asan_report_ ## type ## size(uptr addr); \
189void __asan_report_ ## type ## size(uptr addr) { \
Kostya Serebryany9f311bb2012-03-15 01:36:00 +0000190 GET_CALLER_PC_BP_SP; \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000191 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000192}
193
194ASAN_REPORT_ERROR(load, false, 1)
195ASAN_REPORT_ERROR(load, false, 2)
196ASAN_REPORT_ERROR(load, false, 4)
197ASAN_REPORT_ERROR(load, false, 8)
198ASAN_REPORT_ERROR(load, false, 16)
199ASAN_REPORT_ERROR(store, true, 1)
200ASAN_REPORT_ERROR(store, true, 2)
201ASAN_REPORT_ERROR(store, true, 4)
202ASAN_REPORT_ERROR(store, true, 8)
203ASAN_REPORT_ERROR(store, true, 16)
204
205// Force the linker to keep the symbols for various ASan interface functions.
206// We want to keep those in the executable in order to let the instrumented
207// dynamic libraries access the symbol even if it is not used by the executable
208// itself. This should help if the build system is removing dead code at link
209// time.
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000210static NOINLINE void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000211 volatile int fake_condition = 0; // prevent dead condition elimination.
Alexander Potapenko448fe9a2012-08-09 09:46:12 +0000212 // __asan_report_* functions are noreturn, so we need a switch to prevent
213 // the compiler from removing any of them.
214 switch (fake_condition) {
215 case 1: __asan_report_load1(0); break;
216 case 2: __asan_report_load2(0); break;
217 case 3: __asan_report_load4(0); break;
218 case 4: __asan_report_load8(0); break;
219 case 5: __asan_report_load16(0); break;
220 case 6: __asan_report_store1(0); break;
221 case 7: __asan_report_store2(0); break;
222 case 8: __asan_report_store4(0); break;
223 case 9: __asan_report_store8(0); break;
224 case 10: __asan_report_store16(0); break;
225 case 11: __asan_register_global(0, 0, 0); break;
226 case 12: __asan_register_globals(0, 0); break;
227 case 13: __asan_unregister_globals(0, 0); break;
228 case 14: __asan_set_death_callback(0); break;
229 case 15: __asan_set_error_report_callback(0); break;
230 case 16: __asan_handle_no_return(); break;
Alexander Potapenko5a9938d2012-08-09 16:05:17 +0000231 case 17: __asan_address_is_poisoned(0); break;
232 case 18: __asan_get_allocated_size(0); break;
233 case 19: __asan_get_current_allocated_bytes(); break;
234 case 20: __asan_get_estimated_allocated_size(0); break;
235 case 21: __asan_get_free_bytes(); break;
236 case 22: __asan_get_heap_size(); break;
237 case 23: __asan_get_ownership(0); break;
238 case 24: __asan_get_unmapped_bytes(); break;
239 case 25: __asan_poison_memory_region(0, 0); break;
240 case 26: __asan_unpoison_memory_region(0, 0); break;
241 case 27: __asan_set_error_exit_code(0); break;
242 case 28: __asan_stack_free(0, 0, 0); break;
243 case 29: __asan_stack_malloc(0, 0); break;
Alexey Samsonove2430d22012-08-13 14:05:00 +0000244 case 30: __asan_set_on_error_callback(0); break;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000245 }
246}
247
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000248static void asan_atexit() {
Alexey Samsonove9541012012-06-06 13:11:29 +0000249 AsanPrintf("AddressSanitizer exit stats:\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000250 __asan_print_accumulated_stats();
251}
252
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000253} // namespace __asan
254
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000255// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000256using namespace __asan; // NOLINT
257
Alexander Potapenko5a9938d2012-08-09 16:05:17 +0000258int NOINLINE __asan_set_error_exit_code(int exit_code) {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000259 int old = flags()->exitcode;
260 flags()->exitcode = exit_code;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000261 return old;
262}
263
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000264void NOINLINE __asan_handle_no_return() {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000265 int local_stack;
266 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
267 CHECK(curr_thread);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000268 uptr top = curr_thread->stack_top();
269 uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1);
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000270 PoisonShadow(bottom, top - bottom, 0);
271}
272
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000273void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000274 death_callback = callback;
275}
276
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000277void __asan_init() {
278 if (asan_inited) return;
279 asan_init_is_running = true;
280
281 // Make sure we are not statically linked.
282 AsanDoesNotSupportStaticLinkage();
283
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000284 // Initialize flags.
Alexey Samsonov3dbeabb2012-06-14 14:07:21 +0000285 const char *options = GetEnv("ASAN_OPTIONS");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000286 InitializeFlags(flags(), options);
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000287
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000288 if (flags()->verbosity && options) {
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000289 Report("Parsed ASAN_OPTIONS: %s\n", options);
290 }
291
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000292 if (flags()->atexit) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000293 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000294 }
295
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000296 // interceptors
297 InitializeAsanInterceptors();
298
299 ReplaceSystemMalloc();
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000300 ReplaceOperatorsNewAndDelete();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000301
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000302 if (flags()->verbosity) {
Alexey Samsonove4309e82012-06-06 10:54:25 +0000303 Printf("|| `[%p, %p]` || HighMem ||\n",
304 (void*)kHighMemBeg, (void*)kHighMemEnd);
305 Printf("|| `[%p, %p]` || HighShadow ||\n",
306 (void*)kHighShadowBeg, (void*)kHighShadowEnd);
307 Printf("|| `[%p, %p]` || ShadowGap ||\n",
308 (void*)kShadowGapBeg, (void*)kShadowGapEnd);
309 Printf("|| `[%p, %p]` || LowShadow ||\n",
310 (void*)kLowShadowBeg, (void*)kLowShadowEnd);
311 Printf("|| `[%p, %p]` || LowMem ||\n",
312 (void*)kLowMemBeg, (void*)kLowMemEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000313 Printf("MemToShadow(shadow): %p %p %p %p\n",
Alexey Samsonove4309e82012-06-06 10:54:25 +0000314 (void*)MEM_TO_SHADOW(kLowShadowBeg),
315 (void*)MEM_TO_SHADOW(kLowShadowEnd),
316 (void*)MEM_TO_SHADOW(kHighShadowBeg),
317 (void*)MEM_TO_SHADOW(kHighShadowEnd));
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000318 Printf("red_zone=%zu\n", (uptr)flags()->redzone);
319 Printf("malloc_context_size=%zu\n", (uptr)flags()->malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000320
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000321 Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE);
322 Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY);
323 Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000324 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
325 }
326
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000327 if (flags()->disable_core) {
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000328 DisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000329 }
330
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000331 uptr shadow_start = kLowShadowBeg;
332 if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity;
333 uptr shadow_end = kHighShadowEnd;
334 if (MemoryRangeIsAvailable(shadow_start, shadow_end)) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000335 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000336 // mmap the low shadow plus at least one page.
337 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000338 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000339 // mmap the high shadow.
340 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000341 // protect the gap
Alexey Samsonovf607fc12012-06-14 14:42:58 +0000342 void *prot = Mprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000343 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000344 } else {
345 Report("Shadow memory range interleaves with an existing memory mapping. "
346 "ASan cannot proceed correctly. ABORTING.\n");
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000347 DumpProcessMap();
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000348 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000349 }
350
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000351 InstallSignalHandlers();
352
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000353 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
354 // should be set to 1 prior to initializing the threads.
355 asan_inited = 1;
356 asan_init_is_running = false;
357
358 asanThreadRegistry().Init();
359 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000360 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000361
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000362 if (flags()->verbosity) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000363 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000364 }
365}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000366
367#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000368 // On Linux, we force __asan_init to be called before anyone else
369 // by placing it into .preinit_array section.
370 // FIXME: do we have anything like this on Mac?
371 __attribute__((section(".preinit_array")))
372 typeof(__asan_init) *__asan_preinit =__asan_init;
373#elif defined(_WIN32) && defined(_DLL)
374 // On Windows, when using dynamic CRT (/MD), we can put a pointer
375 // to __asan_init into the global list of C initializers.
376 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000377 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000378 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000379#endif