Skip to content

Commit db5a565

Browse files
author
Marcos Pividori
committedFeb 3, 2017
[sanitizer coverage] Fix Instrumentation to work on Windows.
On Windows, the symbols "___stop___sancov_guards" and "___start___sancov_guards" are not defined automatically. So, we need to take a different approach. We define 3 sections: Section ".SCOV$A" will only hold a variable ___start___sancov_guard. Section ".SCOV$M" will hold the main data. Section ".SCOV$Z" will only hold a variable ___stop___sancov_guards. When linking, they will be merged sorted by the characters after the $, so we can use the pointers of the variables ___[start|stop]___sancov_guard to know the actual range of addresses of that section. In this diff, I updated instrumentation to include all the guard arrays in section ".SCOV$M". Differential Revision: https://reviews.llvm.org/D28434 llvm-svn: 293987
1 parent 37c22a3 commit db5a565

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed
 

‎llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

+29-21
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,6 @@ static cl::opt<bool> ClUse8bitCounters("sanitizer-coverage-8bit-counters",
138138
cl::desc("Experimental 8-bit counters"),
139139
cl::Hidden, cl::init(false));
140140

141-
static StringRef getSanCovTracePCGuardSection(const Module &M) {
142-
return Triple(M.getTargetTriple()).isOSBinFormatMachO()
143-
? "__DATA,__sancov_guards"
144-
: "__sancov_guards";
145-
}
146-
147-
static StringRef getSanCovTracePCGuardSectionStart(const Module &M) {
148-
return Triple(M.getTargetTriple()).isOSBinFormatMachO()
149-
? "\1section$start$__DATA$__sancov_guards"
150-
: "__start___sancov_guards";
151-
}
152-
153-
static StringRef getSanCovTracePCGuardSectionEnd(const Module &M) {
154-
return Triple(M.getTargetTriple()).isOSBinFormatMachO()
155-
? "\1section$end$__DATA$__sancov_guards"
156-
: "__stop___sancov_guards";
157-
}
158-
159141
namespace {
160142

161143
SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
@@ -233,6 +215,9 @@ class SanitizerCoverageModule : public ModulePass {
233215
SanCovWithCheckFunction->getNumUses() + SanCovTraceBB->getNumUses() +
234216
SanCovTraceEnter->getNumUses();
235217
}
218+
StringRef getSanCovTracePCGuardSection() const;
219+
StringRef getSanCovTracePCGuardSectionStart() const;
220+
StringRef getSanCovTracePCGuardSectionEnd() const;
236221
Function *SanCovFunction;
237222
Function *SanCovWithCheckFunction;
238223
Function *SanCovIndirCallFunction, *SanCovTracePCIndir;
@@ -244,6 +229,7 @@ class SanitizerCoverageModule : public ModulePass {
244229
InlineAsm *EmptyAsm;
245230
Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy;
246231
Module *CurModule;
232+
Triple TargetTriple;
247233
LLVMContext *C;
248234
const DataLayout *DL;
249235

@@ -263,6 +249,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
263249
C = &(M.getContext());
264250
DL = &M.getDataLayout();
265251
CurModule = &M;
252+
TargetTriple = Triple(M.getTargetTriple());
266253
HasSancovGuardsSection = false;
267254
IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
268255
IntptrPtrTy = PointerType::getUnqual(IntptrTy);
@@ -382,11 +369,11 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
382369
Function *CtorFunc;
383370
GlobalVariable *SecStart = new GlobalVariable(
384371
M, Int32PtrTy, false, GlobalVariable::ExternalLinkage, nullptr,
385-
getSanCovTracePCGuardSectionStart(*CurModule));
372+
getSanCovTracePCGuardSectionStart());
386373
SecStart->setVisibility(GlobalValue::HiddenVisibility);
387374
GlobalVariable *SecEnd = new GlobalVariable(
388375
M, Int32PtrTy, false, GlobalVariable::ExternalLinkage, nullptr,
389-
getSanCovTracePCGuardSectionEnd(*CurModule));
376+
getSanCovTracePCGuardSectionEnd());
390377
SecEnd->setVisibility(GlobalValue::HiddenVisibility);
391378

392379
std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
@@ -534,7 +521,7 @@ void SanitizerCoverageModule::CreateFunctionGuardArray(size_t NumGuards,
534521
Constant::getNullValue(ArrayOfInt32Ty), "__sancov_gen_");
535522
if (auto Comdat = F.getComdat())
536523
FunctionGuardArray->setComdat(Comdat);
537-
FunctionGuardArray->setSection(getSanCovTracePCGuardSection(*CurModule));
524+
FunctionGuardArray->setSection(getSanCovTracePCGuardSection());
538525
}
539526

540527
bool SanitizerCoverageModule::InjectCoverage(Function &F,
@@ -772,6 +759,27 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
772759
}
773760
}
774761

762+
StringRef SanitizerCoverageModule::getSanCovTracePCGuardSection() const {
763+
if (TargetTriple.getObjectFormat() == Triple::COFF)
764+
return ".SCOV$M";
765+
if (TargetTriple.isOSBinFormatMachO())
766+
return "__DATA,__sancov_guards";
767+
return "__sancov_guards";
768+
}
769+
770+
StringRef SanitizerCoverageModule::getSanCovTracePCGuardSectionStart() const {
771+
if (TargetTriple.isOSBinFormatMachO())
772+
return "\1section$start$__DATA$__sancov_guards";
773+
return "__start___sancov_guards";
774+
}
775+
776+
StringRef SanitizerCoverageModule::getSanCovTracePCGuardSectionEnd() const {
777+
if (TargetTriple.isOSBinFormatMachO())
778+
return "\1section$end$__DATA$__sancov_guards";
779+
return "__stop___sancov_guards";
780+
}
781+
782+
775783
char SanitizerCoverageModule::ID = 0;
776784
INITIALIZE_PASS_BEGIN(SanitizerCoverageModule, "sancov",
777785
"SanitizerCoverage: TODO."

0 commit comments

Comments
 (0)
Please sign in to comment.