llvm.org GIT mirror llvm / 671dc85
[SanitizerCoverage] Use different module ctor names for trace-pc-guard and inline-8bit-counters Fixes the main issue in PR41693 When both modes are used, two functions are created: `sancov.module_ctor`, `sancov.module_ctor.$LastUnique`, where $LastUnique is the current LastUnique counter that may be different in another module. `sancov.module_ctor.$LastUnique` belongs to the comdat group of the same name (due to the non-null third field of the ctor in llvm.global_ctors). COMDAT group section [ 9] `.group' [sancov.module_ctor] contains 6 sections: [Index] Name [ 10] .text.sancov.module_ctor [ 11] .rela.text.sancov.module_ctor [ 12] .text.sancov.module_ctor.6 [ 13] .rela.text.sancov.module_ctor.6 [ 23] .init_array.2 [ 24] .rela.init_array.2 # 2 problems: # 1) If sancov.module_ctor in this module is discarded, this group # has a relocation to a discarded section. ld.bfd and gold will # error. (Another issue: it is silently accepted by lld) # 2) The comdat group has an unstable name that may be different in # another translation unit. Even if the linker allows the dangling relocation # (with --noinhibit-exec), there will be many undesired .init_array entries COMDAT group section [ 25] `.group' [sancov.module_ctor.6] contains 2 sections: [Index] Name [ 26] .init_array.2 [ 27] .rela.init_array.2 By using different module ctor names, the associated comdat group names will also be different and thus stable across modules. Reviewed By: morehouse, phosek Differential Revision: https://reviews.llvm.org/D61510 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360107 91177308-0d34-0410-b5e6-96231b3b80d8 Fangrui Song 5 months ago
4 changed file(s) with 30 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
6060 static const char *const SanCovTraceDiv8 = "__sanitizer_cov_trace_div8";
6161 static const char *const SanCovTraceGep = "__sanitizer_cov_trace_gep";
6262 static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch";
63 static const char *const SanCovModuleCtorName = "sancov.module_ctor";
63 static const char *const SanCovModuleCtorTracePcGuardName =
64 "sancov.module_ctor_trace_pc_guard";
65 static const char *const SanCovModuleCtor8bitCountersName =
66 "sancov.module_ctor_8bit_counters";
6467 static const uint64_t SanCtorAndDtorPriority = 2;
6568
6669 static const char *const SanCovTracePCGuardName =
208211 void CreateFunctionLocalArrays(Function &F, ArrayRef AllBlocks);
209212 void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx,
210213 bool IsLeafFunc = true);
211 Function *CreateInitCallsForSections(Module &M, const char *InitFunctionName,
212 Type *Ty, const char *Section);
214 Function *CreateInitCallsForSections(Module &M, const char *CtorName,
215 const char *InitFunctionName, Type *Ty,
216 const char *Section);
213217 std::pair CreateSecStartEnd(Module &M, const char *Section,
214218 Type *Ty);
215219
274278 }
275279
276280 Function *SanitizerCoverageModule::CreateInitCallsForSections(
277 Module &M, const char *InitFunctionName, Type *Ty,
281 Module &M, const char *CtorName, const char *InitFunctionName, Type *Ty,
278282 const char *Section) {
279283 auto SecStartEnd = CreateSecStartEnd(M, Section, Ty);
280284 auto SecStart = SecStartEnd.first;
281285 auto SecEnd = SecStartEnd.second;
282286 Function *CtorFunc;
283287 std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
284 M, SanCovModuleCtorName, InitFunctionName, {Ty, Ty}, {SecStart, SecEnd});
288 M, CtorName, InitFunctionName, {Ty, Ty}, {SecStart, SecEnd});
289 assert(CtorFunc->getName() == CtorName);
285290
286291 if (TargetTriple.supportsCOMDAT()) {
287292 // Use comdat to dedup CtorFunc.
288 CtorFunc->setComdat(M.getOrInsertComdat(SanCovModuleCtorName));
293 CtorFunc->setComdat(M.getOrInsertComdat(CtorName));
289294 appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc);
290295 } else {
291296 appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
402407 Function *Ctor = nullptr;
403408
404409 if (FunctionGuardArray)
405 Ctor = CreateInitCallsForSections(M, SanCovTracePCGuardInitName, Int32PtrTy,
410 Ctor = CreateInitCallsForSections(M, SanCovModuleCtorTracePcGuardName,
411 SanCovTracePCGuardInitName, Int32PtrTy,
406412 SanCovGuardsSectionName);
407413 if (Function8bitCounterArray)
408 Ctor = CreateInitCallsForSections(M, SanCov8bitCountersInitName, Int8PtrTy,
414 Ctor = CreateInitCallsForSections(M, SanCovModuleCtor8bitCountersName,
415 SanCov8bitCountersInitName, Int8PtrTy,
409416 SanCovCountersSectionName);
410417 if (Ctor && Options.PCTable) {
411418 auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy);
3737 ; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc_indir
3838 ; CHECK_TRACE_PC_GUARD: ret void
3939
40 ; CHECK_TRACE_PC_GUARD-LABEL: define internal void @sancov.module_ctor() comdat
41
40 ; CHECK_TRACE_PC_GUARD-LABEL: define internal void @sancov.module_ctor_trace_pc_guard() comdat
0 ; RUN: opt < %s -sancov -sanitizer-coverage-level=1 -sanitizer-coverage-trace-pc-guard -sanitizer-coverage-inline-8bit-counters -S | FileCheck %s
1
2 ; Module ctors should have stable names across modules, not something like
3 ; @sancov.module_ctor.3 that may cause duplicate ctors after linked together.
4
5 ; CHECK: define internal void @sancov.module_ctor_trace_pc_guard() comdat {
6 ; CHECK: define internal void @sancov.module_ctor_8bit_counters() comdat {
7
8 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
9 target triple = "x86_64-unknown-linux-gnu"
10 define void @foo() {
11 ret void
12 }
3737 ; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc_indir
3838 ; CHECK_TRACE_PC_GUARD: ret void
3939
40 ; CHECK_TRACE_PC_GUARD-LABEL: define internal void @sancov.module_ctor() {
41
40 ; CHECK_TRACE_PC_GUARD-LABEL: define internal void @sancov.module_ctor_trace_pc_guard() {