xref: /freebsd/contrib/llvm-project/compiler-rt/lib/asan/asan_globals_win.cpp (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 //===-- asan_globals_win.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Global registration code that is linked into every Windows DLL and EXE.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "asan_interface_internal.h"
14 #if SANITIZER_WINDOWS
15 
16 namespace __asan {
17 
18 #pragma section(".ASAN$GA", read, write)
19 #pragma section(".ASAN$GZ", read, write)
20 extern "C" __declspec(allocate(".ASAN$GA"))
21     ALIGNED(sizeof(__asan_global)) __asan_global __asan_globals_start = {};
22 extern "C" __declspec(allocate(".ASAN$GZ"))
23     ALIGNED(sizeof(__asan_global)) __asan_global __asan_globals_end = {};
24 #pragma comment(linker, "/merge:.ASAN=.data")
25 
26 static void call_on_globals(void (*hook)(__asan_global *, uptr)) {
27   __asan_global *start = &__asan_globals_start + 1;
28   __asan_global *end = &__asan_globals_end;
29   uptr bytediff = (uptr)end - (uptr)start;
30   if (bytediff % sizeof(__asan_global) != 0) {
31 #if defined(SANITIZER_DLL_THUNK) || defined(SANITIZER_DYNAMIC_RUNTIME_THUNK)
32     __debugbreak();
33 #else
34     CHECK("corrupt asan global array");
35 #endif
36   }
37   // We know end >= start because the linker sorts the portion after the dollar
38   // sign alphabetically.
39   uptr n = end - start;
40   hook(start, n);
41 }
42 
43 static void register_dso_globals() {
44   call_on_globals(&__asan_register_globals);
45 }
46 
47 static void unregister_dso_globals() {
48   call_on_globals(&__asan_unregister_globals);
49 }
50 
51 // Register globals
52 #pragma section(".CRT$XCU", long, read)
53 #pragma section(".CRT$XTX", long, read)
54 extern "C" __declspec(allocate(".CRT$XCU"))
55 void (*const __asan_dso_reg_hook)() = &register_dso_globals;
56 extern "C" __declspec(allocate(".CRT$XTX"))
57 void (*const __asan_dso_unreg_hook)() = &unregister_dso_globals;
58 
59 } // namespace __asan
60 
61 #endif  // SANITIZER_WINDOWS
62