Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 6008ca2

Browse files
committed
[SanitizerCoverage] Optimize stack-depth instrumentation.
Summary: Use the initialexec TLS type and eliminate calls to the TLS wrapper. Fixes the sanitizer-x86_64-linux-fuzzer bot failure. Reviewers: vitalybuka, kcc Reviewed By: kcc Subscribers: hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D37026 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@311490 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5288e91 commit 6008ca2

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ static const char *const SanCovCountersSectionName = "sancov_cntrs";
7777
static const char *const SanCovPCsSectionName = "sancov_pcs";
7878

7979
static const char *const SanCovLowestStackName = "__sancov_lowest_stack";
80-
static const char *const SanCovLowestStackTLSWrapperName =
81-
"_ZTW21__sancov_lowest_stack";
8280

8381
static cl::opt<int> ClCoverageLevel(
8482
"sanitizer-coverage-level",
@@ -229,7 +227,6 @@ class SanitizerCoverageModule : public ModulePass {
229227
Function *SanCovTraceDivFunction[2];
230228
Function *SanCovTraceGepFunction;
231229
Function *SanCovTraceSwitchFunction;
232-
Function *SanCovLowestStackTLSWrapper;
233230
GlobalVariable *SanCovLowestStack;
234231
InlineAsm *EmptyAsm;
235232
Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy,
@@ -351,20 +348,11 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
351348

352349
Constant *SanCovLowestStackConstant =
353350
M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
354-
SanCovLowestStackTLSWrapper =
355-
checkSanitizerInterfaceFunction(M.getOrInsertFunction(
356-
SanCovLowestStackTLSWrapperName, IntptrTy->getPointerTo()));
357-
if (Options.StackDepth) {
358-
assert(isa<GlobalVariable>(SanCovLowestStackConstant));
359-
SanCovLowestStack = cast<GlobalVariable>(SanCovLowestStackConstant);
360-
if (!SanCovLowestStack->isDeclaration()) {
361-
// Check that the user has correctly defined:
362-
// thread_local uintptr_t __sancov_lowest_stack
363-
// and initialize it.
364-
assert(SanCovLowestStack->isThreadLocal());
365-
SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
366-
}
367-
}
351+
SanCovLowestStack = cast<GlobalVariable>(SanCovLowestStackConstant);
352+
SanCovLowestStack->setThreadLocalMode(
353+
GlobalValue::ThreadLocalMode::InitialExecTLSModel);
354+
if (Options.StackDepth && !SanCovLowestStack->isDeclaration())
355+
SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
368356

369357
// Make sure smaller parameters are zero-extended to i64 as required by the
370358
// x86_64 ABI.
@@ -484,9 +472,6 @@ bool SanitizerCoverageModule::runOnFunction(Function &F) {
484472
if (F.getName() == "__local_stdio_printf_options" ||
485473
F.getName() == "__local_stdio_scanf_options")
486474
return false;
487-
// Avoid infinite recursion by not instrumenting stack depth TLS wrapper
488-
if (F.getName() == SanCovLowestStackTLSWrapperName)
489-
return false;
490475
// Don't instrument functions using SEH for now. Splitting basic blocks like
491476
// we do for coverage breaks WinEHPrepare.
492477
// FIXME: Remove this when SEH no longer uses landingpad pattern matching.
@@ -771,12 +756,11 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
771756
auto FrameAddrPtr =
772757
IRB.CreateCall(GetFrameAddr, {Constant::getNullValue(Int32Ty)});
773758
auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy);
774-
auto LowestStackPtr = IRB.CreateCall(SanCovLowestStackTLSWrapper);
775-
auto LowestStack = IRB.CreateLoad(LowestStackPtr);
759+
auto LowestStack = IRB.CreateLoad(SanCovLowestStack);
776760
auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack);
777761
auto ThenTerm = SplitBlockAndInsertIfThen(IsStackLower, &*IP, false);
778762
IRBuilder<> ThenIRB(ThenTerm);
779-
ThenIRB.CreateStore(FrameAddrInt, LowestStackPtr);
763+
ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
780764
}
781765
}
782766

test/Instrumentation/SanitizerCoverage/stack-depth.ll

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@
88
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
99
target triple = "x86_64-unknown-linux-gnu"
1010

11-
; CHECK: @__sancov_lowest_stack = thread_local global i64 -1
11+
; CHECK: @__sancov_lowest_stack = thread_local(initialexec) global i64 -1
1212
@__sancov_lowest_stack = thread_local global i64 0, align 8
1313

1414
define i32 @foo() {
1515
entry:
1616
; CHECK-LABEL: define i32 @foo
1717
; CHECK: [[framePtr:%[^ \t]+]] = call i8* @llvm.frameaddress(i32 0)
1818
; CHECK: [[frameInt:%[^ \t]+]] = ptrtoint i8* [[framePtr]] to [[$intType:i[0-9]+]]
19-
; CHECK: [[lowestPtr:%[^ \t]+]] = call [[$intType]]* @_ZTW21__sancov_lowest_stack
20-
; CHECK: [[lowestInt:%[^ \t]+]] = load [[$intType]], [[$intType]]* [[lowestPtr]]
21-
; CHECK: [[cmp:%[^ \t]+]] = icmp ult [[$intType]] [[frameInt]], [[lowestInt]]
19+
; CHECK: [[lowest:%[^ \t]+]] = load [[$intType]], [[$intType]]* @__sancov_lowest_stack
20+
; CHECK: [[cmp:%[^ \t]+]] = icmp ult [[$intType]] [[frameInt]], [[lowest]]
2221
; CHECK: br i1 [[cmp]], label %[[ifLabel:[^ \t]+]], label
2322
; CHECK: <label>:[[ifLabel]]:
24-
; CHECK: store [[$intType]] [[frameInt]], [[$intType]]* [[lowestPtr]]
23+
; CHECK: store [[$intType]] [[frameInt]], [[$intType]]* @__sancov_lowest_stack
2524
; CHECK: ret i32 7
2625

2726
ret i32 7
@@ -32,12 +31,11 @@ entry:
3231
; CHECK-LABEL: define i32 @bar
3332
; CHECK: [[framePtr:%[^ \t]+]] = call i8* @llvm.frameaddress(i32 0)
3433
; CHECK: [[frameInt:%[^ \t]+]] = ptrtoint i8* [[framePtr]] to [[$intType]]
35-
; CHECK: [[lowestPtr:%[^ \t]+]] = call [[$intType]]* @_ZTW21__sancov_lowest_stack
36-
; CHECK: [[lowestInt:%[^ \t]+]] = load [[$intType]], [[$intType]]* [[lowestPtr]]
37-
; CHECK: [[cmp:%[^ \t]+]] = icmp ult [[$intType]] [[frameInt]], [[lowestInt]]
34+
; CHECK: [[lowest:%[^ \t]+]] = load [[$intType]], [[$intType]]* @__sancov_lowest_stack
35+
; CHECK: [[cmp:%[^ \t]+]] = icmp ult [[$intType]] [[frameInt]], [[lowest]]
3836
; CHECK: br i1 [[cmp]], label %[[ifLabel:[^ \t]+]], label
3937
; CHECK: <label>:[[ifLabel]]:
40-
; CHECK: store [[$intType]] [[frameInt]], [[$intType]]* [[lowestPtr]]
38+
; CHECK: store [[$intType]] [[frameInt]], [[$intType]]* @__sancov_lowest_stack
4139
; CHECK: %call = call i32 @foo()
4240
; CHECK: ret i32 %call
4341

0 commit comments

Comments
 (0)