asan_rtl.cc revision 0b694fcab9b2f33bdd6691cbea4e80a5c27191b1
146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)//===-- asan_rtl.cc -------------------------------------------------------===//
246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)//
346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)//
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// License. See LICENSE.TXT for details.
746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)//
846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)//===----------------------------------------------------------------------===//
946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)//
1046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// This file is a part of AddressSanitizer, an address sanity checker.
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)//
1246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Main file of the ASan run-time library.
1346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)//===----------------------------------------------------------------------===//
1446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "asan_allocator.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "asan_interceptors.h"
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "asan_internal.h"
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "asan_mapping.h"
1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "asan_poisoning.h"
1946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "asan_report.h"
2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "asan_stack.h"
2146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "asan_stats.h"
2246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "asan_thread.h"
2346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "sanitizer_common/sanitizer_atomic.h"
2446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "sanitizer_common/sanitizer_flags.h"
2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "sanitizer_common/sanitizer_libc.h"
2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "sanitizer_common/sanitizer_symbolizer.h"
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace __asan {
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)uptr AsanMappingProfile[kAsanMappingProfileSize];
316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)static void AsanDie() {
336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  static atomic_uint32_t num_calls;
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // Don't die twice - run a busy loop.
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    while (1) { }
3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (flags()->sleep_before_dying) {
3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying);
4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    SleepForSeconds(flags()->sleep_before_dying);
4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
4246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (flags()->unmap_shadow_on_exit) {
4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (kMidMemBeg) {
4446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      UnmapOrDie((void*)kLowShadowBeg, kMidMemBeg - kLowShadowBeg);
4546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      UnmapOrDie((void*)kMidMemEnd, kHighShadowEnd - kMidMemEnd);
4646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    } else {
4746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
4846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    }
4946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
5046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (death_callback)
5146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    death_callback();
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (flags()->abort_on_error)
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    Abort();
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  internal__exit(flags()->exitcode);
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
5646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)static void AsanCheckFailed(const char *file, int line, const char *cond,
5846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                            u64 v1, u64 v2) {
5946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  Report("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n",
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)             file, line, cond, (uptr)v1, (uptr)v2);
6146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // FIXME: check for infinite recursion without a thread-local counter here.
6246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  PRINT_CURRENT_STACK();
6346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  Die();
6446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
6546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
6646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// -------------------------- Flags ------------------------- {{{1
6746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)static const int kDefaultMallocContextSize = 30;
6846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
6946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)Flags asan_flags_dont_use_directly;  // use via flags().
7046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
7146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)static const char *MaybeCallAsanDefaultOptions() {
726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return (&__asan_default_options) ? __asan_default_options() : "";
7346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
7446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
7546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)static const char *MaybeUseAsanDefaultOptionsCompileDefiniton() {
7646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#ifdef ASAN_DEFAULT_OPTIONS
7746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Stringize the macro value.
7846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)# define ASAN_STRINGIZE(x) #x
796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)# define ASAN_STRINGIZE_OPTIONS(options) ASAN_STRINGIZE(options)
8046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return ASAN_STRINGIZE_OPTIONS(ASAN_DEFAULT_OPTIONS);
8146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#else
8246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return "";
8346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif
846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)static void ParseFlagsFromString(Flags *f, const char *str) {
876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ParseCommonFlagsFromString(str);
886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  CHECK((uptr)common_flags()->malloc_context_size <= kStackTraceMax);
8946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
9046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->quarantine_size, "quarantine_size");
9146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->verbosity, "verbosity");
9246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->redzone, "redzone");
9346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CHECK_GE(f->redzone, 16);
9446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CHECK(IsPowerOfTwo(f->redzone));
9546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
9646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->debug, "debug");
9746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->report_globals, "report_globals");
9846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->check_initialization_order, "check_initialization_order");
9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
10046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->replace_str, "replace_str");
10146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->replace_intrin, "replace_intrin");
10246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free");
10346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->use_fake_stack, "use_fake_stack");
10446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size");
10546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->malloc_fill_byte, "malloc_fill_byte");
10646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ParseFlag(str, &f->exitcode, "exitcode");
107  ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning");
108  ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying");
109  ParseFlag(str, &f->handle_segv, "handle_segv");
110  ParseFlag(str, &f->allow_user_segv_handler, "allow_user_segv_handler");
111  ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack");
112  ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size");
113  ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit");
114  ParseFlag(str, &f->abort_on_error, "abort_on_error");
115  ParseFlag(str, &f->print_stats, "print_stats");
116  ParseFlag(str, &f->print_legend, "print_legend");
117  ParseFlag(str, &f->atexit, "atexit");
118  ParseFlag(str, &f->disable_core, "disable_core");
119  ParseFlag(str, &f->allow_reexec, "allow_reexec");
120  ParseFlag(str, &f->print_full_thread_history, "print_full_thread_history");
121  ParseFlag(str, &f->log_path, "log_path");
122  ParseFlag(str, &f->poison_heap, "poison_heap");
123  ParseFlag(str, &f->alloc_dealloc_mismatch, "alloc_dealloc_mismatch");
124  ParseFlag(str, &f->use_stack_depot, "use_stack_depot");
125  ParseFlag(str, &f->strict_memcmp, "strict_memcmp");
126  ParseFlag(str, &f->strict_init_order, "strict_init_order");
127}
128
129void InitializeFlags(Flags *f, const char *env) {
130  CommonFlags *cf = common_flags();
131  cf->external_symbolizer_path = GetEnv("ASAN_SYMBOLIZER_PATH");
132  cf->symbolize = true;
133  cf->malloc_context_size = kDefaultMallocContextSize;
134  cf->fast_unwind_on_fatal = false;
135  cf->fast_unwind_on_malloc = true;
136  cf->strip_path_prefix = "";
137
138  internal_memset(f, 0, sizeof(*f));
139  f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 26 : 1UL << 28;
140  f->verbosity = 0;
141  f->redzone = 16;
142  f->debug = false;
143  f->report_globals = 1;
144  f->check_initialization_order = false;
145  f->replace_str = true;
146  f->replace_intrin = true;
147  f->mac_ignore_invalid_free = false;
148  f->use_fake_stack = true;
149  f->max_malloc_fill_size = 0x1000;  // By default, fill only the first 4K.
150  f->malloc_fill_byte = 0xbe;
151  f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
152  f->allow_user_poisoning = true;
153  f->sleep_before_dying = 0;
154  f->handle_segv = ASAN_NEEDS_SEGV;
155  f->allow_user_segv_handler = false;
156  f->use_sigaltstack = false;
157  f->check_malloc_usable_size = true;
158  f->unmap_shadow_on_exit = false;
159  f->abort_on_error = false;
160  f->print_stats = false;
161  f->print_legend = true;
162  f->atexit = false;
163  f->disable_core = (SANITIZER_WORDSIZE == 64);
164  f->allow_reexec = true;
165  f->print_full_thread_history = true;
166  f->log_path = 0;
167  f->poison_heap = true;
168  // Turn off alloc/dealloc mismatch checker on Mac for now.
169  // TODO(glider): Fix known issues and enable this back.
170  f->alloc_dealloc_mismatch = (SANITIZER_MAC == 0);;
171  f->use_stack_depot = true;
172  f->strict_memcmp = true;
173  f->strict_init_order = false;
174
175  // Override from compile definition.
176  ParseFlagsFromString(f, MaybeUseAsanDefaultOptionsCompileDefiniton());
177
178  // Override from user-specified string.
179  ParseFlagsFromString(f, MaybeCallAsanDefaultOptions());
180  if (flags()->verbosity) {
181    Report("Using the defaults from __asan_default_options: %s\n",
182           MaybeCallAsanDefaultOptions());
183  }
184
185  // Override from command line.
186  ParseFlagsFromString(f, env);
187}
188
189// -------------------------- Globals --------------------- {{{1
190int asan_inited;
191bool asan_init_is_running;
192void (*death_callback)(void);
193
194#if !ASAN_FIXED_MAPPING
195uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;
196#endif
197
198// -------------------------- Misc ---------------- {{{1
199void ShowStatsAndAbort() {
200  __asan_print_accumulated_stats();
201  Die();
202}
203
204// ---------------------- mmap -------------------- {{{1
205// Reserve memory range [beg, end].
206static void ReserveShadowMemoryRange(uptr beg, uptr end) {
207  CHECK_EQ((beg % GetPageSizeCached()), 0);
208  CHECK_EQ(((end + 1) % GetPageSizeCached()), 0);
209  uptr size = end - beg + 1;
210  void *res = MmapFixedNoReserve(beg, size);
211  if (res != (void*)beg) {
212    Report("ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
213           "Perhaps you're using ulimit -v\n", size);
214    Abort();
215  }
216}
217
218// --------------- LowLevelAllocateCallbac ---------- {{{1
219static void OnLowLevelAllocate(uptr ptr, uptr size) {
220  PoisonShadow(ptr, size, kAsanInternalHeapMagic);
221}
222
223// -------------------------- Run-time entry ------------------- {{{1
224// exported functions
225#define ASAN_REPORT_ERROR(type, is_write, size)                     \
226extern "C" NOINLINE INTERFACE_ATTRIBUTE                        \
227void __asan_report_ ## type ## size(uptr addr);                \
228void __asan_report_ ## type ## size(uptr addr) {               \
229  GET_CALLER_PC_BP_SP;                                              \
230  __asan_report_error(pc, bp, sp, addr, is_write, size);            \
231}
232
233ASAN_REPORT_ERROR(load, false, 1)
234ASAN_REPORT_ERROR(load, false, 2)
235ASAN_REPORT_ERROR(load, false, 4)
236ASAN_REPORT_ERROR(load, false, 8)
237ASAN_REPORT_ERROR(load, false, 16)
238ASAN_REPORT_ERROR(store, true, 1)
239ASAN_REPORT_ERROR(store, true, 2)
240ASAN_REPORT_ERROR(store, true, 4)
241ASAN_REPORT_ERROR(store, true, 8)
242ASAN_REPORT_ERROR(store, true, 16)
243
244#define ASAN_REPORT_ERROR_N(type, is_write)                    \
245extern "C" NOINLINE INTERFACE_ATTRIBUTE                        \
246void __asan_report_ ## type ## _n(uptr addr, uptr size);       \
247void __asan_report_ ## type ## _n(uptr addr, uptr size) {      \
248  GET_CALLER_PC_BP_SP;                                         \
249  __asan_report_error(pc, bp, sp, addr, is_write, size);       \
250}
251
252ASAN_REPORT_ERROR_N(load, false)
253ASAN_REPORT_ERROR_N(store, true)
254
255// Force the linker to keep the symbols for various ASan interface functions.
256// We want to keep those in the executable in order to let the instrumented
257// dynamic libraries access the symbol even if it is not used by the executable
258// itself. This should help if the build system is removing dead code at link
259// time.
260static NOINLINE void force_interface_symbols() {
261  volatile int fake_condition = 0;  // prevent dead condition elimination.
262  // __asan_report_* functions are noreturn, so we need a switch to prevent
263  // the compiler from removing any of them.
264  switch (fake_condition) {
265    case 1: __asan_report_load1(0); break;
266    case 2: __asan_report_load2(0); break;
267    case 3: __asan_report_load4(0); break;
268    case 4: __asan_report_load8(0); break;
269    case 5: __asan_report_load16(0); break;
270    case 6: __asan_report_store1(0); break;
271    case 7: __asan_report_store2(0); break;
272    case 8: __asan_report_store4(0); break;
273    case 9: __asan_report_store8(0); break;
274    case 10: __asan_report_store16(0); break;
275    case 12: __asan_register_globals(0, 0); break;
276    case 13: __asan_unregister_globals(0, 0); break;
277    case 14: __asan_set_death_callback(0); break;
278    case 15: __asan_set_error_report_callback(0); break;
279    case 16: __asan_handle_no_return(); break;
280    case 17: __asan_address_is_poisoned(0); break;
281    case 18: __asan_get_allocated_size(0); break;
282    case 19: __asan_get_current_allocated_bytes(); break;
283    case 20: __asan_get_estimated_allocated_size(0); break;
284    case 21: __asan_get_free_bytes(); break;
285    case 22: __asan_get_heap_size(); break;
286    case 23: __asan_get_ownership(0); break;
287    case 24: __asan_get_unmapped_bytes(); break;
288    case 25: __asan_poison_memory_region(0, 0); break;
289    case 26: __asan_unpoison_memory_region(0, 0); break;
290    case 27: __asan_set_error_exit_code(0); break;
291    case 28: __asan_stack_free(0, 0, 0); break;
292    case 29: __asan_stack_malloc(0, 0); break;
293    case 30: __asan_before_dynamic_init(0); break;
294    case 31: __asan_after_dynamic_init(); break;
295    case 32: __asan_poison_stack_memory(0, 0); break;
296    case 33: __asan_unpoison_stack_memory(0, 0); break;
297    case 34: __asan_region_is_poisoned(0, 0); break;
298    case 35: __asan_describe_address(0); break;
299  }
300}
301
302static void asan_atexit() {
303  Printf("AddressSanitizer exit stats:\n");
304  __asan_print_accumulated_stats();
305  // Print AsanMappingProfile.
306  for (uptr i = 0; i < kAsanMappingProfileSize; i++) {
307    if (AsanMappingProfile[i] == 0) continue;
308    Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]);
309  }
310}
311
312static void InitializeHighMemEnd() {
313#if !ASAN_FIXED_MAPPING
314#if SANITIZER_WORDSIZE == 64
315# if defined(__powerpc64__)
316  // FIXME:
317  // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
318  // We somehow need to figure our which one we are using now and choose
319  // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
320  // Note that with 'ulimit -s unlimited' the stack is moved away from the top
321  // of the address space, so simply checking the stack address is not enough.
322  kHighMemEnd = (1ULL << 44) - 1;  // 0x00000fffffffffffUL
323# else
324  kHighMemEnd = (1ULL << 47) - 1;  // 0x00007fffffffffffUL;
325# endif
326#else  // SANITIZER_WORDSIZE == 32
327  kHighMemEnd = (1ULL << 32) - 1;  // 0xffffffff;
328#endif  // SANITIZER_WORDSIZE
329#endif  // !ASAN_FIXED_MAPPING
330}
331
332static void ProtectGap(uptr a, uptr size) {
333  CHECK_EQ(a, (uptr)Mprotect(a, size));
334}
335
336static void PrintAddressSpaceLayout() {
337  Printf("|| `[%p, %p]` || HighMem    ||\n",
338         (void*)kHighMemBeg, (void*)kHighMemEnd);
339  Printf("|| `[%p, %p]` || HighShadow ||\n",
340         (void*)kHighShadowBeg, (void*)kHighShadowEnd);
341  if (kMidMemBeg) {
342    Printf("|| `[%p, %p]` || ShadowGap3 ||\n",
343           (void*)kShadowGap3Beg, (void*)kShadowGap3End);
344    Printf("|| `[%p, %p]` || MidMem     ||\n",
345           (void*)kMidMemBeg, (void*)kMidMemEnd);
346    Printf("|| `[%p, %p]` || ShadowGap2 ||\n",
347           (void*)kShadowGap2Beg, (void*)kShadowGap2End);
348    Printf("|| `[%p, %p]` || MidShadow  ||\n",
349           (void*)kMidShadowBeg, (void*)kMidShadowEnd);
350  }
351  Printf("|| `[%p, %p]` || ShadowGap  ||\n",
352         (void*)kShadowGapBeg, (void*)kShadowGapEnd);
353  if (kLowShadowBeg) {
354    Printf("|| `[%p, %p]` || LowShadow  ||\n",
355           (void*)kLowShadowBeg, (void*)kLowShadowEnd);
356    Printf("|| `[%p, %p]` || LowMem     ||\n",
357           (void*)kLowMemBeg, (void*)kLowMemEnd);
358  }
359  Printf("MemToShadow(shadow): %p %p %p %p",
360         (void*)MEM_TO_SHADOW(kLowShadowBeg),
361         (void*)MEM_TO_SHADOW(kLowShadowEnd),
362         (void*)MEM_TO_SHADOW(kHighShadowBeg),
363         (void*)MEM_TO_SHADOW(kHighShadowEnd));
364  if (kMidMemBeg) {
365    Printf(" %p %p",
366           (void*)MEM_TO_SHADOW(kMidShadowBeg),
367           (void*)MEM_TO_SHADOW(kMidShadowEnd));
368  }
369  Printf("\n");
370  Printf("red_zone=%zu\n", (uptr)flags()->redzone);
371  Printf("malloc_context_size=%zu\n",
372         (uptr)common_flags()->malloc_context_size);
373
374  Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE);
375  Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY);
376  Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET);
377  CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
378  if (kMidMemBeg)
379    CHECK(kMidShadowBeg > kLowShadowEnd &&
380          kMidMemBeg > kMidShadowEnd &&
381          kHighShadowBeg > kMidMemEnd);
382}
383
384}  // namespace __asan
385
386// ---------------------- Interface ---------------- {{{1
387using namespace __asan;  // NOLINT
388
389#if !SANITIZER_SUPPORTS_WEAK_HOOKS
390extern "C" {
391SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
392const char* __asan_default_options() { return ""; }
393}  // extern "C"
394#endif
395
396int NOINLINE __asan_set_error_exit_code(int exit_code) {
397  int old = flags()->exitcode;
398  flags()->exitcode = exit_code;
399  return old;
400}
401
402void NOINLINE __asan_handle_no_return() {
403  int local_stack;
404  AsanThread *curr_thread = GetCurrentThread();
405  CHECK(curr_thread);
406  uptr PageSize = GetPageSizeCached();
407  uptr top = curr_thread->stack_top();
408  uptr bottom = ((uptr)&local_stack - PageSize) & ~(PageSize-1);
409  PoisonShadow(bottom, top - bottom, 0);
410}
411
412void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
413  death_callback = callback;
414}
415
416void __asan_init() {
417  if (asan_inited) return;
418  SanitizerToolName = "AddressSanitizer";
419  CHECK(!asan_init_is_running && "ASan init calls itself!");
420  asan_init_is_running = true;
421  InitializeHighMemEnd();
422
423  // Make sure we are not statically linked.
424  AsanDoesNotSupportStaticLinkage();
425
426  // Install tool-specific callbacks in sanitizer_common.
427  SetDieCallback(AsanDie);
428  SetCheckFailedCallback(AsanCheckFailed);
429  SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
430
431  // Initialize flags. This must be done early, because most of the
432  // initialization steps look at flags().
433  const char *options = GetEnv("ASAN_OPTIONS");
434  InitializeFlags(flags(), options);
435  __sanitizer_set_report_path(flags()->log_path);
436
437  if (flags()->verbosity && options) {
438    Report("Parsed ASAN_OPTIONS: %s\n", options);
439  }
440
441  // Re-exec ourselves if we need to set additional env or command line args.
442  MaybeReexec();
443
444  // Setup internal allocator callback.
445  SetLowLevelAllocateCallback(OnLowLevelAllocate);
446
447  if (flags()->atexit) {
448    Atexit(asan_atexit);
449  }
450
451  // interceptors
452  InitializeAsanInterceptors();
453
454  ReplaceSystemMalloc();
455  ReplaceOperatorsNewAndDelete();
456
457  uptr shadow_start = kLowShadowBeg;
458  if (kLowShadowBeg) shadow_start -= GetMmapGranularity();
459  uptr shadow_end = kHighShadowEnd;
460  bool full_shadow_is_available =
461      MemoryRangeIsAvailable(shadow_start, shadow_end);
462
463#if SANITIZER_LINUX && defined(__x86_64__) && !ASAN_FIXED_MAPPING
464  if (!full_shadow_is_available) {
465    kMidMemBeg = kLowMemEnd < 0x3000000000ULL ? 0x3000000000ULL : 0;
466    kMidMemEnd = kLowMemEnd < 0x3000000000ULL ? 0x4fffffffffULL : 0;
467  }
468#endif
469
470  if (flags()->verbosity)
471    PrintAddressSpaceLayout();
472
473  if (flags()->disable_core) {
474    DisableCoreDumper();
475  }
476
477  if (full_shadow_is_available) {
478    // mmap the low shadow plus at least one page at the left.
479    if (kLowShadowBeg)
480      ReserveShadowMemoryRange(shadow_start, kLowShadowEnd);
481    // mmap the high shadow.
482    ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
483    // protect the gap.
484    ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
485  } else if (kMidMemBeg &&
486      MemoryRangeIsAvailable(shadow_start, kMidMemBeg - 1) &&
487      MemoryRangeIsAvailable(kMidMemEnd + 1, shadow_end)) {
488    CHECK(kLowShadowBeg != kLowShadowEnd);
489    // mmap the low shadow plus at least one page at the left.
490    ReserveShadowMemoryRange(shadow_start, kLowShadowEnd);
491    // mmap the mid shadow.
492    ReserveShadowMemoryRange(kMidShadowBeg, kMidShadowEnd);
493    // mmap the high shadow.
494    ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
495    // protect the gaps.
496    ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
497    ProtectGap(kShadowGap2Beg, kShadowGap2End - kShadowGap2Beg + 1);
498    ProtectGap(kShadowGap3Beg, kShadowGap3End - kShadowGap3Beg + 1);
499  } else {
500    Report("Shadow memory range interleaves with an existing memory mapping. "
501           "ASan cannot proceed correctly. ABORTING.\n");
502    DumpProcessMap();
503    Die();
504  }
505
506  InstallSignalHandlers();
507  // Start symbolizer process if necessary.
508  const char* external_symbolizer = common_flags()->external_symbolizer_path;
509  if (common_flags()->symbolize && external_symbolizer &&
510      external_symbolizer[0]) {
511    InitializeExternalSymbolizer(external_symbolizer);
512  }
513
514  // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
515  // should be set to 1 prior to initializing the threads.
516  asan_inited = 1;
517  asan_init_is_running = false;
518
519  // Create main thread.
520  AsanTSDInit(AsanThread::TSDDtor);
521  AsanThread *main_thread = AsanThread::Create(0, 0);
522  CreateThreadContextArgs create_main_args = { main_thread, 0 };
523  u32 main_tid = asanThreadRegistry().CreateThread(
524      0, true, 0, &create_main_args);
525  CHECK_EQ(0, main_tid);
526  SetCurrentThread(main_thread);
527  main_thread->ThreadStart(internal_getpid());
528  force_interface_symbols();  // no-op.
529
530  InitializeAllocator();
531
532  if (flags()->verbosity) {
533    Report("AddressSanitizer Init done\n");
534  }
535}
536