diff --git a/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp @@ -1062,6 +1062,16 @@ Mod->getOrInsertFunction("__dfsan_cmp_callback", DFSanCmpCallbackFnTy); } +static Constant *getOrInsertGlobal(Module &M, bool &Changed, StringRef Name, + Type *Ty) { + Constant *C = M.getOrInsertGlobal(Name, Ty); + if (GlobalVariable *G = dyn_cast(C)) { + Changed |= G->getThreadLocalMode() != GlobalVariable::InitialExecTLSModel; + G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); + } + return C; +} + bool DataFlowSanitizer::runImpl(Module &M) { init(M); @@ -1073,19 +1083,13 @@ bool Changed = false; - Type *ArgTLSTy = ArrayType::get(Type::getInt64Ty(*Ctx), kArgTLSSize / 8); - ArgTLS = Mod->getOrInsertGlobal("__dfsan_arg_tls", ArgTLSTy); - if (GlobalVariable *G = dyn_cast(ArgTLS)) { - Changed |= G->getThreadLocalMode() != GlobalVariable::InitialExecTLSModel; - G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); - } - Type *RetvalTLSTy = - ArrayType::get(Type::getInt64Ty(*Ctx), kRetvalTLSSize / 8); - RetvalTLS = Mod->getOrInsertGlobal("__dfsan_retval_tls", RetvalTLSTy); - if (GlobalVariable *G = dyn_cast(RetvalTLS)) { - Changed |= G->getThreadLocalMode() != GlobalVariable::InitialExecTLSModel; - G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); - } + // These globals must be kept in sync with the ones in dfsan.cpp. + ArgTLS = getOrInsertGlobal( + *Mod, Changed, "__dfsan_arg_tls", + ArrayType::get(Type::getInt64Ty(*Ctx), kArgTLSSize / 8)); + RetvalTLS = getOrInsertGlobal( + *Mod, Changed, "__dfsan_retval_tls", + ArrayType::get(Type::getInt64Ty(*Ctx), kRetvalTLSSize / 8)); ExternalShadowMask = Mod->getOrInsertGlobal(kDFSanExternShadowPtrMask, IntptrTy);