[llvm] r293987 - [sanitizer coverage] Fix Instrumentation to work on Windows.

Marcos Pividori via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 2 17:08:06 PST 2017


Author: mpividori
Date: Thu Feb  2 19:08:06 2017
New Revision: 293987

URL: http://llvm.org/viewvc/llvm-project?rev=293987&view=rev
Log:
[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

Modified:
    llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Modified: llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp?rev=293987&r1=293986&r2=293987&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp Thu Feb  2 19:08:06 2017
@@ -138,24 +138,6 @@ static cl::opt<bool> ClUse8bitCounters("
                                        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::runOnModul
   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::runOnModul
       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::CreateFunc
       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::InjectCove
   }
 }
 
+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."




More information about the llvm-commits mailing list