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
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
61 static THREADLOCAL bool is_in_symbolizer;
62 static THREADLOCAL bool is_in_loader;
63
64 extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_track_origins;
65
__msan_get_track_origins()66 int __msan_get_track_origins() {
67 return &__msan_track_origins ? __msan_track_origins : 0;
68 }
69
70 extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_keep_going;
71
72 namespace __msan {
73
IsRunningUnderDr()74 static bool IsRunningUnderDr() {
75 bool result = false;
76 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
77 const sptr kBufSize = 4095;
78 char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__);
79 while (proc_maps.Next(/* start */0, /* end */0, /* file_offset */0,
80 filename, kBufSize, /* protection */0)) {
81 if (internal_strstr(filename, "libdynamorio") != 0) {
82 result = true;
83 break;
84 }
85 }
86 UnmapOrDie(filename, kBufSize);
87 return result;
88 }
89
EnterSymbolizer()90 void EnterSymbolizer() { is_in_symbolizer = true; }
ExitSymbolizer()91 void ExitSymbolizer() { is_in_symbolizer = false; }
IsInSymbolizer()92 bool IsInSymbolizer() { return is_in_symbolizer; }
93
EnterLoader()94 void EnterLoader() { is_in_loader = true; }
ExitLoader()95 void ExitLoader() { is_in_loader = false; }
96
97 extern "C" {
98 SANITIZER_INTERFACE_ATTRIBUTE
__msan_is_in_loader()99 bool __msan_is_in_loader() { return is_in_loader; }
100 }
101
102 static Flags msan_flags;
103
flags()104 Flags *flags() {
105 return &msan_flags;
106 }
107
108 int msan_inited = 0;
109 bool msan_init_is_running;
110
111 int msan_report_count = 0;
112
113 // Array of stack origins.
114 // FIXME: make it resizable.
115 static const uptr kNumStackOriginDescrs = 1024 * 1024;
116 static const char *StackOriginDescr[kNumStackOriginDescrs];
117 static atomic_uint32_t NumStackOriginDescrs;
118
ParseFlagsFromString(Flags * f,const char * str)119 static void ParseFlagsFromString(Flags *f, const char *str) {
120 ParseCommonFlagsFromString(str);
121 ParseFlag(str, &f->poison_heap_with_zeroes, "poison_heap_with_zeroes");
122 ParseFlag(str, &f->poison_stack_with_zeroes, "poison_stack_with_zeroes");
123 ParseFlag(str, &f->poison_in_malloc, "poison_in_malloc");
124 ParseFlag(str, &f->exit_code, "exit_code");
125 if (f->exit_code < 0 || f->exit_code > 127) {
126 Printf("Exit code not in [0, 128) range: %d\n", f->exit_code);
127 f->exit_code = 1;
128 Die();
129 }
130 ParseFlag(str, &f->report_umrs, "report_umrs");
131 ParseFlag(str, &f->verbosity, "verbosity");
132 ParseFlag(str, &f->wrap_signals, "wrap_signals");
133 ParseFlag(str, &f->keep_going, "keep_going");
134 }
135
InitializeFlags(Flags * f,const char * options)136 static void InitializeFlags(Flags *f, const char *options) {
137 CommonFlags *cf = common_flags();
138 cf->external_symbolizer_path = GetEnv("MSAN_SYMBOLIZER_PATH");
139 cf->strip_path_prefix = "";
140 cf->fast_unwind_on_fatal = false;
141 cf->fast_unwind_on_malloc = true;
142 cf->malloc_context_size = 20;
143 cf->handle_ioctl = true;
144 cf->log_path = 0;
145
146 internal_memset(f, 0, sizeof(*f));
147 f->poison_heap_with_zeroes = false;
148 f->poison_stack_with_zeroes = false;
149 f->poison_in_malloc = true;
150 f->exit_code = 77;
151 f->report_umrs = true;
152 f->verbosity = 0;
153 f->wrap_signals = true;
154 f->keep_going = !!&__msan_keep_going;
155
156 // Override from user-specified string.
157 if (__msan_default_options)
158 ParseFlagsFromString(f, __msan_default_options());
159 ParseFlagsFromString(f, options);
160 }
161
GetCurrentStackBounds(uptr * stack_top,uptr * stack_bottom)162 static void GetCurrentStackBounds(uptr *stack_top, uptr *stack_bottom) {
163 if (__msan_stack_bounds.stack_top == 0) {
164 // Break recursion (GetStackTrace -> GetThreadStackTopAndBottom ->
165 // realloc -> GetStackTrace).
166 __msan_stack_bounds.stack_top = __msan_stack_bounds.stack_bottom = 1;
167 GetThreadStackTopAndBottom(/* at_initialization */false,
168 &__msan_stack_bounds.stack_top,
169 &__msan_stack_bounds.stack_bottom);
170 }
171 *stack_top = __msan_stack_bounds.stack_top;
172 *stack_bottom = __msan_stack_bounds.stack_bottom;
173 }
174
GetStackTrace(StackTrace * stack,uptr max_s,uptr pc,uptr bp,bool fast)175 void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp,
176 bool fast) {
177 if (!fast) {
178 // Block reports from our interceptors during _Unwind_Backtrace.
179 SymbolizerScope sym_scope;
180 return stack->SlowUnwindStack(pc, max_s);
181 }
182
183 uptr stack_top, stack_bottom;
184 GetCurrentStackBounds(&stack_top, &stack_bottom);
185 stack->size = 0;
186 stack->trace[0] = pc;
187 stack->max_size = max_s;
188 stack->FastUnwindStack(pc, bp, stack_top, stack_bottom);
189 }
190
PrintWarning(uptr pc,uptr bp)191 void PrintWarning(uptr pc, uptr bp) {
192 PrintWarningWithOrigin(pc, bp, __msan_origin_tls);
193 }
194
OriginIsValid(u32 origin)195 bool OriginIsValid(u32 origin) {
196 return origin != 0 && origin != (u32)-1;
197 }
198
PrintWarningWithOrigin(uptr pc,uptr bp,u32 origin)199 void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin) {
200 if (msan_expect_umr) {
201 // Printf("Expected UMR\n");
202 __msan_origin_tls = origin;
203 msan_expected_umr_found = 1;
204 return;
205 }
206
207 ++msan_report_count;
208
209 StackTrace stack;
210 GetStackTrace(&stack, kStackTraceMax, pc, bp,
211 common_flags()->fast_unwind_on_fatal);
212
213 u32 report_origin =
214 (__msan_get_track_origins() && OriginIsValid(origin)) ? origin : 0;
215 ReportUMR(&stack, report_origin);
216
217 if (__msan_get_track_origins() && !OriginIsValid(origin)) {
218 Printf(" ORIGIN: invalid (%x). Might be a bug in MemorySanitizer, "
219 "please report to MemorySanitizer developers.\n",
220 origin);
221 }
222 }
223
UnpoisonParam(uptr n)224 void UnpoisonParam(uptr n) {
225 internal_memset(__msan_param_tls, 0, n * sizeof(*__msan_param_tls));
226 }
227
228 } // namespace __msan
229
230 // Interface.
231
232 using namespace __msan;
233
__msan_warning()234 void __msan_warning() {
235 GET_CALLER_PC_BP_SP;
236 (void)sp;
237 PrintWarning(pc, bp);
238 if (!__msan::flags()->keep_going) {
239 Printf("Exiting\n");
240 Die();
241 }
242 }
243
__msan_warning_noreturn()244 void __msan_warning_noreturn() {
245 GET_CALLER_PC_BP_SP;
246 (void)sp;
247 PrintWarning(pc, bp);
248 Printf("Exiting\n");
249 Die();
250 }
251
__msan_init()252 void __msan_init() {
253 if (msan_inited) return;
254 msan_init_is_running = 1;
255 SanitizerToolName = "MemorySanitizer";
256
257 InstallAtExitHandler();
258 SetDieCallback(MsanDie);
259 InitTlsSize();
260 InitializeInterceptors();
261
262 if (MSAN_REPLACE_OPERATORS_NEW_AND_DELETE)
263 ReplaceOperatorsNewAndDelete();
264 const char *msan_options = GetEnv("MSAN_OPTIONS");
265 InitializeFlags(&msan_flags, msan_options);
266 __sanitizer_set_report_path(common_flags()->log_path);
267 if (StackSizeIsUnlimited()) {
268 if (flags()->verbosity)
269 Printf("Unlimited stack, doing reexec\n");
270 // A reasonably large stack size. It is bigger than the usual 8Mb, because,
271 // well, the program could have been run with unlimited stack for a reason.
272 SetStackSizeLimitInBytes(32 * 1024 * 1024);
273 ReExec();
274 }
275
276 if (flags()->verbosity)
277 Printf("MSAN_OPTIONS: %s\n", msan_options ? msan_options : "<empty>");
278
279 msan_running_under_dr = IsRunningUnderDr();
280 __msan_clear_on_return();
281 if (__msan_get_track_origins() && flags()->verbosity > 0)
282 Printf("msan_track_origins\n");
283 if (!InitShadow(/* prot1 */ false, /* prot2 */ true, /* map_shadow */ true,
284 __msan_get_track_origins())) {
285 // FIXME: prot1 = false is only required when running under DR.
286 Printf("FATAL: MemorySanitizer can not mmap the shadow memory.\n");
287 Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
288 Printf("FATAL: Disabling ASLR is known to cause this error.\n");
289 Printf("FATAL: If running under GDB, try "
290 "'set disable-randomization off'.\n");
291 DumpProcessMap();
292 Die();
293 }
294
295 const char *external_symbolizer = common_flags()->external_symbolizer_path;
296 if (external_symbolizer && external_symbolizer[0]) {
297 CHECK(InitializeExternalSymbolizer(external_symbolizer));
298 }
299
300 GetThreadStackTopAndBottom(/* at_initialization */true,
301 &__msan_stack_bounds.stack_top,
302 &__msan_stack_bounds.stack_bottom);
303 if (flags()->verbosity)
304 Printf("MemorySanitizer init done\n");
305 msan_init_is_running = 0;
306 msan_inited = 1;
307 }
308
__msan_set_exit_code(int exit_code)309 void __msan_set_exit_code(int exit_code) {
310 flags()->exit_code = exit_code;
311 }
312
__msan_set_keep_going(int keep_going)313 void __msan_set_keep_going(int keep_going) {
314 flags()->keep_going = keep_going;
315 }
316
__msan_set_expect_umr(int expect_umr)317 void __msan_set_expect_umr(int expect_umr) {
318 if (expect_umr) {
319 msan_expected_umr_found = 0;
320 } else if (!msan_expected_umr_found) {
321 GET_CALLER_PC_BP_SP;
322 (void)sp;
323 StackTrace stack;
324 GetStackTrace(&stack, kStackTraceMax, pc, bp,
325 common_flags()->fast_unwind_on_fatal);
326 ReportExpectedUMRNotFound(&stack);
327 Die();
328 }
329 msan_expect_umr = expect_umr;
330 }
331
__msan_print_shadow(const void * x,uptr size)332 void __msan_print_shadow(const void *x, uptr size) {
333 unsigned char *s = (unsigned char*)MEM_TO_SHADOW(x);
334 u32 *o = (u32*)MEM_TO_ORIGIN(x);
335 for (uptr i = 0; i < size; i++) {
336 Printf("%x%x ", s[i] >> 4, s[i] & 0xf);
337 }
338 Printf("\n");
339 if (__msan_get_track_origins()) {
340 for (uptr i = 0; i < size / 4; i++) {
341 Printf(" o: %x ", o[i]);
342 }
343 Printf("\n");
344 }
345 }
346
__msan_print_param_shadow()347 void __msan_print_param_shadow() {
348 for (int i = 0; i < 16; i++) {
349 Printf("#%d:%zx ", i, __msan_param_tls[i]);
350 }
351 Printf("\n");
352 }
353
__msan_test_shadow(const void * x,uptr size)354 sptr __msan_test_shadow(const void *x, uptr size) {
355 unsigned char *s = (unsigned char*)MEM_TO_SHADOW((uptr)x);
356 for (uptr i = 0; i < size; ++i)
357 if (s[i])
358 return i;
359 return -1;
360 }
361
__msan_set_poison_in_malloc(int do_poison)362 int __msan_set_poison_in_malloc(int do_poison) {
363 int old = flags()->poison_in_malloc;
364 flags()->poison_in_malloc = do_poison;
365 return old;
366 }
367
__msan_has_dynamic_component()368 int __msan_has_dynamic_component() {
369 return msan_running_under_dr;
370 }
371
372 NOINLINE
__msan_clear_on_return()373 void __msan_clear_on_return() {
374 __msan_param_tls[0] = 0;
375 }
376
get_tls_base()377 static void* get_tls_base() {
378 u64 p;
379 asm("mov %%fs:0, %0"
380 : "=r"(p) ::);
381 return (void*)p;
382 }
383
__msan_get_retval_tls_offset()384 int __msan_get_retval_tls_offset() {
385 // volatile here is needed to avoid UB, because the compiler thinks that we
386 // are doing address arithmetics on unrelated pointers, and takes some
387 // shortcuts
388 volatile sptr retval_tls_p = (sptr)&__msan_retval_tls;
389 volatile sptr tls_base_p = (sptr)get_tls_base();
390 return retval_tls_p - tls_base_p;
391 }
392
__msan_get_param_tls_offset()393 int __msan_get_param_tls_offset() {
394 // volatile here is needed to avoid UB, because the compiler thinks that we
395 // are doing address arithmetics on unrelated pointers, and takes some
396 // shortcuts
397 volatile sptr param_tls_p = (sptr)&__msan_param_tls;
398 volatile sptr tls_base_p = (sptr)get_tls_base();
399 return param_tls_p - tls_base_p;
400 }
401
__msan_partial_poison(const void * data,void * shadow,uptr size)402 void __msan_partial_poison(const void* data, void* shadow, uptr size) {
403 internal_memcpy((void*)MEM_TO_SHADOW((uptr)data), shadow, size);
404 }
405
__msan_load_unpoisoned(void * src,uptr size,void * dst)406 void __msan_load_unpoisoned(void *src, uptr size, void *dst) {
407 internal_memcpy(dst, src, size);
408 __msan_unpoison(dst, size);
409 }
410
__msan_set_origin(const void * a,uptr size,u32 origin)411 void __msan_set_origin(const void *a, uptr size, u32 origin) {
412 // Origin mapping is 4 bytes per 4 bytes of application memory.
413 // Here we extend the range such that its left and right bounds are both
414 // 4 byte aligned.
415 if (!__msan_get_track_origins()) return;
416 uptr x = MEM_TO_ORIGIN((uptr)a);
417 uptr beg = x & ~3UL; // align down.
418 uptr end = (x + size + 3) & ~3UL; // align up.
419 u64 origin64 = ((u64)origin << 32) | origin;
420 // This is like memset, but the value is 32-bit. We unroll by 2 two write
421 // 64-bits at once. May want to unroll further to get 128-bit stores.
422 if (beg & 7ULL) {
423 *(u32*)beg = origin;
424 beg += 4;
425 }
426 for (uptr addr = beg; addr < (end & ~7UL); addr += 8)
427 *(u64*)addr = origin64;
428 if (end & 7ULL)
429 *(u32*)(end - 4) = origin;
430 }
431
432 // 'descr' is created at compile time and contains '----' in the beginning.
433 // When we see descr for the first time we replace '----' with a uniq id
434 // and set the origin to (id | (31-th bit)).
__msan_set_alloca_origin(void * a,uptr size,const char * descr)435 void __msan_set_alloca_origin(void *a, uptr size, const char *descr) {
436 static const u32 dash = '-';
437 static const u32 first_timer =
438 dash + (dash << 8) + (dash << 16) + (dash << 24);
439 u32 *id_ptr = (u32*)descr;
440 bool print = false; // internal_strstr(descr + 4, "AllocaTOTest") != 0;
441 u32 id = *id_ptr;
442 if (id == first_timer) {
443 id = atomic_fetch_add(&NumStackOriginDescrs,
444 1, memory_order_relaxed);
445 *id_ptr = id;
446 CHECK_LT(id, kNumStackOriginDescrs);
447 StackOriginDescr[id] = descr + 4;
448 if (print)
449 Printf("First time: id=%d %s \n", id, descr + 4);
450 }
451 id |= 1U << 31;
452 if (print)
453 Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id);
454 __msan_set_origin(a, size, id);
455 }
456
__msan_get_origin_descr_if_stack(u32 id)457 const char *__msan_get_origin_descr_if_stack(u32 id) {
458 if ((id >> 31) == 0) return 0;
459 id &= (1U << 31) - 1;
460 CHECK_LT(id, kNumStackOriginDescrs);
461 return StackOriginDescr[id];
462 }
463
464
__msan_get_origin(const void * a)465 u32 __msan_get_origin(const void *a) {
466 if (!__msan_get_track_origins()) return 0;
467 uptr x = (uptr)a;
468 uptr aligned = x & ~3ULL;
469 uptr origin_ptr = MEM_TO_ORIGIN(aligned);
470 return *(u32*)origin_ptr;
471 }
472
__msan_get_umr_origin()473 u32 __msan_get_umr_origin() {
474 return __msan_origin_tls;
475 }
476
__sanitizer_unaligned_load16(const uu16 * p)477 u16 __sanitizer_unaligned_load16(const uu16 *p) {
478 __msan_retval_tls[0] = *(uu16 *)MEM_TO_SHADOW((uptr)p);
479 return *p;
480 }
__sanitizer_unaligned_load32(const uu32 * p)481 u32 __sanitizer_unaligned_load32(const uu32 *p) {
482 __msan_retval_tls[0] = *(uu32 *)MEM_TO_SHADOW((uptr)p);
483 return *p;
484 }
__sanitizer_unaligned_load64(const uu64 * p)485 u64 __sanitizer_unaligned_load64(const uu64 *p) {
486 __msan_retval_tls[0] = *(uu64 *)MEM_TO_SHADOW((uptr)p);
487 return *p;
488 }
__sanitizer_unaligned_store16(uu16 * p,u16 x)489 void __sanitizer_unaligned_store16(uu16 *p, u16 x) {
490 *(uu16 *)MEM_TO_SHADOW((uptr)p) = __msan_param_tls[1];
491 *p = x;
492 }
__sanitizer_unaligned_store32(uu32 * p,u32 x)493 void __sanitizer_unaligned_store32(uu32 *p, u32 x) {
494 *(uu32 *)MEM_TO_SHADOW((uptr)p) = __msan_param_tls[1];
495 *p = x;
496 }
__sanitizer_unaligned_store64(uu64 * p,u64 x)497 void __sanitizer_unaligned_store64(uu64 *p, u64 x) {
498 *(uu64 *)MEM_TO_SHADOW((uptr)p) = __msan_param_tls[1];
499 *p = x;
500 }
501
502 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
503 extern "C" {
504 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
__msan_default_options()505 const char* __msan_default_options() { return ""; }
506 } // extern "C"
507 #endif
508
509