Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_internal.h -----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // ASan-private header which defines various general utilities. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #ifndef ASAN_INTERNAL_H |
| 15 | #define ASAN_INTERNAL_H |
| 16 | |
Alexey Samsonov | 7ed1d2b | 2012-07-10 07:41:27 +0000 | [diff] [blame] | 17 | #include "asan_flags.h" |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 18 | #include "asan_interface_internal.h" |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_common.h" |
Alexey Samsonov | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_internal_defs.h" |
Kostya Serebryany | 1b5ea8f | 2012-08-28 14:11:57 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_stacktrace.h" |
Kostya Serebryany | 16e0075 | 2012-05-31 13:42:53 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_libc.h" |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 23 | |
Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 24 | #define ASAN_DEFAULT_FAILURE_EXITCODE 1 |
| 25 | |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 26 | #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 27 | # error "The AddressSanitizer run-time should not be" |
| 28 | " instrumented by AddressSanitizer" |
| 29 | #endif |
| 30 | |
Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 31 | // Build-time configuration options. |
| 32 | |
Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 33 | // If set, asan will intercept C++ exception api call(s). |
| 34 | #ifndef ASAN_HAS_EXCEPTIONS |
| 35 | # define ASAN_HAS_EXCEPTIONS 1 |
| 36 | #endif |
| 37 | |
Evgeniy Stepanov | 8ae44ac | 2012-02-27 13:07:29 +0000 | [diff] [blame] | 38 | // If set, values like allocator chunk size, as well as defaults for some flags |
| 39 | // will be changed towards less memory overhead. |
| 40 | #ifndef ASAN_LOW_MEMORY |
Kostya Serebryany | e52810d | 2012-12-25 07:17:17 +0000 | [diff] [blame] | 41 | #if SANITIZER_WORDSIZE == 32 |
Evgeniy Stepanov | 9712af9 | 2012-09-28 10:07:53 +0000 | [diff] [blame] | 42 | # define ASAN_LOW_MEMORY 1 |
Kostya Serebryany | e52810d | 2012-12-25 07:17:17 +0000 | [diff] [blame] | 43 | #else |
Evgeniy Stepanov | 9712af9 | 2012-09-28 10:07:53 +0000 | [diff] [blame] | 44 | # define ASAN_LOW_MEMORY 0 |
| 45 | # endif |
Evgeniy Stepanov | 8ae44ac | 2012-02-27 13:07:29 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 48 | #ifndef ASAN_DYNAMIC |
| 49 | # ifdef PIC |
| 50 | # define ASAN_DYNAMIC 1 |
| 51 | # else |
| 52 | # define ASAN_DYNAMIC 0 |
| 53 | # endif |
| 54 | #endif |
| 55 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 56 | // All internal functions in asan reside inside the __asan namespace |
| 57 | // to avoid namespace collisions with the user programs. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 58 | // Separate namespace also makes it simpler to distinguish the asan run-time |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 59 | // functions from the instrumented user code in a profile. |
| 60 | namespace __asan { |
| 61 | |
| 62 | class AsanThread; |
Kostya Serebryany | 1b5ea8f | 2012-08-28 14:11:57 +0000 | [diff] [blame] | 63 | using __sanitizer::StackTrace; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 64 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 65 | void AsanInitFromRtl(); |
| 66 | |
Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 67 | // asan_rtl.cc |
Timur Iskhodzhanov | b55c88d | 2012-03-13 16:29:25 +0000 | [diff] [blame] | 68 | void NORETURN ShowStatsAndAbort(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 69 | |
Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 70 | // asan_malloc_linux.cc / asan_malloc_mac.cc |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 71 | void ReplaceSystemMalloc(); |
| 72 | |
Alexander Potapenko | f73a6a3 | 2012-02-13 17:09:40 +0000 | [diff] [blame] | 73 | // asan_linux.cc / asan_mac.cc / asan_win.cc |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 74 | void *AsanDoesNotSupportStaticLinkage(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 75 | void AsanCheckDynamicRTPrereqs(); |
| 76 | void AsanCheckIncompatibleRT(); |
Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 77 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 78 | void AsanOnSIGSEGV(int, void *siginfo, void *context); |
Kostya Serebryany | ef14ff6 | 2012-01-06 02:12:25 +0000 | [diff] [blame] | 79 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 80 | void DisableReexec(); |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 81 | void MaybeReexec(); |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 82 | void ReadContextStack(void *context, uptr *stack, uptr *ssize); |
Alexander Potapenko | 75b19eb | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 83 | void AsanPlatformThreadInit(); |
Alexey Samsonov | 46efcb0 | 2013-05-24 11:46:56 +0000 | [diff] [blame] | 84 | void StopInitOrderChecking(); |
Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 85 | |
| 86 | // Wrapper for TLS/TSD. |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 87 | void AsanTSDInit(void (*destructor)(void *tsd)); |
Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 88 | void *AsanTSDGet(); |
| 89 | void AsanTSDSet(void *tsd); |
Sergey Matveev | e86e35f | 2013-10-14 12:01:05 +0000 | [diff] [blame] | 90 | void PlatformTSDDtor(void *tsd); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 91 | |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 92 | void AppendToErrorMessageBuffer(const char *buffer); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 93 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 94 | void *AsanDlSymNext(const char *sym); |
| 95 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 96 | void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 97 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 98 | // Platform-specific options. |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 99 | #if SANITIZER_MAC |
Alexey Samsonov | 38dd4ed | 2012-03-20 10:54:40 +0000 | [diff] [blame] | 100 | bool PlatformHasDifferentMemcpyAndMemmove(); |
| 101 | # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \ |
| 102 | (PlatformHasDifferentMemcpyAndMemmove()) |
| 103 | #else |
| 104 | # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true |
Alexey Samsonov | 649a270 | 2013-04-03 07:29:53 +0000 | [diff] [blame] | 105 | #endif // SANITIZER_MAC |
Alexey Samsonov | 38dd4ed | 2012-03-20 10:54:40 +0000 | [diff] [blame] | 106 | |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 107 | // Add convenient macro for interface functions that may be represented as |
| 108 | // weak hooks. |
| 109 | #define ASAN_MALLOC_HOOK(ptr, size) \ |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 110 | if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size) |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 111 | #define ASAN_FREE_HOOK(ptr) \ |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 112 | if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr) |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 113 | #define ASAN_ON_ERROR() \ |
| 114 | if (&__asan_on_error) __asan_on_error() |
| 115 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 116 | extern int asan_inited; |
| 117 | // Used to avoid infinite recursion in __asan_init(). |
| 118 | extern bool asan_init_is_running; |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 119 | extern void (*death_callback)(void); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 120 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 121 | // These magic values are written to shadow for better error reporting. |
| 122 | const int kAsanHeapLeftRedzoneMagic = 0xfa; |
| 123 | const int kAsanHeapRightRedzoneMagic = 0xfb; |
| 124 | const int kAsanHeapFreeMagic = 0xfd; |
| 125 | const int kAsanStackLeftRedzoneMagic = 0xf1; |
| 126 | const int kAsanStackMidRedzoneMagic = 0xf2; |
| 127 | const int kAsanStackRightRedzoneMagic = 0xf3; |
| 128 | const int kAsanStackPartialRedzoneMagic = 0xf4; |
| 129 | const int kAsanStackAfterReturnMagic = 0xf5; |
Kostya Serebryany | 3945c58 | 2012-08-21 14:10:25 +0000 | [diff] [blame] | 130 | const int kAsanInitializationOrderMagic = 0xf6; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 131 | const int kAsanUserPoisonedMemoryMagic = 0xf7; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 132 | const int kAsanContiguousContainerOOBMagic = 0xfc; |
Alexey Samsonov | d4b5db8 | 2012-12-04 01:38:15 +0000 | [diff] [blame] | 133 | const int kAsanStackUseAfterScopeMagic = 0xf8; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 134 | const int kAsanGlobalRedzoneMagic = 0xf9; |
Kostya Serebryany | 6b30e2c | 2011-12-15 17:41:30 +0000 | [diff] [blame] | 135 | const int kAsanInternalHeapMagic = 0xfe; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 136 | const int kAsanArrayCookieMagic = 0xac; |
| 137 | const int kAsanIntraObjectRedzone = 0xbb; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 138 | const int kAsanAllocaLeftMagic = 0xca; |
| 139 | const int kAsanAllocaRightMagic = 0xcb; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 140 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 141 | static const uptr kCurrentStackFrameMagic = 0x41B58AB3; |
| 142 | static const uptr kRetiredStackFrameMagic = 0x45E0360E; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 143 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 144 | } // namespace __asan |
| 145 | |
| 146 | #endif // ASAN_INTERNAL_H |