Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions unified/ql/lib/codeql/unified/internal/ParameterEx.qll
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private newtype TParameterEx =
TImplicitReceiverParameter(Callable c) {
exists(LocalVariable v | v.isImplicitReceiverParameter(c))
} or
TImplicitCalleeParameter(Callable c) or
TImplicitFunctionExprParameter(FunctionExpr fe, int i) {
i = [0 .. fe.getNumberOfImplicitParameters() - 1]
} or
Expand Down Expand Up @@ -40,6 +41,13 @@ abstract private class ParameterExImpl extends TParameterEx {
*/
predicate isImplicitReceiverParameter(Callable c) { this = TImplicitReceiverParameter(c) }

/**
* Holds if this entity represents an implicit callee parameter of `c`.
*
* Only used to model variable capture.
*/
predicate isImplicitCalleeParameter(Callable c) { this = TImplicitCalleeParameter(c) }

/** Holds if this entity represents implicit parameter `i` of `fe`. */
predicate isImplicitFunctionExprParameter(FunctionExpr fe, int i) {
this = TImplicitFunctionExprParameter(fe, i)
Expand Down Expand Up @@ -75,6 +83,8 @@ abstract private class ParameterExImpl extends TParameterEx {
this.isImplicitFunctionExprParameter(result, _)
or
this.isDefaultConstructorParameter(result, _, _)
or
this.isImplicitCalleeParameter(result)
}

/** Gets the external name of this parameter, if any. */
Expand All @@ -86,6 +96,11 @@ abstract private class ParameterExImpl extends TParameterEx {
/** Gets the type declaration of this parameter, if any. */
abstract AstNode getType();

/**
* Gets the control flow node associated with this parameter.
*/
abstract ControlFlowNode getControlFlowNode();

/** Gets a textual representation of this parameter. */
abstract string toString();

Expand All @@ -104,6 +119,13 @@ private class AstParameterEx extends ParameterExImpl, TAstParameter {

override AstNode getType() { result = p.getType() }

override ControlFlowNode getControlFlowNode() {
result.injects(p.getPattern())
or
not exists(p.getPattern()) and
result.injects(p)
}

override string toString() { result = p.toString() }

override Location getLocation() { result = p.getLocation() }
Expand All @@ -120,6 +142,10 @@ private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitRece

override AstNode getType() { none() }

override ControlFlowNode getControlFlowNode() {
result.(ControlFlow::EntryNode).getEnclosingCallable() = c
}

override string toString() {
exists(LocalVariable v |
v.isImplicitReceiverParameter(c) and
Expand All @@ -130,6 +156,26 @@ private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitRece
override Location getLocation() { result = c.getLocation() }
}

private class ImplicitCalleeParameter extends ParameterExImpl, TImplicitCalleeParameter {
Callable c;

ImplicitCalleeParameter() { this = TImplicitCalleeParameter(c) }

override string getExternalName() { none() }

override predicate hasDefault() { none() }

override AstNode getType() { none() }

override ControlFlowNode getControlFlowNode() {
result.(ControlFlow::EntryNode).getEnclosingCallable() = c
}

override string toString() { result = "implicit callee parameter" }

override Location getLocation() { result = c.getLocation() }
}

/**
* An implicit parameter of a function expression inferred from usage of
* implicit parameters inside its body.
Expand All @@ -148,6 +194,10 @@ private class ImplicitFunctionExprParameterEx extends ParameterExImpl,

override AstNode getType() { none() }

override ControlFlowNode getControlFlowNode() {
result.(ControlFlow::EntryNode).getEnclosingCallable() = fe
}

override string toString() { result = "implicit parameter " + i }

override Location getLocation() { result = fe.getLocation() }
Expand All @@ -167,6 +217,10 @@ private class DefaultConstructorParameterEx extends ParameterExImpl, TDefaultCon

override predicate hasDefault() { hasDefault = true }

override ControlFlowNode getControlFlowNode() {
result.(ControlFlow::EntryNode).getEnclosingCallable() = cd
}

override string toString() { result = name + " [" + cd.getName() + " default constructor]" }

override Location getLocation() { result = cd.getLocation() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ Node getNodeFromCaptureSsaNode(CaptureSsaOutput::ClosureNode n) {
exists(CaptureSsaOutput::ThisParameterNode thisParam, Callable callable |
n = thisParam and
callable = thisParam.getCallable() and
result
.(BuilderNode)
.isImplicitParameter(toDataFlowCallable(callable), any(ParameterPosition p | p.isCallee()))
result.(BuilderNode).asParameterEx().isImplicitCalleeParameter(callable)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ predicate step(BuilderNode node1, Step step, BuilderNode node2) {
)
or
exists(Callable callable |
node1.isReceiverParameter(callable) and
node1.asParameterEx().isImplicitReceiverParameter(callable) and
step.value() and
node2.isLocalVariableWrite(callable, getImplicitReceiverVariable(callable))
)
or
exists(FunctionExpr fe, LocalVariable var, ParameterPosition pos |
var.isImplicitFunctionExprParameter(fe, pos.asPositional()) and
node1.isImplicitParameter(toDataFlowCallable(fe), pos) and
exists(FunctionExpr fe, LocalVariable var, int pos |
var.isImplicitFunctionExprParameter(fe, pos) and
node1.asParameterEx().isImplicitFunctionExprParameter(fe, pos) and
step.value() and
node2.isLocalVariableWrite(fe, var)
)
Expand Down Expand Up @@ -239,7 +239,11 @@ predicate step(BuilderNode node1, Step step, BuilderNode node2) {
node2.isResultValue(expr)
)
or
none() // Temporarily disable compilation errors from unsatisfiable types
exists(Parameter p |
p = node1.asParameterEx().asParameter() and
node2.isIncomingValue(p.getPattern()) and
if exists(p.getDefault()) then step.value() else step.valueMustFlow()
)
}

/** Holds if `node` should be included in the debug view. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ private import unified
private import AllDataFlow
private import AllDataFlow as D
private import codeql.dataflow.DataFlow
private import codeql.unified.internal.ParameterEx
private import codeql.util.Void
private import codeql.util.Unit

Expand Down Expand Up @@ -74,20 +75,31 @@ module DataFlowInput implements InputSig<Location> {

DataFlowCallable nodeGetEnclosingCallable(Node node) { result = node.getEnclosingCallableEx() }

private predicate isParameterNodeImpl(Node p, DataFlowCallable c, ParameterPosition pos) {
exists(Parameter param |
p.asExpr() = param.getPattern() and
c.asSourceCallable() = param.getEnclosingCallable()
private predicate isParameterNodeImpl(Node n, DataFlowCallable c, ParameterPosition pos) {
exists(ParameterEx p, Callable callable |
p = n.(BuilderNode).asParameterEx() and
callable = c.asSourceCallable()
|
pos.asPositional() = param.getPositionalIndex()
exists(Parameter param |
param = p.asParameter() and
callable = param.getDeclaringCallable()
|
pos.asPositional() = param.getPositionalIndex()
or
pos.asNamed() = param.getExternalName()
)
or
pos.asNamed() = param.getExternalName()
p.isImplicitReceiverParameter(callable) and
pos.isReceiver()
or
p.isImplicitCalleeParameter(callable) and
pos.isCallee()
or
p.isImplicitFunctionExprParameter(callable, pos.asPositional())
)
or
p.(BuilderNode).isImplicitParameter(c, pos)
or
FlowSummaryOutput::summaryParameterNode(getSummaryNode(p), pos) and
c = p.getEnclosingCallableEx()
FlowSummaryOutput::summaryParameterNode(getSummaryNode(n), pos) and
c = n.getEnclosingCallableEx()
}

class ParameterNode extends Node {
Expand Down Expand Up @@ -280,7 +292,7 @@ module DataFlowInput implements InputSig<Location> {
predicate isUnreachableInCall(NodeRegion nr, DataFlowCall call) { none() } // TODO

predicate allowParameterReturnInSelf(ParameterNode p) {
p.isReceiverParameter(_)
p.(BuilderNode).asParameterEx().isImplicitReceiverParameter(_)
or
captureSsaAllowParameterReturnInSelf(p)
or
Expand Down
58 changes: 20 additions & 38 deletions unified/ql/lib/codeql/unified/internal/dataflow/DataFlowNode.qll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ private import unified
private import AllDataFlow
private import codeql.unified.internal.ExprPositions
private import codeql.unified.internal.LocalNameBinding
private import codeql.unified.internal.ParameterEx
private import codeql.util.Boolean

private predicate hasIncomingValueAtCfgNode(Expr expr, ControlFlowNode cfgNode) {
Expand Down Expand Up @@ -122,15 +123,7 @@ newtype TDataFlowNode =
not exists(callable.asSummarizedCallable()) // represented by TSummaryNode
} or
TCallableNode(DataFlowCallable callable) or
TImplicitParameterNode(DataFlowCallable callable, ParameterPosition pos) {
not exists(callable.asSummarizedCallable()) and // represented by TSummaryNode
(
pos instanceof ImplicitParameterPosition
or
pos.asPositional() =
[0 .. callable.asSourceCallable().(FunctionExpr).getNumberOfImplicitParameters() - 1]
)
} or
TParameterExNode(ParameterEx p) { exists(toDataFlowCallable(p.getCallable())) } or
TImplicitArgumentNode(DataFlowCall call, ImplicitArgumentPosition pos, Boolean isPost) {
not call.isSummaryCall(_, _) // represented by TSummaryNode
} or
Expand All @@ -140,7 +133,7 @@ newtype TDataFlowNode =

class TDataFlowNodeStage1 =
TValueNode or TStrictlyIncomingValue or TExprPostUpdateNode or TLocalVariableRefNode or
TReturnNode or TCallableNode or TImplicitParameterNode or TImplicitArgumentNode;
TReturnNode or TCallableNode or TParameterExNode or TImplicitArgumentNode;

class TDataFlowNodeStage2 = TDataFlowNodeStage1 or TLocalSsaNode;

Expand Down Expand Up @@ -214,26 +207,14 @@ class BuilderNode extends TDataFlowNodeStage1 {
*/
predicate isReturnNodeEx(DataFlowCallable callable) { this = TReturnNode(callable) }

/** Holds if this node represents an implicit parameter of `callable`. */
predicate isImplicitParameter(DataFlowCallable callable, ParameterPosition pos) {
this = TImplicitParameterNode(callable, pos)
}
/** Gets the `ParameterEx` associated with this node, if any. */
ParameterEx asParameterEx() { this = TParameterExNode(result) }

/** Holds if this node represents an implicit argument to `call` (or its post-update). */
predicate isImplicitArgument(DataFlowCall call, ImplicitArgumentPosition pos, boolean isPost) {
this = TImplicitArgumentNode(call, pos, isPost)
}

/**
* Holds if this represents the receiver passed to the given callable.
*
* Note that for non-methods and closures that capture the receiver from the enclosing method,
* this node still exists but will typically not flow anywhere.
*/
predicate isReceiverParameter(Callable callable) {
this.isImplicitParameter(toDataFlowCallable(callable), any(ParameterPosition p | p.isReceiver()))
}

/** Holds if this node represents the receiver argument passed to `call`. */
predicate isReceiverArgumentEx(DataFlowCall call) {
this.isImplicitArgument(call, any(ArgumentPosition p | p.isReceiver()), false)
Expand Down Expand Up @@ -318,12 +299,12 @@ class BuilderNode extends TDataFlowNodeStage1 {
result = "[variable " + kind + "] " + v.toString()
)
or
exists(ParameterEx p |
this = TParameterExNode(p) and
result = p.toString()
)
or
exists(DataFlowCallable callable |
exists(ParameterPosition pos |
this.isImplicitParameter(callable, pos) and
result = "[" + pos + " param] " + callable.toString()
)
or
this.isCallableEx(callable) and
result = "[callable] " + callable.toString()
)
Expand Down Expand Up @@ -351,11 +332,13 @@ class BuilderNode extends TDataFlowNodeStage1 {
result = repr.getLocation()
)
or
exists(ParameterEx p |
this = TParameterExNode(p) and
result = p.getLocation()
)
or
exists(DataFlowCallable callable |
this.isImplicitParameter(callable, _)
or
this.isCallableEx(callable)
|
this.isCallableEx(callable) and
result = callable.getLocation()
)
or
Expand Down Expand Up @@ -394,10 +377,9 @@ class BuilderNode extends TDataFlowNodeStage1 {
performsVariableAccess(repr, var, kind, cfgNode)
)
or
cfgNode = this.asParameterEx().getControlFlowNode()
or
exists(DataFlowCallable callable |
this.isImplicitParameter(callable, _) and
cfgNode.(ControlFlow::EntryNode).getEnclosingCallable() = callable.asSourceCallable()
or
this.isCallableEx(callable) and
cfgNode.injects(callable.asSourceCallable())
)
Expand Down Expand Up @@ -504,7 +486,7 @@ class Node extends TDataFlowNode {
* this node still exists but will typically not flow anywhere.
*/
predicate isReceiverParameter(Callable callable) {
this.(BuilderNode).isReceiverParameter(callable)
this.(BuilderNode).asParameterEx().isImplicitReceiverParameter(callable)
}

/** Holds if this node represents the receiver argument passed to `call`. */
Expand Down Expand Up @@ -575,7 +557,7 @@ class Node extends TDataFlowNode {
result.asSourceCallable() = node.getEnclosingCallable()
)
or
this.(BuilderNode).isImplicitParameter(result, _)
result.asSourceCallable() = this.(BuilderNode).asParameterEx().getCallable()
or
exists(DataFlowCall call |
this.(BuilderNode).isImplicitArgument(call, _, _) and
Expand Down
8 changes: 2 additions & 6 deletions unified/ql/test/library-tests/dataflow/test.expected
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ edges
| calls.swift:49:32:49:32 | y | calls.swift:51:14:51:14 | y | provenance | |
| calls.swift:53:12:53:25 | source(...) | calls.swift:49:19:49:19 | x | provenance | |
| calls.swift:53:28:53:41 | source(...) | calls.swift:49:32:49:32 | y | provenance | |
| calls.swift:61:19:61:19 | x [Return] [field] | calls.swift:66:12:66:12 | [post] b [field] | provenance | |
| calls.swift:62:9:62:9 | [post] x [field] | calls.swift:61:19:61:19 | x [Return] [field] | provenance | |
| calls.swift:62:9:62:9 | [post] x [field] | calls.swift:66:12:66:12 | [post] b [field] | provenance | |
| calls.swift:62:9:62:15 | ... .field | calls.swift:62:9:62:9 | [post] x [field] | provenance | |
| calls.swift:62:19:62:32 | source(...) | calls.swift:62:9:62:15 | ... .field | provenance | |
| calls.swift:66:12:66:12 | [post] b [field] | calls.swift:67:10:67:10 | b [field] | provenance | |
Expand Down Expand Up @@ -580,8 +579,7 @@ edges
| subscripts.swift:143:26:143:40 | source(...) | subscripts.swift:143:5:143:22 | ... .subscript(...) | provenance | |
| subscripts.swift:144:10:144:10 | t [field] | subscripts.swift:144:10:144:21 | ... .getField(...) | provenance | |
| subscripts.swift:154:24:154:31 | newValue [field] | subscripts.swift:154:24:154:37 | ... .field | provenance | |
| subscripts.swift:157:19:157:19 | s [Return] [field] | subscripts.swift:159:12:159:16 | [post] & ... [field] | provenance | |
| subscripts.swift:157:33:157:33 | [post] s [field] | subscripts.swift:157:19:157:19 | s [Return] [field] | provenance | |
| subscripts.swift:157:33:157:33 | [post] s [field] | subscripts.swift:159:12:159:16 | [post] & ... [field] | provenance | |
| subscripts.swift:157:33:157:39 | ... .field | subscripts.swift:157:33:157:33 | [post] s [field] | provenance | |
| subscripts.swift:157:43:157:57 | source(...) | subscripts.swift:157:33:157:39 | ... .field | provenance | |
| subscripts.swift:159:12:159:16 | [post] & ... [field] | subscripts.swift:159:13:159:16 | [post] ... .subscript(...) [field] | provenance | |
Expand Down Expand Up @@ -917,7 +915,6 @@ nodes
| calls.swift:51:14:51:14 | y | semmle.label | y |
| calls.swift:53:12:53:25 | source(...) | semmle.label | source(...) |
| calls.swift:53:28:53:41 | source(...) | semmle.label | source(...) |
| calls.swift:61:19:61:19 | x [Return] [field] | semmle.label | x [Return] [field] |
| calls.swift:62:9:62:9 | [post] x [field] | semmle.label | [post] x [field] |
| calls.swift:62:9:62:15 | ... .field | semmle.label | ... .field |
| calls.swift:62:19:62:32 | source(...) | semmle.label | source(...) |
Expand Down Expand Up @@ -1461,7 +1458,6 @@ nodes
| subscripts.swift:144:10:144:21 | ... .getField(...) | semmle.label | ... .getField(...) |
| subscripts.swift:154:24:154:31 | newValue [field] | semmle.label | newValue [field] |
| subscripts.swift:154:24:154:37 | ... .field | semmle.label | ... .field |
| subscripts.swift:157:19:157:19 | s [Return] [field] | semmle.label | s [Return] [field] |
| subscripts.swift:157:33:157:33 | [post] s [field] | semmle.label | [post] s [field] |
| subscripts.swift:157:33:157:39 | ... .field | semmle.label | ... .field |
| subscripts.swift:157:43:157:57 | source(...) | semmle.label | source(...) |
Expand Down
2 changes: 1 addition & 1 deletion unified/ql/test/library-tests/mad/test.expected
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ isSource
| test.swift:89:9:89:24 | ... .text | local |
| test.swift:176:13:176:16 | text | local |
| test.swift:177:13:177:21 | ... .text | local |
| test.swift:209:5:213:5 | [self param] func ShorthandTestSub.sourceParams | shorthand-source |
| test.swift:209:5:213:5 | self | shorthand-source |
| test.swift:210:11:210:11 | x | shorthand-source |
| test.swift:232:13:232:13 | v | subscript-setter-source |
| test.swift:253:11:253:11 | x | optional-source |
Loading
Loading