Skip to content

Commit bc543bc

Browse files
committed
Ruby: Adopt shared SSA data-flow integration
1 parent f183382 commit bc543bc

File tree

7 files changed

+283
-334
lines changed

7 files changed

+283
-334
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
private import codeql.ruby.CFG
2+
3+
/** Holds if the guard `guard` controls block `bb` upon evaluating to `branch`. */
4+
pragma[nomagic]
5+
predicate guardControlsBlock(CfgNodes::AstCfgNode guard, BasicBlock bb, boolean branch) {
6+
exists(ConditionBlock conditionBlock, SuccessorTypes::ConditionalSuccessor s |
7+
guard = conditionBlock.getLastNode() and
8+
s.getValue() = branch and
9+
conditionBlock.controls(bb, s)
10+
)
11+
}

ruby/ql/lib/codeql/ruby/dataflow/SSA.qll

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,32 @@ module Ssa {
202202
final VariableWriteAccessCfgNode getWriteAccess() { result = write }
203203

204204
/**
205-
* Holds if this SSA definition represents a direct assignment of `value`
206-
* to the underlying variable.
205+
* Holds if this SSA definition assigns `value` to the underlying variable.
206+
*
207+
* This is either a direct assignment, `x = value`, or an assignment via
208+
* simple pattern matching
209+
*
210+
* ```rb
211+
* case value
212+
* in Foo => x then ...
213+
* in y => then ...
214+
* end
215+
* ```
207216
*/
208217
predicate assigns(CfgNodes::ExprCfgNode value) {
209218
exists(CfgNodes::ExprNodes::AssignExprCfgNode a, BasicBlock bb, int i |
210219
this.definesAt(_, bb, i) and
211220
a = bb.getNode(i) and
212221
value = a.getRhs()
213222
)
223+
or
224+
exists(CfgNodes::ExprNodes::CaseExprCfgNode case, CfgNodes::AstCfgNode pattern |
225+
case.getValue() = value and
226+
pattern = case.getBranch(_).(CfgNodes::ExprNodes::InClauseCfgNode).getPattern()
227+
|
228+
this.getWriteAccess() =
229+
[pattern, pattern.(CfgNodes::ExprNodes::AsPatternCfgNode).getVariableAccess()]
230+
)
214231
}
215232

216233
final override string toString() { result = write.toString() }

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,8 +1093,7 @@ private module TrackSingletonMethodOnInstanceInput implements CallGraphConstruct
10931093
singletonMethodOnInstance(_, _, nodeFromPreExpr.getExpr())
10941094
)
10951095
|
1096-
nodeFromPreExpr =
1097-
LocalFlow::getParameterDefNode(p.getParameter()).getDefinitionExt().getARead()
1096+
nodeFromPreExpr = getParameterDef(p.getParameter()).getARead()
10981097
or
10991098
nodeFromPreExpr = p.(SelfParameterNodeImpl).getSelfDefinition().getARead()
11001099
)

0 commit comments

Comments
 (0)