Skip to content

Commit 536f3f3

Browse files
authored
Merge pull request #428 from hvitved/csharp/more-guards
Approved by calumgrant
2 parents 737ec70 + 29f163f commit 536f3f3

File tree

11 files changed

+599
-197
lines changed

11 files changed

+599
-197
lines changed

csharp/ql/src/semmle/code/csharp/controlflow/ControlFlowGraph.qll

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,7 @@ module ControlFlow {
24732473
not startsBB(succ)
24742474
}
24752475

2476-
predicate bbIndex(ControlFlowElement bbStart, ControlFlowElement cfe, int i) =
2476+
private predicate bbIndex(ControlFlowElement bbStart, ControlFlowElement cfe, int i) =
24772477
shortestDistances(startsBB/1, intraBBSucc/2)(bbStart, cfe, i)
24782478

24792479
private predicate succBB(PreBasicBlock pred, PreBasicBlock succ) {
@@ -2488,13 +2488,9 @@ module ControlFlow {
24882488
idominance(entryBB/1, succBB/2)(_, dom, bb)
24892489

24902490
class PreBasicBlock extends ControlFlowElement {
2491-
PreBasicBlock() {
2492-
startsBB(this)
2493-
}
2491+
PreBasicBlock() { startsBB(this) }
24942492

2495-
PreBasicBlock getASuccessor() {
2496-
result = succ(this.getLastElement(), _)
2497-
}
2493+
PreBasicBlock getASuccessor() { result = succ(this.getLastElement(), _) }
24982494

24992495
PreBasicBlock getAPredecessor() {
25002496
result.getASuccessor() = this
@@ -2548,6 +2544,22 @@ module ControlFlow {
25482544
exists(succExit(this.getLastElement(), c))
25492545
) > 1
25502546
}
2547+
2548+
private predicate isCandidateSuccessor(PreBasicBlock succ, ConditionalCompletion c) {
2549+
succ = succ(this.getLastElement(), c) and
2550+
forall(PreBasicBlock pred |
2551+
pred = succ.getAPredecessor() and pred != this |
2552+
succ.dominates(pred)
2553+
)
2554+
}
2555+
2556+
predicate controls(PreBasicBlock controlled, ConditionalSuccessor s) {
2557+
exists(PreBasicBlock succ, ConditionalCompletion c |
2558+
isCandidateSuccessor(succ, c) |
2559+
succ.dominates(controlled) and
2560+
s.matchesCompletion(c)
2561+
)
2562+
}
25512563
}
25522564
}
25532565

@@ -2729,29 +2741,21 @@ module ControlFlow {
27292741
ssaRefRank(bb2, i2, v, _) = 1
27302742
}
27312743

2732-
private cached module PreSsaCached {
2733-
cached
2734-
predicate firstReadSameVar(Definition def, LocalScopeVariableRead read) {
2735-
exists(SimpleLocalScopeVariable v, PreBasicBlock b1, int i1, PreBasicBlock b2, int i2 |
2736-
adjacentVarRefs(v, b1, i1, b2, i2) and
2737-
defAt(b1, i1, def, v) and
2738-
readAt(b2, i2, read, v)
2739-
)
2740-
}
2741-
2742-
cached
2743-
predicate adjacentReadPairSameVar(LocalScopeVariableRead read1, LocalScopeVariableRead read2) {
2744-
exists(SimpleLocalScopeVariable v, PreBasicBlock bb1, int i1, PreBasicBlock bb2, int i2 |
2745-
adjacentVarRefs(v, bb1, i1, bb2, i2) and
2746-
readAt(bb1, i1, read1, v) and
2747-
readAt(bb2, i2, read2, v)
2748-
)
2749-
}
2744+
predicate firstReadSameVar(Definition def, LocalScopeVariableRead read) {
2745+
exists(SimpleLocalScopeVariable v, PreBasicBlock b1, int i1, PreBasicBlock b2, int i2 |
2746+
adjacentVarRefs(v, b1, i1, b2, i2) and
2747+
defAt(b1, i1, def, v) and
2748+
readAt(b2, i2, read, v)
2749+
)
2750+
}
27502751

2751-
cached
2752-
predicate forceCachingInSameStage() { any() }
2752+
predicate adjacentReadPairSameVar(LocalScopeVariableRead read1, LocalScopeVariableRead read2) {
2753+
exists(SimpleLocalScopeVariable v, PreBasicBlock bb1, int i1, PreBasicBlock bb2, int i2 |
2754+
adjacentVarRefs(v, bb1, i1, bb2, i2) and
2755+
readAt(bb1, i1, read1, v) and
2756+
readAt(bb2, i2, read2, v)
2757+
)
27532758
}
2754-
import PreSsaCached
27552759
}
27562760

27572761
/**
@@ -3837,10 +3841,12 @@ module ControlFlow {
38373841
}
38383842

38393843
private cached module Cached {
3844+
private import semmle.code.csharp.controlflow.Guards as Guards
3845+
38403846
cached
38413847
newtype TPreSsaDef =
38423848
TExplicitPreSsaDef(PreBasicBlocks::PreBasicBlock bb, int i, AssignableDefinition def, LocalScopeVariable v) {
3843-
PreSsa::forceCachingInSameStage() and
3849+
Guards::Internal::CachedWithCFG::forceCachingInSameStage() and
38443850
PreSsa::assignableDefAt(bb, i, def, v)
38453851
}
38463852
or

0 commit comments

Comments
 (0)