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