| 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 | # include "sanitizer_common/sanitizer_win_defs.h" |
| 16 | |
| 17 | namespace __asan { |
| 18 | |
| 19 | # pragma section(".ASAN$GA", read, write) |
| 20 | # pragma section(".ASAN$GZ", read, write) |
| 21 | extern "C" alignas(sizeof(__asan_global)) |
| 22 | IN_SECTION(".ASAN$GA" ) __asan_global __asan_globals_start = {}; |
| 23 | extern "C" alignas(sizeof(__asan_global)) |
| 24 | IN_SECTION(".ASAN$GZ" ) __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) || \ |
| 33 | defined(SANITIZER_DYNAMIC_RUNTIME_THUNK) || \ |
| 34 | defined(SANITIZER_STATIC_RUNTIME_THUNK) |
| 35 | __debugbreak(); |
| 36 | #else |
| 37 | CHECK("corrupt asan global array" ); |
| 38 | #endif |
| 39 | } |
| 40 | // We know end >= start because the linker sorts the portion after the dollar |
| 41 | // sign alphabetically. |
| 42 | uptr n = end - start; |
| 43 | hook(start, n); |
| 44 | } |
| 45 | |
| 46 | static void register_dso_globals() { |
| 47 | call_on_globals(&__asan_register_globals); |
| 48 | } |
| 49 | |
| 50 | static void unregister_dso_globals() { |
| 51 | call_on_globals(&__asan_unregister_globals); |
| 52 | } |
| 53 | |
| 54 | // Register globals |
| 55 | # pragma section(".CRT$XCU", long, read) |
| 56 | # pragma section(".CRT$XTX", long, read) |
| 57 | extern "C" IN_SECTION(".CRT$XCU" ) void (*const __asan_dso_reg_hook)() = |
| 58 | ®ister_dso_globals; |
| 59 | extern "C" IN_SECTION(".CRT$XTX" ) void (*const __asan_dso_unreg_hook)() = |
| 60 | &unregister_dso_globals; |
| 61 | |
| 62 | } // namespace __asan |
| 63 | |
| 64 | #endif // SANITIZER_WINDOWS |
| 65 | |