]> granicus.if.org Git - llvm/commitdiff
[sanitizer-coverage] change trace-pc to use 8-byte guards
authorKostya Serebryany <kcc@google.com>
Sat, 17 Sep 2016 05:03:05 +0000 (05:03 +0000)
committerKostya Serebryany <kcc@google.com>
Sat, 17 Sep 2016 05:03:05 +0000 (05:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@281809 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Instrumentation/SanitizerCoverage.cpp
test/Instrumentation/SanitizerCoverage/tracing.ll

index f8844bdcf388d0ae2b7cdd90fe61911a5cecca2f..3c137c97679491ed11e86ac7c6e3e4391bf7afb4 100644 (file)
@@ -293,7 +293,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
   SanCovTracePC = checkSanitizerInterfaceFunction(
       M.getOrInsertFunction(SanCovTracePCName, VoidTy, nullptr));
   SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
-      SanCovTracePCGuardName, VoidTy, IRB.getInt8PtrTy(), nullptr));
+      SanCovTracePCGuardName, VoidTy, Int64PtrTy, nullptr));
   SanCovTraceEnter = checkSanitizerInterfaceFunction(
       M.getOrInsertFunction(SanCovTraceEnterName, VoidTy, Int32PtrTy, nullptr));
   SanCovTraceBB = checkSanitizerInterfaceFunction(
@@ -352,16 +352,18 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
   if (Options.TracePCGuard) {
     Function *CtorFunc;
     std::string SectionName(SanCovTracePCGuardSection);
-    auto Start =
-        new GlobalVariable(M, Int8PtrTy, false, GlobalVariable::ExternalLinkage,
-                           nullptr, "__start_" + SectionName);
-    auto Stop =
-        new GlobalVariable(M, Int8PtrTy, false, GlobalVariable::ExternalLinkage,
-                           nullptr, "__stop_" + SectionName);
+    GlobalVariable *Bounds[2];
+    const char *Prefix[2] = {"__start_", "__stop_"};
+    for (int i = 0; i < 2; i++) {
+      Bounds[i] = new GlobalVariable(M, Int64PtrTy, false,
+                                     GlobalVariable::ExternalLinkage, nullptr,
+                                     Prefix[i] + SectionName);
+      Bounds[i]->setVisibility(GlobalValue::HiddenVisibility);
+    }
     std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
         M, SanCovModuleCtorName, SanCovTracePCGuardInitName,
-        {Int8PtrTy, Int8PtrTy}, {IRB.CreatePointerCast(Start, Int8PtrTy),
-                                 IRB.CreatePointerCast(Stop, Int8PtrTy)});
+        {Int64PtrTy, Int64PtrTy}, {IRB.CreatePointerCast(Bounds[0], Int64PtrTy),
+                                 IRB.CreatePointerCast(Bounds[1], Int64PtrTy)});
 
     appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
 
@@ -662,23 +664,24 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
     IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC.
     IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
   } else if (Options.TracePCGuard) {
-    auto GuardVar = new GlobalVariable(*F.getParent(), IRB.getInt8Ty(), false,
-                                       GlobalVariable::LinkOnceODRLinkage,
-                                       Constant::getNullValue(IRB.getInt8Ty()),
-                                       "__sancov_guard." + F.getName());
+    auto GuardVar = new GlobalVariable(
+        *F.getParent(), Int64Ty, false, GlobalVariable::LinkOnceODRLinkage,
+        Constant::getNullValue(Int64Ty), "__sancov_guard." + F.getName());
     // TODO: add debug into to GuardVar.
     GuardVar->setSection(SanCovTracePCGuardSection);
-    auto GuardPtr = IRB.CreatePointerCast(GuardVar, IRB.getInt8PtrTy());
-    auto GuardLoad = IRB.CreateLoad(GuardPtr);
-    GuardLoad->setAtomic(AtomicOrdering::Monotonic);
-    GuardLoad->setAlignment(1);
-    SetNoSanitizeMetadata(GuardLoad);  // Don't instrument with e.g. asan.
-    auto Cmp = IRB.CreateICmpNE(
-        Constant::getAllOnesValue(GuardLoad->getType()), GuardLoad);
-    auto Ins = SplitBlockAndInsertIfThen(
-        Cmp, &*IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
-    IRB.SetCurrentDebugLocation(EntryLoc);
-    IRB.SetInsertPoint(Ins);
+    auto GuardPtr = IRB.CreatePointerCast(GuardVar, Int64PtrTy);
+    if (!UseCalls) {
+      auto GuardLoad = IRB.CreateLoad(GuardPtr);
+      GuardLoad->setAtomic(AtomicOrdering::Monotonic);
+      GuardLoad->setAlignment(8);
+      SetNoSanitizeMetadata(GuardLoad);  // Don't instrument with e.g. asan.
+      auto Cmp = IRB.CreateICmpSGE(
+          GuardLoad, Constant::getNullValue(GuardLoad->getType()));
+      auto Ins = SplitBlockAndInsertIfThen(
+          Cmp, &*IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
+      IRB.SetCurrentDebugLocation(EntryLoc);
+      IRB.SetInsertPoint(Ins);
+    }
     IRB.CreateCall(SanCovTracePCGuard, GuardPtr);
     IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
   } else if (Options.TraceBB) {
index 5b6a17bd9e7b35561f31c3ea6b2092ab823fabc2..df804f6f7e18e0439bd4c06946c541e42e55cff7 100644 (file)
@@ -46,4 +46,4 @@ entry:
 ; CHECK_PC_GUARD: call void @__sanitizer_cov_trace_pc_guard
 ; CHECK_PC_GUARD-NOT: call void @__sanitizer_cov_trace_pc
 ; CHECK_PC_GUARD: ret void
-; CHECK_PC_GUARD: call void @__sanitizer_cov_trace_pc_guard_init(i8* bitcast (i8** @__start___sancov_guards to i8*), i8* bitcast (i8** @__stop___sancov_guards to i8*))
+; CHECK_PC_GUARD: call void @__sanitizer_cov_trace_pc_guard_init(i64* bitcast (i64** @__start___sancov_guards to i64*), i64* bitcast (i64** @__stop___sancov_guards to i64*))