Evgeniy Stepanov | 97edeb3 | 2012-12-25 11:53:51 +0000 | [diff] [blame] | 1 | //===-- msan.h --------------------------------------------------*- C++ -*-===// |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 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 MemorySanitizer. |
| 11 | // |
| 12 | // Private MSan header. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef MSAN_H |
| 16 | #define MSAN_H |
| 17 | |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_flags.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_internal_defs.h" |
| 20 | #include "sanitizer_common/sanitizer_stacktrace.h" |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 21 | #include "msan_interface_internal.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 22 | #include "msan_flags.h" |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 23 | #include "ubsan/ubsan_platform.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 24 | |
Evgeniy Stepanov | a897400 | 2013-04-05 12:03:47 +0000 | [diff] [blame] | 25 | #ifndef MSAN_REPLACE_OPERATORS_NEW_AND_DELETE |
| 26 | # define MSAN_REPLACE_OPERATORS_NEW_AND_DELETE 1 |
| 27 | #endif |
| 28 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 29 | #ifndef MSAN_CONTAINS_UBSAN |
| 30 | # define MSAN_CONTAINS_UBSAN CAN_SANITIZE_UB |
| 31 | #endif |
| 32 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 33 | struct MappingDesc { |
| 34 | uptr start; |
| 35 | uptr end; |
| 36 | enum Type { |
| 37 | INVALID, APP, SHADOW, ORIGIN |
| 38 | } type; |
| 39 | const char *name; |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | #if SANITIZER_LINUX && defined(__mips64) |
| 44 | |
| 45 | // Everything is above 0x00e000000000. |
| 46 | const MappingDesc kMemoryLayout[] = { |
| 47 | {0x000000000000ULL, 0x00a000000000ULL, MappingDesc::INVALID, "invalid"}, |
| 48 | {0x00a000000000ULL, 0x00c000000000ULL, MappingDesc::SHADOW, "shadow"}, |
| 49 | {0x00c000000000ULL, 0x00e000000000ULL, MappingDesc::ORIGIN, "origin"}, |
| 50 | {0x00e000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app"}}; |
| 51 | |
| 52 | #define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x4000000000ULL) |
| 53 | #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x002000000000) |
| 54 | |
| 55 | #elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64 |
| 56 | |
| 57 | // Low memory: main binary, MAP_32BIT mappings and modules |
| 58 | // High memory: heap, modules and main thread stack |
| 59 | const MappingDesc kMemoryLayout[] = { |
| 60 | {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "low memory"}, |
| 61 | {0x010000000000ULL, 0x100000000000ULL, MappingDesc::INVALID, "invalid"}, |
| 62 | {0x100000000000ULL, 0x310000000000ULL, MappingDesc::SHADOW, "shadow"}, |
| 63 | {0x310000000000ULL, 0x380000000000ULL, MappingDesc::INVALID, "invalid"}, |
| 64 | {0x380000000000ULL, 0x590000000000ULL, MappingDesc::ORIGIN, "origin"}, |
| 65 | {0x590000000000ULL, 0x600000000000ULL, MappingDesc::INVALID, "invalid"}, |
| 66 | {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}}; |
| 67 | |
| 68 | // Maps low and high app ranges to contiguous space with zero base: |
| 69 | // Low: 0000 0000 0000 - 00ff ffff ffff -> 2000 0000 0000 - 20ff ffff ffff |
| 70 | // High: 6000 0000 0000 - 7fff ffff ffff -> 0000 0000 0000 - 1fff ffff ffff |
| 71 | #define LINEARIZE_MEM(mem) \ |
| 72 | (((uptr)(mem) & ~0xc00000000000ULL) ^ 0x200000000000ULL) |
| 73 | #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x100000000000ULL) |
| 74 | #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x280000000000) |
| 75 | |
| 76 | #elif SANITIZER_LINUX && SANITIZER_WORDSIZE == 64 |
| 77 | |
| 78 | // Requries PIE binary and ASLR enabled. |
| 79 | // Main thread stack and DSOs at 0x7f0000000000 (sometimes 0x7e0000000000). |
| 80 | // Heap at 0x600000000000. |
| 81 | const MappingDesc kMemoryLayout[] = { |
| 82 | {0x000000000000ULL, 0x200000000000ULL, MappingDesc::INVALID, "invalid"}, |
| 83 | {0x200000000000ULL, 0x400000000000ULL, MappingDesc::SHADOW, "shadow"}, |
| 84 | {0x400000000000ULL, 0x600000000000ULL, MappingDesc::ORIGIN, "origin"}, |
| 85 | {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app"}}; |
| 86 | |
| 87 | #define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x400000000000ULL) |
| 88 | #define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x200000000000ULL) |
| 89 | |
| 90 | #else |
| 91 | #error "Unsupported platform" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 92 | #endif |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 93 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 94 | const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]); |
| 95 | |
| 96 | #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem)))) |
| 97 | |
| 98 | #ifndef __clang__ |
| 99 | __attribute__((optimize("unroll-loops"))) |
| 100 | #endif |
| 101 | inline bool addr_is_type(uptr addr, MappingDesc::Type mapping_type) { |
| 102 | // It is critical for performance that this loop is unrolled (because then it is |
| 103 | // simplified into just a few constant comparisons). |
| 104 | #ifdef __clang__ |
| 105 | #pragma unroll |
| 106 | #endif |
| 107 | for (unsigned i = 0; i < kMemoryLayoutSize; ++i) |
| 108 | if (kMemoryLayout[i].type == mapping_type && |
| 109 | addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end) |
| 110 | return true; |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | #define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP) |
| 115 | #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW) |
| 116 | #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN) |
| 117 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 118 | // These constants must be kept in sync with the ones in MemorySanitizer.cc. |
| 119 | const int kMsanParamTlsSize = 800; |
| 120 | const int kMsanRetvalTlsSize = 800; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 121 | |
| 122 | namespace __msan { |
| 123 | extern int msan_inited; |
| 124 | extern bool msan_init_is_running; |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 125 | extern int msan_report_count; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 126 | |
| 127 | bool ProtectRange(uptr beg, uptr end); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 128 | bool InitShadow(bool init_origins); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 129 | char *GetProcSelfMaps(); |
| 130 | void InitializeInterceptors(); |
| 131 | |
Evgeniy Stepanov | 7c6bd40 | 2013-10-22 14:31:30 +0000 | [diff] [blame] | 132 | void MsanAllocatorThreadFinish(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 133 | void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 134 | void *MsanReallocate(StackTrace *stack, void *oldp, uptr size, |
| 135 | uptr alignment, bool zeroise); |
Evgeniy Stepanov | effdc7e | 2013-09-16 11:03:31 +0000 | [diff] [blame] | 136 | void MsanDeallocate(StackTrace *stack, void *ptr); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 137 | void InstallTrapHandler(); |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 138 | void InstallAtExitHandler(); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 139 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 140 | const char *GetStackOriginDescr(u32 id, uptr *pc); |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 141 | |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 142 | void EnterSymbolizer(); |
| 143 | void ExitSymbolizer(); |
| 144 | bool IsInSymbolizer(); |
| 145 | |
Reid Kleckner | 93c2602 | 2013-03-06 16:11:58 +0000 | [diff] [blame] | 146 | struct SymbolizerScope { |
| 147 | SymbolizerScope() { EnterSymbolizer(); } |
| 148 | ~SymbolizerScope() { ExitSymbolizer(); } |
| 149 | }; |
| 150 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 151 | void MsanDie(); |
| 152 | void PrintWarning(uptr pc, uptr bp); |
| 153 | void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin); |
| 154 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 155 | void GetStackTrace(BufferedStackTrace *stack, uptr max_s, uptr pc, uptr bp, |
Alexey Samsonov | f16dc42 | 2013-11-07 07:28:33 +0000 | [diff] [blame] | 156 | bool request_fast_unwind); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 157 | |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 158 | void ReportUMR(StackTrace *stack, u32 origin); |
| 159 | void ReportExpectedUMRNotFound(StackTrace *stack); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 160 | void ReportStats(); |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 161 | void ReportAtExitStatistics(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 162 | void DescribeMemoryRange(const void *x, uptr size); |
| 163 | void ReportUMRInsideAddressRange(const char *what, const void *start, uptr size, |
| 164 | uptr offset); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 165 | |
Alexey Samsonov | c2918bf | 2013-06-27 07:50:56 +0000 | [diff] [blame] | 166 | // Unpoison first n function arguments. |
| 167 | void UnpoisonParam(uptr n); |
Evgeniy Stepanov | 91659d5 | 2013-08-27 12:59:39 +0000 | [diff] [blame] | 168 | void UnpoisonThreadLocalState(); |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 169 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 170 | // Returns a "chained" origin id, pointing to the given stack trace followed by |
| 171 | // the previous origin id. |
| 172 | u32 ChainOrigin(u32 id, StackTrace *stack); |
| 173 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 174 | const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1; |
| 175 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 176 | #define GET_MALLOC_STACK_TRACE \ |
| 177 | BufferedStackTrace stack; \ |
| 178 | if (__msan_get_track_origins() && msan_inited) \ |
| 179 | GetStackTrace(&stack, common_flags()->malloc_context_size, \ |
| 180 | StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), \ |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 181 | common_flags()->fast_unwind_on_malloc) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 182 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 183 | #define GET_STORE_STACK_TRACE_PC_BP(pc, bp) \ |
| 184 | BufferedStackTrace stack; \ |
| 185 | if (__msan_get_track_origins() > 1 && msan_inited) \ |
| 186 | GetStackTrace(&stack, flags()->store_context_size, pc, bp, \ |
| 187 | common_flags()->fast_unwind_on_malloc) |
| 188 | |
| 189 | #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \ |
| 190 | BufferedStackTrace stack; \ |
| 191 | if (msan_inited) \ |
| 192 | GetStackTrace(&stack, kStackTraceMax, pc, bp, \ |
| 193 | common_flags()->fast_unwind_on_fatal) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 194 | |
| 195 | #define GET_STORE_STACK_TRACE \ |
| 196 | GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME()) |
| 197 | |
Evgeniy Stepanov | 0e38a67 | 2013-08-27 14:08:15 +0000 | [diff] [blame] | 198 | class ScopedThreadLocalStateBackup { |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 199 | public: |
Evgeniy Stepanov | 0e38a67 | 2013-08-27 14:08:15 +0000 | [diff] [blame] | 200 | ScopedThreadLocalStateBackup() { Backup(); } |
| 201 | ~ScopedThreadLocalStateBackup() { Restore(); } |
| 202 | void Backup(); |
| 203 | void Restore(); |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 204 | private: |
Evgeniy Stepanov | 0e38a67 | 2013-08-27 14:08:15 +0000 | [diff] [blame] | 205 | u64 va_arg_overflow_size_tls; |
| 206 | }; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 207 | |
| 208 | extern void (*death_callback)(void); |
| 209 | |
| 210 | void MsanTSDInit(void (*destructor)(void *tsd)); |
| 211 | void *MsanTSDGet(); |
| 212 | void MsanTSDSet(void *tsd); |
| 213 | void MsanTSDDtor(void *tsd); |
| 214 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 215 | } // namespace __msan |
| 216 | |
Evgeniy Stepanov | 5c48a8c | 2013-08-02 14:26:58 +0000 | [diff] [blame] | 217 | #define MSAN_MALLOC_HOOK(ptr, size) \ |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 218 | if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size) |
Evgeniy Stepanov | 5c48a8c | 2013-08-02 14:26:58 +0000 | [diff] [blame] | 219 | #define MSAN_FREE_HOOK(ptr) \ |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 220 | if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr) |
Evgeniy Stepanov | 5c48a8c | 2013-08-02 14:26:58 +0000 | [diff] [blame] | 221 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 222 | #endif // MSAN_H |