asan_internal.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_stacktrace.h"
  20. #include "sanitizer_common/sanitizer_libc.h"
  21. #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
  22. # error "The AddressSanitizer run-time should not be"
  23. " 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 ReadContextStack(void *context, uptr *stack, uptr *ssize);
  89. void StopInitOrderChecking();
  90. // Wrapper for TLS/TSD.
  91. void AsanTSDInit(void (*destructor)(void *tsd));
  92. void *AsanTSDGet();
  93. void AsanTSDSet(void *tsd);
  94. void PlatformTSDDtor(void *tsd);
  95. void AppendToErrorMessageBuffer(const char *buffer);
  96. void *AsanDlSymNext(const char *sym);
  97. // Returns `true` iff most of ASan init process should be skipped due to the
  98. // ASan library being loaded via `dlopen()`. Platforms may perform any
  99. // `dlopen()` specific initialization inside this function.
  100. bool HandleDlopenInit();
  101. // Add convenient macro for interface functions that may be represented as
  102. // weak hooks.
  103. #define ASAN_MALLOC_HOOK(ptr, size) \
  104. do { \
  105. if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size); \
  106. RunMallocHooks(ptr, size); \
  107. } while (false)
  108. #define ASAN_FREE_HOOK(ptr) \
  109. do { \
  110. if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr); \
  111. RunFreeHooks(ptr); \
  112. } while (false)
  113. #define ASAN_ON_ERROR() \
  114. if (&__asan_on_error) __asan_on_error()
  115. extern int asan_inited;
  116. // Used to avoid infinite recursion in __asan_init().
  117. extern bool asan_init_is_running;
  118. extern void (*death_callback)(void);
  119. // These magic values are written to shadow for better error reporting.
  120. const int kAsanHeapLeftRedzoneMagic = 0xfa;
  121. const int kAsanHeapFreeMagic = 0xfd;
  122. const int kAsanStackLeftRedzoneMagic = 0xf1;
  123. const int kAsanStackMidRedzoneMagic = 0xf2;
  124. const int kAsanStackRightRedzoneMagic = 0xf3;
  125. const int kAsanStackAfterReturnMagic = 0xf5;
  126. const int kAsanInitializationOrderMagic = 0xf6;
  127. const int kAsanUserPoisonedMemoryMagic = 0xf7;
  128. const int kAsanContiguousContainerOOBMagic = 0xfc;
  129. const int kAsanStackUseAfterScopeMagic = 0xf8;
  130. const int kAsanGlobalRedzoneMagic = 0xf9;
  131. const int kAsanInternalHeapMagic = 0xfe;
  132. const int kAsanArrayCookieMagic = 0xac;
  133. const int kAsanIntraObjectRedzone = 0xbb;
  134. const int kAsanAllocaLeftMagic = 0xca;
  135. const int kAsanAllocaRightMagic = 0xcb;
  136. static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
  137. static const uptr kRetiredStackFrameMagic = 0x45E0360E;
  138. } // namespace __asan
  139. #endif // ASAN_INTERNAL_H