SanitizerCoverage: Support sanitizer guard section on darwin

MachO's sections need a segment as well as a section name, and the
section start and end symbols are spelled differently than on ELF.

llvm-svn: 293733
This commit is contained in:
Justin Bogner 2017-02-01 02:38:39 +00:00
parent 657e39979b
commit ae642b4739
2 changed files with 39 additions and 13 deletions

View File

@ -78,7 +78,6 @@ static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch";
static const char *const SanCovModuleCtorName = "sancov.module_ctor";
static const uint64_t SanCtorAndDtorPriority = 2;
static const char *const SanCovTracePCGuardSection = "__sancov_guards";
static const char *const SanCovTracePCGuardName =
"__sanitizer_cov_trace_pc_guard";
static const char *const SanCovTracePCGuardInitName =
@ -139,6 +138,24 @@ static cl::opt<bool> ClUse8bitCounters("sanitizer-coverage-8bit-counters",
cl::desc("Experimental 8-bit counters"),
cl::Hidden, cl::init(false));
static StringRef getSanCovTracePCGuardSection(const Module &M) {
return Triple(M.getTargetTriple()).isOSBinFormatMachO()
? "__DATA,__sancov_guards"
: "__sancov_guards";
}
static StringRef getSanCovTracePCGuardSectionStart(const Module &M) {
return Triple(M.getTargetTriple()).isOSBinFormatMachO()
? "\1section$start$__DATA$__sancov_guards"
: "__start___sancov_guards";
}
static StringRef getSanCovTracePCGuardSectionEnd(const Module &M) {
return Triple(M.getTargetTriple()).isOSBinFormatMachO()
? "\1section$end$__DATA$__sancov_guards"
: "__stop___sancov_guards";
}
namespace {
SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
@ -363,20 +380,20 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
if (Options.TracePCGuard) {
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, Int32PtrTy, false,
GlobalVariable::ExternalLinkage, nullptr,
Prefix[i] + SectionName);
Bounds[i]->setVisibility(GlobalValue::HiddenVisibility);
}
GlobalVariable *SecStart = new GlobalVariable(
M, Int32PtrTy, false, GlobalVariable::ExternalLinkage, nullptr,
getSanCovTracePCGuardSectionStart(*CurModule));
SecStart->setVisibility(GlobalValue::HiddenVisibility);
GlobalVariable *SecEnd = new GlobalVariable(
M, Int32PtrTy, false, GlobalVariable::ExternalLinkage, nullptr,
getSanCovTracePCGuardSectionEnd(*CurModule));
SecEnd->setVisibility(GlobalValue::HiddenVisibility);
std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
M, SanCovModuleCtorName, SanCovTracePCGuardInitName,
{Int32PtrTy, Int32PtrTy},
{IRB.CreatePointerCast(Bounds[0], Int32PtrTy),
IRB.CreatePointerCast(Bounds[1], Int32PtrTy)});
{IRB.CreatePointerCast(SecStart, Int32PtrTy),
IRB.CreatePointerCast(SecEnd, Int32PtrTy)});
appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
}
@ -517,7 +534,7 @@ void SanitizerCoverageModule::CreateFunctionGuardArray(size_t NumGuards,
Constant::getNullValue(ArrayOfInt32Ty), "__sancov_gen_");
if (auto Comdat = F.getComdat())
FunctionGuardArray->setComdat(Comdat);
FunctionGuardArray->setSection(SanCovTracePCGuardSection);
FunctionGuardArray->setSection(getSanCovTracePCGuardSection(*CurModule));
}
bool SanitizerCoverageModule::InjectCoverage(Function &F,

View File

@ -3,6 +3,7 @@
; RUN: opt < %s -sancov -sanitizer-coverage-level=3 -sanitizer-coverage-experimental-tracing -S | FileCheck %s --check-prefix=CHECK3
; RUN: opt < %s -sancov -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc -S | FileCheck %s --check-prefix=CHECK_PC
; RUN: opt < %s -sancov -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard -S | FileCheck %s --check-prefix=CHECK_PC_GUARD
; RUN: opt < %s -sancov -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard -S -mtriple=x86_64-apple-macosx | FileCheck %s --check-prefix=CHECK_PC_GUARD_DARWIN
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"
target triple = "x86_64-unknown-linux-gnu"
@ -47,3 +48,11 @@ entry:
; CHECK_PC_GUARD-NOT: call void @__sanitizer_cov_trace_pc
; CHECK_PC_GUARD: ret void
; CHECK_PC_GUARD: call void @__sanitizer_cov_trace_pc_guard_init(i32* bitcast (i32** @__start___sancov_guards to i32*), i32* bitcast (i32** @__stop___sancov_guards to i32*))
; CHECK_PC_GUARD_DARWIN-LABEL: define void @foo
; CHECK_PC_GUARD_DARWIN: call void @__sanitizer_cov_trace_pc_guard
; CHECK_PC_GUARD_DARWIN: call void @__sanitizer_cov_trace_pc_guard
; CHECK_PC_GUARD_DARWIN: call void @__sanitizer_cov_trace_pc_guard
; CHECK_PC_GUARD_DARWIN-NOT: call void @__sanitizer_cov_trace_pc
; CHECK_PC_GUARD_DARWIN: ret void
; CHECK_PC_GUARD_DARWIN: call void @__sanitizer_cov_trace_pc_guard_init(i32* bitcast (i32** @"\01section$start$__DATA$__sancov_guards" to i32*), i32* bitcast (i32** @"\01section$end$__DATA$__sancov_guards" to i32*))