Home | History | Annotate | Line # | Download | only in asan
      1 //===-- asan_globals_win.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 // Global registration code that is linked into every Windows DLL and EXE.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "asan_interface_internal.h"
     15 #if SANITIZER_WINDOWS
     16 
     17 namespace __asan {
     18 
     19 #pragma section(".ASAN$GA", read, write)  // NOLINT
     20 #pragma section(".ASAN$GZ", read, write)  // NOLINT
     21 extern "C" __declspec(allocate(".ASAN$GA"))
     22     ALIGNED(sizeof(__asan_global)) __asan_global __asan_globals_start = {};
     23 extern "C" __declspec(allocate(".ASAN$GZ"))
     24     ALIGNED(sizeof(__asan_global)) __asan_global __asan_globals_end = {};
     25 #pragma comment(linker, "/merge:.ASAN=.data")
     26 
     27 static void call_on_globals(void (*hook)(__asan_global *, uptr)) {
     28   __asan_global *start = &__asan_globals_start + 1;
     29   __asan_global *end = &__asan_globals_end;
     30   uptr bytediff = (uptr)end - (uptr)start;
     31   if (bytediff % sizeof(__asan_global) != 0) {
     32 #if defined(SANITIZER_DLL_THUNK) || defined(SANITIZER_DYNAMIC_RUNTIME_THUNK)
     33     __debugbreak();
     34 #else
     35     CHECK("corrupt asan global array");
     36 #endif
     37   }
     38   // We know end >= start because the linker sorts the portion after the dollar
     39   // sign alphabetically.
     40   uptr n = end - start;
     41   hook(start, n);
     42 }
     43 
     44 static void register_dso_globals() {
     45   call_on_globals(&__asan_register_globals);
     46 }
     47 
     48 static void unregister_dso_globals() {
     49   call_on_globals(&__asan_unregister_globals);
     50 }
     51 
     52 // Register globals
     53 #pragma section(".CRT$XCU", long, read)  // NOLINT
     54 #pragma section(".CRT$XTX", long, read)  // NOLINT
     55 extern "C" __declspec(allocate(".CRT$XCU"))
     56 void (*const __asan_dso_reg_hook)() = &register_dso_globals;
     57 extern "C" __declspec(allocate(".CRT$XTX"))
     58 void (*const __asan_dso_unreg_hook)() = &unregister_dso_globals;
     59 
     60 } // namespace __asan
     61 
     62 #endif  // SANITIZER_WINDOWS
     63