asan_globals_win.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "asan_interface_internal.h"
  13. #if SANITIZER_WINDOWS
  14. namespace __asan {
  15. #pragma section(".ASAN$GA", read, write)
  16. #pragma section(".ASAN$GZ", read, write)
  17. extern "C" __declspec(allocate(".ASAN$GA"))
  18. ALIGNED(sizeof(__asan_global)) __asan_global __asan_globals_start = {};
  19. extern "C" __declspec(allocate(".ASAN$GZ"))
  20. ALIGNED(sizeof(__asan_global)) __asan_global __asan_globals_end = {};
  21. #pragma comment(linker, "/merge:.ASAN=.data")
  22. static void call_on_globals(void (*hook)(__asan_global *, uptr)) {
  23. __asan_global *start = &__asan_globals_start + 1;
  24. __asan_global *end = &__asan_globals_end;
  25. uptr bytediff = (uptr)end - (uptr)start;
  26. if (bytediff % sizeof(__asan_global) != 0) {
  27. #if defined(SANITIZER_DLL_THUNK) || defined(SANITIZER_DYNAMIC_RUNTIME_THUNK)
  28. __debugbreak();
  29. #else
  30. CHECK("corrupt asan global array");
  31. #endif
  32. }
  33. // We know end >= start because the linker sorts the portion after the dollar
  34. // sign alphabetically.
  35. uptr n = end - start;
  36. hook(start, n);
  37. }
  38. static void register_dso_globals() {
  39. call_on_globals(&__asan_register_globals);
  40. }
  41. static void unregister_dso_globals() {
  42. call_on_globals(&__asan_unregister_globals);
  43. }
  44. // Register globals
  45. #pragma section(".CRT$XCU", long, read)
  46. #pragma section(".CRT$XTX", long, read)
  47. extern "C" __declspec(allocate(".CRT$XCU"))
  48. void (*const __asan_dso_reg_hook)() = &register_dso_globals;
  49. extern "C" __declspec(allocate(".CRT$XTX"))
  50. void (*const __asan_dso_unreg_hook)() = &unregister_dso_globals;
  51. } // namespace __asan
  52. #endif // SANITIZER_WINDOWS