[llvm] r282465 - [sanitizer-coverage] don't emit the CTOR function if nothing has been instrumented

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 26 18:08:33 PDT 2016


Author: kcc
Date: Mon Sep 26 20:08:33 2016
New Revision: 282465

URL: http://llvm.org/viewvc/llvm-project?rev=282465&view=rev
Log:
[sanitizer-coverage] don't emit the CTOR function if nothing has been instrumented

Added:
    llvm/trunk/test/Instrumentation/SanitizerCoverage/no-func.ll
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=282465&r1=282464&r2=282465&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp Mon Sep 26 20:08:33 2016
@@ -230,6 +230,7 @@ private:
 
   GlobalVariable *GuardArray;
   GlobalVariable *EightBitCounterArray;
+  bool HasSancovGuardsSection;
 
   SanitizerCoverageOptions Options;
 };
@@ -242,6 +243,7 @@ bool SanitizerCoverageModule::runOnModul
   C = &(M.getContext());
   DL = &M.getDataLayout();
   CurModule = &M;
+  HasSancovGuardsSection = false;
   IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
   IntptrPtrTy = PointerType::getUnqual(IntptrTy);
   Type *VoidTy = Type::getVoidTy(*C);
@@ -351,24 +353,25 @@ bool SanitizerCoverageModule::runOnModul
       new GlobalVariable(M, ModNameStrConst->getType(), true,
                          GlobalValue::PrivateLinkage, ModNameStrConst);
   if (Options.TracePCGuard) {
-    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, IntptrPtrTy, false,
-                                     GlobalVariable::ExternalLinkage, nullptr,
-                                     Prefix[i] + SectionName);
-      Bounds[i]->setVisibility(GlobalValue::HiddenVisibility);
-    }
-    std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
-        M, SanCovModuleCtorName, SanCovTracePCGuardInitName,
-        {IntptrPtrTy, IntptrPtrTy},
-        {IRB.CreatePointerCast(Bounds[0], IntptrPtrTy),
-         IRB.CreatePointerCast(Bounds[1], IntptrPtrTy)});
-
-    appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
+    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, IntptrPtrTy, false,
+                                       GlobalVariable::ExternalLinkage, nullptr,
+                                       Prefix[i] + SectionName);
+        Bounds[i]->setVisibility(GlobalValue::HiddenVisibility);
+      }
+      std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
+          M, SanCovModuleCtorName, SanCovTracePCGuardInitName,
+          {IntptrPtrTy, IntptrPtrTy},
+          {IRB.CreatePointerCast(Bounds[0], IntptrPtrTy),
+            IRB.CreatePointerCast(Bounds[1], IntptrPtrTy)});
 
+      appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
+    }
   } else if (!Options.TracePC) {
     Function *CtorFunc;
     std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
@@ -673,6 +676,7 @@ void SanitizerCoverageModule::InjectCove
       GuardVar->setComdat(Comdat);
     // TODO: add debug into to GuardVar.
     GuardVar->setSection(SanCovTracePCGuardSection);
+    HasSancovGuardsSection = true;
     auto GuardPtr = IRB.CreatePointerCast(GuardVar, IntptrPtrTy);
     if (!UseCalls) {
       auto GuardLoad = IRB.CreateLoad(GuardPtr);

Added: llvm/trunk/test/Instrumentation/SanitizerCoverage/no-func.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/SanitizerCoverage/no-func.ll?rev=282465&view=auto
==============================================================================
--- llvm/trunk/test/Instrumentation/SanitizerCoverage/no-func.ll (added)
+++ llvm/trunk/test/Instrumentation/SanitizerCoverage/no-func.ll Mon Sep 26 20:08:33 2016
@@ -0,0 +1,9 @@
+; Tests that we don't insert __sanitizer_cov_trace_pc_guard_init or some such
+; when there is no instrumentation.
+; RUN: opt < %s -sancov -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard  -S | FileCheck %s
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at a = global i32 0, align 4
+
+; CHECK-NOT: call void




More information about the llvm-commits mailing list