Skip to content

Commit a1f6ffd

Browse files
committed
Ruby: Fix join orders following DB stats removal
1 parent 750f1ae commit a1f6ffd

File tree

4 files changed

+61
-43
lines changed

4 files changed

+61
-43
lines changed

ruby/ql/lib/codeql/ruby/ast/internal/Module.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ private TMethodOrExpr lookupMethodOrConst(Module m, string name) {
639639
// For now, we restrict the scope of top-level declarations to their file.
640640
// This may remove some plausible targets, but also removes a lot of
641641
// implausible targets
642-
if getNode(result).getEnclosingModule() instanceof Toplevel
643-
then getNode(result).getFile() = m.getADeclaration().getFile()
644-
else any()
642+
forall(File file | file = getNode(result).getEnclosingModule().(Toplevel).getFile() |
643+
file = m.getADeclaration().getFile()
644+
)
645645
}

ruby/ql/lib/codeql/ruby/ast/internal/Scope.qll

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ cached
149149
private module Cached {
150150
/** Gets the enclosing scope of a node */
151151
cached
152-
Scope::Range scopeOf(Ruby::AstNode n) {
152+
Scope::Range scopeOfImpl(Ruby::AstNode n) {
153153
exists(Ruby::AstNode p | p = parentOf(n) |
154154
p = result
155155
or
@@ -163,7 +163,7 @@ private module Cached {
163163
* and synthesized scopes into account.
164164
*/
165165
cached
166-
Scope scopeOfInclSynth(AstNode n) {
166+
Scope scopeOfInclSynthImpl(AstNode n) {
167167
exists(AstNode p | p = parentOfInclSynth(n) |
168168
p = result
169169
or
@@ -172,7 +172,15 @@ private module Cached {
172172
}
173173
}
174174

175-
import Cached
175+
pragma[inline]
176+
Scope::Range scopeOf(Ruby::AstNode n) {
177+
pragma[only_bind_into](result) = Cached::scopeOfImpl(pragma[only_bind_out](n))
178+
}
179+
180+
pragma[inline]
181+
Scope scopeOfInclSynth(AstNode n) {
182+
pragma[only_bind_into](result) = Cached::scopeOfInclSynthImpl(pragma[only_bind_out](n))
183+
}
176184

177185
abstract class ScopeImpl extends AstNode, TScopeType {
178186
final Scope getOuterScopeImpl() { result = scopeOfInclSynth(this) }

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -687,26 +687,30 @@ pragma[nomagic]
687687
private CfgScope getTargetInstance(DataFlowCall call, string method) {
688688
exists(boolean exact |
689689
result = lookupInstanceMethodCall(call, method, exact) and
690-
(
691-
if result.(Method).isPrivate()
692-
then
693-
call.asCall().getReceiver().getExpr() instanceof SelfVariableAccess and
694-
// For now, we restrict the scope of top-level declarations to their file.
695-
// This may remove some plausible targets, but also removes a lot of
696-
// implausible targets
697-
(
698-
isToplevelMethodInFile(result, call.asCall().getFile()) or
699-
not isToplevelMethodInFile(result, _)
700-
)
701-
else any()
702-
) and
703-
if result.(Method).isProtected()
704-
then
705-
result = lookupMethod(call.asCall().getExpr().getEnclosingModule().getModule(), method, exact)
706-
else any()
690+
(if result.(Method).isPrivate() then result = privateFilter(call) else any()) and
691+
if result.(Method).isProtected() then result = protectedFilter(call, method, exact) else any()
707692
)
708693
}
709694

695+
bindingset[call, result]
696+
pragma[inline_late]
697+
private CfgScope privateFilter(DataFlowCall call) {
698+
call.asCall().getReceiver().getExpr() instanceof SelfVariableAccess and
699+
// For now, we restrict the scope of top-level declarations to their file.
700+
// This may remove some plausible targets, but also removes a lot of
701+
// implausible targets
702+
(
703+
isToplevelMethodInFile(result, call.asCall().getFile()) or
704+
not isToplevelMethodInFile(result, _)
705+
)
706+
}
707+
708+
bindingset[call, method, exact, result]
709+
pragma[inline_late]
710+
private CfgScope protectedFilter(DataFlowCall call, string method, boolean exact) {
711+
result = lookupMethod(call.asCall().getExpr().getEnclosingModule().getModule(), method, exact)
712+
}
713+
710714
private module TrackBlockInput implements CallGraphConstruction::Simple::InputSig {
711715
class State = Block;
712716

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ abstract class NodeImpl extends Node {
2828
DataFlowCallable getEnclosingCallable() { result = TCfgScope(this.getCfgScope()) }
2929

3030
/** Do not call: use `getEnclosingCallable()` instead. */
31-
abstract CfgScope getCfgScope();
31+
abstract CfgScope getCfgScopeImpl();
32+
33+
/** Do not call: use `getEnclosingCallable()` instead. */
34+
pragma[inline]
35+
final CfgScope getCfgScope() {
36+
pragma[only_bind_into](result) = pragma[only_bind_out](this).getCfgScopeImpl()
37+
}
3238

3339
/** Do not call: use `getLocation()` instead. */
3440
abstract Location getLocationImpl();
@@ -38,7 +44,7 @@ abstract class NodeImpl extends Node {
3844
}
3945

4046
private class ExprNodeImpl extends ExprNode, NodeImpl {
41-
override CfgScope getCfgScope() { result = this.getExprNode().getExpr().getCfgScope() }
47+
override CfgScope getCfgScopeImpl() { result = this.getExprNode().getExpr().getCfgScope() }
4248

4349
override Location getLocationImpl() { result = this.getExprNode().getLocation() }
4450

@@ -780,7 +786,7 @@ class SsaNode extends NodeImpl, TSsaNode {
780786
/** Holds if this node should be hidden from path explanations. */
781787
predicate isHidden() { any() }
782788

783-
override CfgScope getCfgScope() { result = node.getBasicBlock().getScope() }
789+
override CfgScope getCfgScopeImpl() { result = node.getBasicBlock().getScope() }
784790

785791
override Location getLocationImpl() { result = node.getLocation() }
786792

@@ -827,7 +833,7 @@ class CapturedVariableNode extends NodeImpl, TCapturedVariableNode {
827833
/** Gets the captured variable represented by this node. */
828834
VariableCapture::CapturedVariable getVariable() { result = variable }
829835

830-
override CfgScope getCfgScope() { result = variable.getCallable() }
836+
override CfgScope getCfgScopeImpl() { result = variable.getCallable() }
831837

832838
override Location getLocationImpl() { result = variable.getLocation() }
833839

@@ -849,7 +855,7 @@ class ReturningStatementNode extends NodeImpl, TReturningNode {
849855
/** Gets the expression corresponding to this node. */
850856
CfgNodes::ReturningCfgNode getReturningNode() { result = n }
851857

852-
override CfgScope getCfgScope() { result = n.getScope() }
858+
override CfgScope getCfgScopeImpl() { result = n.getScope() }
853859

854860
override Location getLocationImpl() { result = n.getLocation() }
855861

@@ -867,7 +873,7 @@ class CaptureNode extends NodeImpl, TCaptureNode {
867873

868874
VariableCapture::Flow::SynthesizedCaptureNode getSynthesizedCaptureNode() { result = cn }
869875

870-
override CfgScope getCfgScope() { result = cn.getEnclosingCallable() }
876+
override CfgScope getCfgScopeImpl() { result = cn.getEnclosingCallable() }
871877

872878
override Location getLocationImpl() { result = cn.getLocation() }
873879

@@ -935,7 +941,7 @@ private module ParameterNodes {
935941
)
936942
}
937943

938-
override CfgScope getCfgScope() { result = parameter.getCallable() }
944+
override CfgScope getCfgScopeImpl() { result = parameter.getCallable() }
939945

940946
override Location getLocationImpl() { result = parameter.getLocation() }
941947

@@ -979,7 +985,7 @@ private module ParameterNodes {
979985

980986
final override SelfVariable getSelfVariable() { result.getDeclaringScope() = method }
981987

982-
override CfgScope getCfgScope() { result = method }
988+
override CfgScope getCfgScopeImpl() { result = method }
983989

984990
override Location getLocationImpl() { result = method.getLocation() }
985991
}
@@ -1001,7 +1007,7 @@ private module ParameterNodes {
10011007

10021008
final override SelfVariable getSelfVariable() { result.getDeclaringScope() = t }
10031009

1004-
override CfgScope getCfgScope() { result = t }
1010+
override CfgScope getCfgScopeImpl() { result = t }
10051011

10061012
override Location getLocationImpl() { result = t.getLocation() }
10071013
}
@@ -1028,7 +1034,7 @@ private module ParameterNodes {
10281034
callable = c.asCfgScope() and pos.isLambdaSelf()
10291035
}
10301036

1031-
override CfgScope getCfgScope() { result = callable }
1037+
override CfgScope getCfgScopeImpl() { result = callable }
10321038

10331039
override Location getLocationImpl() { result = callable.getLocation() }
10341040

@@ -1071,7 +1077,7 @@ private module ParameterNodes {
10711077
c.asCfgScope() = method and pos.isBlock()
10721078
}
10731079

1074-
override CfgScope getCfgScope() { result = method }
1080+
override CfgScope getCfgScopeImpl() { result = method }
10751081

10761082
override Location getLocationImpl() {
10771083
result = this.getParameter().getLocation()
@@ -1130,7 +1136,7 @@ private module ParameterNodes {
11301136
c = callable and pos.isSynthHashSplat()
11311137
}
11321138

1133-
final override CfgScope getCfgScope() { result = callable.asCfgScope() }
1139+
final override CfgScope getCfgScopeImpl() { result = callable.asCfgScope() }
11341140

11351141
final override DataFlowCallable getEnclosingCallable() { result = callable }
11361142

@@ -1193,7 +1199,7 @@ private module ParameterNodes {
11931199
)
11941200
}
11951201

1196-
final override CfgScope getCfgScope() { result = callable.asCfgScope() }
1202+
final override CfgScope getCfgScopeImpl() { result = callable.asCfgScope() }
11971203

11981204
final override DataFlowCallable getEnclosingCallable() { result = callable }
11991205

@@ -1240,7 +1246,7 @@ private module ParameterNodes {
12401246
cs = getArrayContent(pos)
12411247
}
12421248

1243-
final override CfgScope getCfgScope() { result = callable.asCfgScope() }
1249+
final override CfgScope getCfgScopeImpl() { result = callable.asCfgScope() }
12441250

12451251
final override DataFlowCallable getEnclosingCallable() { result = callable }
12461252

@@ -1278,7 +1284,7 @@ class FlowSummaryNode extends NodeImpl, TFlowSummaryNode {
12781284
result = this.getSummaryNode().getSummarizedCallable()
12791285
}
12801286

1281-
override CfgScope getCfgScope() { none() }
1287+
override CfgScope getCfgScopeImpl() { none() }
12821288

12831289
override DataFlowCallable getEnclosingCallable() {
12841290
result.asLibraryCallable() = this.getSummarizedCallable()
@@ -1349,7 +1355,7 @@ module ArgumentNodes {
13491355
this.sourceArgumentOf(call.asCall(), pos)
13501356
}
13511357

1352-
override CfgScope getCfgScope() { result = yield.getScope() }
1358+
override CfgScope getCfgScopeImpl() { result = yield.getScope() }
13531359

13541360
override Location getLocationImpl() { result = yield.getLocation() }
13551361
}
@@ -1379,7 +1385,7 @@ module ArgumentNodes {
13791385
this.sourceArgumentOf(call.asCall(), pos)
13801386
}
13811387

1382-
override CfgScope getCfgScope() { result = sup.getScope() }
1388+
override CfgScope getCfgScopeImpl() { result = sup.getScope() }
13831389

13841390
override Location getLocationImpl() { result = sup.getLocation() }
13851391
}
@@ -1415,7 +1421,7 @@ module ArgumentNodes {
14151421
this.sourceArgumentOf(call.asCall(), pos)
14161422
}
14171423

1418-
final override CfgScope getCfgScope() { result = call_.getExpr().getCfgScope() }
1424+
final override CfgScope getCfgScopeImpl() { result = call_.getExpr().getCfgScope() }
14191425

14201426
final override Location getLocationImpl() { result = call_.getLocation() }
14211427
}
@@ -1563,7 +1569,7 @@ module ArgumentNodes {
15631569
)
15641570
}
15651571

1566-
override CfgScope getCfgScope() { result = c.getExpr().getCfgScope() }
1572+
override CfgScope getCfgScopeImpl() { result = c.getExpr().getCfgScope() }
15671573

15681574
override Location getLocationImpl() { result = c.getLocation() }
15691575

@@ -2074,7 +2080,7 @@ private module PostUpdateNodes {
20742080

20752081
override ExprNode getPreUpdateNode() { e = result.getExprNode() }
20762082

2077-
override CfgScope getCfgScope() { result = e.getExpr().getCfgScope() }
2083+
override CfgScope getCfgScopeImpl() { result = e.getExpr().getCfgScope() }
20782084

20792085
override Location getLocationImpl() { result = e.getLocation() }
20802086

0 commit comments

Comments
 (0)