Skip to content

Commit 6fb7e43

Browse files
committed
Python: test direct guard specialization semantics
Prove that the query-shaped shared-SSA relation for direct truthiness guards is equivalent to the generic BarrierGuard abstraction on the modification-of-default-value regression corpus. This guards the performance specialization against semantic drift before changing production code. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 07c775e7-cd7c-4e1c-8d97-5194ffd43e1a
1 parent 38c4d43 commit 6fb7e43

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎python/ql/test/query-tests/Functions/ModificationOfParameterWithDefault/BarrierGuardSpecialization.expected‎

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import python
2+
import semmle.python.dataflow.new.DataFlow
3+
private import semmle.python.controlflow.internal.Cfg as Cfg
4+
private import semmle.python.dataflow.new.internal.SsaImpl as SsaImpl
5+
6+
private predicate truthyGuard(DataFlow::GuardNode guard, Cfg::ControlFlowNode node, boolean branch) {
7+
node = guard and branch = true
8+
}
9+
10+
private predicate falseyGuard(DataFlow::GuardNode guard, Cfg::ControlFlowNode node, boolean branch) {
11+
node = guard and branch = false
12+
}
13+
14+
private predicate genericRelation(DataFlow::ExprNode node, boolean branch) {
15+
branch = true and node = DataFlow::BarrierGuard<truthyGuard/3>::getABarrierNode()
16+
or
17+
branch = false and node = DataFlow::BarrierGuard<falseyGuard/3>::getABarrierNode()
18+
}
19+
20+
private predicate directRelation(DataFlow::ExprNode node, boolean branch) {
21+
exists(
22+
DataFlow::GuardNode guard, SsaImpl::EssaDefinition def, Cfg::NameNode checked, Cfg::NameNode use
23+
|
24+
checked = guard and
25+
SsaImpl::AdjacentUses::useOfDef(def, checked) and
26+
SsaImpl::AdjacentUses::useOfDef(def, use) and
27+
checked != use and
28+
guard.controlsBlock(use.getBasicBlock(), branch) and
29+
node.asCfgNode() = use
30+
)
31+
}
32+
33+
from DataFlow::ExprNode node, boolean branch
34+
where
35+
directRelation(node, branch) and not genericRelation(node, branch)
36+
or
37+
genericRelation(node, branch) and not directRelation(node, branch)
38+
select node, branch

0 commit comments

Comments
 (0)