Skip to content

Commit faaead6

Browse files
committed
Ruby: Block for steps into self parameters in trackModuleAccess
1 parent bda9826 commit faaead6

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,14 @@ private DataFlow::LocalSourceNode trackModuleAccess(Module m, TypeTracker t) {
537537
)
538538
}
539539

540+
/**
541+
* We exclude steps into `self` parameters, and instead rely on the type of the
542+
* enclosing module.
543+
*/
540544
pragma[nomagic]
541545
private DataFlow::LocalSourceNode trackModuleAccessRec(Module m, TypeTracker t, StepSummary summary) {
542-
StepSummary::step(trackModuleAccess(m, t), result, summary)
546+
StepSummary::step(trackModuleAccess(m, t), result, summary) and
547+
not result instanceof SelfParameterNode
543548
}
544549

545550
pragma[nomagic]
@@ -603,17 +608,22 @@ private predicate isInstance(DataFlow::Node n, Module tp, boolean exact) {
603608
or
604609
exists(RelevantCall call, DataFlow::LocalSourceNode sourceNode |
605610
flowsToMethodCallReceiver(call, sourceNode, "new") and
606-
exact = true and
607611
n.asExpr() = call
608612
|
609613
// `C.new`
610-
sourceNode = trackModuleAccess(tp)
614+
sourceNode = trackModuleAccess(tp) and
615+
exact = true
611616
or
612617
// `self.new` inside a module
613-
selfInModule(sourceNode.(SsaSelfDefinitionNode).getVariable(), tp)
618+
selfInModule(sourceNode.(SsaSelfDefinitionNode).getVariable(), tp) and
619+
exact = true
614620
or
615621
// `self.new` inside a singleton method
616-
selfInMethod(sourceNode.(SsaSelfDefinitionNode).getVariable(), any(SingletonMethod sm), tp)
622+
exists(MethodBase target |
623+
selfInMethod(sourceNode.(SsaSelfDefinitionNode).getVariable(), target, tp) and
624+
singletonMethod(target, _, _) and
625+
exact = false
626+
)
617627
)
618628
or
619629
// `self` reference in method or top-level (but not in module or singleton method,

ruby/ql/test/library-tests/modules/callgraph.expected

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ getTarget
148148
| calls.rb:384:13:384:22 | call to singleton1 | calls.rb:379:9:381:11 | singleton1 |
149149
| calls.rb:384:13:384:22 | call to singleton1 | calls.rb:414:9:416:11 | singleton1 |
150150
| calls.rb:388:13:388:20 | call to new | calls.rb:117:5:117:16 | new |
151+
| calls.rb:388:13:388:30 | call to instance1 | calls.rb:402:5:404:7 | instance1 |
152+
| calls.rb:388:13:388:30 | call to instance1 | calls.rb:423:5:425:7 | instance1 |
151153
| calls.rb:393:9:393:44 | call to puts | calls.rb:102:5:102:30 | puts |
152154
| calls.rb:397:9:397:18 | call to singleton2 | calls.rb:392:5:394:7 | singleton2 |
153155
| calls.rb:397:9:397:18 | call to singleton2 | calls.rb:419:5:421:7 | singleton2 |
@@ -226,11 +228,7 @@ getTarget
226228
| calls.rb:592:9:592:23 | call to call_singleton1 | calls.rb:587:5:589:7 | call_singleton1 |
227229
| calls.rb:592:9:592:23 | call to call_singleton1 | calls.rb:600:5:602:7 | call_singleton1 |
228230
| calls.rb:592:9:592:23 | call to call_singleton1 | calls.rb:609:5:611:7 | call_singleton1 |
229-
| calls.rb:601:9:601:18 | call to singleton1 | calls.rb:584:5:585:7 | singleton1 |
230231
| calls.rb:601:9:601:18 | call to singleton1 | calls.rb:597:5:598:7 | singleton1 |
231-
| calls.rb:601:9:601:18 | call to singleton1 | calls.rb:606:5:607:7 | singleton1 |
232-
| calls.rb:610:9:610:18 | call to singleton1 | calls.rb:584:5:585:7 | singleton1 |
233-
| calls.rb:610:9:610:18 | call to singleton1 | calls.rb:597:5:598:7 | singleton1 |
234232
| calls.rb:610:9:610:18 | call to singleton1 | calls.rb:606:5:607:7 | singleton1 |
235233
| calls.rb:614:1:614:31 | call to call_call_singleton1 | calls.rb:591:5:593:7 | call_call_singleton1 |
236234
| calls.rb:615:1:615:31 | call to call_call_singleton1 | calls.rb:591:5:593:7 | call_call_singleton1 |
@@ -319,7 +317,6 @@ unresolvedCall
319317
| calls.rb:274:1:274:14 | call to singleton_g |
320318
| calls.rb:276:1:276:14 | call to singleton_g |
321319
| calls.rb:313:9:313:20 | call to instance |
322-
| calls.rb:388:13:388:30 | call to instance1 |
323320
| calls.rb:434:8:434:13 | call to rand |
324321
| calls.rb:434:8:434:17 | ... > ... |
325322
| calls.rb:451:9:451:10 | call to m3 |

0 commit comments

Comments
 (0)