From: Marcos Pividori Date: Fri, 3 Feb 2017 01:08:06 +0000 (+0000) Subject: [sanitizer coverage] Fix Instrumentation to work on Windows. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=045de6fddb4efb05b17c393219b59714244d3ba1;p=llvm [sanitizer coverage] Fix Instrumentation to work on Windows. On Windows, the symbols "___stop___sancov_guards" and "___start___sancov_guards" are not defined automatically. So, we need to take a different approach. We define 3 sections: Section ".SCOV$A" will only hold a variable ___start___sancov_guard. Section ".SCOV$M" will hold the main data. Section ".SCOV$Z" will only hold a variable ___stop___sancov_guards. When linking, they will be merged sorted by the characters after the $, so we can use the pointers of the variables ___[start|stop]___sancov_guard to know the actual range of addresses of that section. In this diff, I updated instrumentation to include all the guard arrays in section ".SCOV$M". Differential Revision: https://reviews.llvm.org/D28434 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293987 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/lib/Transforms/Instrumentation/SanitizerCoverage.cpp index b6ec70a413e..ee5158fa1d5 100644 --- a/lib/Transforms/Instrumentation/SanitizerCoverage.cpp +++ b/lib/Transforms/Instrumentation/SanitizerCoverage.cpp @@ -138,24 +138,6 @@ static cl::opt ClUse8bitCounters("sanitizer-coverage-8bit-counters", cl::desc("Experimental 8-bit counters"), cl::Hidden, cl::init(false)); -static StringRef getSanCovTracePCGuardSection(const Module &M) { - return Triple(M.getTargetTriple()).isOSBinFormatMachO() - ? "__DATA,__sancov_guards" - : "__sancov_guards"; -} - -static StringRef getSanCovTracePCGuardSectionStart(const Module &M) { - return Triple(M.getTargetTriple()).isOSBinFormatMachO() - ? "\1section$start$__DATA$__sancov_guards" - : "__start___sancov_guards"; -} - -static StringRef getSanCovTracePCGuardSectionEnd(const Module &M) { - return Triple(M.getTargetTriple()).isOSBinFormatMachO() - ? "\1section$end$__DATA$__sancov_guards" - : "__stop___sancov_guards"; -} - namespace { SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) { @@ -233,6 +215,9 @@ private: SanCovWithCheckFunction->getNumUses() + SanCovTraceBB->getNumUses() + SanCovTraceEnter->getNumUses(); } + StringRef getSanCovTracePCGuardSection() const; + StringRef getSanCovTracePCGuardSectionStart() const; + StringRef getSanCovTracePCGuardSectionEnd() const; Function *SanCovFunction; Function *SanCovWithCheckFunction; Function *SanCovIndirCallFunction, *SanCovTracePCIndir; @@ -244,6 +229,7 @@ private: InlineAsm *EmptyAsm; Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy; Module *CurModule; + Triple TargetTriple; LLVMContext *C; const DataLayout *DL; @@ -263,6 +249,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) { C = &(M.getContext()); DL = &M.getDataLayout(); CurModule = &M; + TargetTriple = Triple(M.getTargetTriple()); HasSancovGuardsSection = false; IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits()); IntptrPtrTy = PointerType::getUnqual(IntptrTy); @@ -382,11 +369,11 @@ bool SanitizerCoverageModule::runOnModule(Module &M) { Function *CtorFunc; GlobalVariable *SecStart = new GlobalVariable( M, Int32PtrTy, false, GlobalVariable::ExternalLinkage, nullptr, - getSanCovTracePCGuardSectionStart(*CurModule)); + getSanCovTracePCGuardSectionStart()); SecStart->setVisibility(GlobalValue::HiddenVisibility); GlobalVariable *SecEnd = new GlobalVariable( M, Int32PtrTy, false, GlobalVariable::ExternalLinkage, nullptr, - getSanCovTracePCGuardSectionEnd(*CurModule)); + getSanCovTracePCGuardSectionEnd()); SecEnd->setVisibility(GlobalValue::HiddenVisibility); std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions( @@ -534,7 +521,7 @@ void SanitizerCoverageModule::CreateFunctionGuardArray(size_t NumGuards, Constant::getNullValue(ArrayOfInt32Ty), "__sancov_gen_"); if (auto Comdat = F.getComdat()) FunctionGuardArray->setComdat(Comdat); - FunctionGuardArray->setSection(getSanCovTracePCGuardSection(*CurModule)); + FunctionGuardArray->setSection(getSanCovTracePCGuardSection()); } bool SanitizerCoverageModule::InjectCoverage(Function &F, @@ -772,6 +759,27 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB, } } +StringRef SanitizerCoverageModule::getSanCovTracePCGuardSection() const { + if (TargetTriple.getObjectFormat() == Triple::COFF) + return ".SCOV$M"; + if (TargetTriple.isOSBinFormatMachO()) + return "__DATA,__sancov_guards"; + return "__sancov_guards"; +} + +StringRef SanitizerCoverageModule::getSanCovTracePCGuardSectionStart() const { + if (TargetTriple.isOSBinFormatMachO()) + return "\1section$start$__DATA$__sancov_guards"; + return "__start___sancov_guards"; +} + +StringRef SanitizerCoverageModule::getSanCovTracePCGuardSectionEnd() const { + if (TargetTriple.isOSBinFormatMachO()) + return "\1section$end$__DATA$__sancov_guards"; + return "__stop___sancov_guards"; +} + + char SanitizerCoverageModule::ID = 0; INITIALIZE_PASS_BEGIN(SanitizerCoverageModule, "sancov", "SanitizerCoverage: TODO."