Skip to content

Commit 1380be0

Browse files
committed
wip
1 parent c1c86d2 commit 1380be0

19 files changed

+106
-79
lines changed

rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ final class MethodCallExprCfgNode extends Nodes::MethodCallExprCfgNode {
212212
MethodCallExprCfgNode() { node = this.getAstNode() }
213213

214214
/** Gets the `i`th argument of this call. */
215-
ExprCfgNode getArgument(int i) {
216-
any(ChildMapping mapping).hasCfgChild(node, node.getArgList().getArg(i), this, result)
215+
ExprCfgNode getPositionalArgument(int i) {
216+
any(ChildMapping mapping).hasCfgChild(node, node.getPositionalArgument(i), this, result)
217217
}
218218
}
219219

@@ -236,8 +236,8 @@ final class CallCfgNode extends ExprCfgNode {
236236
}
237237

238238
/** Gets the `i`th argument of this call, if any. */
239-
ExprCfgNode getArgument(int i) {
240-
any(ChildMapping mapping).hasCfgChild(node, node.getArgument(i), this, result)
239+
ExprCfgNode getPositionalArgument(int i) {
240+
any(ChildMapping mapping).hasCfgChild(node, node.getPositionalArgument(i), this, result)
241241
}
242242
}
243243

@@ -257,8 +257,8 @@ final class CallExprCfgNode extends Nodes::CallExprCfgNode {
257257
CallExprCfgNode() { node = this.getAstNode() }
258258

259259
/** Gets the `i`th argument of this call. */
260-
ExprCfgNode getArgument(int i) {
261-
any(ChildMapping mapping).hasCfgChild(node, node.getArgList().getArg(i), this, result)
260+
ExprCfgNode getSyntacticArgument(int i) {
261+
any(ChildMapping mapping).hasCfgChild(node, node.getSyntacticArgument(i), this, result)
262262
}
263263
}
264264

rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,13 @@ module ExprTrees {
210210
override AstNode getChildNode(int i) { i = 0 and result = super.getExpr() }
211211
}
212212

213-
class BinaryOpExprTree extends StandardPostOrderTree instanceof BinaryExpr {
214-
BinaryOpExprTree() { not this instanceof BinaryLogicalOperation }
215-
216-
override AstNode getChildNode(int i) {
217-
i = 0 and result = super.getLhs()
218-
or
219-
i = 1 and result = super.getRhs()
213+
class ArgsExprTree extends StandardPostOrderTree instanceof ArgsExpr {
214+
ArgsExprTree() {
215+
not this instanceof CallExpr and
216+
not this instanceof BinaryLogicalOperation
220217
}
218+
219+
override AstNode getChildNode(int i) { result = super.getSyntacticArgument(i) }
221220
}
222221

223222
class LogicalOrExprTree extends PostOrderTree, LogicalOrExpr {
@@ -296,7 +295,7 @@ module ExprTrees {
296295
override AstNode getChildNode(int i) {
297296
i = 0 and result = super.getFunction()
298297
or
299-
result = super.getArgList().getArg(i - 1)
298+
result = super.getSyntacticArgument(i - 1)
300299
}
301300
}
302301

@@ -371,14 +370,6 @@ module ExprTrees {
371370
}
372371
}
373372

374-
class IndexExprTree extends StandardPostOrderTree instanceof IndexExpr {
375-
override AstNode getChildNode(int i) {
376-
i = 0 and result = super.getBase()
377-
or
378-
i = 1 and result = super.getIndex()
379-
}
380-
}
381-
382373
class LetExprTree extends StandardPostOrderTree, LetExpr {
383374
override AstNode getChildNode(int i) {
384375
i = 0 and
@@ -510,12 +501,6 @@ module ExprTrees {
510501
}
511502
}
512503

513-
class MethodCallExprTree extends StandardPostOrderTree, MethodCallExpr {
514-
override AstNode getChildNode(int i) {
515-
if i = 0 then result = this.getReceiver() else result = this.getArgList().getArg(i - 1)
516-
}
517-
}
518-
519504
class OffsetOfExprTree extends LeafTree instanceof OffsetOfExpr { }
520505

521506
class ParenExprTree extends ControlFlowTree, ParenExpr {
@@ -534,10 +519,6 @@ module ExprTrees {
534519

535520
class PathExprTree extends LeafTree instanceof PathExpr { }
536521

537-
class PrefixExprTree extends StandardPostOrderTree instanceof PrefixExpr {
538-
override AstNode getChildNode(int i) { i = 0 and result = super.getExpr() }
539-
}
540-
541522
class RangeExprTree extends StandardPostOrderTree instanceof RangeExpr {
542523
override AstNode getChildNode(int i) {
543524
i = 0 and result = super.getStart()

rust/ql/lib/codeql/rust/dataflow/internal/Content.qll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ final class TuplePositionContent extends FieldContent, TTuplePositionContent {
163163
}
164164

165165
/**
166-
* A content for the index of an argument to at function call.
166+
* A content for the index of an argument to at closure call.
167167
*
168168
* Used by the model generator to create flow summaries for higher-order
169169
* functions.
170170
*/
171-
final class FunctionCallArgumentContent extends Content, TFunctionCallArgumentContent {
171+
final class ClosureCallArgumentContent extends Content, TClosureCallArgumentContent {
172172
private int pos;
173173

174-
FunctionCallArgumentContent() { this = TFunctionCallArgumentContent(pos) }
174+
ClosureCallArgumentContent() { this = TClosureCallArgumentContent(pos) }
175175

176176
int getPosition() { result = pos }
177177

@@ -269,6 +269,8 @@ newtype TContent =
269269
)]
270270
} or
271271
TFunctionCallReturnContent() or
272-
TFunctionCallArgumentContent(int pos) { pos in [0 .. any(CallExpr c).getNumberOfArguments() - 1] } or
272+
TClosureCallArgumentContent(int pos) {
273+
pos in [0 .. any(ClosureCallExpr c).getNumberOfPositionalArguments()]
274+
} or
273275
TCapturedVariableContent(VariableCapture::CapturedVariable v) or
274276
TReferenceContent()

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ final class ParameterPosition extends TParameterPosition {
131131
final class ArgumentPosition extends ParameterPosition {
132132
/** Gets the argument of `call` at this position, if any. */
133133
Expr getArgument(Call call) {
134-
result = call.getArgument(this.getPosition())
134+
result = call.getPositionalArgument(this.getPosition())
135135
or
136136
result = call.getReceiver() and this.isSelf()
137137
}
@@ -669,7 +669,7 @@ module RustDataFlow implements InputSig<Location> {
669669
pragma[nomagic]
670670
additional predicate storeContentStep(Node node1, Content c, Node node2) {
671671
exists(CallExpr call, int pos |
672-
node1.asExpr() = call.getArgument(pragma[only_bind_into](pos)) and
672+
node1.asExpr() = call.getSyntacticArgument(pragma[only_bind_into](pos)) and
673673
node2.asExpr() = call and
674674
c = TTupleFieldContent(getCallExprTupleField(call, pragma[only_bind_into](pos)))
675675
)
@@ -717,7 +717,7 @@ module RustDataFlow implements InputSig<Location> {
717717
exists(DataFlowCall call, int i |
718718
isArgumentNode(node1, call, TPositionalParameterPosition(i)) and
719719
lambdaCall(call, _, node2.(PostUpdateNode).getPreUpdateNode()) and
720-
c.(FunctionCallArgumentContent).getPosition() = i
720+
c.(ClosureCallArgumentContent).getPosition() = i
721721
)
722722
or
723723
VariableCapture::storeStep(node1, c, node2)

rust/ql/lib/codeql/rust/dataflow/internal/SsaImpl.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,7 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
326326
predicate ssaDefHasSource(WriteDefinition def) { none() } // handled in `DataFlowImpl.qll` instead
327327

328328
private predicate isArg(Call call, Expr e) {
329-
call.getAnArgument() = e
330-
or
331-
call.getReceiver() = e
329+
call.getASyntacticArgument() = e
332330
or
333331
exists(Expr mid |
334332
isArg(call, mid) and

rust/ql/lib/codeql/rust/elements/internal/ArgsExprImpl.qll

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@ module Impl {
1010
* Either a `CallExpr`, a `MethodCallExpr`, an `Operation`, or an `IndexExpr`.
1111
*/
1212
abstract class ArgsExpr extends ExprImpl::Expr {
13-
/** Gets the `i`th positional argument of this expression, if any. */
14-
Expr getArgument(int i) { none() }
13+
/**
14+
* Gets the `i`th syntactic argument of this expression.
15+
*
16+
* Examples:
17+
* ```rust
18+
* foo(42, "bar"); // `42` is argument 0 and `"bar"` is argument 1
19+
* foo.bar(42); // `foo` is argument 0 and `42` is argument 1
20+
* x + y; // `x` is argument 0 and `y` is argument 1
21+
* x[y]; // `x` is argument 0 and `y` is argument 1
22+
* ```
23+
*/
24+
Expr getSyntacticArgument(int i) { none() }
1525

16-
/** Gets a positional argument of this expression, if any. */
17-
Expr getAnArgument() { result = this.getArgument(_) }
26+
/** Gets an argument of this expression. */
27+
Expr getASyntacticArgument() { result = this.getSyntacticArgument(_) }
1828

19-
/** Gets the number of positional arguments of this expression. */
20-
int getNumberOfArguments() { result = count(Expr arg | arg = this.getArgument(_)) }
29+
/** Gets the number of arguments of this expression. */
30+
int getNumberOfSyntacticArguments() {
31+
result = count(Expr arg | arg = this.getSyntacticArgument(_))
32+
}
2133

2234
/** Gets the resolved target (function or tuple struct/variant), if any. */
2335
Addressable getResolvedTarget() { result = TypeInference::resolveCallTarget(this) }

rust/ql/lib/codeql/rust/elements/internal/CallExprImpl.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ module Impl {
4242
class CallExpr extends Generated::CallExpr, ArgsExprImpl::ArgsExpr {
4343
override string toStringImpl() { result = this.getFunction().toAbbreviatedString() + "(...)" }
4444

45-
override Expr getArgument(int i) { result = this.getArgList().getArg(i) }
45+
override Expr getSyntacticArgument(int i) { result = this.getArgList().getArg(i) }
4646

4747
// todo: remove once internal query has been updated
48-
Expr getArg(int i) { result = this.getArgument(i) }
48+
Expr getArg(int i) { result = this.getSyntacticArgument(i) }
4949

5050
// todo: remove once internal query has been updated
51-
int getNumberOfArgs() { result = this.getNumberOfArguments() }
51+
int getNumberOfArgs() { result = this.getNumberOfSyntacticArguments() }
5252
}
5353

5454
/**
@@ -74,10 +74,10 @@ module Impl {
7474

7575
private predicate isMethodCall() { this.getResolvedTarget() instanceof Method }
7676

77-
override Expr getArgument(int i) {
77+
override Expr getPositionalArgument(int i) {
7878
if this.isMethodCall()
79-
then result = super.getArgList().getArg(i + 1)
80-
else result = super.getArgList().getArg(i)
79+
then result = this.getSyntacticArgument(i + 1)
80+
else result = super.getSyntacticArgument(i)
8181
}
8282

8383
override Expr getReceiver() { this.isMethodCall() and result = super.getArgList().getArg(0) }

rust/ql/lib/codeql/rust/elements/internal/CallImpl.qll

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,37 @@ module Impl {
1919
/**
2020
* Gets the receiver of this call, if any.
2121
*
22-
* This is either an actual receiver of a method call, the first argument of a call
23-
* to a method
24-
* the first operand of an operation,
25-
* or the base expression of an index expression.
22+
* Examples:
23+
* ```rust
24+
* foo(42, "bar"); // no receiver
25+
* foo.bar(42); // `foo` is receiver
26+
* x + y; // `x` is receiver
27+
* x[y]; // `x` is receiver
28+
* ```
2629
*/
2730
Expr getReceiver() { none() }
2831

32+
/**
33+
* Gets the `i`th positional argument of this call, if any.
34+
*
35+
* Examples:
36+
* ```rust
37+
* foo(42, "bar"); // `42` is argument 0 and `"bar"` is argument 1
38+
* foo.bar(42); // `42` is argument 0
39+
* x + y; // `y` is argument 0
40+
* x[y]; // `y` is argument 0
41+
* ```
42+
*/
43+
Expr getPositionalArgument(int i) { none() }
44+
45+
/** Gets a positional argument of this expression. */
46+
Expr getAPositionalArgument() { result = this.getPositionalArgument(_) }
47+
48+
/** Gets the number of positional arguments of this expression. */
49+
int getNumberOfPositionalArguments() {
50+
result = count(Expr arg | arg = this.getPositionalArgument(_))
51+
}
52+
2953
/** Gets the resolved target of this call, if any. */
3054
Function getStaticTarget() { result = TypeInference::resolveCallTarget(this) }
3155

rust/ql/lib/codeql/rust/elements/internal/IndexExprImpl.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ module Impl {
2828
this.getBase().toAbbreviatedString() + "[" + this.getIndex().toAbbreviatedString() + "]"
2929
}
3030

31-
override Expr getArgument(int i) { i = 0 and result = this.getIndex() }
31+
override Expr getSyntacticArgument(int i) {
32+
i = 0 and result = this.getBase()
33+
or
34+
i = 1 and result = this.getIndex()
35+
}
36+
37+
override Expr getPositionalArgument(int i) { i = 0 and result = this.getIndex() }
3238

3339
override Expr getReceiver() { result = this.getBase() }
3440
}

rust/ql/lib/codeql/rust/elements/internal/MethodCallExprImpl.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ module Impl {
4646
result = strictconcat(int i | | this.toStringPart(i) order by i)
4747
}
4848

49-
override Expr getArgument(int i) { result = this.getArgList().getArg(i) }
49+
override Expr getSyntacticArgument(int i) {
50+
i = 0 and result = this.getReceiver()
51+
or
52+
result = this.getPositionalArgument(i - 1)
53+
}
54+
55+
override Expr getPositionalArgument(int i) { result = this.getArgList().getArg(i) }
5056

5157
override Expr getReceiver() { result = Generated::MethodCallExpr.super.getReceiver() }
5258
}

0 commit comments

Comments
 (0)