From 0f22fac2742bd3e382bba577671c19841ac2b5f7 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Tue, 14 Aug 2018 22:04:34 +0000 Subject: [PATCH] [SanitizerCoverage] Add associated metadata to PC guards. Summary: Without this metadata LLD strips unused PC table entries but won't strip unused guards. This metadata also seems to influence the linker to change the ordering in the PC guard section to match that of the PC table section. The libFuzzer runtime library depends on the ordering of the PC table and PC guard sections being the same. This is not generally guaranteed, so we may need to redesign PC tables/guards/counters in the future. Reviewers: eugenis Reviewed By: eugenis Subscribers: kcc, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D50483 llvm-svn: 339733 --- compiler-rt/test/fuzzer/gc-sections.test | 7 ++++++- llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler-rt/test/fuzzer/gc-sections.test b/compiler-rt/test/fuzzer/gc-sections.test index b8abfbbdf17b..e915c4cc9eb0 100644 --- a/compiler-rt/test/fuzzer/gc-sections.test +++ b/compiler-rt/test/fuzzer/gc-sections.test @@ -8,8 +8,13 @@ With gc-sections. Currently, we can't remove unused code except with LLD. RUN: %cpp_compiler %S/GcSectionsTest.cpp -o %t -fuse-ld=lld -ffunction-sections -Wl,-gc-sections RUN: nm %t | not grep UnusedFunctionShouldBeRemovedByLinker RUN: %run %t -runs=0 2>&1 | FileCheck %s -CHECK-NOT: ERROR: The size of coverage PC tables does not match With gc sections, with trace-pc. Unused code is removed. RUN: %cpp_compiler %S/GcSectionsTest.cpp -o %t -fsanitize-coverage=0 -fsanitize-coverage=trace-pc -ffunction-sections -Wl,-gc-sections RUN: nm %t | not grep UnusedFunctionShouldBeRemovedByLinker + +RUN: %cpp_compiler %S/GcSectionsTest.cpp -o %t -fsanitize-coverage=0 -fsanitize-coverage=trace-pc-guard,pc-table -fuse-ld=lld -ffunction-sections -Wl,-gc-sections +RUN: nm %t | not grep UnusedFunctionShouldBeRemovedByLinker +RUN: %run %t -runs=0 2>&1 | FileCheck %s + +CHECK-NOT: ERROR: The size of coverage PC tables does not match diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp index a4dd48c8dd6a..c0b219d1d1b7 100644 --- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp +++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp @@ -591,6 +591,9 @@ void SanitizerCoverageModule::CreateFunctionLocalArrays( 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) { Function8bitCounterArray = CreateFunctionLocalArrayInSection( -- 2.34.1