1 //===-- msan.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of MemorySanitizer.
10 //
11 // MemorySanitizer runtime.
12 //===----------------------------------------------------------------------===//
13
14 #include "msan.h"
15 #include "msan_chained_origin_depot.h"
16 #include "msan_origin.h"
17 #include "msan_report.h"
18 #include "msan_thread.h"
19 #include "msan_poisoning.h"
20 #include "sanitizer_common/sanitizer_atomic.h"
21 #include "sanitizer_common/sanitizer_common.h"
22 #include "sanitizer_common/sanitizer_flags.h"
23 #include "sanitizer_common/sanitizer_flag_parser.h"
24 #include "sanitizer_common/sanitizer_libc.h"
25 #include "sanitizer_common/sanitizer_procmaps.h"
26 #include "sanitizer_common/sanitizer_stacktrace.h"
27 #include "sanitizer_common/sanitizer_symbolizer.h"
28 #include "sanitizer_common/sanitizer_stackdepot.h"
29 #include "ubsan/ubsan_flags.h"
30 #include "ubsan/ubsan_init.h"
31
32 // ACHTUNG! No system header includes in this file.
33
34 using namespace __sanitizer;
35
36 // Globals.
37 static THREADLOCAL int msan_expect_umr = 0;
38 static THREADLOCAL int msan_expected_umr_found = 0;
39
40 // Function argument shadow. Each argument starts at the next available 8-byte
41 // aligned address.
42 SANITIZER_INTERFACE_ATTRIBUTE
43 THREADLOCAL u64 __msan_param_tls[kMsanParamTlsSize / sizeof(u64)];
44
45 // Function argument origin. Each argument starts at the same offset as the
46 // corresponding shadow in (__msan_param_tls). Slightly weird, but changing this
47 // would break compatibility with older prebuilt binaries.
48 SANITIZER_INTERFACE_ATTRIBUTE
49 THREADLOCAL u32 __msan_param_origin_tls[kMsanParamTlsSize / sizeof(u32)];
50
51 SANITIZER_INTERFACE_ATTRIBUTE
52 THREADLOCAL u64 __msan_retval_tls[kMsanRetvalTlsSize / sizeof(u64)];
53
54 SANITIZER_INTERFACE_ATTRIBUTE
55 THREADLOCAL u32 __msan_retval_origin_tls;
56
57 SANITIZER_INTERFACE_ATTRIBUTE
58 ALIGNED(16) THREADLOCAL u64 __msan_va_arg_tls[kMsanParamTlsSize / sizeof(u64)];
59
60 SANITIZER_INTERFACE_ATTRIBUTE
61 ALIGNED(16)
62 THREADLOCAL u32 __msan_va_arg_origin_tls[kMsanParamTlsSize / sizeof(u32)];
63
64 SANITIZER_INTERFACE_ATTRIBUTE
65 THREADLOCAL u64 __msan_va_arg_overflow_size_tls;
66
67 SANITIZER_INTERFACE_ATTRIBUTE
68 THREADLOCAL u32 __msan_origin_tls;
69
70 static THREADLOCAL int is_in_symbolizer;
71
72 extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_track_origins;
73
__msan_get_track_origins()74 int __msan_get_track_origins() {
75 return &__msan_track_origins ? __msan_track_origins : 0;
76 }
77
78 extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_keep_going;
79
80 namespace __msan {
81
EnterSymbolizer()82 void EnterSymbolizer() { ++is_in_symbolizer; }
ExitSymbolizer()83 void ExitSymbolizer() { --is_in_symbolizer; }
IsInSymbolizer()84 bool IsInSymbolizer() { return is_in_symbolizer; }
85
86 static Flags msan_flags;
87
flags()88 Flags *flags() {
89 return &msan_flags;
90 }
91
92 int msan_inited = 0;
93 bool msan_init_is_running;
94
95 int msan_report_count = 0;
96
97 // Array of stack origins.
98 // FIXME: make it resizable.
99 static const uptr kNumStackOriginDescrs = 1024 * 1024;
100 static const char *StackOriginDescr[kNumStackOriginDescrs];
101 static uptr StackOriginPC[kNumStackOriginDescrs];
102 static atomic_uint32_t NumStackOriginDescrs;
103
SetDefaults()104 void Flags::SetDefaults() {
105 #define MSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
106 #include "msan_flags.inc"
107 #undef MSAN_FLAG
108 }
109
110 // keep_going is an old name for halt_on_error,
111 // and it has inverse meaning.
112 class FlagHandlerKeepGoing : public FlagHandlerBase {
113 bool *halt_on_error_;
114
115 public:
FlagHandlerKeepGoing(bool * halt_on_error)116 explicit FlagHandlerKeepGoing(bool *halt_on_error)
117 : halt_on_error_(halt_on_error) {}
Parse(const char * value)118 bool Parse(const char *value) final {
119 bool tmp;
120 FlagHandler<bool> h(&tmp);
121 if (!h.Parse(value)) return false;
122 *halt_on_error_ = !tmp;
123 return true;
124 }
Format(char * buffer,uptr size)125 bool Format(char *buffer, uptr size) final {
126 const char *keep_going_str = (*halt_on_error_) ? "false" : "true";
127 return FormatString(buffer, size, keep_going_str);
128 }
129 };
130
RegisterMsanFlags(FlagParser * parser,Flags * f)131 static void RegisterMsanFlags(FlagParser *parser, Flags *f) {
132 #define MSAN_FLAG(Type, Name, DefaultValue, Description) \
133 RegisterFlag(parser, #Name, Description, &f->Name);
134 #include "msan_flags.inc"
135 #undef MSAN_FLAG
136
137 FlagHandlerKeepGoing *fh_keep_going =
138 new (FlagParser::Alloc) FlagHandlerKeepGoing(&f->halt_on_error);
139 parser->RegisterHandler("keep_going", fh_keep_going,
140 "deprecated, use halt_on_error");
141 }
142
InitializeFlags()143 static void InitializeFlags() {
144 SetCommonFlagsDefaults();
145 {
146 CommonFlags cf;
147 cf.CopyFrom(*common_flags());
148 cf.external_symbolizer_path = GetEnv("MSAN_SYMBOLIZER_PATH");
149 cf.malloc_context_size = 20;
150 cf.handle_ioctl = true;
151 // FIXME: test and enable.
152 cf.check_printf = false;
153 cf.intercept_tls_get_addr = true;
154 cf.exitcode = 77;
155 OverrideCommonFlags(cf);
156 }
157
158 Flags *f = flags();
159 f->SetDefaults();
160
161 FlagParser parser;
162 RegisterMsanFlags(&parser, f);
163 RegisterCommonFlags(&parser);
164
165 #if MSAN_CONTAINS_UBSAN
166 __ubsan::Flags *uf = __ubsan::flags();
167 uf->SetDefaults();
168
169 FlagParser ubsan_parser;
170 __ubsan::RegisterUbsanFlags(&ubsan_parser, uf);
171 RegisterCommonFlags(&ubsan_parser);
172 #endif
173
174 // Override from user-specified string.
175 if (__msan_default_options)
176 parser.ParseString(__msan_default_options());
177 #if MSAN_CONTAINS_UBSAN
178 const char *ubsan_default_options = __ubsan::MaybeCallUbsanDefaultOptions();
179 ubsan_parser.ParseString(ubsan_default_options);
180 #endif
181
182 parser.ParseStringFromEnv("MSAN_OPTIONS");
183 #if MSAN_CONTAINS_UBSAN
184 ubsan_parser.ParseStringFromEnv("UBSAN_OPTIONS");
185 #endif
186
187 InitializeCommonFlags();
188
189 if (Verbosity()) ReportUnrecognizedFlags();
190
191 if (common_flags()->help) parser.PrintFlagDescriptions();
192
193 // Check if deprecated exit_code MSan flag is set.
194 if (f->exit_code != -1) {
195 if (Verbosity())
196 Printf("MSAN_OPTIONS=exit_code is deprecated! "
197 "Please use MSAN_OPTIONS=exitcode instead.\n");
198 CommonFlags cf;
199 cf.CopyFrom(*common_flags());
200 cf.exitcode = f->exit_code;
201 OverrideCommonFlags(cf);
202 }
203
204 // Check flag values:
205 if (f->origin_history_size < 0 ||
206 f->origin_history_size > Origin::kMaxDepth) {
207 Printf(
208 "Origin history size invalid: %d. Must be 0 (unlimited) or in [1, %d] "
209 "range.\n",
210 f->origin_history_size, Origin::kMaxDepth);
211 Die();
212 }
213 // Limiting to kStackDepotMaxUseCount / 2 to avoid overflow in
214 // StackDepotHandle::inc_use_count_unsafe.
215 if (f->origin_history_per_stack_limit < 0 ||
216 f->origin_history_per_stack_limit > kStackDepotMaxUseCount / 2) {
217 Printf(
218 "Origin per-stack limit invalid: %d. Must be 0 (unlimited) or in [1, "
219 "%d] range.\n",
220 f->origin_history_per_stack_limit, kStackDepotMaxUseCount / 2);
221 Die();
222 }
223 if (f->store_context_size < 1) f->store_context_size = 1;
224 }
225
PrintWarning(uptr pc,uptr bp)226 void PrintWarning(uptr pc, uptr bp) {
227 PrintWarningWithOrigin(pc, bp, __msan_origin_tls);
228 }
229
PrintWarningWithOrigin(uptr pc,uptr bp,u32 origin)230 void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin) {
231 if (msan_expect_umr) {
232 // Printf("Expected UMR\n");
233 __msan_origin_tls = origin;
234 msan_expected_umr_found = 1;
235 return;
236 }
237
238 ++msan_report_count;
239
240 GET_FATAL_STACK_TRACE_PC_BP(pc, bp);
241
242 u32 report_origin =
243 (__msan_get_track_origins() && Origin::isValidId(origin)) ? origin : 0;
244 ReportUMR(&stack, report_origin);
245
246 if (__msan_get_track_origins() && !Origin::isValidId(origin)) {
247 Printf(
248 " ORIGIN: invalid (%x). Might be a bug in MemorySanitizer origin "
249 "tracking.\n This could still be a bug in your code, too!\n",
250 origin);
251 }
252 }
253
UnpoisonParam(uptr n)254 void UnpoisonParam(uptr n) {
255 internal_memset(__msan_param_tls, 0, n * sizeof(*__msan_param_tls));
256 }
257
258 // Backup MSan runtime TLS state.
259 // Implementation must be async-signal-safe.
260 // Instances of this class may live on the signal handler stack, and data size
261 // may be an issue.
Backup()262 void ScopedThreadLocalStateBackup::Backup() {
263 va_arg_overflow_size_tls = __msan_va_arg_overflow_size_tls;
264 }
265
Restore()266 void ScopedThreadLocalStateBackup::Restore() {
267 // A lame implementation that only keeps essential state and resets the rest.
268 __msan_va_arg_overflow_size_tls = va_arg_overflow_size_tls;
269
270 internal_memset(__msan_param_tls, 0, sizeof(__msan_param_tls));
271 internal_memset(__msan_retval_tls, 0, sizeof(__msan_retval_tls));
272 internal_memset(__msan_va_arg_tls, 0, sizeof(__msan_va_arg_tls));
273 internal_memset(__msan_va_arg_origin_tls, 0,
274 sizeof(__msan_va_arg_origin_tls));
275
276 if (__msan_get_track_origins()) {
277 internal_memset(&__msan_retval_origin_tls, 0,
278 sizeof(__msan_retval_origin_tls));
279 internal_memset(__msan_param_origin_tls, 0,
280 sizeof(__msan_param_origin_tls));
281 }
282 }
283
UnpoisonThreadLocalState()284 void UnpoisonThreadLocalState() {
285 }
286
GetStackOriginDescr(u32 id,uptr * pc)287 const char *GetStackOriginDescr(u32 id, uptr *pc) {
288 CHECK_LT(id, kNumStackOriginDescrs);
289 if (pc) *pc = StackOriginPC[id];
290 return StackOriginDescr[id];
291 }
292
ChainOrigin(u32 id,StackTrace * stack)293 u32 ChainOrigin(u32 id, StackTrace *stack) {
294 MsanThread *t = GetCurrentThread();
295 if (t && t->InSignalHandler())
296 return id;
297
298 Origin o = Origin::FromRawId(id);
299 stack->tag = StackTrace::TAG_UNKNOWN;
300 Origin chained = Origin::CreateChainedOrigin(o, stack);
301 return chained.raw_id();
302 }
303
304 } // namespace __msan
305
UnwindImpl(uptr pc,uptr bp,void * context,bool request_fast,u32 max_depth)306 void __sanitizer::BufferedStackTrace::UnwindImpl(
307 uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {
308 using namespace __msan;
309 MsanThread *t = GetCurrentThread();
310 if (!t || !StackTrace::WillUseFastUnwind(request_fast)) {
311 // Block reports from our interceptors during _Unwind_Backtrace.
312 SymbolizerScope sym_scope;
313 return Unwind(max_depth, pc, bp, context, 0, 0, false);
314 }
315 if (StackTrace::WillUseFastUnwind(request_fast))
316 Unwind(max_depth, pc, bp, nullptr, t->stack_top(), t->stack_bottom(), true);
317 else
318 Unwind(max_depth, pc, 0, context, 0, 0, false);
319 }
320
321 // Interface.
322
323 using namespace __msan;
324
325 #define MSAN_MAYBE_WARNING(type, size) \
326 void __msan_maybe_warning_##size(type s, u32 o) { \
327 GET_CALLER_PC_BP_SP; \
328 (void) sp; \
329 if (UNLIKELY(s)) { \
330 PrintWarningWithOrigin(pc, bp, o); \
331 if (__msan::flags()->halt_on_error) { \
332 Printf("Exiting\n"); \
333 Die(); \
334 } \
335 } \
336 }
337
338 MSAN_MAYBE_WARNING(u8, 1)
339 MSAN_MAYBE_WARNING(u16, 2)
340 MSAN_MAYBE_WARNING(u32, 4)
341 MSAN_MAYBE_WARNING(u64, 8)
342
343 #define MSAN_MAYBE_STORE_ORIGIN(type, size) \
344 void __msan_maybe_store_origin_##size(type s, void *p, u32 o) { \
345 if (UNLIKELY(s)) { \
346 if (__msan_get_track_origins() > 1) { \
347 GET_CALLER_PC_BP_SP; \
348 (void) sp; \
349 GET_STORE_STACK_TRACE_PC_BP(pc, bp); \
350 o = ChainOrigin(o, &stack); \
351 } \
352 *(u32 *)MEM_TO_ORIGIN((uptr)p & ~3UL) = o; \
353 } \
354 }
355
356 MSAN_MAYBE_STORE_ORIGIN(u8, 1)
357 MSAN_MAYBE_STORE_ORIGIN(u16, 2)
358 MSAN_MAYBE_STORE_ORIGIN(u32, 4)
359 MSAN_MAYBE_STORE_ORIGIN(u64, 8)
360
__msan_warning()361 void __msan_warning() {
362 GET_CALLER_PC_BP_SP;
363 (void)sp;
364 PrintWarning(pc, bp);
365 if (__msan::flags()->halt_on_error) {
366 if (__msan::flags()->print_stats)
367 ReportStats();
368 Printf("Exiting\n");
369 Die();
370 }
371 }
372
__msan_warning_noreturn()373 void __msan_warning_noreturn() {
374 GET_CALLER_PC_BP_SP;
375 (void)sp;
376 PrintWarning(pc, bp);
377 if (__msan::flags()->print_stats)
378 ReportStats();
379 Printf("Exiting\n");
380 Die();
381 }
382
OnStackUnwind(const SignalContext & sig,const void *,BufferedStackTrace * stack)383 static void OnStackUnwind(const SignalContext &sig, const void *,
384 BufferedStackTrace *stack) {
385 stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,
386 common_flags()->fast_unwind_on_fatal);
387 }
388
MsanOnDeadlySignal(int signo,void * siginfo,void * context)389 static void MsanOnDeadlySignal(int signo, void *siginfo, void *context) {
390 HandleDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr);
391 }
392
MsanCheckFailed(const char * file,int line,const char * cond,u64 v1,u64 v2)393 static void MsanCheckFailed(const char *file, int line, const char *cond,
394 u64 v1, u64 v2) {
395 Report("MemorySanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", file,
396 line, cond, (uptr)v1, (uptr)v2);
397 PRINT_CURRENT_STACK_CHECK();
398 Die();
399 }
400
__msan_init()401 void __msan_init() {
402 CHECK(!msan_init_is_running);
403 if (msan_inited) return;
404 msan_init_is_running = 1;
405 SanitizerToolName = "MemorySanitizer";
406
407 AvoidCVE_2016_2143();
408
409 CacheBinaryName();
410 InitializeFlags();
411
412 // Install tool-specific callbacks in sanitizer_common.
413 SetCheckFailedCallback(MsanCheckFailed);
414
415 __sanitizer_set_report_path(common_flags()->log_path);
416
417 InitializeInterceptors();
418 CheckASLR();
419 InitTlsSize();
420 InstallDeadlySignalHandlers(MsanOnDeadlySignal);
421 InstallAtExitHandler(); // Needs __cxa_atexit interceptor.
422
423 DisableCoreDumperIfNecessary();
424 if (StackSizeIsUnlimited()) {
425 VPrintf(1, "Unlimited stack, doing reexec\n");
426 // A reasonably large stack size. It is bigger than the usual 8Mb, because,
427 // well, the program could have been run with unlimited stack for a reason.
428 SetStackSizeLimitInBytes(32 * 1024 * 1024);
429 ReExec();
430 }
431
432 __msan_clear_on_return();
433 if (__msan_get_track_origins())
434 VPrintf(1, "msan_track_origins\n");
435 if (!InitShadow(__msan_get_track_origins())) {
436 Printf("FATAL: MemorySanitizer can not mmap the shadow memory.\n");
437 Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
438 Printf("FATAL: Disabling ASLR is known to cause this error.\n");
439 Printf("FATAL: If running under GDB, try "
440 "'set disable-randomization off'.\n");
441 DumpProcessMap();
442 Die();
443 }
444
445 Symbolizer::GetOrInit()->AddHooks(EnterSymbolizer, ExitSymbolizer);
446
447 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
448
449 MsanTSDInit(MsanTSDDtor);
450
451 MsanAllocatorInit();
452
453 MsanThread *main_thread = MsanThread::Create(nullptr, nullptr);
454 SetCurrentThread(main_thread);
455 main_thread->ThreadStart();
456
457 #if MSAN_CONTAINS_UBSAN
458 __ubsan::InitAsPlugin();
459 #endif
460
461 VPrintf(1, "MemorySanitizer init done\n");
462
463 msan_init_is_running = 0;
464 msan_inited = 1;
465 }
466
__msan_set_keep_going(int keep_going)467 void __msan_set_keep_going(int keep_going) {
468 flags()->halt_on_error = !keep_going;
469 }
470
__msan_set_expect_umr(int expect_umr)471 void __msan_set_expect_umr(int expect_umr) {
472 if (expect_umr) {
473 msan_expected_umr_found = 0;
474 } else if (!msan_expected_umr_found) {
475 GET_CALLER_PC_BP_SP;
476 (void)sp;
477 GET_FATAL_STACK_TRACE_PC_BP(pc, bp);
478 ReportExpectedUMRNotFound(&stack);
479 Die();
480 }
481 msan_expect_umr = expect_umr;
482 }
483
__msan_print_shadow(const void * x,uptr size)484 void __msan_print_shadow(const void *x, uptr size) {
485 if (!MEM_IS_APP(x)) {
486 Printf("Not a valid application address: %p\n", x);
487 return;
488 }
489
490 DescribeMemoryRange(x, size);
491 }
492
__msan_dump_shadow(const void * x,uptr size)493 void __msan_dump_shadow(const void *x, uptr size) {
494 if (!MEM_IS_APP(x)) {
495 Printf("Not a valid application address: %p\n", x);
496 return;
497 }
498
499 unsigned char *s = (unsigned char*)MEM_TO_SHADOW(x);
500 for (uptr i = 0; i < size; i++)
501 Printf("%x%x ", s[i] >> 4, s[i] & 0xf);
502 Printf("\n");
503 }
504
__msan_test_shadow(const void * x,uptr size)505 sptr __msan_test_shadow(const void *x, uptr size) {
506 if (!MEM_IS_APP(x)) return -1;
507 unsigned char *s = (unsigned char *)MEM_TO_SHADOW((uptr)x);
508 for (uptr i = 0; i < size; ++i)
509 if (s[i])
510 return i;
511 return -1;
512 }
513
__msan_check_mem_is_initialized(const void * x,uptr size)514 void __msan_check_mem_is_initialized(const void *x, uptr size) {
515 if (!__msan::flags()->report_umrs) return;
516 sptr offset = __msan_test_shadow(x, size);
517 if (offset < 0)
518 return;
519
520 GET_CALLER_PC_BP_SP;
521 (void)sp;
522 ReportUMRInsideAddressRange(__func__, x, size, offset);
523 __msan::PrintWarningWithOrigin(pc, bp,
524 __msan_get_origin(((const char *)x) + offset));
525 if (__msan::flags()->halt_on_error) {
526 Printf("Exiting\n");
527 Die();
528 }
529 }
530
__msan_set_poison_in_malloc(int do_poison)531 int __msan_set_poison_in_malloc(int do_poison) {
532 int old = flags()->poison_in_malloc;
533 flags()->poison_in_malloc = do_poison;
534 return old;
535 }
536
__msan_has_dynamic_component()537 int __msan_has_dynamic_component() { return false; }
538
539 NOINLINE
__msan_clear_on_return()540 void __msan_clear_on_return() {
541 __msan_param_tls[0] = 0;
542 }
543
__msan_partial_poison(const void * data,void * shadow,uptr size)544 void __msan_partial_poison(const void* data, void* shadow, uptr size) {
545 internal_memcpy((void*)MEM_TO_SHADOW((uptr)data), shadow, size);
546 }
547
__msan_load_unpoisoned(const void * src,uptr size,void * dst)548 void __msan_load_unpoisoned(const void *src, uptr size, void *dst) {
549 internal_memcpy(dst, src, size);
550 __msan_unpoison(dst, size);
551 }
552
__msan_set_origin(const void * a,uptr size,u32 origin)553 void __msan_set_origin(const void *a, uptr size, u32 origin) {
554 if (__msan_get_track_origins()) SetOrigin(a, size, origin);
555 }
556
557 // 'descr' is created at compile time and contains '----' in the beginning.
558 // When we see descr for the first time we replace '----' with a uniq id
559 // and set the origin to (id | (31-th bit)).
__msan_set_alloca_origin(void * a,uptr size,char * descr)560 void __msan_set_alloca_origin(void *a, uptr size, char *descr) {
561 __msan_set_alloca_origin4(a, size, descr, 0);
562 }
563
__msan_set_alloca_origin4(void * a,uptr size,char * descr,uptr pc)564 void __msan_set_alloca_origin4(void *a, uptr size, char *descr, uptr pc) {
565 static const u32 dash = '-';
566 static const u32 first_timer =
567 dash + (dash << 8) + (dash << 16) + (dash << 24);
568 u32 *id_ptr = (u32*)descr;
569 bool print = false; // internal_strstr(descr + 4, "AllocaTOTest") != 0;
570 u32 id = *id_ptr;
571 if (id == first_timer) {
572 u32 idx = atomic_fetch_add(&NumStackOriginDescrs, 1, memory_order_relaxed);
573 CHECK_LT(idx, kNumStackOriginDescrs);
574 StackOriginDescr[idx] = descr + 4;
575 #if SANITIZER_PPC64V1
576 // On PowerPC64 ELFv1, the address of a function actually points to a
577 // three-doubleword data structure with the first field containing
578 // the address of the function's code.
579 if (pc)
580 pc = *reinterpret_cast<uptr*>(pc);
581 #endif
582 StackOriginPC[idx] = pc;
583 id = Origin::CreateStackOrigin(idx).raw_id();
584 *id_ptr = id;
585 if (print)
586 Printf("First time: idx=%d id=%d %s %p \n", idx, id, descr + 4, pc);
587 }
588 if (print)
589 Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id);
590 __msan_set_origin(a, size, id);
591 }
592
__msan_chain_origin(u32 id)593 u32 __msan_chain_origin(u32 id) {
594 GET_CALLER_PC_BP_SP;
595 (void)sp;
596 GET_STORE_STACK_TRACE_PC_BP(pc, bp);
597 return ChainOrigin(id, &stack);
598 }
599
__msan_get_origin(const void * a)600 u32 __msan_get_origin(const void *a) {
601 if (!__msan_get_track_origins()) return 0;
602 uptr x = (uptr)a;
603 uptr aligned = x & ~3ULL;
604 uptr origin_ptr = MEM_TO_ORIGIN(aligned);
605 return *(u32*)origin_ptr;
606 }
607
__msan_origin_is_descendant_or_same(u32 this_id,u32 prev_id)608 int __msan_origin_is_descendant_or_same(u32 this_id, u32 prev_id) {
609 Origin o = Origin::FromRawId(this_id);
610 while (o.raw_id() != prev_id && o.isChainedOrigin())
611 o = o.getNextChainedOrigin(nullptr);
612 return o.raw_id() == prev_id;
613 }
614
__msan_get_umr_origin()615 u32 __msan_get_umr_origin() {
616 return __msan_origin_tls;
617 }
618
__sanitizer_unaligned_load16(const uu16 * p)619 u16 __sanitizer_unaligned_load16(const uu16 *p) {
620 *(uu16 *)&__msan_retval_tls[0] = *(uu16 *)MEM_TO_SHADOW((uptr)p);
621 if (__msan_get_track_origins())
622 __msan_retval_origin_tls = GetOriginIfPoisoned((uptr)p, sizeof(*p));
623 return *p;
624 }
__sanitizer_unaligned_load32(const uu32 * p)625 u32 __sanitizer_unaligned_load32(const uu32 *p) {
626 *(uu32 *)&__msan_retval_tls[0] = *(uu32 *)MEM_TO_SHADOW((uptr)p);
627 if (__msan_get_track_origins())
628 __msan_retval_origin_tls = GetOriginIfPoisoned((uptr)p, sizeof(*p));
629 return *p;
630 }
__sanitizer_unaligned_load64(const uu64 * p)631 u64 __sanitizer_unaligned_load64(const uu64 *p) {
632 __msan_retval_tls[0] = *(uu64 *)MEM_TO_SHADOW((uptr)p);
633 if (__msan_get_track_origins())
634 __msan_retval_origin_tls = GetOriginIfPoisoned((uptr)p, sizeof(*p));
635 return *p;
636 }
__sanitizer_unaligned_store16(uu16 * p,u16 x)637 void __sanitizer_unaligned_store16(uu16 *p, u16 x) {
638 u16 s = *(uu16 *)&__msan_param_tls[1];
639 *(uu16 *)MEM_TO_SHADOW((uptr)p) = s;
640 if (s && __msan_get_track_origins())
641 if (uu32 o = __msan_param_origin_tls[2])
642 SetOriginIfPoisoned((uptr)p, (uptr)&s, sizeof(s), o);
643 *p = x;
644 }
__sanitizer_unaligned_store32(uu32 * p,u32 x)645 void __sanitizer_unaligned_store32(uu32 *p, u32 x) {
646 u32 s = *(uu32 *)&__msan_param_tls[1];
647 *(uu32 *)MEM_TO_SHADOW((uptr)p) = s;
648 if (s && __msan_get_track_origins())
649 if (uu32 o = __msan_param_origin_tls[2])
650 SetOriginIfPoisoned((uptr)p, (uptr)&s, sizeof(s), o);
651 *p = x;
652 }
__sanitizer_unaligned_store64(uu64 * p,u64 x)653 void __sanitizer_unaligned_store64(uu64 *p, u64 x) {
654 u64 s = __msan_param_tls[1];
655 *(uu64 *)MEM_TO_SHADOW((uptr)p) = s;
656 if (s && __msan_get_track_origins())
657 if (uu32 o = __msan_param_origin_tls[2])
658 SetOriginIfPoisoned((uptr)p, (uptr)&s, sizeof(s), o);
659 *p = x;
660 }
661
__msan_set_death_callback(void (* callback)(void))662 void __msan_set_death_callback(void (*callback)(void)) {
663 SetUserDieCallback(callback);
664 }
665
666 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
667 extern "C" {
668 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
__msan_default_options()669 const char* __msan_default_options() { return ""; }
670 } // extern "C"
671 #endif
672
673 extern "C" {
674 SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_print_stack_trace()675 void __sanitizer_print_stack_trace() {
676 GET_FATAL_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME());
677 stack.Print();
678 }
679 } // extern "C"
680