[PATCH] D28434: [Sanitizer Coverage] Fix Instrumentation to work on Windows.

Marcos Pividori via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 6 20:18:43 PST 2017


mpividori created this revision.
mpividori added reviewers: kcc, aizatsky, rnk, zturner.
mpividori added a subscriber: llvm-commits.
mpividori set the repository for this revision to rL LLVM.

Hi,

I modified Sanitizer Coverage Instrumentation's code, to work on Windows.
In Windows, the symbols "___stop___sancov_guards" and "___start___sancov_guards" are not defined automatically. 
So, we need to take a different approach.

As suggested in: https://msdn.microsoft.com/en-us/library/7977wcck.aspx
I define 3 sections: ".SCOV$A", ".SCOV$M" and ".SCOV$Z".

- 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.

___[start|stop]___sancov_guard should be defined only once per module, because of that, I include them inside the static asan runtime: "clang_rt.asan_dynamic_runtime_thunk", that will be included in both, dlls and executables (that changes are in a different diff).

In this diff, I updated the instrumentation to include all the guard arrays in section ".SCOV$M".


Repository:
  rL LLVM

https://reviews.llvm.org/D28434

Files:
  lib/Transforms/Instrumentation/SanitizerCoverage.cpp


Index: lib/Transforms/Instrumentation/SanitizerCoverage.cpp
===================================================================
--- lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -78,7 +78,10 @@
 static const char *const SanCovModuleCtorName = "sancov.module_ctor";
 static const uint64_t SanCtorAndDtorPriority = 2;
 
-static const char *const SanCovTracePCGuardSection = "__sancov_guards";
+static const char *const SanCovTracePCGuardSectionStart =
+    "__start___sancov_guards";
+static const char *const SanCovTracePCGuardSectionStop =
+    "__stop___sancov_guards";
 static const char *const SanCovTracePCGuardName =
     "__sanitizer_cov_trace_pc_guard";
 static const char *const SanCovTracePCGuardInitName =
@@ -216,6 +219,7 @@
            SanCovWithCheckFunction->getNumUses() + SanCovTraceBB->getNumUses() +
            SanCovTraceEnter->getNumUses();
   }
+  StringRef getSanCovTracePCGuardSection() const;
   Function *SanCovFunction;
   Function *SanCovWithCheckFunction;
   Function *SanCovIndirCallFunction, *SanCovTracePCIndir;
@@ -227,6 +231,7 @@
   InlineAsm *EmptyAsm;
   Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy;
   Module *CurModule;
+  Triple TargetTriple;
   LLVMContext *C;
   const DataLayout *DL;
 
@@ -246,6 +251,7 @@
   C = &(M.getContext());
   DL = &M.getDataLayout();
   CurModule = &M;
+  TargetTriple = Triple(M.getTargetTriple());
   HasSancovGuardsSection = false;
   IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
   IntptrPtrTy = PointerType::getUnqual(IntptrTy);
@@ -363,20 +369,20 @@
   if (Options.TracePCGuard) {
     if (HasSancovGuardsSection) {
       Function *CtorFunc;
-      std::string SectionName(SanCovTracePCGuardSection);
-      GlobalVariable *Bounds[2];
-      const char *Prefix[2] = {"__start_", "__stop_"};
-      for (int i = 0; i < 2; i++) {
-        Bounds[i] = new GlobalVariable(M, Int32PtrTy, false,
-                                       GlobalVariable::ExternalLinkage, nullptr,
-                                       Prefix[i] + SectionName);
-        Bounds[i]->setVisibility(GlobalValue::HiddenVisibility);
-      }
+      GlobalVariable *SectionStart, *SectionStop;
+      SectionStart = new GlobalVariable(M, Int32PtrTy, false,
+          GlobalVariable::ExternalLinkage, nullptr,
+          SanCovTracePCGuardSectionStart);
+      SectionStart->setVisibility(GlobalValue::HiddenVisibility);
+      SectionStop = new GlobalVariable(M, Int32PtrTy, false,
+          GlobalVariable::ExternalLinkage, nullptr,
+          SanCovTracePCGuardSectionStop);
+      SectionStop->setVisibility(GlobalValue::HiddenVisibility);
       std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
           M, SanCovModuleCtorName, SanCovTracePCGuardInitName,
           {Int32PtrTy, Int32PtrTy},
-          {IRB.CreatePointerCast(Bounds[0], Int32PtrTy),
-            IRB.CreatePointerCast(Bounds[1], Int32PtrTy)});
+          {IRB.CreatePointerCast(SectionStart, Int32PtrTy),
+            IRB.CreatePointerCast(SectionStop, Int32PtrTy)});
 
       appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
     }
@@ -517,7 +523,7 @@
       Constant::getNullValue(ArrayOfInt32Ty), "__sancov_gen_");
   if (auto Comdat = F.getComdat())
     FunctionGuardArray->setComdat(Comdat);
-  FunctionGuardArray->setSection(SanCovTracePCGuardSection);
+  FunctionGuardArray->setSection(getSanCovTracePCGuardSection());
 }
 
 bool SanitizerCoverageModule::InjectCoverage(Function &F,
@@ -755,6 +761,12 @@
   }
 }
 
+StringRef SanitizerCoverageModule::getSanCovTracePCGuardSection() const {
+  if (TargetTriple.getObjectFormat() == Triple::COFF)
+    return ".SCOV$M";
+  return "__sancov_guards";
+}
+
 char SanitizerCoverageModule::ID = 0;
 INITIALIZE_PASS_BEGIN(SanitizerCoverageModule, "sancov",
                       "SanitizerCoverage: TODO."


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28434.83493.patch
Type: text/x-patch
Size: 3922 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170107/090ad728/attachment.bin>


More information about the llvm-commits mailing list