Skip to content

Commit a8b19d2

Browse files
authored
Merge pull request #19147 from aschackmull/ssa/writedef-source-refactor
Ssa: Refactor data flow integration to make the input signature simpler
2 parents 1c93e53 + 0d1ac77 commit a8b19d2

File tree

11 files changed

+54
-112
lines changed

11 files changed

+54
-112
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,6 @@ class GlobalDef extends Definition {
956956
private module SsaImpl = SsaImplCommon::Make<Location, SsaInput>;
957957

958958
private module DataFlowIntegrationInput implements SsaImpl::DataFlowIntegrationInputSig {
959-
private import codeql.util.Void
960-
961959
class Expr extends Instruction {
962960
Expr() {
963961
exists(IRBlock bb, int i |
@@ -977,13 +975,7 @@ private module DataFlowIntegrationInput implements SsaImpl::DataFlowIntegrationI
977975
)
978976
}
979977

980-
predicate ssaDefAssigns(SsaImpl::WriteDefinition def, Expr value) { none() }
981-
982-
class Parameter extends Void {
983-
Location getLocation() { none() }
984-
}
985-
986-
predicate ssaDefInitializesParam(SsaImpl::WriteDefinition def, Parameter p) { none() }
978+
predicate ssaDefHasSource(SsaImpl::WriteDefinition def) { none() }
987979

988980
predicate allowFlowIntoUncertainDef(SsaImpl::UncertainWriteDefinition def) { any() }
989981

csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ module SsaFlow {
506506
result.(Impl::ExprPostUpdateNode).getExpr() =
507507
n.(PostUpdateNode).getPreUpdateNode().(ExprNode).getControlFlowNode()
508508
or
509-
result.(Impl::ParameterNode).getParameter() = n.(ExplicitParameterNode).getSsaDefinition()
509+
result.(Impl::WriteDefSourceNode).getDefinition() = n.(ExplicitParameterNode).getSsaDefinition()
510510
}
511511

512512
predicate localFlowStep(Ssa::SourceVariable v, Node nodeFrom, Node nodeTo, boolean isUseStep) {

csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImpl.qll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,16 +1023,12 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
10231023

10241024
Expr getARead(Definition def) { exists(getAReadAtNode(def, result)) }
10251025

1026-
predicate ssaDefAssigns(WriteDefinition def, Expr value) {
1026+
predicate ssaDefHasSource(WriteDefinition def) {
10271027
// exclude flow directly from RHS to SSA definition, as we instead want to
1028-
// go from RHS to matching assingnable definition, and from there to SSA definition
1029-
none()
1028+
// go from RHS to matching assignable definition, and from there to SSA definition
1029+
def instanceof Ssa::ImplicitParameterDefinition
10301030
}
10311031

1032-
class Parameter = Ssa::ImplicitParameterDefinition;
1033-
1034-
predicate ssaDefInitializesParam(WriteDefinition def, Parameter p) { def = p }
1035-
10361032
/**
10371033
* Allows for flow into uncertain defintions that are not call definitions,
10381034
* as we, conservatively, consider such definitions to be certain.

java/ql/lib/semmle/code/java/dataflow/internal/DataFlowNodes.qll

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,27 @@ private predicate deadcode(Expr e) {
2626
module SsaFlow {
2727
module Impl = SsaImpl::DataFlowIntegration;
2828

29+
private predicate ssaDefAssigns(SsaExplicitUpdate def, Expr value) {
30+
exists(VariableUpdate upd | upd = def.getDefiningExpr() |
31+
value = upd.(VariableAssign).getSource() or
32+
value = upd.(AssignOp) or
33+
value = upd.(RecordBindingVariableExpr)
34+
)
35+
}
36+
2937
Impl::Node asNode(Node n) {
3038
n = TSsaNode(result)
3139
or
3240
result.(Impl::ExprNode).getExpr() = n.asExpr()
3341
or
3442
result.(Impl::ExprPostUpdateNode).getExpr() = n.(PostUpdateNode).getPreUpdateNode().asExpr()
3543
or
36-
TExplicitParameterNode(result.(Impl::ParameterNode).getParameter()) = n
44+
exists(Parameter p |
45+
n = TExplicitParameterNode(p) and
46+
result.(Impl::WriteDefSourceNode).getDefinition().(SsaImplicitInit).isParameterDefinition(p)
47+
)
48+
or
49+
ssaDefAssigns(result.(Impl::WriteDefSourceNode).getDefinition(), n.asExpr())
3750
}
3851

3952
predicate localFlowStep(SsaSourceVariable v, Node nodeFrom, Node nodeTo, boolean isUseStep) {

java/ql/lib/semmle/code/java/dataflow/internal/SsaImpl.qll

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -647,22 +647,8 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
647647

648648
Expr getARead(Definition def) { result = getAUse(def) }
649649

650-
class Parameter = J::Parameter;
651-
652-
predicate ssaDefAssigns(Impl::WriteDefinition def, Expr value) {
653-
exists(VariableUpdate upd | upd = def.(SsaExplicitUpdate).getDefiningExpr() |
654-
value = upd.(VariableAssign).getSource() or
655-
value = upd.(AssignOp) or
656-
value = upd.(RecordBindingVariableExpr)
657-
)
658-
}
659-
660-
predicate ssaDefInitializesParam(Impl::WriteDefinition def, Parameter p) {
661-
def.(SsaImplicitInit).getSourceVariable() =
662-
any(SsaSourceVariable v |
663-
v.getVariable() = p and
664-
v.getEnclosingCallable() = p.getCallable()
665-
)
650+
predicate ssaDefHasSource(WriteDefinition def) {
651+
def instanceof SsaExplicitUpdate or def.(SsaImplicitInit).isParameterDefinition(_)
666652
}
667653

668654
predicate allowFlowIntoUncertainDef(UncertainWriteDefinition def) {

javascript/ql/lib/semmle/javascript/dataflow/internal/sharedlib/Ssa.qll

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@ module SsaDataflowInput implements DataFlowIntegrationInputSig {
5656
predicate hasCfgNode(js::BasicBlock bb, int i) { this = bb.getNode(i) }
5757
}
5858

59-
predicate ssaDefAssigns(WriteDefinition def, Expr value) {
60-
// This library only handles use-use flow after a post-update, there are no definitions, only uses.
61-
none()
62-
}
63-
64-
class Parameter = js::Parameter;
65-
66-
predicate ssaDefInitializesParam(WriteDefinition def, Parameter p) {
59+
predicate ssaDefHasSource(WriteDefinition def) {
6760
// This library only handles use-use flow after a post-update, there are no definitions, only uses.
6861
none()
6962
}

ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowPrivate.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ module SsaFlow {
108108
or
109109
result.(Impl::ExprPostUpdateNode).getExpr() = n.(PostUpdateNode).getPreUpdateNode().asExpr()
110110
or
111-
n = toParameterNode(result.(Impl::ParameterNode).getParameter())
111+
exists(SsaImpl::ParameterExt p |
112+
n = toParameterNode(p) and
113+
p.isInitializedBy(result.(Impl::WriteDefSourceNode).getDefinition())
114+
)
115+
or
116+
result.(Impl::WriteDefSourceNode).getDefinition().(Ssa::WriteDefinition).assigns(n.asExpr())
112117
}
113118

114119
predicate localFlowStep(

ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImpl.qll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,20 +473,16 @@ class ParameterExt extends TParameterExt {
473473
private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInputSig {
474474
private import codeql.ruby.controlflow.internal.Guards as Guards
475475

476-
class Parameter = ParameterExt;
477-
478476
class Expr extends Cfg::CfgNodes::ExprCfgNode {
479477
predicate hasCfgNode(SsaInput::BasicBlock bb, int i) { this = bb.getNode(i) }
480478
}
481479

482480
Expr getARead(Definition def) { result = Cached::getARead(def) }
483481

484-
predicate ssaDefAssigns(WriteDefinition def, Expr value) {
485-
def.(Ssa::WriteDefinition).assigns(value)
482+
predicate ssaDefHasSource(WriteDefinition def) {
483+
any(ParameterExt p).isInitializedBy(def) or def.(Ssa::WriteDefinition).assigns(_)
486484
}
487485

488-
predicate ssaDefInitializesParam(WriteDefinition def, Parameter p) { p.isInitializedBy(def) }
489-
490486
class Guard extends Cfg::CfgNodes::AstCfgNode {
491487
/**
492488
* Holds if the control flow branching from `bb1` is dependent on this guard,

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,13 @@ predicate isArgumentForCall(ExprCfgNode arg, CallExprBaseCfgNode call, Parameter
172172
module SsaFlow {
173173
private module SsaFlow = SsaImpl::DataFlowIntegration;
174174

175-
private ParameterNode toParameterNode(ParamCfgNode p) {
176-
result.(SourceParameterNode).getParameter() = p
177-
}
178-
179175
/** Converts a control flow node into an SSA control flow node. */
180176
SsaFlow::Node asNode(Node n) {
181177
n = TSsaNode(result)
182178
or
183179
result.(SsaFlow::ExprNode).getExpr() = n.asExpr()
184180
or
185181
result.(SsaFlow::ExprPostUpdateNode).getExpr() = n.(PostUpdateNode).getPreUpdateNode().asExpr()
186-
or
187-
n = toParameterNode(result.(SsaFlow::ParameterNode).getParameter())
188182
}
189183

190184
predicate localFlowStep(

rust/ql/lib/codeql/rust/dataflow/internal/SsaImpl.qll

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,7 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
340340

341341
Expr getARead(Definition def) { result = Cached::getARead(def) }
342342

343-
/** Holds if SSA definition `def` assigns `value` to the underlying variable. */
344-
predicate ssaDefAssigns(WriteDefinition def, Expr value) {
345-
none() // handled in `DataFlowImpl.qll` instead
346-
}
343+
predicate ssaDefHasSource(WriteDefinition def) { none() } // handled in `DataFlowImpl.qll` instead
347344

348345
private predicate isArg(CfgNodes::CallExprBaseCfgNode call, CfgNodes::ExprCfgNode e) {
349346
call.getArgument(_) = e
@@ -364,13 +361,6 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
364361
)
365362
}
366363

367-
class Parameter = CfgNodes::ParamBaseCfgNode;
368-
369-
/** Holds if SSA definition `def` initializes parameter `p` at function entry. */
370-
predicate ssaDefInitializesParam(WriteDefinition def, Parameter p) {
371-
none() // handled in `DataFlowImpl.qll` instead
372-
}
373-
374364
class Guard extends CfgNodes::AstCfgNode {
375365
/**
376366
* Holds if the control flow branching from `bb1` is dependent on this guard,

0 commit comments

Comments
 (0)