64 lines · cpp
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-exception6//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_WINDOWS15 16namespace __asan {17 18#pragma section(".ASAN$GA", read, write)19#pragma section(".ASAN$GZ", read, write)20extern "C" alignas(sizeof(__asan_global))21 __declspec(allocate(".ASAN$GA")) __asan_global __asan_globals_start = {};22extern "C" alignas(sizeof(__asan_global))23 __declspec(allocate(".ASAN$GZ")) __asan_global __asan_globals_end = {};24#pragma comment(linker, "/merge:.ASAN=.data")25 26static 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#else36 CHECK("corrupt asan global array");37#endif38 }39 // We know end >= start because the linker sorts the portion after the dollar40 // sign alphabetically.41 uptr n = end - start;42 hook(start, n);43}44 45static void register_dso_globals() {46 call_on_globals(&__asan_register_globals);47}48 49static void unregister_dso_globals() {50 call_on_globals(&__asan_unregister_globals);51}52 53// Register globals54#pragma section(".CRT$XCU", long, read)55#pragma section(".CRT$XTX", long, read)56extern "C" __declspec(allocate(".CRT$XCU"))57void (*const __asan_dso_reg_hook)() = ®ister_dso_globals;58extern "C" __declspec(allocate(".CRT$XTX"))59void (*const __asan_dso_unreg_hook)() = &unregister_dso_globals;60 61} // namespace __asan62 63#endif // SANITIZER_WINDOWS64