Skip to content

Commit a78a44d

Browse files
committed
[sanitizer-coverage] prune trace-cmp instrumentation for CMP isntructions that feed into the backedge branch. Instrumenting these CMP instructions is almost always useless (and harmful) for fuzzing
llvm-svn: 352818
1 parent 50d6579 commit a78a44d

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

clang/docs/SanitizerCoverage.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ and with ``-fsanitize-coverage=trace-gep`` --
248248
the `LLVM GEP instructions <https://llvm.org/docs/GetElementPtr.html>`_
249249
(to capture array indices).
250250

251+
Unless ``no-prune`` option is provided, some of the comparison instructions
252+
will not be instrumented.
253+
251254
.. code-block:: c++
252255

253256
// Called before a comparison instruction.

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,37 @@ static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB,
483483
&& !(isFullPostDominator(BB, PDT) && !BB->getSinglePredecessor());
484484
}
485485

486+
487+
// Returns true iff From->To is a backedge.
488+
// A twist here is that we treat From->To as a backedge if
489+
// * To dominates From or
490+
// * To->UniqueSuccessor dominates From
491+
static bool IsBackEdge(BasicBlock *From, BasicBlock *To,
492+
const DominatorTree *DT) {
493+
if (DT->dominates(To, From))
494+
return true;
495+
if (auto Next = To->getUniqueSuccessor())
496+
if (DT->dominates(Next, From))
497+
return true;
498+
return false;
499+
}
500+
501+
// Prunes uninteresting Cmp instrumentation:
502+
// * CMP instructions that feed into loop backedge branch.
503+
//
504+
// Note that Cmp pruning is controlled by the same flag as the
505+
// BB pruning.
506+
static bool IsInterestingCmp(ICmpInst *CMP, const DominatorTree *DT,
507+
const SanitizerCoverageOptions &Options) {
508+
if (!Options.NoPrune)
509+
if (CMP->hasOneUse())
510+
if (auto BR = dyn_cast<BranchInst>(CMP->user_back()))
511+
for (BasicBlock *B : BR->successors())
512+
if (IsBackEdge(BR->getParent(), B, DT))
513+
return false;
514+
return true;
515+
}
516+
486517
bool SanitizerCoverageModule::runOnFunction(Function &F) {
487518
if (F.empty())
488519
return false;
@@ -531,8 +562,9 @@ bool SanitizerCoverageModule::runOnFunction(Function &F) {
531562
IndirCalls.push_back(&Inst);
532563
}
533564
if (Options.TraceCmp) {
534-
if (isa<ICmpInst>(&Inst))
535-
CmpTraceTargets.push_back(&Inst);
565+
if (ICmpInst *CMP = dyn_cast<ICmpInst>(&Inst))
566+
if (IsInterestingCmp(CMP, DT, Options))
567+
CmpTraceTargets.push_back(&Inst);
536568
if (isa<SwitchInst>(&Inst))
537569
SwitchTraceTargets.push_back(&Inst);
538570
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; Test -sanitizer-coverage-trace-compares=1 and how it prunes backedge compares.
2+
; RUN: opt < %s -sancov -sanitizer-coverage-level=1 -sanitizer-coverage-trace-compares=1 -sanitizer-coverage-prune-blocks=1 -S | FileCheck %s --check-prefix=PRUNE
3+
; RUN: opt < %s -sancov -sanitizer-coverage-level=1 -sanitizer-coverage-trace-compares=1 -sanitizer-coverage-prune-blocks=0 -S | FileCheck %s --check-prefix=NOPRUNE
4+
5+
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"
6+
target triple = "x86_64-unknown-linux-gnu"
7+
8+
define dso_local void @foo(i32* nocapture readnone %a, i32 %n) local_unnamed_addr {
9+
entry:
10+
br label %do.body
11+
12+
do.body:
13+
%i.0 = phi i32 [ 0, %entry ], [ %inc, %do.body ]
14+
tail call void (...) @bar()
15+
%inc = add nuw nsw i32 %i.0, 1
16+
%cmp = icmp slt i32 %inc, %n
17+
;PRUNE-LABEL: foo
18+
;PRUNE-NOT: __sanitizer_cov_trace_cmp4
19+
;PRUNE: ret void
20+
21+
;NOPRUNE-LABEL: foo
22+
;NOPRUNE: call void @__sanitizer_cov_trace_cmp4
23+
;NOPRUNE-NEXT: icmp
24+
;NOPRUNE: ret void
25+
26+
br i1 %cmp, label %do.body, label %do.end
27+
28+
do.end:
29+
ret void
30+
}
31+
32+
declare dso_local void @bar(...) local_unnamed_addr

0 commit comments

Comments
 (0)