@@ -422,6 +422,7 @@ class MemorySanitizer : public FunctionPass {
422
422
friend struct VarArgPowerPC64Helper ;
423
423
424
424
void initializeCallbacks (Module &M);
425
+ void createUserspaceApi (Module &M);
425
426
426
427
// / Track origins (allocation points) of uninitialized values.
427
428
int TrackOrigins;
@@ -455,8 +456,11 @@ class MemorySanitizer : public FunctionPass {
455
456
// / function.
456
457
GlobalVariable *OriginTLS;
457
458
459
+ // / Are the instrumentation callbacks set up?
460
+ bool CallbacksInitialized = false ;
461
+
458
462
// / The run-time callback to print a warning.
459
- Value *WarningFn = nullptr ;
463
+ Value *WarningFn;
460
464
461
465
// These arrays are indexed by log2(AccessSize).
462
466
Value *MaybeWarningFn[kNumberOfAccessSizes ];
@@ -522,12 +526,8 @@ static GlobalVariable *createPrivateNonConstGlobalForString(Module &M,
522
526
GlobalValue::PrivateLinkage, StrConst, " " );
523
527
}
524
528
525
- // / Insert extern declaration of runtime-provided functions and globals.
526
- void MemorySanitizer::initializeCallbacks (Module &M) {
527
- // Only do this once.
528
- if (WarningFn)
529
- return ;
530
-
529
+ // / Insert declarations for userspace-specific functions and globals.
530
+ void MemorySanitizer::createUserspaceApi (Module &M) {
531
531
IRBuilder<> IRB (*C);
532
532
// Create the callback.
533
533
// FIXME: this function should have "Cold" calling conv,
@@ -536,6 +536,38 @@ void MemorySanitizer::initializeCallbacks(Module &M) {
536
536
: " __msan_warning_noreturn" ;
537
537
WarningFn = M.getOrInsertFunction (WarningFnName, IRB.getVoidTy ());
538
538
539
+ // Create the global TLS variables.
540
+ RetvalTLS = new GlobalVariable (
541
+ M, ArrayType::get (IRB.getInt64Ty (), kRetvalTLSSize / 8 ), false ,
542
+ GlobalVariable::ExternalLinkage, nullptr , " __msan_retval_tls" , nullptr ,
543
+ GlobalVariable::InitialExecTLSModel);
544
+
545
+ RetvalOriginTLS = new GlobalVariable (
546
+ M, OriginTy, false , GlobalVariable::ExternalLinkage, nullptr ,
547
+ " __msan_retval_origin_tls" , nullptr , GlobalVariable::InitialExecTLSModel);
548
+
549
+ ParamTLS = new GlobalVariable (
550
+ M, ArrayType::get (IRB.getInt64Ty (), kParamTLSSize / 8 ), false ,
551
+ GlobalVariable::ExternalLinkage, nullptr , " __msan_param_tls" , nullptr ,
552
+ GlobalVariable::InitialExecTLSModel);
553
+
554
+ ParamOriginTLS = new GlobalVariable (
555
+ M, ArrayType::get (OriginTy, kParamTLSSize / 4 ), false ,
556
+ GlobalVariable::ExternalLinkage, nullptr , " __msan_param_origin_tls" ,
557
+ nullptr , GlobalVariable::InitialExecTLSModel);
558
+
559
+ VAArgTLS = new GlobalVariable (
560
+ M, ArrayType::get (IRB.getInt64Ty (), kParamTLSSize / 8 ), false ,
561
+ GlobalVariable::ExternalLinkage, nullptr , " __msan_va_arg_tls" , nullptr ,
562
+ GlobalVariable::InitialExecTLSModel);
563
+ VAArgOverflowSizeTLS = new GlobalVariable (
564
+ M, IRB.getInt64Ty (), false , GlobalVariable::ExternalLinkage, nullptr ,
565
+ " __msan_va_arg_overflow_size_tls" , nullptr ,
566
+ GlobalVariable::InitialExecTLSModel);
567
+ OriginTLS = new GlobalVariable (
568
+ M, IRB.getInt32Ty (), false , GlobalVariable::ExternalLinkage, nullptr ,
569
+ " __msan_origin_tls" , nullptr , GlobalVariable::InitialExecTLSModel);
570
+
539
571
for (size_t AccessSizeIndex = 0 ; AccessSizeIndex < kNumberOfAccessSizes ;
540
572
AccessSizeIndex++) {
541
573
unsigned AccessSize = 1 << AccessSizeIndex;
@@ -556,6 +588,17 @@ void MemorySanitizer::initializeCallbacks(Module &M) {
556
588
MsanPoisonStackFn =
557
589
M.getOrInsertFunction (" __msan_poison_stack" , IRB.getVoidTy (),
558
590
IRB.getInt8PtrTy (), IntptrTy);
591
+ }
592
+
593
+ // / Insert extern declaration of runtime-provided functions and globals.
594
+ void MemorySanitizer::initializeCallbacks (Module &M) {
595
+ // Only do this once.
596
+ if (CallbacksInitialized)
597
+ return ;
598
+
599
+ IRBuilder<> IRB (*C);
600
+ // Initialize callbacks that are common for kernel and userspace
601
+ // instrumentation.
559
602
MsanChainOriginFn = M.getOrInsertFunction (
560
603
" __msan_chain_origin" , IRB.getInt32Ty (), IRB.getInt32Ty ());
561
604
MemmoveFn = M.getOrInsertFunction (
@@ -567,41 +610,13 @@ void MemorySanitizer::initializeCallbacks(Module &M) {
567
610
MemsetFn = M.getOrInsertFunction (
568
611
" __msan_memset" , IRB.getInt8PtrTy (), IRB.getInt8PtrTy (), IRB.getInt32Ty (),
569
612
IntptrTy);
570
-
571
- // Create globals.
572
- RetvalTLS = new GlobalVariable (
573
- M, ArrayType::get (IRB.getInt64Ty (), kRetvalTLSSize / 8 ), false ,
574
- GlobalVariable::ExternalLinkage, nullptr , " __msan_retval_tls" , nullptr ,
575
- GlobalVariable::InitialExecTLSModel);
576
- RetvalOriginTLS = new GlobalVariable (
577
- M, OriginTy, false , GlobalVariable::ExternalLinkage, nullptr ,
578
- " __msan_retval_origin_tls" , nullptr , GlobalVariable::InitialExecTLSModel);
579
-
580
- ParamTLS = new GlobalVariable (
581
- M, ArrayType::get (IRB.getInt64Ty (), kParamTLSSize / 8 ), false ,
582
- GlobalVariable::ExternalLinkage, nullptr , " __msan_param_tls" , nullptr ,
583
- GlobalVariable::InitialExecTLSModel);
584
- ParamOriginTLS = new GlobalVariable (
585
- M, ArrayType::get (OriginTy, kParamTLSSize / 4 ), false ,
586
- GlobalVariable::ExternalLinkage, nullptr , " __msan_param_origin_tls" ,
587
- nullptr , GlobalVariable::InitialExecTLSModel);
588
-
589
- VAArgTLS = new GlobalVariable (
590
- M, ArrayType::get (IRB.getInt64Ty (), kParamTLSSize / 8 ), false ,
591
- GlobalVariable::ExternalLinkage, nullptr , " __msan_va_arg_tls" , nullptr ,
592
- GlobalVariable::InitialExecTLSModel);
593
- VAArgOverflowSizeTLS = new GlobalVariable (
594
- M, IRB.getInt64Ty (), false , GlobalVariable::ExternalLinkage, nullptr ,
595
- " __msan_va_arg_overflow_size_tls" , nullptr ,
596
- GlobalVariable::InitialExecTLSModel);
597
- OriginTLS = new GlobalVariable (
598
- M, IRB.getInt32Ty (), false , GlobalVariable::ExternalLinkage, nullptr ,
599
- " __msan_origin_tls" , nullptr , GlobalVariable::InitialExecTLSModel);
600
-
601
613
// We insert an empty inline asm after __msan_report* to avoid callback merge.
602
614
EmptyAsm = InlineAsm::get (FunctionType::get (IRB.getVoidTy (), false ),
603
615
StringRef (" " ), StringRef (" " ),
604
616
/* hasSideEffects=*/ true );
617
+
618
+ createUserspaceApi (M);
619
+ CallbacksInitialized = true ;
605
620
}
606
621
607
622
// / Module-level initialization.
0 commit comments