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