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# if defined(__GNUC__) && !defined(__clang__)
18# include <intrin.h>
19# endif
20
21namespace __asan {
22
23# if !defined(__GNUC__) || defined(__clang__)
24# pragma section(".ASAN$GA", read, write)
25# pragma section(".ASAN$GZ", read, write)
26# endif
27extern "C" alignas(sizeof(__asan_global))
28 IN_SECTION(".ASAN$GA") __asan_global __asan_globals_start = {};
29extern "C" alignas(sizeof(__asan_global))
30 IN_SECTION(".ASAN$GZ") __asan_global __asan_globals_end = {};
31# pragma comment(linker, "/merge:.ASAN=.data")
32
33static void call_on_globals(void (*hook)(__asan_global *, uptr)) {
34 __asan_global *start = &__asan_globals_start + 1;
35 __asan_global *end = &__asan_globals_end;
36 uptr bytediff = (uptr)end - (uptr)start;
37 if (bytediff % sizeof(__asan_global) != 0) {
38# if defined(SANITIZER_DLL_THUNK) || \
39 defined(SANITIZER_DYNAMIC_RUNTIME_THUNK) || \
40 defined(SANITIZER_STATIC_RUNTIME_THUNK)
41 __debugbreak();
42#else
43 CHECK("corrupt asan global array");
44#endif
45 }
46 // We know end >= start because the linker sorts the portion after the dollar
47 // sign alphabetically.
48 uptr n = end - start;
49 hook(start, n);
50}
51
52static void register_dso_globals() {
53 call_on_globals(&__asan_register_globals);
54}
55
56static void unregister_dso_globals() {
57 call_on_globals(&__asan_unregister_globals);
58}
59
60// Register globals
61# if !defined(__GNUC__) || defined(__clang__)
62# pragma section(".CRT$XCU", long, read)
63# pragma section(".CRT$XTX", long, read)
64# endif
65extern "C" IN_SECTION(".CRT$XCU") void (*const __asan_dso_reg_hook)() =
66 &register_dso_globals;
67extern "C" IN_SECTION(".CRT$XTX") void (*const __asan_dso_unreg_hook)() =
68 &unregister_dso_globals;
69
70} // namespace __asan
71
72#endif // SANITIZER_WINDOWS
73