]> git.proxmox.com Git - rustc.git/blob - src/compiler-rt/lib/asan/asan_rtl.cc
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / compiler-rt / lib / asan / asan_rtl.cc
1 //===-- asan_rtl.cc -------------------------------------------------------===//
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
15 #include "asan_activation.h"
16 #include "asan_allocator.h"
17 #include "asan_interceptors.h"
18 #include "asan_interface_internal.h"
19 #include "asan_internal.h"
20 #include "asan_mapping.h"
21 #include "asan_poisoning.h"
22 #include "asan_report.h"
23 #include "asan_stack.h"
24 #include "asan_stats.h"
25 #include "asan_suppressions.h"
26 #include "asan_thread.h"
27 #include "sanitizer_common/sanitizer_atomic.h"
28 #include "sanitizer_common/sanitizer_flags.h"
29 #include "sanitizer_common/sanitizer_libc.h"
30 #include "sanitizer_common/sanitizer_symbolizer.h"
31 #include "lsan/lsan_common.h"
32 #include "ubsan/ubsan_init.h"
33 #include "ubsan/ubsan_platform.h"
34
35 int __asan_option_detect_stack_use_after_return; // Global interface symbol.
36 uptr *__asan_test_only_reported_buggy_pointer; // Used only for testing asan.
37
38 namespace __asan {
39
40 uptr AsanMappingProfile[kAsanMappingProfileSize];
41
42 static void AsanDie() {
43 static atomic_uint32_t num_calls;
44 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
45 // Don't die twice - run a busy loop.
46 while (1) { }
47 }
48 if (flags()->sleep_before_dying) {
49 Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying);
50 SleepForSeconds(flags()->sleep_before_dying);
51 }
52 if (flags()->unmap_shadow_on_exit) {
53 if (kMidMemBeg) {
54 UnmapOrDie((void*)kLowShadowBeg, kMidMemBeg - kLowShadowBeg);
55 UnmapOrDie((void*)kMidMemEnd, kHighShadowEnd - kMidMemEnd);
56 } else {
57 UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
58 }
59 }
60 }
61
62 static void AsanCheckFailed(const char *file, int line, const char *cond,
63 u64 v1, u64 v2) {
64 Report("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", file,
65 line, cond, (uptr)v1, (uptr)v2);
66 // FIXME: check for infinite recursion without a thread-local counter here.
67 PRINT_CURRENT_STACK_CHECK();
68 Die();
69 }
70
71 // -------------------------- Globals --------------------- {{{1
72 int asan_inited;
73 bool asan_init_is_running;
74
75 #if !ASAN_FIXED_MAPPING
76 uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;
77 #endif
78
79 // -------------------------- Misc ---------------- {{{1
80 void ShowStatsAndAbort() {
81 __asan_print_accumulated_stats();
82 Die();
83 }
84
85 // ---------------------- mmap -------------------- {{{1
86 // Reserve memory range [beg, end].
87 // We need to use inclusive range because end+1 may not be representable.
88 void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name) {
89 CHECK_EQ((beg % GetPageSizeCached()), 0);
90 CHECK_EQ(((end + 1) % GetPageSizeCached()), 0);
91 uptr size = end - beg + 1;
92 DecreaseTotalMmap(size); // Don't count the shadow against mmap_limit_mb.
93 void *res = MmapFixedNoReserve(beg, size, name);
94 if (res != (void*)beg) {
95 Report("ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
96 "Perhaps you're using ulimit -v\n", size);
97 Abort();
98 }
99 if (common_flags()->no_huge_pages_for_shadow)
100 NoHugePagesInRegion(beg, size);
101 if (common_flags()->use_madv_dontdump)
102 DontDumpShadowMemory(beg, size);
103 }
104
105 // --------------- LowLevelAllocateCallbac ---------- {{{1
106 static void OnLowLevelAllocate(uptr ptr, uptr size) {
107 PoisonShadow(ptr, size, kAsanInternalHeapMagic);
108 }
109
110 // -------------------------- Run-time entry ------------------- {{{1
111 // exported functions
112 #define ASAN_REPORT_ERROR(type, is_write, size) \
113 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
114 void __asan_report_ ## type ## size(uptr addr) { \
115 GET_CALLER_PC_BP_SP; \
116 __asan_report_error(pc, bp, sp, addr, is_write, size, 0); \
117 } \
118 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
119 void __asan_report_exp_ ## type ## size(uptr addr, u32 exp) { \
120 GET_CALLER_PC_BP_SP; \
121 __asan_report_error(pc, bp, sp, addr, is_write, size, exp); \
122 }
123
124 ASAN_REPORT_ERROR(load, false, 1)
125 ASAN_REPORT_ERROR(load, false, 2)
126 ASAN_REPORT_ERROR(load, false, 4)
127 ASAN_REPORT_ERROR(load, false, 8)
128 ASAN_REPORT_ERROR(load, false, 16)
129 ASAN_REPORT_ERROR(store, true, 1)
130 ASAN_REPORT_ERROR(store, true, 2)
131 ASAN_REPORT_ERROR(store, true, 4)
132 ASAN_REPORT_ERROR(store, true, 8)
133 ASAN_REPORT_ERROR(store, true, 16)
134
135 #define ASAN_REPORT_ERROR_N(type, is_write) \
136 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
137 void __asan_report_ ## type ## _n(uptr addr, uptr size) { \
138 GET_CALLER_PC_BP_SP; \
139 __asan_report_error(pc, bp, sp, addr, is_write, size, 0); \
140 } \
141 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
142 void __asan_report_exp_ ## type ## _n(uptr addr, uptr size, u32 exp) { \
143 GET_CALLER_PC_BP_SP; \
144 __asan_report_error(pc, bp, sp, addr, is_write, size, exp); \
145 }
146
147 ASAN_REPORT_ERROR_N(load, false)
148 ASAN_REPORT_ERROR_N(store, true)
149
150 #define ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp_arg) \
151 uptr sp = MEM_TO_SHADOW(addr); \
152 uptr s = size <= SHADOW_GRANULARITY ? *reinterpret_cast<u8 *>(sp) \
153 : *reinterpret_cast<u16 *>(sp); \
154 if (UNLIKELY(s)) { \
155 if (UNLIKELY(size >= SHADOW_GRANULARITY || \
156 ((s8)((addr & (SHADOW_GRANULARITY - 1)) + size - 1)) >= \
157 (s8)s)) { \
158 if (__asan_test_only_reported_buggy_pointer) { \
159 *__asan_test_only_reported_buggy_pointer = addr; \
160 } else { \
161 GET_CALLER_PC_BP_SP; \
162 __asan_report_error(pc, bp, sp, addr, is_write, size, exp_arg); \
163 } \
164 } \
165 }
166
167 #define ASAN_MEMORY_ACCESS_CALLBACK(type, is_write, size) \
168 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
169 void __asan_##type##size(uptr addr) { \
170 ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, 0) \
171 } \
172 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
173 void __asan_exp_##type##size(uptr addr, u32 exp) { \
174 ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp) \
175 }
176
177 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 1)
178 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 2)
179 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 4)
180 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 8)
181 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 16)
182 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 1)
183 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 2)
184 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 4)
185 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 8)
186 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 16)
187
188 extern "C"
189 NOINLINE INTERFACE_ATTRIBUTE
190 void __asan_loadN(uptr addr, uptr size) {
191 if (__asan_region_is_poisoned(addr, size)) {
192 GET_CALLER_PC_BP_SP;
193 __asan_report_error(pc, bp, sp, addr, false, size, 0);
194 }
195 }
196
197 extern "C"
198 NOINLINE INTERFACE_ATTRIBUTE
199 void __asan_exp_loadN(uptr addr, uptr size, u32 exp) {
200 if (__asan_region_is_poisoned(addr, size)) {
201 GET_CALLER_PC_BP_SP;
202 __asan_report_error(pc, bp, sp, addr, false, size, exp);
203 }
204 }
205
206 extern "C"
207 NOINLINE INTERFACE_ATTRIBUTE
208 void __asan_storeN(uptr addr, uptr size) {
209 if (__asan_region_is_poisoned(addr, size)) {
210 GET_CALLER_PC_BP_SP;
211 __asan_report_error(pc, bp, sp, addr, true, size, 0);
212 }
213 }
214
215 extern "C"
216 NOINLINE INTERFACE_ATTRIBUTE
217 void __asan_exp_storeN(uptr addr, uptr size, u32 exp) {
218 if (__asan_region_is_poisoned(addr, size)) {
219 GET_CALLER_PC_BP_SP;
220 __asan_report_error(pc, bp, sp, addr, true, size, exp);
221 }
222 }
223
224 // Force the linker to keep the symbols for various ASan interface functions.
225 // We want to keep those in the executable in order to let the instrumented
226 // dynamic libraries access the symbol even if it is not used by the executable
227 // itself. This should help if the build system is removing dead code at link
228 // time.
229 static NOINLINE void force_interface_symbols() {
230 volatile int fake_condition = 0; // prevent dead condition elimination.
231 // __asan_report_* functions are noreturn, so we need a switch to prevent
232 // the compiler from removing any of them.
233 switch (fake_condition) {
234 case 1: __asan_report_load1(0); break;
235 case 2: __asan_report_load2(0); break;
236 case 3: __asan_report_load4(0); break;
237 case 4: __asan_report_load8(0); break;
238 case 5: __asan_report_load16(0); break;
239 case 6: __asan_report_load_n(0, 0); break;
240 case 7: __asan_report_store1(0); break;
241 case 8: __asan_report_store2(0); break;
242 case 9: __asan_report_store4(0); break;
243 case 10: __asan_report_store8(0); break;
244 case 11: __asan_report_store16(0); break;
245 case 12: __asan_report_store_n(0, 0); break;
246 case 13: __asan_report_exp_load1(0, 0); break;
247 case 14: __asan_report_exp_load2(0, 0); break;
248 case 15: __asan_report_exp_load4(0, 0); break;
249 case 16: __asan_report_exp_load8(0, 0); break;
250 case 17: __asan_report_exp_load16(0, 0); break;
251 case 18: __asan_report_exp_load_n(0, 0, 0); break;
252 case 19: __asan_report_exp_store1(0, 0); break;
253 case 20: __asan_report_exp_store2(0, 0); break;
254 case 21: __asan_report_exp_store4(0, 0); break;
255 case 22: __asan_report_exp_store8(0, 0); break;
256 case 23: __asan_report_exp_store16(0, 0); break;
257 case 24: __asan_report_exp_store_n(0, 0, 0); break;
258 case 25: __asan_register_globals(nullptr, 0); break;
259 case 26: __asan_unregister_globals(nullptr, 0); break;
260 case 27: __asan_set_death_callback(nullptr); break;
261 case 28: __asan_set_error_report_callback(nullptr); break;
262 case 29: __asan_handle_no_return(); break;
263 case 30: __asan_address_is_poisoned(nullptr); break;
264 case 31: __asan_poison_memory_region(nullptr, 0); break;
265 case 32: __asan_unpoison_memory_region(nullptr, 0); break;
266 case 34: __asan_before_dynamic_init(nullptr); break;
267 case 35: __asan_after_dynamic_init(); break;
268 case 36: __asan_poison_stack_memory(0, 0); break;
269 case 37: __asan_unpoison_stack_memory(0, 0); break;
270 case 38: __asan_region_is_poisoned(0, 0); break;
271 case 39: __asan_describe_address(0); break;
272 }
273 }
274
275 static void asan_atexit() {
276 Printf("AddressSanitizer exit stats:\n");
277 __asan_print_accumulated_stats();
278 // Print AsanMappingProfile.
279 for (uptr i = 0; i < kAsanMappingProfileSize; i++) {
280 if (AsanMappingProfile[i] == 0) continue;
281 Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]);
282 }
283 }
284
285 static void InitializeHighMemEnd() {
286 #if !ASAN_FIXED_MAPPING
287 kHighMemEnd = GetMaxVirtualAddress();
288 // Increase kHighMemEnd to make sure it's properly
289 // aligned together with kHighMemBeg:
290 kHighMemEnd |= SHADOW_GRANULARITY * GetPageSizeCached() - 1;
291 #endif // !ASAN_FIXED_MAPPING
292 CHECK_EQ((kHighMemBeg % GetPageSizeCached()), 0);
293 }
294
295 static void ProtectGap(uptr addr, uptr size) {
296 void *res = MmapNoAccess(addr, size, "shadow gap");
297 if (addr == (uptr)res)
298 return;
299 // A few pages at the start of the address space can not be protected.
300 // But we really want to protect as much as possible, to prevent this memory
301 // being returned as a result of a non-FIXED mmap().
302 if (addr == kZeroBaseShadowStart) {
303 uptr step = GetPageSizeCached();
304 while (size > step && addr < kZeroBaseMaxShadowStart) {
305 addr += step;
306 size -= step;
307 void *res = MmapNoAccess(addr, size, "shadow gap");
308 if (addr == (uptr)res)
309 return;
310 }
311 }
312
313 Report("ERROR: Failed to protect the shadow gap. "
314 "ASan cannot proceed correctly. ABORTING.\n");
315 DumpProcessMap();
316 Die();
317 }
318
319 static void PrintAddressSpaceLayout() {
320 Printf("|| `[%p, %p]` || HighMem ||\n",
321 (void*)kHighMemBeg, (void*)kHighMemEnd);
322 Printf("|| `[%p, %p]` || HighShadow ||\n",
323 (void*)kHighShadowBeg, (void*)kHighShadowEnd);
324 if (kMidMemBeg) {
325 Printf("|| `[%p, %p]` || ShadowGap3 ||\n",
326 (void*)kShadowGap3Beg, (void*)kShadowGap3End);
327 Printf("|| `[%p, %p]` || MidMem ||\n",
328 (void*)kMidMemBeg, (void*)kMidMemEnd);
329 Printf("|| `[%p, %p]` || ShadowGap2 ||\n",
330 (void*)kShadowGap2Beg, (void*)kShadowGap2End);
331 Printf("|| `[%p, %p]` || MidShadow ||\n",
332 (void*)kMidShadowBeg, (void*)kMidShadowEnd);
333 }
334 Printf("|| `[%p, %p]` || ShadowGap ||\n",
335 (void*)kShadowGapBeg, (void*)kShadowGapEnd);
336 if (kLowShadowBeg) {
337 Printf("|| `[%p, %p]` || LowShadow ||\n",
338 (void*)kLowShadowBeg, (void*)kLowShadowEnd);
339 Printf("|| `[%p, %p]` || LowMem ||\n",
340 (void*)kLowMemBeg, (void*)kLowMemEnd);
341 }
342 Printf("MemToShadow(shadow): %p %p %p %p",
343 (void*)MEM_TO_SHADOW(kLowShadowBeg),
344 (void*)MEM_TO_SHADOW(kLowShadowEnd),
345 (void*)MEM_TO_SHADOW(kHighShadowBeg),
346 (void*)MEM_TO_SHADOW(kHighShadowEnd));
347 if (kMidMemBeg) {
348 Printf(" %p %p",
349 (void*)MEM_TO_SHADOW(kMidShadowBeg),
350 (void*)MEM_TO_SHADOW(kMidShadowEnd));
351 }
352 Printf("\n");
353 Printf("redzone=%zu\n", (uptr)flags()->redzone);
354 Printf("max_redzone=%zu\n", (uptr)flags()->max_redzone);
355 Printf("quarantine_size_mb=%zuM\n", (uptr)flags()->quarantine_size_mb);
356 Printf("malloc_context_size=%zu\n",
357 (uptr)common_flags()->malloc_context_size);
358
359 Printf("SHADOW_SCALE: %d\n", (int)SHADOW_SCALE);
360 Printf("SHADOW_GRANULARITY: %d\n", (int)SHADOW_GRANULARITY);
361 Printf("SHADOW_OFFSET: 0x%zx\n", (uptr)SHADOW_OFFSET);
362 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
363 if (kMidMemBeg)
364 CHECK(kMidShadowBeg > kLowShadowEnd &&
365 kMidMemBeg > kMidShadowEnd &&
366 kHighShadowBeg > kMidMemEnd);
367 }
368
369 static void AsanInitInternal() {
370 if (LIKELY(asan_inited)) return;
371 SanitizerToolName = "AddressSanitizer";
372 CHECK(!asan_init_is_running && "ASan init calls itself!");
373 asan_init_is_running = true;
374
375 CacheBinaryName();
376
377 // Initialize flags. This must be done early, because most of the
378 // initialization steps look at flags().
379 InitializeFlags();
380
381 CheckVMASize();
382
383 AsanCheckIncompatibleRT();
384 AsanCheckDynamicRTPrereqs();
385
386 SetCanPoisonMemory(flags()->poison_heap);
387 SetMallocContextSize(common_flags()->malloc_context_size);
388
389 InitializeHighMemEnd();
390
391 // Make sure we are not statically linked.
392 AsanDoesNotSupportStaticLinkage();
393
394 // Install tool-specific callbacks in sanitizer_common.
395 AddDieCallback(AsanDie);
396 SetCheckFailedCallback(AsanCheckFailed);
397 SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
398
399 __sanitizer_set_report_path(common_flags()->log_path);
400
401 // Enable UAR detection, if required.
402 __asan_option_detect_stack_use_after_return =
403 flags()->detect_stack_use_after_return;
404
405 // Re-exec ourselves if we need to set additional env or command line args.
406 MaybeReexec();
407
408 // Setup internal allocator callback.
409 SetLowLevelAllocateCallback(OnLowLevelAllocate);
410
411 InitializeAsanInterceptors();
412
413 // Enable system log ("adb logcat") on Android.
414 // Doing this before interceptors are initialized crashes in:
415 // AsanInitInternal -> android_log_write -> __interceptor_strcmp
416 AndroidLogInit();
417
418 ReplaceSystemMalloc();
419
420 uptr shadow_start = kLowShadowBeg;
421 if (kLowShadowBeg)
422 shadow_start -= GetMmapGranularity();
423 bool full_shadow_is_available =
424 MemoryRangeIsAvailable(shadow_start, kHighShadowEnd);
425
426 #if SANITIZER_LINUX && defined(__x86_64__) && defined(_LP64) && \
427 !ASAN_FIXED_MAPPING
428 if (!full_shadow_is_available) {
429 kMidMemBeg = kLowMemEnd < 0x3000000000ULL ? 0x3000000000ULL : 0;
430 kMidMemEnd = kLowMemEnd < 0x3000000000ULL ? 0x4fffffffffULL : 0;
431 }
432 #endif
433
434 if (Verbosity()) PrintAddressSpaceLayout();
435
436 DisableCoreDumperIfNecessary();
437
438 if (full_shadow_is_available) {
439 // mmap the low shadow plus at least one page at the left.
440 if (kLowShadowBeg)
441 ReserveShadowMemoryRange(shadow_start, kLowShadowEnd, "low shadow");
442 // mmap the high shadow.
443 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd, "high shadow");
444 // protect the gap.
445 ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
446 CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);
447 } else if (kMidMemBeg &&
448 MemoryRangeIsAvailable(shadow_start, kMidMemBeg - 1) &&
449 MemoryRangeIsAvailable(kMidMemEnd + 1, kHighShadowEnd)) {
450 CHECK(kLowShadowBeg != kLowShadowEnd);
451 // mmap the low shadow plus at least one page at the left.
452 ReserveShadowMemoryRange(shadow_start, kLowShadowEnd, "low shadow");
453 // mmap the mid shadow.
454 ReserveShadowMemoryRange(kMidShadowBeg, kMidShadowEnd, "mid shadow");
455 // mmap the high shadow.
456 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd, "high shadow");
457 // protect the gaps.
458 ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
459 ProtectGap(kShadowGap2Beg, kShadowGap2End - kShadowGap2Beg + 1);
460 ProtectGap(kShadowGap3Beg, kShadowGap3End - kShadowGap3Beg + 1);
461 } else {
462 Report("Shadow memory range interleaves with an existing memory mapping. "
463 "ASan cannot proceed correctly. ABORTING.\n");
464 Report("ASan shadow was supposed to be located in the [%p-%p] range.\n",
465 shadow_start, kHighShadowEnd);
466 DumpProcessMap();
467 Die();
468 }
469
470 AsanTSDInit(PlatformTSDDtor);
471 InstallDeadlySignalHandlers(AsanOnDeadlySignal);
472
473 AllocatorOptions allocator_options;
474 allocator_options.SetFrom(flags(), common_flags());
475 InitializeAllocator(allocator_options);
476
477 MaybeStartBackgroudThread();
478 SetSoftRssLimitExceededCallback(AsanSoftRssLimitExceededCallback);
479
480 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
481 // should be set to 1 prior to initializing the threads.
482 asan_inited = 1;
483 asan_init_is_running = false;
484
485 if (flags()->atexit)
486 Atexit(asan_atexit);
487
488 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
489
490 // Now that ASan runtime is (mostly) initialized, deactivate it if
491 // necessary, so that it can be re-activated when requested.
492 if (flags()->start_deactivated)
493 AsanDeactivate();
494
495 // interceptors
496 InitTlsSize();
497
498 // Create main thread.
499 AsanThread *main_thread = AsanThread::Create(
500 /* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ 0,
501 /* stack */ nullptr, /* detached */ true);
502 CHECK_EQ(0, main_thread->tid());
503 SetCurrentThread(main_thread);
504 main_thread->ThreadStart(internal_getpid(),
505 /* signal_thread_is_registered */ nullptr);
506 force_interface_symbols(); // no-op.
507 SanitizerInitializeUnwinder();
508
509 #if CAN_SANITIZE_LEAKS
510 __lsan::InitCommonLsan();
511 if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) {
512 Atexit(__lsan::DoLeakCheck);
513 }
514 #endif // CAN_SANITIZE_LEAKS
515
516 #if CAN_SANITIZE_UB
517 __ubsan::InitAsPlugin();
518 #endif
519
520 InitializeSuppressions();
521
522 VReport(1, "AddressSanitizer Init done\n");
523 }
524
525 // Initialize as requested from some part of ASan runtime library (interceptors,
526 // allocator, etc).
527 void AsanInitFromRtl() {
528 AsanInitInternal();
529 }
530
531 #if ASAN_DYNAMIC
532 // Initialize runtime in case it's LD_PRELOAD-ed into unsanitized executable
533 // (and thus normal initializers from .preinit_array or modules haven't run).
534
535 class AsanInitializer {
536 public: // NOLINT
537 AsanInitializer() {
538 AsanInitFromRtl();
539 }
540 };
541
542 static AsanInitializer asan_initializer;
543 #endif // ASAN_DYNAMIC
544
545 } // namespace __asan
546
547 // ---------------------- Interface ---------------- {{{1
548 using namespace __asan; // NOLINT
549
550 void NOINLINE __asan_handle_no_return() {
551 int local_stack;
552 AsanThread *curr_thread = GetCurrentThread();
553 uptr PageSize = GetPageSizeCached();
554 uptr top, bottom;
555 if (curr_thread) {
556 top = curr_thread->stack_top();
557 bottom = ((uptr)&local_stack - PageSize) & ~(PageSize - 1);
558 } else {
559 // If we haven't seen this thread, try asking the OS for stack bounds.
560 uptr tls_addr, tls_size, stack_size;
561 GetThreadStackAndTls(/*main=*/false, &bottom, &stack_size, &tls_addr,
562 &tls_size);
563 top = bottom + stack_size;
564 }
565 static const uptr kMaxExpectedCleanupSize = 64 << 20; // 64M
566 if (top - bottom > kMaxExpectedCleanupSize) {
567 static bool reported_warning = false;
568 if (reported_warning)
569 return;
570 reported_warning = true;
571 Report("WARNING: ASan is ignoring requested __asan_handle_no_return: "
572 "stack top: %p; bottom %p; size: %p (%zd)\n"
573 "False positive error reports may follow\n"
574 "For details see "
575 "http://code.google.com/p/address-sanitizer/issues/detail?id=189\n",
576 top, bottom, top - bottom, top - bottom);
577 return;
578 }
579 PoisonShadow(bottom, top - bottom, 0);
580 if (curr_thread && curr_thread->has_fake_stack())
581 curr_thread->fake_stack()->HandleNoReturn();
582 }
583
584 void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
585 SetUserDieCallback(callback);
586 }
587
588 // Initialize as requested from instrumented application code.
589 // We use this call as a trigger to wake up ASan from deactivated state.
590 void __asan_init() {
591 AsanActivate();
592 AsanInitInternal();
593 }
594
595 void __asan_version_mismatch_check() {
596 // Do nothing.
597 }