msan.cc revision 1d333c5a34d896f239001e3fe69a660e40d15301
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" 20#include "sanitizer_common/sanitizer_procmaps.h" 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 28using namespace __sanitizer; 29 30// Globals. 31static THREADLOCAL int msan_expect_umr = 0; 32static THREADLOCAL int msan_expected_umr_found = 0; 33 34static int msan_running_under_dr = 0; 35 36SANITIZER_INTERFACE_ATTRIBUTE 37THREADLOCAL u64 __msan_param_tls[kMsanParamTlsSizeInWords]; 38 39SANITIZER_INTERFACE_ATTRIBUTE 40THREADLOCAL u32 __msan_param_origin_tls[kMsanParamTlsSizeInWords]; 41 42SANITIZER_INTERFACE_ATTRIBUTE 43THREADLOCAL u64 __msan_retval_tls[kMsanRetvalTlsSizeInWords]; 44 45SANITIZER_INTERFACE_ATTRIBUTE 46THREADLOCAL u32 __msan_retval_origin_tls; 47 48SANITIZER_INTERFACE_ATTRIBUTE 49THREADLOCAL u64 __msan_va_arg_tls[kMsanParamTlsSizeInWords]; 50 51SANITIZER_INTERFACE_ATTRIBUTE 52THREADLOCAL u64 __msan_va_arg_overflow_size_tls; 53 54SANITIZER_INTERFACE_ATTRIBUTE 55THREADLOCAL u32 __msan_origin_tls; 56 57static THREADLOCAL struct { 58 uptr stack_top, stack_bottom; 59} __msan_stack_bounds; 60 61extern "C" const int __msan_track_origins; 62int __msan_get_track_origins() { 63 return __msan_track_origins; 64} 65 66namespace __msan { 67 68static bool IsRunningUnderDr() { 69 bool result = false; 70 MemoryMappingLayout proc_maps; 71 const sptr kBufSize = 4095; 72 char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__); 73 while (proc_maps.Next(/* start */0, /* end */0, /* file_offset */0, 74 filename, kBufSize)) { 75 if (internal_strstr(filename, "libdynamorio") != 0) { 76 result = true; 77 break; 78 } 79 } 80 UnmapOrDie(filename, kBufSize); 81 return result; 82} 83 84static Flags msan_flags; 85 86Flags *flags() { 87 return &msan_flags; 88} 89 90int msan_inited = 0; 91bool msan_init_is_running; 92 93int msan_report_count = 0; 94 95// Array of stack origins. 96// FIXME: make it resizable. 97static const uptr kNumStackOriginDescrs = 1024 * 1024; 98static const char *StackOriginDescr[kNumStackOriginDescrs]; 99static atomic_uint32_t NumStackOriginDescrs; 100 101static void ParseFlagsFromString(Flags *f, const char *str) { 102 ParseFlag(str, &f->poison_heap_with_zeroes, "poison_heap_with_zeroes"); 103 ParseFlag(str, &f->poison_stack_with_zeroes, "poison_stack_with_zeroes"); 104 ParseFlag(str, &f->poison_in_malloc, "poison_in_malloc"); 105 ParseFlag(str, &f->exit_code, "exit_code"); 106 if (f->exit_code < 0 || f->exit_code > 127) { 107 Printf("Exit code not in [0, 128) range: %d\n", f->exit_code); 108 f->exit_code = 1; 109 Die(); 110 } 111 ParseFlag(str, &f->num_callers, "num_callers"); 112 ParseFlag(str, &f->report_umrs, "report_umrs"); 113 ParseFlag(str, &f->verbosity, "verbosity"); 114 ParseFlag(str, &f->strip_path_prefix, "strip_path_prefix"); 115} 116 117static void InitializeFlags(Flags *f, const char *options) { 118 internal_memset(f, 0, sizeof(*f)); 119 120 f->poison_heap_with_zeroes = false; 121 f->poison_stack_with_zeroes = false; 122 f->poison_in_malloc = true; 123 f->exit_code = 77; 124 f->num_callers = 20; 125 f->report_umrs = true; 126 f->verbosity = 0; 127 f->strip_path_prefix = ""; 128 129 ParseFlagsFromString(f, options); 130} 131 132static void GetCurrentStackBounds(uptr *stack_top, uptr *stack_bottom) { 133 if (__msan_stack_bounds.stack_top == 0) { 134 // Break recursion (GetStackTrace -> GetThreadStackTopAndBottom -> 135 // realloc -> GetStackTrace). 136 __msan_stack_bounds.stack_top = __msan_stack_bounds.stack_bottom = 1; 137 GetThreadStackTopAndBottom(/* at_initialization */false, 138 &__msan_stack_bounds.stack_top, 139 &__msan_stack_bounds.stack_bottom); 140 } 141 *stack_top = __msan_stack_bounds.stack_top; 142 *stack_bottom = __msan_stack_bounds.stack_bottom; 143} 144 145void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp) { 146 uptr stack_top, stack_bottom; 147 GetCurrentStackBounds(&stack_top, &stack_bottom); 148 stack->size = 0; 149 stack->trace[0] = pc; 150 stack->max_size = max_s; 151 stack->FastUnwindStack(pc, bp, stack_top, stack_bottom); 152} 153 154void PrintWarning(uptr pc, uptr bp) { 155 PrintWarningWithOrigin(pc, bp, __msan_origin_tls); 156} 157 158bool OriginIsValid(u32 origin) { 159 return origin != 0 && origin != (u32)-1; 160} 161 162void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin) { 163 if (msan_expect_umr) { 164 // Printf("Expected UMR\n"); 165 __msan_origin_tls = origin; 166 msan_expected_umr_found = 1; 167 return; 168 } 169 170 ++msan_report_count; 171 172 StackTrace stack; 173 GetStackTrace(&stack, kStackTraceMax, pc, bp); 174 175 u32 report_origin = 176 (__msan_track_origins && OriginIsValid(origin)) ? origin : 0; 177 ReportUMR(&stack, report_origin); 178 179 if (__msan_track_origins && !OriginIsValid(origin)) { 180 Printf(" ORIGIN: invalid (%x). Might be a bug in MemorySanitizer, " 181 "please report to MemorySanitizer developers.\n", 182 origin); 183 } 184} 185 186} // namespace __msan 187 188// Interface. 189 190using namespace __msan; 191 192void __msan_warning() { 193 GET_CALLER_PC_BP_SP; 194 (void)sp; 195 PrintWarning(pc, bp); 196} 197 198void __msan_warning_noreturn() { 199 GET_CALLER_PC_BP_SP; 200 (void)sp; 201 PrintWarning(pc, bp); 202 Printf("Exiting\n"); 203 Die(); 204} 205 206void __msan_init() { 207 if (msan_inited) return; 208 msan_init_is_running = 1; 209 SanitizerToolName = "MemorySanitizer"; 210 211 InstallAtExitHandler(); 212 SetDieCallback(MsanDie); 213 InitializeInterceptors(); 214 215 ReplaceOperatorsNewAndDelete(); 216 if (StackSizeIsUnlimited()) { 217 if (flags()->verbosity) 218 Printf("Unlimited stack, doing reexec\n"); 219 // A reasonably large stack size. It is bigger than the usual 8Mb, because, 220 // well, the program could have been run with unlimited stack for a reason. 221 SetStackSizeLimitInBytes(32 * 1024 * 1024); 222 ReExec(); 223 } 224 const char *msan_options = GetEnv("MSAN_OPTIONS"); 225 InitializeFlags(&msan_flags, msan_options); 226 if (flags()->verbosity) 227 Printf("MSAN_OPTIONS: %s\n", msan_options ? msan_options : "<empty>"); 228 msan_running_under_dr = IsRunningUnderDr(); 229 __msan_clear_on_return(); 230 if (__msan_track_origins && flags()->verbosity > 0) 231 Printf("msan_track_origins\n"); 232 if (!InitShadow(/* prot1 */false, /* prot2 */true, /* map_shadow */true, 233 __msan_track_origins)) { 234 // FIXME: prot1 = false is only required when running under DR. 235 Printf("FATAL: MemorySanitizer can not mmap the shadow memory.\n"); 236 Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n"); 237 Printf("FATAL: Disabling ASLR is known to cause this error.\n"); 238 Printf("FATAL: If running under GDB, try " 239 "'set disable-randomization off'.\n"); 240 DumpProcessMap(); 241 Die(); 242 } 243 244 const char *external_symbolizer = GetEnv("MSAN_SYMBOLIZER_PATH"); 245 if (external_symbolizer && external_symbolizer[0]) { 246 CHECK(InitializeExternalSymbolizer(external_symbolizer)); 247 } 248 249 GetThreadStackTopAndBottom(/* at_initialization */true, 250 &__msan_stack_bounds.stack_top, 251 &__msan_stack_bounds.stack_bottom); 252 if (flags()->verbosity) 253 Printf("MemorySanitizer init done\n"); 254 msan_init_is_running = 0; 255 msan_inited = 1; 256} 257 258void __msan_set_exit_code(int exit_code) { 259 flags()->exit_code = exit_code; 260} 261 262void __msan_set_expect_umr(int expect_umr) { 263 if (expect_umr) { 264 msan_expected_umr_found = 0; 265 } else if (!msan_expected_umr_found) { 266 GET_CALLER_PC_BP_SP; 267 (void)sp; 268 StackTrace stack; 269 GetStackTrace(&stack, kStackTraceMax, pc, bp); 270 ReportExpectedUMRNotFound(&stack); 271 Die(); 272 } 273 msan_expect_umr = expect_umr; 274} 275 276void __msan_print_shadow(const void *x, uptr size) { 277 unsigned char *s = (unsigned char*)MEM_TO_SHADOW(x); 278 u32 *o = (u32*)MEM_TO_ORIGIN(x); 279 for (uptr i = 0; i < size; i++) { 280 Printf("%x%x ", s[i] >> 4, s[i] & 0xf); 281 } 282 Printf("\n"); 283 if (__msan_track_origins) { 284 for (uptr i = 0; i < size / 4; i++) { 285 Printf(" o: %x ", o[i]); 286 } 287 Printf("\n"); 288 } 289} 290 291void __msan_print_param_shadow() { 292 for (int i = 0; i < 16; i++) { 293 Printf("#%d:%zx ", i, __msan_param_tls[i]); 294 } 295 Printf("\n"); 296} 297 298sptr __msan_test_shadow(const void *x, uptr size) { 299 unsigned char *s = (unsigned char*)MEM_TO_SHADOW((uptr)x); 300 for (uptr i = 0; i < size; ++i) 301 if (s[i]) 302 return i; 303 return -1; 304} 305 306int __msan_set_poison_in_malloc(int do_poison) { 307 int old = flags()->poison_in_malloc; 308 flags()->poison_in_malloc = do_poison; 309 return old; 310} 311 312int __msan_has_dynamic_component() { 313 return msan_running_under_dr; 314} 315 316NOINLINE 317void __msan_clear_on_return() { 318 __msan_param_tls[0] = 0; 319} 320 321static void* get_tls_base() { 322 u64 p; 323 asm("mov %%fs:0, %0" 324 : "=r"(p) ::); 325 return (void*)p; 326} 327 328int __msan_get_retval_tls_offset() { 329 // volatile here is needed to avoid UB, because the compiler thinks that we 330 // are doing address arithmetics on unrelated pointers, and takes some 331 // shortcuts 332 volatile sptr retval_tls_p = (sptr)&__msan_retval_tls; 333 volatile sptr tls_base_p = (sptr)get_tls_base(); 334 return retval_tls_p - tls_base_p; 335} 336 337int __msan_get_param_tls_offset() { 338 // volatile here is needed to avoid UB, because the compiler thinks that we 339 // are doing address arithmetics on unrelated pointers, and takes some 340 // shortcuts 341 volatile sptr param_tls_p = (sptr)&__msan_param_tls; 342 volatile sptr tls_base_p = (sptr)get_tls_base(); 343 return param_tls_p - tls_base_p; 344} 345 346void __msan_partial_poison(void* data, void* shadow, uptr size) { 347 internal_memcpy((void*)MEM_TO_SHADOW((uptr)data), shadow, size); 348} 349 350void __msan_load_unpoisoned(void *src, uptr size, void *dst) { 351 internal_memcpy(dst, src, size); 352 __msan_unpoison(dst, size); 353} 354 355void __msan_set_origin(void *a, uptr size, u32 origin) { 356 // Origin mapping is 4 bytes per 4 bytes of application memory. 357 // Here we extend the range such that its left and right bounds are both 358 // 4 byte aligned. 359 if (!__msan_track_origins) return; 360 uptr x = MEM_TO_ORIGIN((uptr)a); 361 uptr beg = x & ~3UL; // align down. 362 uptr end = (x + size + 3) & ~3UL; // align up. 363 u64 origin64 = ((u64)origin << 32) | origin; 364 // This is like memset, but the value is 32-bit. We unroll by 2 two write 365 // 64-bits at once. May want to unroll further to get 128-bit stores. 366 if (beg & 7ULL) { 367 *(u32*)beg = origin; 368 beg += 4; 369 } 370 for (uptr addr = beg; addr < (end & ~7UL); addr += 8) 371 *(u64*)addr = origin64; 372 if (end & 7ULL) 373 *(u32*)(end - 4) = origin; 374} 375 376// 'descr' is created at compile time and contains '----' in the beginning. 377// When we see descr for the first time we replace '----' with a uniq id 378// and set the origin to (id | (31-th bit)). 379void __msan_set_alloca_origin(void *a, uptr size, const char *descr) { 380 static const u32 dash = '-'; 381 static const u32 first_timer = 382 dash + (dash << 8) + (dash << 16) + (dash << 24); 383 u32 *id_ptr = (u32*)descr; 384 bool print = false; // internal_strstr(descr + 4, "AllocaTOTest") != 0; 385 u32 id = *id_ptr; 386 if (id == first_timer) { 387 id = atomic_fetch_add(&NumStackOriginDescrs, 388 1, memory_order_relaxed); 389 *id_ptr = id; 390 CHECK_LT(id, kNumStackOriginDescrs); 391 StackOriginDescr[id] = descr + 4; 392 if (print) 393 Printf("First time: id=%d %s \n", id, descr + 4); 394 } 395 id |= 1U << 31; 396 if (print) 397 Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id); 398 __msan_set_origin(a, size, id); 399} 400 401const char *__msan_get_origin_descr_if_stack(u32 id) { 402 if ((id >> 31) == 0) return 0; 403 id &= (1U << 31) - 1; 404 CHECK_LT(id, kNumStackOriginDescrs); 405 return StackOriginDescr[id]; 406} 407 408 409u32 __msan_get_origin(void *a) { 410 if (!__msan_track_origins) return 0; 411 uptr x = (uptr)a; 412 uptr aligned = x & ~3ULL; 413 uptr origin_ptr = MEM_TO_ORIGIN(aligned); 414 return *(u32*)origin_ptr; 415} 416 417u32 __msan_get_umr_origin() { 418 return __msan_origin_tls; 419} 420