Skip to content

Commit cdf59e1

Browse files
committed
Ruby: Cache more predicates
1 parent f9dbf67 commit cdf59e1

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,14 @@ class ExprNode extends Node, TExprNode {
212212
* The value of a parameter at function entry, viewed as a node in a data
213213
* flow graph.
214214
*/
215-
class ParameterNode extends LocalSourceNode instanceof ParameterNodeImpl {
215+
class ParameterNode extends LocalSourceNode {
216+
ParameterNode() { exists(getParameterPosition(this, _)) }
217+
216218
/** Gets the parameter corresponding to this node, if any. */
217-
final Parameter getParameter() { result = super.getParameter() }
219+
final Parameter getParameter() { result = getParameter(this) }
218220

219221
/** Gets the callable that this parameter belongs to. */
220-
final Callable getCallable() { result = super.getCfgScope() }
222+
final Callable getCallable() { result = getCfgScope(this) }
221223

222224
/** Gets the name of the parameter, if any. */
223225
final string getName() { result = this.getParameter().(NamedParameter).getName() }
@@ -348,9 +350,13 @@ class LocalSourceNode extends Node {
348350
* Nodes corresponding to AST elements, for example `ExprNode`, usually refer
349351
* to the value before the update.
350352
*/
351-
class PostUpdateNode extends Node instanceof PostUpdateNodeImpl {
353+
class PostUpdateNode extends Node {
354+
private Node pre;
355+
356+
PostUpdateNode() { pre = getPreUpdateNode(this) }
357+
352358
/** Gets the node before the state update. */
353-
Node getPreUpdateNode() { result = super.getPreUpdateNode() }
359+
Node getPreUpdateNode() { result = pre }
354360
}
355361

356362
/** An SSA definition, viewed as a node in a data flow graph. */
@@ -383,6 +389,28 @@ private module Cached {
383389
)
384390
}
385391

392+
cached
393+
CfgScope getCfgScope(NodeImpl node) { result = node.getCfgScope() }
394+
395+
cached
396+
ReturnNode getAReturnNode(Callable callable) { getCfgScope(result) = callable }
397+
398+
cached
399+
Parameter getParameter(ParameterNodeImpl param) { result = param.getParameter() }
400+
401+
cached
402+
ParameterPosition getParameterPosition(ParameterNodeImpl param, DataFlowCallable c) {
403+
param.isParameterOf(c, result)
404+
}
405+
406+
cached
407+
ParameterPosition getSourceParameterPosition(ParameterNodeImpl param, Callable c) {
408+
param.isSourceParameterOf(c, result)
409+
}
410+
411+
cached
412+
Node getPreUpdateNode(PostUpdateNodeImpl node) { result = node.getPreUpdateNode() }
413+
386414
cached
387415
predicate methodHasSuperCall(MethodNode method, CallNode call) {
388416
call.isSuperCall() and method = call.getEnclosingMethod()
@@ -1271,7 +1299,7 @@ class CallableNode extends StmtSequenceNode {
12711299
Callable asCallableAstNode() { result = callable }
12721300

12731301
private ParameterPosition getParameterPosition(ParameterNodeImpl node) {
1274-
node.isSourceParameterOf(callable, result)
1302+
result = getSourceParameterPosition(node, callable)
12751303
}
12761304

12771305
/** Gets the `n`th positional parameter. */
@@ -1311,7 +1339,7 @@ class CallableNode extends StmtSequenceNode {
13111339
/**
13121340
* Gets a data flow node whose value is about to be returned by this callable.
13131341
*/
1314-
Node getAReturnNode() { result.(ReturnNode).(NodeImpl).getCfgScope() = callable }
1342+
Node getAReturnNode() { result = getAReturnNode(callable) }
13151343

13161344
/**
13171345
* DEPRECATED. Use `getAReturnNode` instead.

shared/dataflow/codeql/dataflow/internal/DataFlowImpl.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2724,7 +2724,7 @@ module MakeImpl<InputSig Lang> {
27242724
pragma[noinline]
27252725
ApHeadContent getHeadContent(Ap ap) { result = ap.getHead() }
27262726

2727-
predicate projectToHeadContent = getContentApprox/1;
2727+
predicate projectToHeadContent = getContentApproxCached/1;
27282728

27292729
class ApOption = ApproxAccessPathFrontOption;
27302730

shared/dataflow/codeql/dataflow/internal/DataFlowImplCommon.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,9 @@ module MakeImplCommon<InputSig Lang> {
975975
cached
976976
predicate paramMustFlow(ParamNode p, ArgNode arg) { localMustFlowStep+(p, arg) }
977977

978+
cached
979+
ContentApprox getContentApproxCached(Content c) { result = getContentApprox(c) }
980+
978981
cached
979982
newtype TCallContext =
980983
TAnyCallContext() or
@@ -1885,7 +1888,7 @@ module MakeImplCommon<InputSig Lang> {
18851888
Content getAHead() {
18861889
exists(ContentApprox cont |
18871890
this = TApproxFrontHead(cont) and
1888-
cont = getContentApprox(result)
1891+
cont = getContentApproxCached(result)
18891892
)
18901893
}
18911894
}

0 commit comments

Comments
 (0)