| 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 | |
| 44 | // All internal functions in asan reside inside the __asan namespace |
| 45 | // to avoid namespace collisions with the user programs. |
| 46 | // Seperate namespace also makes it simpler to distinguish the asan run-time |
| 47 | // functions from the instrumented user code in a profile. |
| 48 | namespace __asan { |
| 49 | |
| 50 | class AsanThread; |
| 51 | struct AsanStackTrace; |
| 52 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 53 | // asan_rtl.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 54 | void CheckFailed(const char *cond, const char *file, int line); |
| 55 | void ShowStatsAndAbort(); |
| 56 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 57 | // asan_globals.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 58 | bool DescribeAddrIfGlobal(uintptr_t addr); |
| 59 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 60 | // asan_malloc_linux.cc / asan_malloc_mac.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 61 | void ReplaceSystemMalloc(); |
| 62 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 63 | // asan_linux.cc / asan_mac.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 64 | void *AsanDoesNotSupportStaticLinkage(); |
| 65 | void *asan_mmap(void *addr, size_t length, int prot, int flags, |
| 66 | int fd, uint64_t offset); |
| 67 | ssize_t asan_write(int fd, const void *buf, size_t count); |
| 68 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 69 | // asan_printf.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 70 | void RawWrite(const char *buffer); |
| 71 | int SNPrint(char *buffer, size_t length, const char *format, ...); |
| 72 | void Printf(const char *format, ...); |
| 73 | void Report(const char *format, ...); |
| 74 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 75 | // asan_poisoning.cc |
| 76 | // Poisons the shadow memory for "size" bytes starting from "addr". |
| 77 | void PoisonShadow(uintptr_t addr, size_t size, uint8_t value); |
| 78 | // Poisons the shadow memory for "redzone_size" bytes starting from |
| 79 | // "addr + size". |
| 80 | void PoisonShadowPartialRightRedzone(uintptr_t addr, |
| 81 | uintptr_t size, |
| 82 | uintptr_t redzone_size, |
| 83 | uint8_t value); |
| 84 | |
| 85 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 86 | extern size_t FLAG_quarantine_size; |
| 87 | extern int FLAG_demangle; |
| 88 | extern bool FLAG_symbolize; |
| 89 | extern int FLAG_v; |
| 90 | extern bool FLAG_mt; |
| 91 | extern size_t FLAG_redzone; |
| 92 | extern int FLAG_debug; |
| 93 | extern bool FLAG_poison_shadow; |
| 94 | extern int FLAG_report_globals; |
| 95 | extern size_t FLAG_malloc_context_size; |
| 96 | extern bool FLAG_stats; |
| 97 | extern bool FLAG_replace_str; |
| 98 | extern bool FLAG_replace_intrin; |
| 99 | extern bool FLAG_replace_cfallocator; |
| 100 | extern bool FLAG_fast_unwind; |
| 101 | extern bool FLAG_use_fake_stack; |
| 102 | extern size_t FLAG_max_malloc_fill_size; |
| 103 | extern int FLAG_exitcode; |
| 104 | extern bool FLAG_allow_user_poisoning; |
| 105 | |
| 106 | extern int asan_inited; |
| 107 | // Used to avoid infinite recursion in __asan_init(). |
| 108 | extern bool asan_init_is_running; |
| 109 | |
| 110 | enum LinkerInitialized { LINKER_INITIALIZED = 0 }; |
| 111 | |
| 112 | #ifndef ASAN_DIE |
| 113 | #define ASAN_DIE _exit(FLAG_exitcode) |
| 114 | #endif // ASAN_DIE |
| 115 | |
| 116 | #define CHECK(cond) do { if (!(cond)) { \ |
| 117 | CheckFailed(#cond, __FILE__, __LINE__); \ |
| 118 | }}while(0) |
| 119 | |
| 120 | #define RAW_CHECK_MSG(expr, msg) do { \ |
| 121 | if (!(expr)) { \ |
| 122 | RawWrite(msg); \ |
| 123 | ASAN_DIE; \ |
| 124 | } \ |
| 125 | } while (0) |
| 126 | |
| 127 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) |
| 128 | |
| 129 | #define UNIMPLEMENTED() CHECK("unimplemented" && 0) |
| 130 | |
| 131 | #define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) |
| 132 | |
| 133 | const size_t kWordSize = __WORDSIZE / 8; |
| 134 | const size_t kWordSizeInBits = 8 * kWordSize; |
| 135 | const size_t kPageSizeBits = 12; |
| 136 | const size_t kPageSize = 1UL << kPageSizeBits; |
| 137 | |
| 138 | #define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0) |
| 139 | #define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0) |
| 140 | |
| 141 | #define GET_BP_PC_SP \ |
| 142 | uintptr_t bp = GET_CURRENT_FRAME(); \ |
| 143 | uintptr_t pc = GET_CALLER_PC(); \ |
| 144 | uintptr_t local_stack; \ |
| 145 | uintptr_t sp = (uintptr_t)&local_stack; |
| 146 | |
| 147 | // These magic values are written to shadow for better error reporting. |
| 148 | const int kAsanHeapLeftRedzoneMagic = 0xfa; |
| 149 | const int kAsanHeapRightRedzoneMagic = 0xfb; |
| 150 | const int kAsanHeapFreeMagic = 0xfd; |
| 151 | const int kAsanStackLeftRedzoneMagic = 0xf1; |
| 152 | const int kAsanStackMidRedzoneMagic = 0xf2; |
| 153 | const int kAsanStackRightRedzoneMagic = 0xf3; |
| 154 | const int kAsanStackPartialRedzoneMagic = 0xf4; |
| 155 | const int kAsanStackAfterReturnMagic = 0xf5; |
| 156 | const int kAsanUserPoisonedMemoryMagic = 0xf7; |
| 157 | const int kAsanGlobalRedzoneMagic = 0xf9; |
| 158 | |
| 159 | static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; |
| 160 | static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; |
| 161 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 162 | // -------------------------- Atomic ---------------- {{{1 |
| 163 | static inline int AtomicInc(int *a) { |
| 164 | if (!FLAG_mt) return ++(*a); |
| 165 | #ifdef ANDROID |
| 166 | return __atomic_inc(a) + 1; |
| 167 | #else |
| 168 | return __sync_add_and_fetch(a, 1); |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | static inline int AtomicDec(int *a) { |
| 173 | if (!FLAG_mt) return --(*a); |
| 174 | #ifdef ANDROID |
| 175 | return __atomic_dec(a) - 1; |
| 176 | #else |
| 177 | return __sync_add_and_fetch(a, -1); |
| 178 | #endif |
| 179 | } |
| 180 | |
| 181 | } // namespace __asan |
| 182 | |
| 183 | #endif // ASAN_INTERNAL_H |