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