Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1 | //===-- msan.cc -----------------------------------------------------------===// |
| 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 | // MemorySanitizer runtime. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "msan.h" |
| 16 | #include "sanitizer_common/sanitizer_atomic.h" |
| 17 | #include "sanitizer_common/sanitizer_common.h" |
| 18 | #include "sanitizer_common/sanitizer_flags.h" |
| 19 | #include "sanitizer_common/sanitizer_libc.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_procmaps.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_stacktrace.h" |
| 22 | #include "sanitizer_common/sanitizer_symbolizer.h" |
| 23 | |
| 24 | #include "interception/interception.h" |
| 25 | |
| 26 | // ACHTUNG! No system header includes in this file. |
| 27 | |
| 28 | using namespace __sanitizer; |
| 29 | |
| 30 | // Globals. |
| 31 | static THREADLOCAL int msan_expect_umr = 0; |
| 32 | static THREADLOCAL int msan_expected_umr_found = 0; |
| 33 | |
| 34 | static int msan_running_under_dr = 0; |
| 35 | |
| 36 | SANITIZER_INTERFACE_ATTRIBUTE |
| 37 | THREADLOCAL u64 __msan_param_tls[kMsanParamTlsSizeInWords]; |
| 38 | |
| 39 | SANITIZER_INTERFACE_ATTRIBUTE |
| 40 | THREADLOCAL u32 __msan_param_origin_tls[kMsanParamTlsSizeInWords]; |
| 41 | |
| 42 | SANITIZER_INTERFACE_ATTRIBUTE |
| 43 | THREADLOCAL u64 __msan_retval_tls[kMsanRetvalTlsSizeInWords]; |
| 44 | |
| 45 | SANITIZER_INTERFACE_ATTRIBUTE |
| 46 | THREADLOCAL u32 __msan_retval_origin_tls; |
| 47 | |
| 48 | SANITIZER_INTERFACE_ATTRIBUTE |
| 49 | THREADLOCAL u64 __msan_va_arg_tls[kMsanParamTlsSizeInWords]; |
| 50 | |
| 51 | SANITIZER_INTERFACE_ATTRIBUTE |
| 52 | THREADLOCAL u64 __msan_va_arg_overflow_size_tls; |
| 53 | |
| 54 | SANITIZER_INTERFACE_ATTRIBUTE |
| 55 | THREADLOCAL u32 __msan_origin_tls; |
| 56 | |
| 57 | static THREADLOCAL struct { |
| 58 | uptr stack_top, stack_bottom; |
| 59 | } __msan_stack_bounds; |
| 60 | |
Evgeniy Stepanov | 27ee7a8 | 2013-09-23 13:26:31 +0000 | [diff] [blame] | 61 | static THREADLOCAL int is_in_symbolizer; |
| 62 | static THREADLOCAL int is_in_loader; |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 63 | |
Evgeniy Stepanov | a879f10 | 2013-05-31 13:04:07 +0000 | [diff] [blame] | 64 | extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_track_origins; |
Evgeniy Stepanov | b36779d | 2013-05-31 12:04:08 +0000 | [diff] [blame] | 65 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 66 | int __msan_get_track_origins() { |
Evgeniy Stepanov | b36779d | 2013-05-31 12:04:08 +0000 | [diff] [blame] | 67 | return &__msan_track_origins ? __msan_track_origins : 0; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Evgeniy Stepanov | bb881c7 | 2013-06-21 12:37:58 +0000 | [diff] [blame] | 70 | extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_keep_going; |
| 71 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 72 | namespace __msan { |
| 73 | |
| 74 | static bool IsRunningUnderDr() { |
| 75 | bool result = false; |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 76 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 77 | const sptr kBufSize = 4095; |
| 78 | char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__); |
| 79 | while (proc_maps.Next(/* start */0, /* end */0, /* file_offset */0, |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 80 | filename, kBufSize, /* protection */0)) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 81 | if (internal_strstr(filename, "libdynamorio") != 0) { |
| 82 | result = true; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | UnmapOrDie(filename, kBufSize); |
| 87 | return result; |
| 88 | } |
| 89 | |
Evgeniy Stepanov | 27ee7a8 | 2013-09-23 13:26:31 +0000 | [diff] [blame] | 90 | void EnterSymbolizer() { ++is_in_symbolizer; } |
| 91 | void ExitSymbolizer() { --is_in_symbolizer; } |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 92 | bool IsInSymbolizer() { return is_in_symbolizer; } |
| 93 | |
Evgeniy Stepanov | 29296c0 | 2013-09-23 13:34:26 +0000 | [diff] [blame] | 94 | void EnterLoader() { ++is_in_loader; } |
Evgeniy Stepanov | 27ee7a8 | 2013-09-23 13:26:31 +0000 | [diff] [blame] | 95 | void ExitLoader() { --is_in_loader; } |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 96 | |
| 97 | extern "C" { |
| 98 | SANITIZER_INTERFACE_ATTRIBUTE |
| 99 | bool __msan_is_in_loader() { return is_in_loader; } |
| 100 | } |
| 101 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 102 | static Flags msan_flags; |
| 103 | |
| 104 | Flags *flags() { |
| 105 | return &msan_flags; |
| 106 | } |
| 107 | |
| 108 | int msan_inited = 0; |
| 109 | bool msan_init_is_running; |
| 110 | |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 111 | int msan_report_count = 0; |
| 112 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 113 | // Array of stack origins. |
| 114 | // FIXME: make it resizable. |
| 115 | static const uptr kNumStackOriginDescrs = 1024 * 1024; |
| 116 | static const char *StackOriginDescr[kNumStackOriginDescrs]; |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 117 | static uptr StackOriginPC[kNumStackOriginDescrs]; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 118 | static atomic_uint32_t NumStackOriginDescrs; |
| 119 | |
| 120 | static void ParseFlagsFromString(Flags *f, const char *str) { |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 121 | ParseCommonFlagsFromString(str); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 122 | ParseFlag(str, &f->poison_heap_with_zeroes, "poison_heap_with_zeroes"); |
| 123 | ParseFlag(str, &f->poison_stack_with_zeroes, "poison_stack_with_zeroes"); |
| 124 | ParseFlag(str, &f->poison_in_malloc, "poison_in_malloc"); |
Evgeniy Stepanov | effdc7e | 2013-09-16 11:03:31 +0000 | [diff] [blame] | 125 | ParseFlag(str, &f->poison_in_malloc, "poison_in_free"); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 126 | ParseFlag(str, &f->exit_code, "exit_code"); |
| 127 | if (f->exit_code < 0 || f->exit_code > 127) { |
| 128 | Printf("Exit code not in [0, 128) range: %d\n", f->exit_code); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 129 | Die(); |
| 130 | } |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 131 | ParseFlag(str, &f->report_umrs, "report_umrs"); |
Evgeniy Stepanov | a213ab6 | 2013-04-05 11:59:16 +0000 | [diff] [blame] | 132 | ParseFlag(str, &f->wrap_signals, "wrap_signals"); |
Dmitry Vyukov | dbac0a4 | 2013-08-13 15:33:00 +0000 | [diff] [blame] | 133 | |
| 134 | // keep_going is an old name for halt_on_error, |
| 135 | // and it has inverse meaning. |
| 136 | f->halt_on_error = !f->halt_on_error; |
| 137 | ParseFlag(str, &f->halt_on_error, "keep_going"); |
| 138 | f->halt_on_error = !f->halt_on_error; |
| 139 | ParseFlag(str, &f->halt_on_error, "halt_on_error"); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void InitializeFlags(Flags *f, const char *options) { |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 143 | CommonFlags *cf = common_flags(); |
| 144 | cf->external_symbolizer_path = GetEnv("MSAN_SYMBOLIZER_PATH"); |
| 145 | cf->strip_path_prefix = ""; |
| 146 | cf->fast_unwind_on_fatal = false; |
| 147 | cf->fast_unwind_on_malloc = true; |
| 148 | cf->malloc_context_size = 20; |
Evgeniy Stepanov | 745dd0d | 2013-06-07 13:00:47 +0000 | [diff] [blame] | 149 | cf->handle_ioctl = true; |
Evgeniy Stepanov | b624606 | 2013-06-25 13:50:44 +0000 | [diff] [blame] | 150 | cf->log_path = 0; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 151 | |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 152 | internal_memset(f, 0, sizeof(*f)); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 153 | f->poison_heap_with_zeroes = false; |
| 154 | f->poison_stack_with_zeroes = false; |
| 155 | f->poison_in_malloc = true; |
Evgeniy Stepanov | effdc7e | 2013-09-16 11:03:31 +0000 | [diff] [blame] | 156 | f->poison_in_free = true; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 157 | f->exit_code = 77; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 158 | f->report_umrs = true; |
Evgeniy Stepanov | a213ab6 | 2013-04-05 11:59:16 +0000 | [diff] [blame] | 159 | f->wrap_signals = true; |
Dmitry Vyukov | dbac0a4 | 2013-08-13 15:33:00 +0000 | [diff] [blame] | 160 | f->halt_on_error = !&__msan_keep_going; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 161 | |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 162 | // Override from user-specified string. |
| 163 | if (__msan_default_options) |
| 164 | ParseFlagsFromString(f, __msan_default_options()); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 165 | ParseFlagsFromString(f, options); |
| 166 | } |
| 167 | |
| 168 | static void GetCurrentStackBounds(uptr *stack_top, uptr *stack_bottom) { |
| 169 | if (__msan_stack_bounds.stack_top == 0) { |
| 170 | // Break recursion (GetStackTrace -> GetThreadStackTopAndBottom -> |
| 171 | // realloc -> GetStackTrace). |
| 172 | __msan_stack_bounds.stack_top = __msan_stack_bounds.stack_bottom = 1; |
| 173 | GetThreadStackTopAndBottom(/* at_initialization */false, |
| 174 | &__msan_stack_bounds.stack_top, |
| 175 | &__msan_stack_bounds.stack_bottom); |
| 176 | } |
| 177 | *stack_top = __msan_stack_bounds.stack_top; |
| 178 | *stack_bottom = __msan_stack_bounds.stack_bottom; |
| 179 | } |
| 180 | |
Evgeniy Stepanov | efbc435 | 2013-02-19 12:43:18 +0000 | [diff] [blame] | 181 | void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp, |
| 182 | bool fast) { |
Reid Kleckner | 93c2602 | 2013-03-06 16:11:58 +0000 | [diff] [blame] | 183 | if (!fast) { |
| 184 | // Block reports from our interceptors during _Unwind_Backtrace. |
| 185 | SymbolizerScope sym_scope; |
Evgeniy Stepanov | efbc435 | 2013-02-19 12:43:18 +0000 | [diff] [blame] | 186 | return stack->SlowUnwindStack(pc, max_s); |
Reid Kleckner | 93c2602 | 2013-03-06 16:11:58 +0000 | [diff] [blame] | 187 | } |
Evgeniy Stepanov | efbc435 | 2013-02-19 12:43:18 +0000 | [diff] [blame] | 188 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 189 | uptr stack_top, stack_bottom; |
| 190 | GetCurrentStackBounds(&stack_top, &stack_bottom); |
Alexey Samsonov | 064da32 | 2013-10-11 09:58:30 +0000 | [diff] [blame] | 191 | stack->FastUnwindStack(pc, bp, stack_top, stack_bottom, max_s); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 194 | void PrintWarning(uptr pc, uptr bp) { |
| 195 | PrintWarningWithOrigin(pc, bp, __msan_origin_tls); |
| 196 | } |
| 197 | |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 198 | bool OriginIsValid(u32 origin) { |
| 199 | return origin != 0 && origin != (u32)-1; |
| 200 | } |
| 201 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 202 | void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 203 | if (msan_expect_umr) { |
| 204 | // Printf("Expected UMR\n"); |
| 205 | __msan_origin_tls = origin; |
| 206 | msan_expected_umr_found = 1; |
| 207 | return; |
| 208 | } |
| 209 | |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 210 | ++msan_report_count; |
| 211 | |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 212 | StackTrace stack; |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 213 | GetStackTrace(&stack, kStackTraceMax, pc, bp, |
| 214 | common_flags()->fast_unwind_on_fatal); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 215 | |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 216 | u32 report_origin = |
Evgeniy Stepanov | b36779d | 2013-05-31 12:04:08 +0000 | [diff] [blame] | 217 | (__msan_get_track_origins() && OriginIsValid(origin)) ? origin : 0; |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 218 | ReportUMR(&stack, report_origin); |
| 219 | |
Evgeniy Stepanov | b36779d | 2013-05-31 12:04:08 +0000 | [diff] [blame] | 220 | if (__msan_get_track_origins() && !OriginIsValid(origin)) { |
Evgeniy Stepanov | 48ed058 | 2013-10-24 11:52:48 +0000 | [diff] [blame^] | 221 | Printf( |
| 222 | " ORIGIN: invalid (%x). Might be a bug in MemorySanitizer origin " |
| 223 | "tracking.\n This could still be a bug in your code, too!\n", |
| 224 | origin); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Alexey Samsonov | c2918bf | 2013-06-27 07:50:56 +0000 | [diff] [blame] | 228 | void UnpoisonParam(uptr n) { |
| 229 | internal_memset(__msan_param_tls, 0, n * sizeof(*__msan_param_tls)); |
| 230 | } |
| 231 | |
Evgeniy Stepanov | 0e38a67 | 2013-08-27 14:08:15 +0000 | [diff] [blame] | 232 | // Backup MSan runtime TLS state. |
| 233 | // Implementation must be async-signal-safe. |
| 234 | // Instances of this class may live on the signal handler stack, and data size |
| 235 | // may be an issue. |
| 236 | void ScopedThreadLocalStateBackup::Backup() { |
| 237 | va_arg_overflow_size_tls = __msan_va_arg_overflow_size_tls; |
| 238 | } |
| 239 | |
| 240 | void ScopedThreadLocalStateBackup::Restore() { |
| 241 | // A lame implementation that only keeps essential state and resets the rest. |
| 242 | __msan_va_arg_overflow_size_tls = va_arg_overflow_size_tls; |
| 243 | |
Evgeniy Stepanov | 91659d5 | 2013-08-27 12:59:39 +0000 | [diff] [blame] | 244 | internal_memset(__msan_param_tls, 0, sizeof(__msan_param_tls)); |
| 245 | internal_memset(__msan_retval_tls, 0, sizeof(__msan_retval_tls)); |
| 246 | internal_memset(__msan_va_arg_tls, 0, sizeof(__msan_va_arg_tls)); |
Evgeniy Stepanov | 0e38a67 | 2013-08-27 14:08:15 +0000 | [diff] [blame] | 247 | |
| 248 | if (__msan_get_track_origins()) { |
| 249 | internal_memset(&__msan_retval_origin_tls, 0, sizeof(__msan_retval_tls)); |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 250 | internal_memset(__msan_param_origin_tls, 0, |
| 251 | sizeof(__msan_param_origin_tls)); |
Evgeniy Stepanov | 0e38a67 | 2013-08-27 14:08:15 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | void UnpoisonThreadLocalState() { |
Evgeniy Stepanov | 91659d5 | 2013-08-27 12:59:39 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 258 | const char *GetOriginDescrIfStack(u32 id, uptr *pc) { |
| 259 | if ((id >> 31) == 0) return 0; |
| 260 | id &= (1U << 31) - 1; |
| 261 | CHECK_LT(id, kNumStackOriginDescrs); |
| 262 | if (pc) *pc = StackOriginPC[id]; |
| 263 | return StackOriginDescr[id]; |
| 264 | } |
| 265 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 266 | } // namespace __msan |
| 267 | |
| 268 | // Interface. |
| 269 | |
| 270 | using namespace __msan; |
| 271 | |
| 272 | void __msan_warning() { |
| 273 | GET_CALLER_PC_BP_SP; |
| 274 | (void)sp; |
| 275 | PrintWarning(pc, bp); |
Dmitry Vyukov | dbac0a4 | 2013-08-13 15:33:00 +0000 | [diff] [blame] | 276 | if (__msan::flags()->halt_on_error) { |
Evgeniy Stepanov | bb881c7 | 2013-06-21 12:37:58 +0000 | [diff] [blame] | 277 | Printf("Exiting\n"); |
| 278 | Die(); |
| 279 | } |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void __msan_warning_noreturn() { |
| 283 | GET_CALLER_PC_BP_SP; |
| 284 | (void)sp; |
| 285 | PrintWarning(pc, bp); |
| 286 | Printf("Exiting\n"); |
| 287 | Die(); |
| 288 | } |
| 289 | |
| 290 | void __msan_init() { |
| 291 | if (msan_inited) return; |
| 292 | msan_init_is_running = 1; |
Kostya Serebryany | 859778a | 2013-01-31 14:11:21 +0000 | [diff] [blame] | 293 | SanitizerToolName = "MemorySanitizer"; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 294 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 295 | SetDieCallback(MsanDie); |
Evgeniy Stepanov | 10fd322 | 2013-03-13 09:01:40 +0000 | [diff] [blame] | 296 | InitTlsSize(); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 297 | InitializeInterceptors(); |
Evgeniy Stepanov | cfc29de | 2013-09-27 11:32:21 +0000 | [diff] [blame] | 298 | InstallAtExitHandler(); // Needs __cxa_atexit interceptor. |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 299 | |
Evgeniy Stepanov | a897400 | 2013-04-05 12:03:47 +0000 | [diff] [blame] | 300 | if (MSAN_REPLACE_OPERATORS_NEW_AND_DELETE) |
| 301 | ReplaceOperatorsNewAndDelete(); |
Evgeniy Stepanov | f35eae8 | 2013-02-19 11:09:29 +0000 | [diff] [blame] | 302 | const char *msan_options = GetEnv("MSAN_OPTIONS"); |
| 303 | InitializeFlags(&msan_flags, msan_options); |
Evgeniy Stepanov | b624606 | 2013-06-25 13:50:44 +0000 | [diff] [blame] | 304 | __sanitizer_set_report_path(common_flags()->log_path); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 305 | if (StackSizeIsUnlimited()) { |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 306 | if (common_flags()->verbosity) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 307 | Printf("Unlimited stack, doing reexec\n"); |
| 308 | // A reasonably large stack size. It is bigger than the usual 8Mb, because, |
| 309 | // well, the program could have been run with unlimited stack for a reason. |
| 310 | SetStackSizeLimitInBytes(32 * 1024 * 1024); |
| 311 | ReExec(); |
| 312 | } |
Evgeniy Stepanov | f35eae8 | 2013-02-19 11:09:29 +0000 | [diff] [blame] | 313 | |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 314 | if (common_flags()->verbosity) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 315 | Printf("MSAN_OPTIONS: %s\n", msan_options ? msan_options : "<empty>"); |
Evgeniy Stepanov | f35eae8 | 2013-02-19 11:09:29 +0000 | [diff] [blame] | 316 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 317 | msan_running_under_dr = IsRunningUnderDr(); |
| 318 | __msan_clear_on_return(); |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 319 | if (__msan_get_track_origins() && common_flags()->verbosity > 0) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 320 | Printf("msan_track_origins\n"); |
Evgeniy Stepanov | b36779d | 2013-05-31 12:04:08 +0000 | [diff] [blame] | 321 | if (!InitShadow(/* prot1 */ false, /* prot2 */ true, /* map_shadow */ true, |
| 322 | __msan_get_track_origins())) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 323 | // FIXME: prot1 = false is only required when running under DR. |
Evgeniy Stepanov | 4c9ddc1 | 2012-12-26 06:37:23 +0000 | [diff] [blame] | 324 | Printf("FATAL: MemorySanitizer can not mmap the shadow memory.\n"); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 325 | Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n"); |
Evgeniy Stepanov | 4c9ddc1 | 2012-12-26 06:37:23 +0000 | [diff] [blame] | 326 | Printf("FATAL: Disabling ASLR is known to cause this error.\n"); |
Kostya Serebryany | 4b48f45 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 327 | Printf("FATAL: If running under GDB, try " |
| 328 | "'set disable-randomization off'.\n"); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 329 | DumpProcessMap(); |
| 330 | Die(); |
| 331 | } |
| 332 | |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 333 | const char *external_symbolizer = common_flags()->external_symbolizer_path; |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 334 | bool symbolizer_started = |
| 335 | getSymbolizer()->InitializeExternal(external_symbolizer); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 336 | if (external_symbolizer && external_symbolizer[0]) { |
Alexey Samsonov | 68c016a | 2013-09-03 13:22:51 +0000 | [diff] [blame] | 337 | CHECK(symbolizer_started); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | GetThreadStackTopAndBottom(/* at_initialization */true, |
| 341 | &__msan_stack_bounds.stack_top, |
| 342 | &__msan_stack_bounds.stack_bottom); |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 343 | if (common_flags()->verbosity) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 344 | Printf("MemorySanitizer init done\n"); |
| 345 | msan_init_is_running = 0; |
| 346 | msan_inited = 1; |
| 347 | } |
| 348 | |
| 349 | void __msan_set_exit_code(int exit_code) { |
| 350 | flags()->exit_code = exit_code; |
| 351 | } |
| 352 | |
Evgeniy Stepanov | bb881c7 | 2013-06-21 12:37:58 +0000 | [diff] [blame] | 353 | void __msan_set_keep_going(int keep_going) { |
Dmitry Vyukov | dbac0a4 | 2013-08-13 15:33:00 +0000 | [diff] [blame] | 354 | flags()->halt_on_error = !keep_going; |
Evgeniy Stepanov | bb881c7 | 2013-06-21 12:37:58 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 357 | void __msan_set_expect_umr(int expect_umr) { |
| 358 | if (expect_umr) { |
| 359 | msan_expected_umr_found = 0; |
| 360 | } else if (!msan_expected_umr_found) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 361 | GET_CALLER_PC_BP_SP; |
| 362 | (void)sp; |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 363 | StackTrace stack; |
Evgeniy Stepanov | 58b52b5 | 2013-03-14 11:47:03 +0000 | [diff] [blame] | 364 | GetStackTrace(&stack, kStackTraceMax, pc, bp, |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 365 | common_flags()->fast_unwind_on_fatal); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 366 | ReportExpectedUMRNotFound(&stack); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 367 | Die(); |
| 368 | } |
| 369 | msan_expect_umr = expect_umr; |
| 370 | } |
| 371 | |
| 372 | void __msan_print_shadow(const void *x, uptr size) { |
| 373 | unsigned char *s = (unsigned char*)MEM_TO_SHADOW(x); |
| 374 | u32 *o = (u32*)MEM_TO_ORIGIN(x); |
| 375 | for (uptr i = 0; i < size; i++) { |
| 376 | Printf("%x%x ", s[i] >> 4, s[i] & 0xf); |
| 377 | } |
| 378 | Printf("\n"); |
Evgeniy Stepanov | b36779d | 2013-05-31 12:04:08 +0000 | [diff] [blame] | 379 | if (__msan_get_track_origins()) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 380 | for (uptr i = 0; i < size / 4; i++) { |
| 381 | Printf(" o: %x ", o[i]); |
| 382 | } |
| 383 | Printf("\n"); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void __msan_print_param_shadow() { |
| 388 | for (int i = 0; i < 16; i++) { |
| 389 | Printf("#%d:%zx ", i, __msan_param_tls[i]); |
| 390 | } |
| 391 | Printf("\n"); |
| 392 | } |
| 393 | |
| 394 | sptr __msan_test_shadow(const void *x, uptr size) { |
| 395 | unsigned char *s = (unsigned char*)MEM_TO_SHADOW((uptr)x); |
| 396 | for (uptr i = 0; i < size; ++i) |
| 397 | if (s[i]) |
| 398 | return i; |
| 399 | return -1; |
| 400 | } |
| 401 | |
| 402 | int __msan_set_poison_in_malloc(int do_poison) { |
| 403 | int old = flags()->poison_in_malloc; |
| 404 | flags()->poison_in_malloc = do_poison; |
| 405 | return old; |
| 406 | } |
| 407 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 408 | int __msan_has_dynamic_component() { |
| 409 | return msan_running_under_dr; |
| 410 | } |
| 411 | |
| 412 | NOINLINE |
| 413 | void __msan_clear_on_return() { |
| 414 | __msan_param_tls[0] = 0; |
| 415 | } |
| 416 | |
| 417 | static void* get_tls_base() { |
| 418 | u64 p; |
| 419 | asm("mov %%fs:0, %0" |
| 420 | : "=r"(p) ::); |
| 421 | return (void*)p; |
| 422 | } |
| 423 | |
| 424 | int __msan_get_retval_tls_offset() { |
| 425 | // volatile here is needed to avoid UB, because the compiler thinks that we |
| 426 | // are doing address arithmetics on unrelated pointers, and takes some |
| 427 | // shortcuts |
| 428 | volatile sptr retval_tls_p = (sptr)&__msan_retval_tls; |
| 429 | volatile sptr tls_base_p = (sptr)get_tls_base(); |
| 430 | return retval_tls_p - tls_base_p; |
| 431 | } |
| 432 | |
| 433 | int __msan_get_param_tls_offset() { |
| 434 | // volatile here is needed to avoid UB, because the compiler thinks that we |
| 435 | // are doing address arithmetics on unrelated pointers, and takes some |
| 436 | // shortcuts |
| 437 | volatile sptr param_tls_p = (sptr)&__msan_param_tls; |
| 438 | volatile sptr tls_base_p = (sptr)get_tls_base(); |
| 439 | return param_tls_p - tls_base_p; |
| 440 | } |
| 441 | |
Alexey Samsonov | 11347bf | 2013-04-23 13:34:19 +0000 | [diff] [blame] | 442 | void __msan_partial_poison(const void* data, void* shadow, uptr size) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 443 | internal_memcpy((void*)MEM_TO_SHADOW((uptr)data), shadow, size); |
| 444 | } |
| 445 | |
| 446 | void __msan_load_unpoisoned(void *src, uptr size, void *dst) { |
| 447 | internal_memcpy(dst, src, size); |
| 448 | __msan_unpoison(dst, size); |
| 449 | } |
| 450 | |
Alexey Samsonov | 11347bf | 2013-04-23 13:34:19 +0000 | [diff] [blame] | 451 | void __msan_set_origin(const void *a, uptr size, u32 origin) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 452 | // Origin mapping is 4 bytes per 4 bytes of application memory. |
| 453 | // Here we extend the range such that its left and right bounds are both |
| 454 | // 4 byte aligned. |
Evgeniy Stepanov | b36779d | 2013-05-31 12:04:08 +0000 | [diff] [blame] | 455 | if (!__msan_get_track_origins()) return; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 456 | uptr x = MEM_TO_ORIGIN((uptr)a); |
| 457 | uptr beg = x & ~3UL; // align down. |
| 458 | uptr end = (x + size + 3) & ~3UL; // align up. |
| 459 | u64 origin64 = ((u64)origin << 32) | origin; |
| 460 | // This is like memset, but the value is 32-bit. We unroll by 2 two write |
| 461 | // 64-bits at once. May want to unroll further to get 128-bit stores. |
| 462 | if (beg & 7ULL) { |
| 463 | *(u32*)beg = origin; |
| 464 | beg += 4; |
| 465 | } |
| 466 | for (uptr addr = beg; addr < (end & ~7UL); addr += 8) |
| 467 | *(u64*)addr = origin64; |
| 468 | if (end & 7ULL) |
| 469 | *(u32*)(end - 4) = origin; |
| 470 | } |
| 471 | |
| 472 | // 'descr' is created at compile time and contains '----' in the beginning. |
| 473 | // When we see descr for the first time we replace '----' with a uniq id |
| 474 | // and set the origin to (id | (31-th bit)). |
| 475 | void __msan_set_alloca_origin(void *a, uptr size, const char *descr) { |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 476 | __msan_set_alloca_origin4(a, size, descr, 0); |
| 477 | } |
| 478 | |
| 479 | void __msan_set_alloca_origin4(void *a, uptr size, const char *descr, uptr pc) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 480 | static const u32 dash = '-'; |
| 481 | static const u32 first_timer = |
| 482 | dash + (dash << 8) + (dash << 16) + (dash << 24); |
| 483 | u32 *id_ptr = (u32*)descr; |
| 484 | bool print = false; // internal_strstr(descr + 4, "AllocaTOTest") != 0; |
| 485 | u32 id = *id_ptr; |
| 486 | if (id == first_timer) { |
| 487 | id = atomic_fetch_add(&NumStackOriginDescrs, |
| 488 | 1, memory_order_relaxed); |
| 489 | *id_ptr = id; |
| 490 | CHECK_LT(id, kNumStackOriginDescrs); |
| 491 | StackOriginDescr[id] = descr + 4; |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 492 | StackOriginPC[id] = pc; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 493 | if (print) |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 494 | Printf("First time: id=%d %s %p \n", id, descr + 4, pc); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 495 | } |
| 496 | id |= 1U << 31; |
| 497 | if (print) |
| 498 | Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id); |
| 499 | __msan_set_origin(a, size, id); |
| 500 | } |
| 501 | |
| 502 | const char *__msan_get_origin_descr_if_stack(u32 id) { |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 503 | return GetOriginDescrIfStack(id, 0); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Alexey Samsonov | 11347bf | 2013-04-23 13:34:19 +0000 | [diff] [blame] | 506 | u32 __msan_get_origin(const void *a) { |
Evgeniy Stepanov | b36779d | 2013-05-31 12:04:08 +0000 | [diff] [blame] | 507 | if (!__msan_get_track_origins()) return 0; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 508 | uptr x = (uptr)a; |
| 509 | uptr aligned = x & ~3ULL; |
| 510 | uptr origin_ptr = MEM_TO_ORIGIN(aligned); |
| 511 | return *(u32*)origin_ptr; |
| 512 | } |
| 513 | |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 514 | u32 __msan_get_umr_origin() { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 515 | return __msan_origin_tls; |
| 516 | } |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 517 | |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 518 | u16 __sanitizer_unaligned_load16(const uu16 *p) { |
| 519 | __msan_retval_tls[0] = *(uu16 *)MEM_TO_SHADOW((uptr)p); |
Evgeniy Stepanov | 7ad195c | 2013-10-16 08:25:13 +0000 | [diff] [blame] | 520 | if (__msan_get_track_origins()) |
| 521 | __msan_retval_origin_tls = *(uu32 *)MEM_TO_ORIGIN((uptr)p); |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 522 | return *p; |
Evgeniy Stepanov | f43f602 | 2013-06-04 13:08:36 +0000 | [diff] [blame] | 523 | } |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 524 | u32 __sanitizer_unaligned_load32(const uu32 *p) { |
| 525 | __msan_retval_tls[0] = *(uu32 *)MEM_TO_SHADOW((uptr)p); |
Evgeniy Stepanov | 7ad195c | 2013-10-16 08:25:13 +0000 | [diff] [blame] | 526 | if (__msan_get_track_origins()) |
| 527 | __msan_retval_origin_tls = *(uu32 *)MEM_TO_ORIGIN((uptr)p); |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 528 | return *p; |
Evgeniy Stepanov | f43f602 | 2013-06-04 13:08:36 +0000 | [diff] [blame] | 529 | } |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 530 | u64 __sanitizer_unaligned_load64(const uu64 *p) { |
| 531 | __msan_retval_tls[0] = *(uu64 *)MEM_TO_SHADOW((uptr)p); |
Evgeniy Stepanov | 7ad195c | 2013-10-16 08:25:13 +0000 | [diff] [blame] | 532 | if (__msan_get_track_origins()) |
| 533 | __msan_retval_origin_tls = *(uu32 *)MEM_TO_ORIGIN((uptr)p); |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 534 | return *p; |
Evgeniy Stepanov | f43f602 | 2013-06-04 13:08:36 +0000 | [diff] [blame] | 535 | } |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 536 | void __sanitizer_unaligned_store16(uu16 *p, u16 x) { |
| 537 | *(uu16 *)MEM_TO_SHADOW((uptr)p) = __msan_param_tls[1]; |
Evgeniy Stepanov | 7ad195c | 2013-10-16 08:25:13 +0000 | [diff] [blame] | 538 | if (__msan_get_track_origins()) |
| 539 | *(uu32 *)MEM_TO_ORIGIN((uptr)p) = __msan_param_origin_tls[1]; |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 540 | *p = x; |
Evgeniy Stepanov | f43f602 | 2013-06-04 13:08:36 +0000 | [diff] [blame] | 541 | } |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 542 | void __sanitizer_unaligned_store32(uu32 *p, u32 x) { |
| 543 | *(uu32 *)MEM_TO_SHADOW((uptr)p) = __msan_param_tls[1]; |
Evgeniy Stepanov | 7ad195c | 2013-10-16 08:25:13 +0000 | [diff] [blame] | 544 | if (__msan_get_track_origins()) |
| 545 | *(uu32 *)MEM_TO_ORIGIN((uptr)p) = __msan_param_origin_tls[1]; |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 546 | *p = x; |
Evgeniy Stepanov | f43f602 | 2013-06-04 13:08:36 +0000 | [diff] [blame] | 547 | } |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 548 | void __sanitizer_unaligned_store64(uu64 *p, u64 x) { |
| 549 | *(uu64 *)MEM_TO_SHADOW((uptr)p) = __msan_param_tls[1]; |
Evgeniy Stepanov | 7ad195c | 2013-10-16 08:25:13 +0000 | [diff] [blame] | 550 | if (__msan_get_track_origins()) |
| 551 | *(uu32 *)MEM_TO_ORIGIN((uptr)p) = __msan_param_origin_tls[1]; |
Evgeniy Stepanov | 2e9ffcb | 2013-06-04 13:49:10 +0000 | [diff] [blame] | 552 | *p = x; |
Evgeniy Stepanov | f43f602 | 2013-06-04 13:08:36 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 555 | #if !SANITIZER_SUPPORTS_WEAK_HOOKS |
| 556 | extern "C" { |
Timur Iskhodzhanov | 3c80c6c | 2013-08-13 11:42:45 +0000 | [diff] [blame] | 557 | SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 558 | const char* __msan_default_options() { return ""; } |
| 559 | } // extern "C" |
| 560 | #endif |
| 561 | |