Skip to content

Commit ba7cb8f

Browse files
committed
C++: fix range analysis back edge detection for irreducible CFGs
1 parent 9c774ac commit ba7cb8f

File tree

2 files changed

+39
-0
lines changed
  • cpp/ql

2 files changed

+39
-0
lines changed

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/SemanticSSA.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ predicate semBackEdge(SemSsaPhiNode phi, SemSsaVariable inp, SemSsaReadPositionP
7070
// Conservatively assume that every edge is a back edge if we don't have dominance information.
7171
(
7272
phi.getBasicBlock().bbDominates(edge.getOrigBlock()) or
73+
trimmedReachable(phi.getBasicBlock(), edge.getOrigBlock()) or
7374
not edge.getOrigBlock().hasDominanceInformation()
7475
)
7576
}
77+
78+
private predicate trimmedReachable(SemBasicBlock b1, SemBasicBlock b2) {
79+
b1 = b2
80+
or
81+
exists(SemBasicBlock mid |
82+
trimmedReachable(b1, mid) and
83+
trimmedEdges(mid, b2)
84+
)
85+
}
86+
87+
private predicate trimmedEdges(SemBasicBlock pred, SemBasicBlock succ) {
88+
pred.getASuccessor() = succ and
89+
not succ.bbDominates(pred)
90+
}

cpp/ql/test/library-tests/ir/range-analysis/test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,27 @@ int f4(int x) {
7070
}
7171
}
7272
}
73+
74+
// No interesting ranges to check here - this irreducible CFG caused an infinite loop due to back edge detection
75+
void gotoLoop(bool b1, bool b2)
76+
{
77+
int j;
78+
79+
if (b1)
80+
return;
81+
82+
if (!b2)
83+
{
84+
for (j = 0; j < 10; ++j)
85+
{
86+
goto main_decode_loop;
87+
}
88+
}
89+
else
90+
{
91+
for (j = 0; j < 10; ++j)
92+
{
93+
main_decode_loop:
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)