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