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 | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame^] | 17 | #include "sanitizer_common/sanitizer_internal_defs.h" |
Kostya Serebryany | 16e0075 | 2012-05-31 13:42:53 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_libc.h" |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 19 | |
Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 20 | #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) |
Kostya Serebryany | d6567c5 | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 21 | # error "This operating system is not supported by AddressSanitizer" |
| 22 | #endif |
| 23 | |
Kostya Serebryany | 8582208 | 2012-01-30 20:55:02 +0000 | [diff] [blame] | 24 | #if defined(_WIN32) |
Alexander Potapenko | 71d3b39 | 2012-02-08 14:14:18 +0000 | [diff] [blame] | 25 | extern "C" void* _ReturnAddress(void); |
| 26 | # pragma intrinsic(_ReturnAddress) |
Alexey Samsonov | adf2b03 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 27 | #endif // defined(_WIN32) |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 28 | |
Alexey Samsonov | e4092f6 | 2012-02-22 12:54:04 +0000 | [diff] [blame] | 29 | // Limits for integral types. We have to redefine it in case we don't |
| 30 | // have stdint.h (like in Visual Studio 9). |
| 31 | #if __WORDSIZE == 64 |
| 32 | # define __INT64_C(c) c ## L |
| 33 | # define __UINT64_C(c) c ## UL |
| 34 | #else |
| 35 | # define __INT64_C(c) c ## LL |
| 36 | # define __UINT64_C(c) c ## ULL |
| 37 | #endif // __WORDSIZE == 64 |
Alexey Samsonov | bfc694d | 2012-02-22 16:12:46 +0000 | [diff] [blame] | 38 | #undef INT32_MIN |
| 39 | #define INT32_MIN (-2147483647-1) |
| 40 | #undef INT32_MAX |
| 41 | #define INT32_MAX (2147483647) |
| 42 | #undef UINT32_MAX |
| 43 | #define UINT32_MAX (4294967295U) |
| 44 | #undef INT64_MIN |
| 45 | #define INT64_MIN (-__INT64_C(9223372036854775807)-1) |
| 46 | #undef INT64_MAX |
| 47 | #define INT64_MAX (__INT64_C(9223372036854775807)) |
| 48 | #undef UINT64_MAX |
| 49 | #define UINT64_MAX (__UINT64_C(18446744073709551615)) |
Alexey Samsonov | e4092f6 | 2012-02-22 12:54:04 +0000 | [diff] [blame] | 50 | |
Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 51 | #define ASAN_DEFAULT_FAILURE_EXITCODE 1 |
| 52 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 53 | #if defined(__linux__) |
| 54 | # define ASAN_LINUX 1 |
| 55 | #else |
| 56 | # define ASAN_LINUX 0 |
| 57 | #endif |
| 58 | |
| 59 | #if defined(__APPLE__) |
| 60 | # define ASAN_MAC 1 |
| 61 | #else |
| 62 | # define ASAN_MAC 0 |
| 63 | #endif |
| 64 | |
| 65 | #if defined(_WIN32) |
| 66 | # define ASAN_WINDOWS 1 |
| 67 | #else |
| 68 | # define ASAN_WINDOWS 0 |
| 69 | #endif |
| 70 | |
Alexey Samsonov | 34a3202 | 2012-03-26 09:07:29 +0000 | [diff] [blame] | 71 | #define ASAN_POSIX (ASAN_LINUX || ASAN_MAC) |
| 72 | |
Kostya Serebryany | 850a49e | 2012-04-06 20:36:18 +0000 | [diff] [blame] | 73 | #if __has_feature(address_sanitizer) |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 74 | # error "The AddressSanitizer run-time should not be" |
| 75 | " instrumented by AddressSanitizer" |
| 76 | #endif |
| 77 | |
Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 78 | // Build-time configuration options. |
| 79 | |
Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 80 | // If set, asan will install its own SEGV signal handler. |
| 81 | #ifndef ASAN_NEEDS_SEGV |
| 82 | # define ASAN_NEEDS_SEGV 1 |
| 83 | #endif |
| 84 | |
| 85 | // If set, asan will intercept C++ exception api call(s). |
| 86 | #ifndef ASAN_HAS_EXCEPTIONS |
| 87 | # define ASAN_HAS_EXCEPTIONS 1 |
| 88 | #endif |
| 89 | |
| 90 | // If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET |
| 91 | // provided by the instrumented objects. Otherwise constants are used. |
| 92 | #ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET |
| 93 | # define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0 |
| 94 | #endif |
| 95 | |
Evgeniy Stepanov | 8ae44ac | 2012-02-27 13:07:29 +0000 | [diff] [blame] | 96 | // If set, values like allocator chunk size, as well as defaults for some flags |
| 97 | // will be changed towards less memory overhead. |
| 98 | #ifndef ASAN_LOW_MEMORY |
| 99 | # define ASAN_LOW_MEMORY 0 |
| 100 | #endif |
| 101 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 102 | // All internal functions in asan reside inside the __asan namespace |
| 103 | // to avoid namespace collisions with the user programs. |
| 104 | // Seperate namespace also makes it simpler to distinguish the asan run-time |
| 105 | // functions from the instrumented user code in a profile. |
| 106 | namespace __asan { |
| 107 | |
| 108 | class AsanThread; |
| 109 | struct AsanStackTrace; |
| 110 | |
Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 111 | // asan_rtl.cc |
Timur Iskhodzhanov | 23bd2bb | 2012-03-13 16:12:03 +0000 | [diff] [blame] | 112 | void NORETURN CheckFailed(const char *cond, const char *file, int line); |
Timur Iskhodzhanov | b55c88d | 2012-03-13 16:29:25 +0000 | [diff] [blame] | 113 | void NORETURN ShowStatsAndAbort(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 114 | |
Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 115 | // asan_globals.cc |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 116 | bool DescribeAddrIfGlobal(uptr addr); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 117 | |
Alexey Samsonov | 4d5f98d | 2012-04-06 08:21:08 +0000 | [diff] [blame] | 118 | void ReplaceOperatorsNewAndDelete(); |
Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 119 | // asan_malloc_linux.cc / asan_malloc_mac.cc |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 120 | void ReplaceSystemMalloc(); |
| 121 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 122 | void OutOfMemoryMessageAndDie(const char *mem_type, uptr size); |
Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 123 | |
Alexander Potapenko | f73a6a3 | 2012-02-13 17:09:40 +0000 | [diff] [blame] | 124 | // asan_linux.cc / asan_mac.cc / asan_win.cc |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 125 | void *AsanDoesNotSupportStaticLinkage(); |
Alexander Potapenko | f73a6a3 | 2012-02-13 17:09:40 +0000 | [diff] [blame] | 126 | bool AsanShadowRangeIsAvailable(); |
Alexander Potapenko | 1e316d7 | 2012-01-13 12:59:48 +0000 | [diff] [blame] | 127 | const char *AsanGetEnv(const char *name); |
Alexander Potapenko | 99d17eb | 2012-02-22 09:11:55 +0000 | [diff] [blame] | 128 | void AsanDumpProcessMap(); |
Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 129 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 130 | void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size); |
| 131 | void *AsanMmapFixedReserve(uptr fixed_addr, uptr size); |
| 132 | void *AsanMprotect(uptr fixed_addr, uptr size); |
| 133 | void *AsanMmapSomewhereOrDie(uptr size, const char *where); |
| 134 | void AsanUnmapOrDie(void *ptr, uptr size); |
Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 135 | |
Kostya Serebryany | ef14ff6 | 2012-01-06 02:12:25 +0000 | [diff] [blame] | 136 | void AsanDisableCoreDumper(); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 137 | void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp); |
Kostya Serebryany | ef14ff6 | 2012-01-06 02:12:25 +0000 | [diff] [blame] | 138 | |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 139 | bool AsanInterceptsSignal(int signum); |
Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 140 | void SetAlternateSignalStack(); |
| 141 | void UnsetAlternateSignalStack(); |
Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 142 | void InstallSignalHandlers(); |
Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 143 | int GetPid(); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 144 | uptr GetThreadSelf(); |
Kostya Serebryany | dde7c33 | 2012-01-11 02:39:16 +0000 | [diff] [blame] | 145 | int AtomicInc(int *a); |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 146 | u16 AtomicExchange(u16 *a, u16 new_val); |
Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 147 | |
| 148 | // Wrapper for TLS/TSD. |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 149 | void AsanTSDInit(void (*destructor)(void *tsd)); |
Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 150 | void *AsanTSDGet(); |
| 151 | void AsanTSDSet(void *tsd); |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 152 | |
Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 153 | // Opens the file 'file_name" and reads up to 'max_len' bytes. |
| 154 | // The resulting buffer is mmaped and stored in '*buff'. |
| 155 | // The size of the mmaped region is stored in '*buff_size', |
Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 156 | // Returns the number of read bytes or 0 if file can not be opened. |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 157 | uptr ReadFileToBuffer(const char *file_name, char **buff, |
| 158 | uptr *buff_size, uptr max_len); |
Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 159 | |
Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 160 | // asan_printf.cc |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 161 | void RawWrite(const char *buffer); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 162 | int SNPrintf(char *buffer, uptr length, const char *format, ...); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 163 | void Printf(const char *format, ...); |
| 164 | void Report(const char *format, ...); |
| 165 | |
Kostya Serebryany | 2d8b3bd | 2011-12-02 18:42:04 +0000 | [diff] [blame] | 166 | // Don't use std::min and std::max, to minimize dependency on libstdc++. |
| 167 | template<class T> T Min(T a, T b) { return a < b ? a : b; } |
| 168 | template<class T> T Max(T a, T b) { return a > b ? a : b; } |
| 169 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 170 | void SortArray(uptr *array, uptr size); |
Kostya Serebryany | 25c7178 | 2012-03-10 01:30:01 +0000 | [diff] [blame] | 171 | |
Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 172 | // asan_poisoning.cc |
| 173 | // Poisons the shadow memory for "size" bytes starting from "addr". |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 174 | void PoisonShadow(uptr addr, uptr size, u8 value); |
Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 175 | // Poisons the shadow memory for "redzone_size" bytes starting from |
| 176 | // "addr + size". |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 177 | void PoisonShadowPartialRightRedzone(uptr addr, |
| 178 | uptr size, |
| 179 | uptr redzone_size, |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 180 | u8 value); |
Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 181 | |
Alexey Samsonov | 38dd4ed | 2012-03-20 10:54:40 +0000 | [diff] [blame] | 182 | // Platfrom-specific options. |
| 183 | #ifdef __APPLE__ |
| 184 | bool PlatformHasDifferentMemcpyAndMemmove(); |
| 185 | # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \ |
| 186 | (PlatformHasDifferentMemcpyAndMemmove()) |
| 187 | #else |
| 188 | # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true |
| 189 | #endif // __APPLE__ |
| 190 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 191 | extern uptr FLAG_quarantine_size; |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 192 | extern s64 FLAG_demangle; |
Alexander Potapenko | 62f10e7 | 2012-05-28 16:21:19 +0000 | [diff] [blame] | 193 | extern bool FLAG_symbolize; |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 194 | extern s64 FLAG_v; |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 195 | extern uptr FLAG_redzone; |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 196 | extern s64 FLAG_debug; |
Alexander Potapenko | 62f10e7 | 2012-05-28 16:21:19 +0000 | [diff] [blame] | 197 | extern bool FLAG_poison_shadow; |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 198 | extern s64 FLAG_report_globals; |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 199 | extern uptr FLAG_malloc_context_size; |
Alexander Potapenko | 62f10e7 | 2012-05-28 16:21:19 +0000 | [diff] [blame] | 200 | extern bool FLAG_replace_str; |
| 201 | extern bool FLAG_replace_intrin; |
| 202 | extern bool FLAG_replace_cfallocator; |
| 203 | extern bool FLAG_fast_unwind; |
| 204 | extern bool FLAG_use_fake_stack; |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 205 | extern uptr FLAG_max_malloc_fill_size; |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 206 | extern s64 FLAG_exitcode; |
Alexander Potapenko | 62f10e7 | 2012-05-28 16:21:19 +0000 | [diff] [blame] | 207 | extern bool FLAG_allow_user_poisoning; |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 208 | extern s64 FLAG_sleep_before_dying; |
Alexander Potapenko | 62f10e7 | 2012-05-28 16:21:19 +0000 | [diff] [blame] | 209 | extern bool FLAG_handle_segv; |
| 210 | extern bool FLAG_use_sigaltstack; |
| 211 | extern bool FLAG_check_malloc_usable_size; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 212 | |
| 213 | extern int asan_inited; |
| 214 | // Used to avoid infinite recursion in __asan_init(). |
| 215 | extern bool asan_init_is_running; |
| 216 | |
| 217 | enum LinkerInitialized { LINKER_INITIALIZED = 0 }; |
| 218 | |
Timur Iskhodzhanov | b55c88d | 2012-03-13 16:29:25 +0000 | [diff] [blame] | 219 | void NORETURN AsanDie(); |
Kostya Serebryany | e1fe0fd | 2012-02-13 21:24:29 +0000 | [diff] [blame] | 220 | void SleepForSeconds(int seconds); |
Timur Iskhodzhanov | b55c88d | 2012-03-13 16:29:25 +0000 | [diff] [blame] | 221 | void NORETURN Exit(int exitcode); |
Kostya Serebryany | f8e6fee | 2012-04-06 01:27:11 +0000 | [diff] [blame] | 222 | void NORETURN Abort(); |
Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 223 | int Atexit(void (*function)(void)); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 224 | |
| 225 | #define CHECK(cond) do { if (!(cond)) { \ |
| 226 | CheckFailed(#cond, __FILE__, __LINE__); \ |
| 227 | }}while(0) |
| 228 | |
| 229 | #define RAW_CHECK_MSG(expr, msg) do { \ |
| 230 | if (!(expr)) { \ |
| 231 | RawWrite(msg); \ |
Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 232 | AsanDie(); \ |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 233 | } \ |
| 234 | } while (0) |
| 235 | |
| 236 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) |
| 237 | |
| 238 | #define UNIMPLEMENTED() CHECK("unimplemented" && 0) |
| 239 | |
| 240 | #define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) |
| 241 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 242 | const uptr kWordSize = __WORDSIZE / 8; |
| 243 | const uptr kWordSizeInBits = 8 * kWordSize; |
| 244 | const uptr kPageSizeBits = 12; |
| 245 | const uptr kPageSize = 1UL << kPageSizeBits; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 246 | |
Timur Iskhodzhanov | 8c505ef | 2012-05-21 14:25:36 +0000 | [diff] [blame] | 247 | #if !defined(_WIN32) || defined(__clang__) |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 248 | # define GET_CALLER_PC() (uptr)__builtin_return_address(0) |
| 249 | # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) |
Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 250 | #else |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 251 | # define GET_CALLER_PC() (uptr)_ReturnAddress() |
Kostya Serebryany | 1c83ae3 | 2012-02-07 18:23:54 +0000 | [diff] [blame] | 252 | // CaptureStackBackTrace doesn't need to know BP on Windows. |
| 253 | // FIXME: This macro is still used when printing error reports though it's not |
| 254 | // clear if the BP value is needed in the ASan reports on Windows. |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 255 | # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF |
Timur Iskhodzhanov | 8c505ef | 2012-05-21 14:25:36 +0000 | [diff] [blame] | 256 | #endif |
| 257 | |
| 258 | #ifndef _WIN32 |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 259 | const uptr kMmapGranularity = kPageSize; |
Timur Iskhodzhanov | 8c505ef | 2012-05-21 14:25:36 +0000 | [diff] [blame] | 260 | # define THREAD_CALLING_CONV |
| 261 | typedef void* thread_return_t; |
| 262 | #else |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 263 | const uptr kMmapGranularity = 1UL << 16; |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 264 | # define THREAD_CALLING_CONV __stdcall |
| 265 | typedef DWORD thread_return_t; |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 266 | |
| 267 | # ifndef ASAN_USE_EXTERNAL_SYMBOLIZER |
Timur Iskhodzhanov | 8c505ef | 2012-05-21 14:25:36 +0000 | [diff] [blame] | 268 | # define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize |
| 269 | bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size); |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 270 | # endif |
Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 271 | #endif |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 272 | |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 273 | typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); |
| 274 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 275 | // These magic values are written to shadow for better error reporting. |
| 276 | const int kAsanHeapLeftRedzoneMagic = 0xfa; |
| 277 | const int kAsanHeapRightRedzoneMagic = 0xfb; |
| 278 | const int kAsanHeapFreeMagic = 0xfd; |
| 279 | const int kAsanStackLeftRedzoneMagic = 0xf1; |
| 280 | const int kAsanStackMidRedzoneMagic = 0xf2; |
| 281 | const int kAsanStackRightRedzoneMagic = 0xf3; |
| 282 | const int kAsanStackPartialRedzoneMagic = 0xf4; |
| 283 | const int kAsanStackAfterReturnMagic = 0xf5; |
| 284 | const int kAsanUserPoisonedMemoryMagic = 0xf7; |
| 285 | const int kAsanGlobalRedzoneMagic = 0xf9; |
Kostya Serebryany | 6b30e2c | 2011-12-15 17:41:30 +0000 | [diff] [blame] | 286 | const int kAsanInternalHeapMagic = 0xfe; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 287 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 288 | static const uptr kCurrentStackFrameMagic = 0x41B58AB3; |
| 289 | static const uptr kRetiredStackFrameMagic = 0x45E0360E; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 290 | |
Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 291 | // --------------------------- Bit twiddling ------- {{{1 |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 292 | inline bool IsPowerOfTwo(uptr x) { |
Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 293 | return (x & (x - 1)) == 0; |
| 294 | } |
| 295 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 296 | inline uptr RoundUpTo(uptr size, uptr boundary) { |
Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 297 | CHECK(IsPowerOfTwo(boundary)); |
| 298 | return (size + boundary - 1) & ~(boundary - 1); |
| 299 | } |
| 300 | |
Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 301 | // -------------------------- LowLevelAllocator ----- {{{1 |
| 302 | // A simple low-level memory allocator for internal use. |
| 303 | class LowLevelAllocator { |
| 304 | public: |
| 305 | explicit LowLevelAllocator(LinkerInitialized) {} |
| 306 | // 'size' must be a power of two. |
| 307 | // Requires an external lock. |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 308 | void *Allocate(uptr size); |
Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 309 | private: |
| 310 | char *allocated_end_; |
| 311 | char *allocated_current_; |
| 312 | }; |
| 313 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 314 | } // namespace __asan |
| 315 | |
| 316 | #endif // ASAN_INTERNAL_H |