[llvm] r342186 - [SanitizerCoverage] Create comdat for global arrays.

Matt Morehouse via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 13 14:45:55 PDT 2018


Author: morehouse
Date: Thu Sep 13 14:45:55 2018
New Revision: 342186

URL: http://llvm.org/viewvc/llvm-project?rev=342186&view=rev
Log:
[SanitizerCoverage] Create comdat for global arrays.

Summary:
Place global arrays in comdat sections with their associated functions.
This makes sure they are stripped along with the functions they
reference, even on the BFD linker.

Reviewers: eugenis

Reviewed By: eugenis

Subscribers: eraman, hiraditya, llvm-commits

Differential Revision: https://reviews.llvm.org/D51902

Modified:
    llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
    llvm/trunk/test/Instrumentation/SanitizerCoverage/div-tracing.ll
    llvm/trunk/test/Instrumentation/SanitizerCoverage/gep-tracing.ll
    llvm/trunk/test/Instrumentation/SanitizerCoverage/inline-8bit-counters.ll
    llvm/trunk/test/Instrumentation/SanitizerCoverage/pc-table.ll
    llvm/trunk/test/Instrumentation/SanitizerCoverage/tracing.ll

Modified: llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp?rev=342186&r1=342185&r2=342186&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp Thu Sep 13 14:45:55 2018
@@ -219,6 +219,8 @@ private:
                    MDNode::get(*C, None));
   }
 
+  Comdat *GetOrCreateFunctionComdat(Function &F);
+
   std::string getSectionName(const std::string &Section) const;
   std::string getSectionStart(const std::string &Section) const;
   std::string getSectionEnd(const std::string &Section) const;
@@ -234,6 +236,7 @@ private:
   Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy,
       *Int16Ty, *Int8Ty, *Int8PtrTy;
   Module *CurModule;
+  std::string CurModuleUniqueId;
   Triple TargetTriple;
   LLVMContext *C;
   const DataLayout *DL;
@@ -304,6 +307,7 @@ bool SanitizerCoverageModule::runOnModul
   C = &(M.getContext());
   DL = &M.getDataLayout();
   CurModule = &M;
+  CurModuleUniqueId = getUniqueModuleId(CurModule);
   TargetTriple = Triple(M.getTargetTriple());
   FunctionGuardArray = nullptr;
   Function8bitCounterArray = nullptr;
@@ -565,17 +569,35 @@ bool SanitizerCoverageModule::runOnFunct
   return true;
 }
 
+Comdat *SanitizerCoverageModule::GetOrCreateFunctionComdat(Function &F) {
+  if (auto Comdat = F.getComdat()) return Comdat;
+  if (!TargetTriple.isOSBinFormatELF()) return nullptr;
+  assert(F.hasName());
+  std::string Name = F.getName();
+  if (F.hasLocalLinkage()) {
+    if (CurModuleUniqueId.empty()) return nullptr;
+    Name += CurModuleUniqueId;
+  }
+  auto Comdat = CurModule->getOrInsertComdat(Name);
+  F.setComdat(Comdat);
+  return Comdat;
+}
+
 GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection(
     size_t NumElements, Function &F, Type *Ty, const char *Section) {
   ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
   auto Array = new GlobalVariable(
       *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
       Constant::getNullValue(ArrayTy), "__sancov_gen_");
-  if (auto Comdat = F.getComdat())
+  if (auto Comdat = GetOrCreateFunctionComdat(F))
     Array->setComdat(Comdat);
   Array->setSection(getSectionName(Section));
   Array->setAlignment(Ty->isPointerTy() ? DL->getPointerSize()
                                         : Ty->getPrimitiveSizeInBits() / 8);
+  GlobalsToAppendToCompilerUsed.push_back(Array);
+  MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
+  Array->addMetadata(LLVMContext::MD_associated, *MD);
+
   return Array;
 }
 
@@ -613,23 +635,12 @@ void SanitizerCoverageModule::CreateFunc
     FunctionGuardArray = CreateFunctionLocalArrayInSection(
         AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName);
     GlobalsToAppendToUsed.push_back(FunctionGuardArray);
-    GlobalsToAppendToCompilerUsed.push_back(FunctionGuardArray);
-    MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
-    FunctionGuardArray->addMetadata(LLVMContext::MD_associated, *MD);
   }
-  if (Options.Inline8bitCounters) {
+  if (Options.Inline8bitCounters)
     Function8bitCounterArray = CreateFunctionLocalArrayInSection(
         AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName);
-    GlobalsToAppendToCompilerUsed.push_back(Function8bitCounterArray);
-    MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
-    Function8bitCounterArray->addMetadata(LLVMContext::MD_associated, *MD);
-  }
-  if (Options.PCTable) {
+  if (Options.PCTable)
     FunctionPCsArray = CreatePCArray(F, AllBlocks);
-    GlobalsToAppendToCompilerUsed.push_back(FunctionPCsArray);
-    MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
-    FunctionPCsArray->addMetadata(LLVMContext::MD_associated, *MD);
-  }
 }
 
 bool SanitizerCoverageModule::InjectCoverage(Function &F,

Modified: llvm/trunk/test/Instrumentation/SanitizerCoverage/div-tracing.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/SanitizerCoverage/div-tracing.ll?rev=342186&r1=342185&r2=342186&view=diff
==============================================================================
--- llvm/trunk/test/Instrumentation/SanitizerCoverage/div-tracing.ll (original)
+++ llvm/trunk/test/Instrumentation/SanitizerCoverage/div-tracing.ll Thu Sep 13 14:45:55 2018
@@ -10,7 +10,7 @@ entry:
   ret i32 %div
 }
 
-; CHECK-LABEL: div_a_b
+; CHECK-LABEL: @div_a_b
 ; CHECK: call void @__sanitizer_cov_trace_div4(i32 %b)
 ; CHECK: ret
 
@@ -21,7 +21,7 @@ entry:
   ret i32 %div
 }
 
-; CHECK-LABEL: div_a_10
+; CHECK-LABEL: @div_a_10
 ; CHECK-NOT: __sanitizer_cov_trace_div
 ; CHECK: ret
 
@@ -31,7 +31,7 @@ entry:
   ret i64 %div
 }
 
-; CHECK-LABEL: div_a_b
+; CHECK-LABEL: @div_a_b_64
 ; CHECK: call void @__sanitizer_cov_trace_div8(i64 %b)
 ; CHECK: ret
 

Modified: llvm/trunk/test/Instrumentation/SanitizerCoverage/gep-tracing.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/SanitizerCoverage/gep-tracing.ll?rev=342186&r1=342185&r2=342186&view=diff
==============================================================================
--- llvm/trunk/test/Instrumentation/SanitizerCoverage/gep-tracing.ll (original)
+++ llvm/trunk/test/Instrumentation/SanitizerCoverage/gep-tracing.ll Thu Sep 13 14:45:55 2018
@@ -26,7 +26,7 @@ entry:
   ret void
 }
 
-; CHECK-LABEL: define void @gep_2([1000 x i32]* nocapture %a, i32 %i, i32 %j) {
+; CHECK-LABEL: define void @gep_2([1000 x i32]* nocapture %a, i32 %i, i32 %j)
 ; CHECK: call void @__sanitizer_cov_trace_gep(i64 %idxprom1)
 ; CHECK: call void @__sanitizer_cov_trace_gep(i64 %idxprom)
 ; CHECK: ret void

Modified: llvm/trunk/test/Instrumentation/SanitizerCoverage/inline-8bit-counters.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/SanitizerCoverage/inline-8bit-counters.ll?rev=342186&r1=342185&r2=342186&view=diff
==============================================================================
--- llvm/trunk/test/Instrumentation/SanitizerCoverage/inline-8bit-counters.ll (original)
+++ llvm/trunk/test/Instrumentation/SanitizerCoverage/inline-8bit-counters.ll Thu Sep 13 14:45:55 2018
@@ -5,7 +5,7 @@ target datalayout = "e-p:64:64:64-i1:8:8
 target triple = "x86_64-unknown-linux-gnu"
 define void @foo() {
 entry:
-; CHECK: section "__sancov_cntrs", align 1
+; CHECK: section "__sancov_cntrs", comdat($foo), align 1
 ; CHECK:  %0 = load i8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @__sancov_gen_, i64 0, i64 0), !nosanitize
 ; CHECK:  %1 = add i8 %0, 1
 ; CHECK:  store i8 %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @__sancov_gen_, i64 0, i64 0), !nosanitize

Modified: llvm/trunk/test/Instrumentation/SanitizerCoverage/pc-table.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/SanitizerCoverage/pc-table.ll?rev=342186&r1=342185&r2=342186&view=diff
==============================================================================
--- llvm/trunk/test/Instrumentation/SanitizerCoverage/pc-table.ll (original)
+++ llvm/trunk/test/Instrumentation/SanitizerCoverage/pc-table.ll Thu Sep 13 14:45:55 2018
@@ -17,7 +17,7 @@ entry:
   ret void
 }
 
-; CHECK: private constant [6 x i64*] [{{.*}}@foo{{.*}}blockaddress{{.*}}blockaddress{{.*}}], section "__sancov_pcs", align 8
+; CHECK: private constant [6 x i64*] [{{.*}}@foo{{.*}}blockaddress{{.*}}blockaddress{{.*}}], section "__sancov_pcs", comdat($foo), align 8
 ; CHECK: define internal void @sancov.module_ctor
 ; CHECK: call void @__sanitizer_cov
 ; CHECK: call void @__sanitizer_cov_pcs_init

Modified: llvm/trunk/test/Instrumentation/SanitizerCoverage/tracing.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/SanitizerCoverage/tracing.ll?rev=342186&r1=342185&r2=342186&view=diff
==============================================================================
--- llvm/trunk/test/Instrumentation/SanitizerCoverage/tracing.ll (original)
+++ llvm/trunk/test/Instrumentation/SanitizerCoverage/tracing.ll Thu Sep 13 14:45:55 2018
@@ -32,7 +32,7 @@ entry:
 ; CHECK_PC: ret void
 ; CHECK_PC-NOT: call void @__sanitizer_cov_module_init
 
-; CHECK_PC_GUARD: section "__sancov_guards", align 4
+; CHECK_PC_GUARD: section "__sancov_guards", comdat($foo), align 4
 ; CHECK_PC_GUARD-LABEL: define void @foo
 ; CHECK_PC_GUARD: call void @__sanitizer_cov_trace_pc_guard
 ; CHECK_PC_GUARD: call void @__sanitizer_cov_trace_pc_guard




More information about the llvm-commits mailing list