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