Skip to content

Commit

Permalink
Ruby: Cache more predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Dec 11, 2023
1 parent f9dbf67 commit cdf59e1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
42 changes: 35 additions & 7 deletions ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowPublic.qll
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,14 @@ class ExprNode extends Node, TExprNode {
* The value of a parameter at function entry, viewed as a node in a data
* flow graph.
*/
class ParameterNode extends LocalSourceNode instanceof ParameterNodeImpl {
class ParameterNode extends LocalSourceNode {
ParameterNode() { exists(getParameterPosition(this, _)) }

/** Gets the parameter corresponding to this node, if any. */
final Parameter getParameter() { result = super.getParameter() }
final Parameter getParameter() { result = getParameter(this) }

/** Gets the callable that this parameter belongs to. */
final Callable getCallable() { result = super.getCfgScope() }
final Callable getCallable() { result = getCfgScope(this) }

/** Gets the name of the parameter, if any. */
final string getName() { result = this.getParameter().(NamedParameter).getName() }
Expand Down Expand Up @@ -348,9 +350,13 @@ class LocalSourceNode extends Node {
* Nodes corresponding to AST elements, for example `ExprNode`, usually refer
* to the value before the update.
*/
class PostUpdateNode extends Node instanceof PostUpdateNodeImpl {
class PostUpdateNode extends Node {
private Node pre;

PostUpdateNode() { pre = getPreUpdateNode(this) }

/** Gets the node before the state update. */
Node getPreUpdateNode() { result = super.getPreUpdateNode() }
Node getPreUpdateNode() { result = pre }
}

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

cached
CfgScope getCfgScope(NodeImpl node) { result = node.getCfgScope() }

cached
ReturnNode getAReturnNode(Callable callable) { getCfgScope(result) = callable }

cached
Parameter getParameter(ParameterNodeImpl param) { result = param.getParameter() }

cached
ParameterPosition getParameterPosition(ParameterNodeImpl param, DataFlowCallable c) {
param.isParameterOf(c, result)
}

cached
ParameterPosition getSourceParameterPosition(ParameterNodeImpl param, Callable c) {
param.isSourceParameterOf(c, result)
}

cached
Node getPreUpdateNode(PostUpdateNodeImpl node) { result = node.getPreUpdateNode() }

cached
predicate methodHasSuperCall(MethodNode method, CallNode call) {
call.isSuperCall() and method = call.getEnclosingMethod()
Expand Down Expand Up @@ -1271,7 +1299,7 @@ class CallableNode extends StmtSequenceNode {
Callable asCallableAstNode() { result = callable }

private ParameterPosition getParameterPosition(ParameterNodeImpl node) {
node.isSourceParameterOf(callable, result)
result = getSourceParameterPosition(node, callable)
}

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

/**
* DEPRECATED. Use `getAReturnNode` instead.
Expand Down
2 changes: 1 addition & 1 deletion shared/dataflow/codeql/dataflow/internal/DataFlowImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -2724,7 +2724,7 @@ module MakeImpl<InputSig Lang> {
pragma[noinline]
ApHeadContent getHeadContent(Ap ap) { result = ap.getHead() }

predicate projectToHeadContent = getContentApprox/1;
predicate projectToHeadContent = getContentApproxCached/1;

class ApOption = ApproxAccessPathFrontOption;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,9 @@ module MakeImplCommon<InputSig Lang> {
cached
predicate paramMustFlow(ParamNode p, ArgNode arg) { localMustFlowStep+(p, arg) }

cached
ContentApprox getContentApproxCached(Content c) { result = getContentApprox(c) }

cached
newtype TCallContext =
TAnyCallContext() or
Expand Down Expand Up @@ -1885,7 +1888,7 @@ module MakeImplCommon<InputSig Lang> {
Content getAHead() {
exists(ContentApprox cont |
this = TApproxFrontHead(cont) and
cont = getContentApprox(result)
cont = getContentApproxCached(result)
)
}
}
Expand Down

0 comments on commit cdf59e1

Please sign in to comment.