[PATCH] D32688: [Coverage] Comdat section name should be same as the variable name in COFF format

Adam Folwarczny via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 2 03:16:51 PDT 2017


adamf updated this revision to Diff 97413.
adamf added a comment.

Fixed use after free from previous patch on non x86_64-pc-win32-coff. 
Updated test according to the fix.


https://reviews.llvm.org/D32688

Files:
  lib/Transforms/Instrumentation/InstrProfiling.cpp
  test/Instrumentation/InstrProfiling/PR23499.ll


Index: test/Instrumentation/InstrProfiling/PR23499.ll
===================================================================
--- test/Instrumentation/InstrProfiling/PR23499.ll
+++ test/Instrumentation/InstrProfiling/PR23499.ll
@@ -20,8 +20,10 @@
 
 
 ; COFF-NOT: __profn__Z3barIvEvv
+; COFF: $__profc__Z3barIvEvv = comdat any
+; COFF: $__profd__Z3barIvEvv = comdat any
 ; COFF: @__profc__Z3barIvEvv = linkonce_odr hidden global [1 x i64] zeroinitializer, section "{{.*}}prfc", comdat, align 8
-; COFF: @__profd__Z3barIvEvv = linkonce_odr hidden global { i64, i64, i64*, i8*, i8*, i32, [2 x i16] } { i64 4947693190065689389, i64 0, i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc__Z3barIvEvv, i32 0, i32 0), i8*{{.*}}, i8* null, i32 1, [2 x i16] zeroinitializer }, section "{{.*}}prfd{{.*}}", comdat($__profc__Z3barIvEvv), align 8
+; COFF: @__profd__Z3barIvEvv = linkonce_odr hidden global { i64, i64, i64*, i8*, i8*, i32, [2 x i16] } { i64 4947693190065689389, i64 0, i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc__Z3barIvEvv, i32 0, i32 0), i8*{{.*}}, i8* null, i32 1, [2 x i16] zeroinitializer }, section "{{.*}}prfd{{.*}}", comdat, align 8
 
 
 declare void @llvm.instrprof.increment(i8*, i64, i32, i32) #1
Index: lib/Transforms/Instrumentation/InstrProfiling.cpp
===================================================================
--- lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -362,7 +362,8 @@
 }
 
 static inline Comdat *getOrCreateProfileComdat(Module &M, Function &F,
-                                               InstrProfIncrementInst *Inc) {
+                                               InstrProfIncrementInst *Inc,
+                                               StringRef VarName) {
   if (!needsComdatForCounter(F, M))
     return nullptr;
 
@@ -370,10 +371,10 @@
   // name. The linker targeting COFF also requires that the COMDAT
   // a section is associated to must precede the associating section. For this
   // reason, we must choose the counter var's name as the name of the comdat.
-  StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF()
-                                ? getInstrProfCountersVarPrefix()
-                                : getInstrProfComdatPrefix());
-  return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix)));
+  if (Triple(M.getTargetTriple()).isOSBinFormatCOFF())
+    return M.getOrInsertComdat(VarName);
+
+  return M.getOrInsertComdat(getVarName(Inc, getInstrProfComdatPrefix()));
 }
 
 static bool needsRuntimeRegistrationOfSectionRange(const Module &M) {
@@ -406,9 +407,6 @@
   // only one copy of counters of the COMDAT function will be emitted after
   // linking.
   Function *Fn = Inc->getParent()->getParent();
-  Comdat *ProfileVarsComdat = nullptr;
-  ProfileVarsComdat = getOrCreateProfileComdat(*M, *Fn, Inc);
-
   uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
   LLVMContext &Ctx = M->getContext();
   ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
@@ -422,7 +420,8 @@
   CounterPtr->setSection(
       getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat()));
   CounterPtr->setAlignment(8);
-  CounterPtr->setComdat(ProfileVarsComdat);
+  CounterPtr->setComdat(getOrCreateProfileComdat(*M, *Fn, Inc,
+                                                 CounterPtr->getName()));
 
   auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
   // Allocate statically the array of pointers to value profile nodes for
@@ -443,7 +442,8 @@
       ValuesVar->setSection(
           getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
       ValuesVar->setAlignment(8);
-      ValuesVar->setComdat(ProfileVarsComdat);
+      ValuesVar->setComdat(getOrCreateProfileComdat(*M, *Fn, Inc,
+                                                    ValuesVar->getName()));
       ValuesPtrExpr =
           ConstantExpr::getBitCast(ValuesVar, Type::getInt8PtrTy(Ctx));
     }
@@ -476,7 +476,7 @@
   Data->setVisibility(NamePtr->getVisibility());
   Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat()));
   Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT);
-  Data->setComdat(ProfileVarsComdat);
+  Data->setComdat(getOrCreateProfileComdat(*M, *Fn, Inc, Data->getName()));
 
   PD.RegionCounters = CounterPtr;
   PD.DataVar = Data;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32688.97413.patch
Type: text/x-patch
Size: 4354 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170502/79e39bf7/attachment.bin>


More information about the llvm-commits mailing list