@@ -321,6 +321,7 @@ static cl::opt<unsigned long long> ClOriginBase("msan-origin-base",
321
321
cl::desc (" Define custom MSan OriginBase" ),
322
322
cl::Hidden, cl::init(0 ));
323
323
324
+ static const char *const kMsanModuleCtorName = " msan.module_ctor" ;
324
325
static const char *const kMsanInitName = " __msan_init" ;
325
326
326
327
namespace {
@@ -586,6 +587,8 @@ class MemorySanitizer {
586
587
587
588
// / An empty volatile inline asm that prevents callback merge.
588
589
InlineAsm *EmptyAsm;
590
+
591
+ Function *MsanCtorFunction;
589
592
};
590
593
591
594
// / A legacy function pass for msan instrumentation.
@@ -839,6 +842,8 @@ Value *MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore, int size) {
839
842
}
840
843
841
844
// / Module-level initialization.
845
+ // /
846
+ // / inserts a call to __msan_init to the module's constructor list.
842
847
void MemorySanitizer::initializeModule (Module &M) {
843
848
auto &DL = M.getDataLayout ();
844
849
@@ -913,7 +918,22 @@ void MemorySanitizer::initializeModule(Module &M) {
913
918
OriginStoreWeights = MDBuilder (*C).createBranchWeights (1 , 1000 );
914
919
915
920
if (!CompileKernel) {
916
- getOrCreateInitFunction (M, kMsanInitName );
921
+ std::tie (MsanCtorFunction, std::ignore) =
922
+ getOrCreateSanitizerCtorAndInitFunctions (
923
+ M, kMsanModuleCtorName , kMsanInitName ,
924
+ /* InitArgTypes=*/ {},
925
+ /* InitArgs=*/ {},
926
+ // This callback is invoked when the functions are created the first
927
+ // time. Hook them into the global ctors list in that case:
928
+ [&](Function *Ctor, Function *) {
929
+ if (!ClWithComdat) {
930
+ appendToGlobalCtors (M, Ctor, 0 );
931
+ return ;
932
+ }
933
+ Comdat *MsanCtorComdat = M.getOrInsertComdat (kMsanModuleCtorName );
934
+ Ctor->setComdat (MsanCtorComdat);
935
+ appendToGlobalCtors (M, Ctor, 0 , Ctor);
936
+ });
917
937
918
938
if (TrackOrigins)
919
939
M.getOrInsertGlobal (" __msan_track_origins" , IRB.getInt32Ty (), [&] {
@@ -4458,6 +4478,8 @@ static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
4458
4478
}
4459
4479
4460
4480
bool MemorySanitizer::sanitizeFunction (Function &F, TargetLibraryInfo &TLI) {
4481
+ if (!CompileKernel && (&F == MsanCtorFunction))
4482
+ return false ;
4461
4483
MemorySanitizerVisitor Visitor (F, *this , TLI);
4462
4484
4463
4485
// Clear out readonly/readnone attributes.
0 commit comments