| 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 | |
| Kostya Serebryany | d6567c5 | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 17 | #if !defined(__linux__) && !defined(__APPLE__) |
| 18 | # error "This operating system is not supported by AddressSanitizer" |
| 19 | #endif |
| 20 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 21 | #include <stdint.h> // for __WORDSIZE |
| 22 | #include <stdlib.h> // for size_t |
| 23 | #include <unistd.h> // for _exit |
| 24 | |
| Daniel Dunbar | 4616633 | 2011-12-02 01:32:27 +0000 | [diff] [blame] | 25 | // If __WORDSIZE was undefined by the platform, define it in terms of the |
| 26 | // compiler built-in __LP64__. |
| 27 | #ifndef __WORDSIZE |
| 28 | #if __LP64__ |
| 29 | #define __WORDSIZE 64 |
| 30 | #else |
| 31 | #define __WORDSIZE 32 |
| 32 | #endif |
| 33 | #endif |
| 34 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 35 | #ifdef ANDROID |
| 36 | #include <sys/atomics.h> |
| 37 | #endif |
| 38 | |
| 39 | #ifdef ADDRESS_SANITIZER |
| 40 | # error "The AddressSanitizer run-time should not be" |
| 41 | " instrumented by AddressSanitizer" |
| 42 | #endif |
| 43 | |
| Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 44 | // Build-time configuration options. |
| 45 | |
| 46 | // If set, sysinfo/sysinfo.h will be used to iterate over /proc/maps. |
| 47 | #ifndef ASAN_USE_SYSINFO |
| 48 | # define ASAN_USE_SYSINFO 1 |
| 49 | #endif |
| 50 | |
| 51 | // If set, asan will install its own SEGV signal handler. |
| 52 | #ifndef ASAN_NEEDS_SEGV |
| 53 | # define ASAN_NEEDS_SEGV 1 |
| 54 | #endif |
| 55 | |
| 56 | // If set, asan will intercept C++ exception api call(s). |
| 57 | #ifndef ASAN_HAS_EXCEPTIONS |
| 58 | # define ASAN_HAS_EXCEPTIONS 1 |
| 59 | #endif |
| 60 | |
| 61 | // If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET |
| 62 | // provided by the instrumented objects. Otherwise constants are used. |
| 63 | #ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET |
| 64 | # define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0 |
| 65 | #endif |
| 66 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 67 | // All internal functions in asan reside inside the __asan namespace |
| 68 | // to avoid namespace collisions with the user programs. |
| 69 | // Seperate namespace also makes it simpler to distinguish the asan run-time |
| 70 | // functions from the instrumented user code in a profile. |
| 71 | namespace __asan { |
| 72 | |
| 73 | class AsanThread; |
| 74 | struct AsanStackTrace; |
| 75 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 76 | // asan_rtl.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 77 | void CheckFailed(const char *cond, const char *file, int line); |
| 78 | void ShowStatsAndAbort(); |
| 79 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 80 | // asan_globals.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 81 | bool DescribeAddrIfGlobal(uintptr_t addr); |
| 82 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 83 | // asan_malloc_linux.cc / asan_malloc_mac.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 84 | void ReplaceSystemMalloc(); |
| 85 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 86 | // asan_linux.cc / asan_mac.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 87 | void *AsanDoesNotSupportStaticLinkage(); |
| 88 | void *asan_mmap(void *addr, size_t length, int prot, int flags, |
| 89 | int fd, uint64_t offset); |
| 90 | ssize_t asan_write(int fd, const void *buf, size_t count); |
| 91 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 92 | // asan_printf.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 93 | void RawWrite(const char *buffer); |
| 94 | int SNPrint(char *buffer, size_t length, const char *format, ...); |
| 95 | void Printf(const char *format, ...); |
| 96 | void Report(const char *format, ...); |
| 97 | |
| Kostya Serebryany | 2d8b3bd | 2011-12-02 18:42:04 +0000 | [diff] [blame] | 98 | // Don't use std::min and std::max, to minimize dependency on libstdc++. |
| 99 | template<class T> T Min(T a, T b) { return a < b ? a : b; } |
| 100 | template<class T> T Max(T a, T b) { return a > b ? a : b; } |
| 101 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 102 | // asan_poisoning.cc |
| 103 | // Poisons the shadow memory for "size" bytes starting from "addr". |
| 104 | void PoisonShadow(uintptr_t addr, size_t size, uint8_t value); |
| 105 | // Poisons the shadow memory for "redzone_size" bytes starting from |
| 106 | // "addr + size". |
| 107 | void PoisonShadowPartialRightRedzone(uintptr_t addr, |
| 108 | uintptr_t size, |
| 109 | uintptr_t redzone_size, |
| 110 | uint8_t value); |
| 111 | |
| 112 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 113 | extern size_t FLAG_quarantine_size; |
| 114 | extern int FLAG_demangle; |
| 115 | extern bool FLAG_symbolize; |
| 116 | extern int FLAG_v; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 117 | extern size_t FLAG_redzone; |
| 118 | extern int FLAG_debug; |
| 119 | extern bool FLAG_poison_shadow; |
| 120 | extern int FLAG_report_globals; |
| 121 | extern size_t FLAG_malloc_context_size; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 122 | extern bool FLAG_replace_str; |
| 123 | extern bool FLAG_replace_intrin; |
| 124 | extern bool FLAG_replace_cfallocator; |
| 125 | extern bool FLAG_fast_unwind; |
| 126 | extern bool FLAG_use_fake_stack; |
| 127 | extern size_t FLAG_max_malloc_fill_size; |
| 128 | extern int FLAG_exitcode; |
| 129 | extern bool FLAG_allow_user_poisoning; |
| 130 | |
| 131 | extern int asan_inited; |
| 132 | // Used to avoid infinite recursion in __asan_init(). |
| 133 | extern bool asan_init_is_running; |
| 134 | |
| 135 | enum LinkerInitialized { LINKER_INITIALIZED = 0 }; |
| 136 | |
| 137 | #ifndef ASAN_DIE |
| 138 | #define ASAN_DIE _exit(FLAG_exitcode) |
| 139 | #endif // ASAN_DIE |
| 140 | |
| 141 | #define CHECK(cond) do { if (!(cond)) { \ |
| 142 | CheckFailed(#cond, __FILE__, __LINE__); \ |
| 143 | }}while(0) |
| 144 | |
| 145 | #define RAW_CHECK_MSG(expr, msg) do { \ |
| 146 | if (!(expr)) { \ |
| 147 | RawWrite(msg); \ |
| 148 | ASAN_DIE; \ |
| 149 | } \ |
| 150 | } while (0) |
| 151 | |
| 152 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) |
| 153 | |
| 154 | #define UNIMPLEMENTED() CHECK("unimplemented" && 0) |
| 155 | |
| 156 | #define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) |
| 157 | |
| 158 | const size_t kWordSize = __WORDSIZE / 8; |
| 159 | const size_t kWordSizeInBits = 8 * kWordSize; |
| 160 | const size_t kPageSizeBits = 12; |
| 161 | const size_t kPageSize = 1UL << kPageSizeBits; |
| 162 | |
| 163 | #define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0) |
| 164 | #define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0) |
| 165 | |
| 166 | #define GET_BP_PC_SP \ |
| 167 | uintptr_t bp = GET_CURRENT_FRAME(); \ |
| 168 | uintptr_t pc = GET_CALLER_PC(); \ |
| 169 | uintptr_t local_stack; \ |
| 170 | uintptr_t sp = (uintptr_t)&local_stack; |
| 171 | |
| 172 | // These magic values are written to shadow for better error reporting. |
| 173 | const int kAsanHeapLeftRedzoneMagic = 0xfa; |
| 174 | const int kAsanHeapRightRedzoneMagic = 0xfb; |
| 175 | const int kAsanHeapFreeMagic = 0xfd; |
| 176 | const int kAsanStackLeftRedzoneMagic = 0xf1; |
| 177 | const int kAsanStackMidRedzoneMagic = 0xf2; |
| 178 | const int kAsanStackRightRedzoneMagic = 0xf3; |
| 179 | const int kAsanStackPartialRedzoneMagic = 0xf4; |
| 180 | const int kAsanStackAfterReturnMagic = 0xf5; |
| 181 | const int kAsanUserPoisonedMemoryMagic = 0xf7; |
| 182 | const int kAsanGlobalRedzoneMagic = 0xf9; |
| Kostya Serebryany | 6b30e2c | 2011-12-15 17:41:30 +0000 | [diff] [blame^] | 183 | const int kAsanInternalHeapMagic = 0xfe; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 184 | |
| 185 | static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; |
| 186 | static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; |
| 187 | |
| Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 188 | // -------------------------- LowLevelAllocator ----- {{{1 |
| 189 | // A simple low-level memory allocator for internal use. |
| 190 | class LowLevelAllocator { |
| 191 | public: |
| 192 | explicit LowLevelAllocator(LinkerInitialized) {} |
| 193 | // 'size' must be a power of two. |
| 194 | // Requires an external lock. |
| 195 | void *Allocate(size_t size); |
| 196 | private: |
| 197 | char *allocated_end_; |
| 198 | char *allocated_current_; |
| 199 | }; |
| 200 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 201 | // -------------------------- Atomic ---------------- {{{1 |
| 202 | static inline int AtomicInc(int *a) { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 203 | #ifdef ANDROID |
| 204 | return __atomic_inc(a) + 1; |
| 205 | #else |
| 206 | return __sync_add_and_fetch(a, 1); |
| 207 | #endif |
| 208 | } |
| 209 | |
| 210 | static inline int AtomicDec(int *a) { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 211 | #ifdef ANDROID |
| 212 | return __atomic_dec(a) - 1; |
| 213 | #else |
| 214 | return __sync_add_and_fetch(a, -1); |
| 215 | #endif |
| 216 | } |
| 217 | |
| 218 | } // namespace __asan |
| 219 | |
| 220 | #endif // ASAN_INTERNAL_H |