From 247be3bee531b927493911d7b87f12f33a852782 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Fri, 20 Nov 2026 11:59:33 +0100 Subject: [PATCH] Unified: Model parameter/argument positions in data flow similar to type inference --- .../dataflow/DataFlowInstantiation.qll | 14 +- .../internal/dataflow/ParameterPositions.qll | 143 ++++++++++++++++-- .../CONSISTENCY/DataFlowConsistency.expected | 5 - 3 files changed, 133 insertions(+), 29 deletions(-) delete mode 100644 unified/ql/test/library-tests/type-inference/CONSISTENCY/DataFlowConsistency.expected diff --git a/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll b/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll index 8bce64150db6..c8eb067be65a 100644 --- a/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll +++ b/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll @@ -77,11 +77,8 @@ module DataFlowInput implements InputSig { private predicate isParameterNodeImpl(Node p, DataFlowCallable c, ParameterPosition pos) { exists(Parameter param | p.asExpr() = param.getPattern() and - c.asSourceCallable() = param.getEnclosingCallable() - | - pos.asPositional() = param.getPositionalIndex() - or - pos.asNamed() = param.getExternalName() + c.asSourceCallable() = param.getEnclosingCallable() and + pos = getParameterPosition(param) ) or p.(BuilderNode).isImplicitParameter(c, pos) @@ -102,11 +99,8 @@ module DataFlowInput implements InputSig { private predicate isArgumentNodeImpl(Node n, DataFlowCall call, ArgumentPosition pos) { exists(Argument arg | n.asExpr() = arg.getValue() and - call.asExplicitCall().getAnArgument() = arg - | - pos.asPositional() = arg.getPositionalIndex() - or - pos.asNamed() = arg.getName() + call.asExplicitCall().getAnArgument() = arg and + pos = getArgumentPosition(_, arg) ) or n.(BuilderNode).isImplicitArgument(call, pos, false) diff --git a/unified/ql/lib/codeql/unified/internal/dataflow/ParameterPositions.qll b/unified/ql/lib/codeql/unified/internal/dataflow/ParameterPositions.qll index d8c49d12a286..4645f62ae45e 100644 --- a/unified/ql/lib/codeql/unified/internal/dataflow/ParameterPositions.qll +++ b/unified/ql/lib/codeql/unified/internal/dataflow/ParameterPositions.qll @@ -1,14 +1,32 @@ private import unified +private int getExternalNameRank(Callable c, Parameter p, string name) { + p = + rank[result](Parameter p0, int pos | + p0 = c.getParameter(pos) and + name = p0.getExternalName() + | + p0 order by pos + ) +} + +private predicate parameterPos(Callable c, Parameter p, int pos, string name, int rnk) { + p = c.getParameter(pos) and + ( + rnk = getExternalNameRank(c, p, name) + or + not exists(p.getExternalName()) and + name = "" and + rnk = 1 + ) +} + private newtype TParameterPosition = TReceiverParameter() or TCalleeParameter() or - TPositionalParameter(int n) { n = [0 .. 20] } or - TNamedParameter(string name) { - name = any(Parameter p).getExternalName() - or - name = any(Argument arg).getName() - } + TSourceParameter(int pos, string name, int rnk) { parameterPos(_, _, pos, name, rnk) } or + TSummaryPositionalParameter(int pos) { pos = [0 .. 20] } or + TSummaryNamedParameter(string name) { name = any(Argument arg).getName() } class ParameterPosition extends TParameterPosition { /** Holds if this represents the receiver passed to a call (usually called `this` or `self`). */ @@ -17,11 +35,89 @@ class ParameterPosition extends TParameterPosition { /** Holds if this represents the function value being invoked in a call. */ predicate isCallee() { this = TCalleeParameter() } - int asPositional() { this = TPositionalParameter(result) } + /** + * Holds if this represents a parameter from source code at postion `pos` and + * with external name `name` and rank `rnk`. + * + * `name` is `""` when there is no external name, and `rnk` is the rank of the + * parameter among parameters with the same external name. + */ + predicate isSourceParameter(int pos, string name, int rnk) { + this = TSourceParameter(pos, name, rnk) + } + + /** Gets the represented positional summary parameter index, if any. */ + int asPositionalSummaryParameter() { this = TSummaryPositionalParameter(result) } + + /** Gets the represented named summary parameter, if any. */ + string asNamedSummaryParameter() { this = TSummaryNamedParameter(result) } + + /** Gets the operand to `Argument` that represents this position in a MaD model. */ + string asModelOperand() { + this.isReceiver() and result = "self" + or + this.isCallee() and result = "callee" + or + result = this.asPositionalSummaryParameter().toString() + or + // Suffix with a colon to prevent a confusing name clash with "receiver". This also aligns with MaD syntax. + result = this.asNamedSummaryParameter() + ":" + } + + /** Gets a textual representation of this position. */ + string toString() { + result = this.asModelOperand() + or + exists(int pos, string name | this.isSourceParameter(pos, name, _) | + if name = "" + then result = pos.toString() + else + // Suffix with a colon to prevent a confusing name clash with "receiver". This also aligns with MaD syntax. + result = name + ":" + ) + } +} + +ParameterPosition getParameterPosition(Parameter p) { + exists(int pos, string name, int rnk | + parameterPos(_, p, pos, name, rnk) and + result = TSourceParameter(pos, name, rnk) + ) +} + +private predicate positionalArgument(CallExpr ce, Argument arg, int pos) { + arg = ce.getArgument(pos) and + not exists(arg.getName()) +} + +private int getNameRank(CallExpr ce, Argument arg, string name) { + arg = + rank[result](Argument arg0, int pos | + arg0 = ce.getArgument(pos) and + name = arg0.getName() + | + arg0 order by pos + ) +} - string asNamed() { this = TNamedParameter(result) } +private newtype TArgumentPosition = + TReceiverArgument() or + TCalleeArgument() or + TPositionalArgument(int pos) { positionalArgument(_, _, pos) } or + TNamedArgument(string name, int rnk) { rnk = getNameRank(_, _, name) } - /** Gets the operand to `Parameter`/`Argument` that represents this position in a MaD model. */ +class ArgumentPosition extends TArgumentPosition { + /** Holds if this represents the receiver passed to a call (usually called `this` or `self`). */ + predicate isReceiver() { this = TReceiverArgument() } + + /** Holds if this represents the function value being invoked in a call. */ + predicate isCallee() { this = TCalleeArgument() } + + int asPositional() { this = TPositionalArgument(result) } + + string asNamed(int rnk) { this = TNamedArgument(result, rnk) } + + /** Gets the operand to `Parameter` that represents this position in a MaD model. */ string asModelOperand() { this.isReceiver() and result = "self" or @@ -30,14 +126,19 @@ class ParameterPosition extends TParameterPosition { result = this.asPositional().toString() or // Suffix with a colon to prevent a confusing name clash with "receiver". This also aligns with MaD syntax. - result = this.asNamed() + ":" + result = this.asNamed(_) + ":" } - /** Gets a string representation of this position. */ + /** Gets a textual representation of this position. */ string toString() { result = this.asModelOperand() } } -class ArgumentPosition = ParameterPosition; +pragma[nomagic] +ArgumentPosition getArgumentPosition(CallExpr ce, Argument arg) { + positionalArgument(ce, arg, result.asPositional()) + or + exists(string name | result.asNamed(getNameRank(ce, arg, name)) = name) +} /** A parameter position that is either `receiver` or `callee`. */ class ImplicitParameterPosition extends ParameterPosition { @@ -45,6 +146,20 @@ class ImplicitParameterPosition extends ParameterPosition { } /** An argument position that is either `receiver` or `callee`. */ -class ImplicitArgumentPosition = ImplicitParameterPosition; +class ImplicitArgumentPosition extends ArgumentPosition { + ImplicitArgumentPosition() { this.isReceiver() or this.isCallee() } +} -predicate parameterMatch(ParameterPosition ppos, ArgumentPosition apos) { apos = ppos } +predicate parameterMatch(ParameterPosition ppos, ArgumentPosition apos) { + ppos.isReceiver() and apos.isReceiver() + or + ppos.isCallee() and apos.isCallee() + or + ppos = TSourceParameter(apos.asPositional(), _, _) + or + ppos = TSummaryPositionalParameter(apos.asPositional()) + or + exists(int rnk | ppos = TSourceParameter(_, apos.asNamed(rnk), rnk)) + or + ppos = TSummaryNamedParameter(apos.asNamed(_)) +} diff --git a/unified/ql/test/library-tests/type-inference/CONSISTENCY/DataFlowConsistency.expected b/unified/ql/test/library-tests/type-inference/CONSISTENCY/DataFlowConsistency.expected deleted file mode 100644 index 2933561f6950..000000000000 --- a/unified/ql/test/library-tests/type-inference/CONSISTENCY/DataFlowConsistency.expected +++ /dev/null @@ -1,5 +0,0 @@ -uniqueParameterNodeAtPosition -| basics.swift:67:1:69:1 | func swap | a: | basics.swift:67:21:67:22 | a1 | Parameters with overlapping positions. | -| basics.swift:67:1:69:1 | func swap | a: | basics.swift:67:31:67:32 | a2 | Parameters with overlapping positions. | -| basics.swift:73:1:73:61 | func f | a: | basics.swift:73:10:73:14 | first | Parameters with overlapping positions. | -| basics.swift:73:1:73:61 | func f | a: | basics.swift:73:34:73:39 | second | Parameters with overlapping positions. |