asan_internal.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //===-- asan_internal.h -----------------------------------------*- C++ -*-===//
  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. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // ASan-private header which defines various general utilities.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef ASAN_INTERNAL_H
  14. #define ASAN_INTERNAL_H
  15. #include "asan_flags.h"
  16. #include "asan_interface_internal.h"
  17. #include "sanitizer_common/sanitizer_common.h"
  18. #include "sanitizer_common/sanitizer_internal_defs.h"
  19. #include "sanitizer_common/sanitizer_libc.h"
  20. #include "sanitizer_common/sanitizer_stacktrace.h"
  21. #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
  22. # error \
  23. "The AddressSanitizer run-time should not be instrumented by AddressSanitizer"
  24. #endif
  25. // Build-time configuration options.
  26. // If set, asan will intercept C++ exception api call(s).
  27. #ifndef ASAN_HAS_EXCEPTIONS
  28. # define ASAN_HAS_EXCEPTIONS 1
  29. #endif
  30. // If set, values like allocator chunk size, as well as defaults for some flags
  31. // will be changed towards less memory overhead.
  32. #ifndef ASAN_LOW_MEMORY
  33. # if SANITIZER_IOS || SANITIZER_ANDROID
  34. # define ASAN_LOW_MEMORY 1
  35. # else
  36. # define ASAN_LOW_MEMORY 0
  37. # endif
  38. #endif
  39. #ifndef ASAN_DYNAMIC
  40. # ifdef PIC
  41. # define ASAN_DYNAMIC 1
  42. # else
  43. # define ASAN_DYNAMIC 0
  44. # endif
  45. #endif
  46. // All internal functions in asan reside inside the __asan namespace
  47. // to avoid namespace collisions with the user programs.
  48. // Separate namespace also makes it simpler to distinguish the asan run-time
  49. // functions from the instrumented user code in a profile.
  50. namespace __asan {
  51. class AsanThread;
  52. using __sanitizer::StackTrace;
  53. void AsanInitFromRtl();
  54. // asan_win.cpp
  55. void InitializePlatformExceptionHandlers();
  56. // Returns whether an address is a valid allocated system heap block.
  57. // 'addr' must point to the beginning of the block.
  58. bool IsSystemHeapAddress(uptr addr);
  59. // asan_rtl.cpp
  60. void PrintAddressSpaceLayout();
  61. void NORETURN ShowStatsAndAbort();
  62. // asan_shadow_setup.cpp
  63. void InitializeShadowMemory();
  64. // asan_malloc_linux.cpp / asan_malloc_mac.cpp
  65. void ReplaceSystemMalloc();
  66. // asan_linux.cpp / asan_mac.cpp / asan_win.cpp
  67. uptr FindDynamicShadowStart();
  68. void *AsanDoesNotSupportStaticLinkage();
  69. void AsanCheckDynamicRTPrereqs();
  70. void AsanCheckIncompatibleRT();
  71. // Unpoisons platform-specific stacks.
  72. // Returns true if all stacks have been unpoisoned.
  73. bool PlatformUnpoisonStacks();
  74. // asan_rtl.cpp
  75. // Unpoison a region containing a stack.
  76. // Performs a sanity check and warns if the bounds don't look right.
  77. // The warning contains the type string to identify the stack type.
  78. void UnpoisonStack(uptr bottom, uptr top, const char *type);
  79. // asan_thread.cpp
  80. AsanThread *CreateMainThread();
  81. // Support function for __asan_(un)register_image_globals. Searches for the
  82. // loaded image containing `needle' and then enumerates all global metadata
  83. // structures declared in that image, applying `op' (e.g.,
  84. // __asan_(un)register_globals) to them.
  85. typedef void (*globals_op_fptr)(__asan_global *, uptr);
  86. void AsanApplyToGlobals(globals_op_fptr op, const void *needle);
  87. void AsanOnDeadlySignal(int, void *siginfo, void *context);
  88. void SignContextStack(void *context);
  89. void ReadContextStack(void *context, uptr *stack, uptr *ssize);
  90. void StopInitOrderChecking();
  91. // Wrapper for TLS/TSD.
  92. void AsanTSDInit(void (*destructor)(void *tsd));
  93. void *AsanTSDGet();
  94. void AsanTSDSet(void *tsd);
  95. void PlatformTSDDtor(void *tsd);
  96. void AppendToErrorMessageBuffer(const char *buffer);
  97. void *AsanDlSymNext(const char *sym);
  98. // Returns `true` iff most of ASan init process should be skipped due to the
  99. // ASan library being loaded via `dlopen()`. Platforms may perform any
  100. // `dlopen()` specific initialization inside this function.
  101. bool HandleDlopenInit();
  102. void InstallAtExitCheckLeaks();
  103. #define ASAN_ON_ERROR() \
  104. if (&__asan_on_error) \
  105. __asan_on_error()
  106. extern int asan_inited;
  107. // Used to avoid infinite recursion in __asan_init().
  108. extern bool asan_init_is_running;
  109. extern bool replace_intrin_cached;
  110. extern void (*death_callback)(void);
  111. // These magic values are written to shadow for better error
  112. // reporting.
  113. const int kAsanHeapLeftRedzoneMagic = 0xfa;
  114. const int kAsanHeapFreeMagic = 0xfd;
  115. const int kAsanStackLeftRedzoneMagic = 0xf1;
  116. const int kAsanStackMidRedzoneMagic = 0xf2;
  117. const int kAsanStackRightRedzoneMagic = 0xf3;
  118. const int kAsanStackAfterReturnMagic = 0xf5;
  119. const int kAsanInitializationOrderMagic = 0xf6;
  120. const int kAsanUserPoisonedMemoryMagic = 0xf7;
  121. const int kAsanContiguousContainerOOBMagic = 0xfc;
  122. const int kAsanStackUseAfterScopeMagic = 0xf8;
  123. const int kAsanGlobalRedzoneMagic = 0xf9;
  124. const int kAsanInternalHeapMagic = 0xfe;
  125. const int kAsanArrayCookieMagic = 0xac;
  126. const int kAsanIntraObjectRedzone = 0xbb;
  127. const int kAsanAllocaLeftMagic = 0xca;
  128. const int kAsanAllocaRightMagic = 0xcb;
  129. static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
  130. static const uptr kRetiredStackFrameMagic = 0x45E0360E;
  131. } // namespace __asan
  132. #endif // ASAN_INTERNAL_H