Skip to content

Commit f12aaee

Browse files
committed
Rust: Refactor classes representing calls
1 parent 4d4a3fe commit f12aaee

File tree

59 files changed

+815
-509
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+815
-509
lines changed

rust/ql/examples/snippets/simple_constant_password.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ module ConstantPasswordConfig implements DataFlow::ConfigSig {
3030

3131
predicate isSink(DataFlow::Node node) {
3232
// `node` is an argument whose corresponding parameter name matches the pattern "pass%"
33-
exists(CallExpr call, Function target, int argIndex, Variable v |
33+
exists(Call call, Function target, int argIndex, Variable v |
3434
call.getStaticTarget() = target and
3535
v.getParameter() = target.getParam(argIndex) and
3636
v.getText().matches("pass%") and
37-
call.getArg(argIndex) = node.asExpr()
37+
call.getArgument(argIndex) = node.asExpr()
3838
)
3939
}
4040
}

rust/ql/examples/snippets/simple_sql_injection.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ module SqlInjectionConfig implements DataFlow::ConfigSig {
2323

2424
predicate isSink(DataFlow::Node node) {
2525
// `node` is the first argument of a call to `sqlx_core::query::query`
26-
exists(CallExpr call |
26+
exists(Call call |
2727
call.getStaticTarget().getCanonicalPath() = "sqlx_core::query::query" and
28-
call.getArg(0) = node.asExpr()
28+
call.getArgument(0) = node.asExpr()
2929
)
3030
}
3131
}

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
private import rust
7-
private import codeql.rust.elements.Call
87
private import ControlFlowGraph
98
private import internal.ControlFlowGraphImpl as CfgImpl
109
private import internal.CfgNodes
@@ -201,28 +200,23 @@ final class BreakExprCfgNode extends Nodes::BreakExprCfgNode {
201200
}
202201

203202
/**
204-
* A function or method call expression. See `CallExpr` and `MethodCallExpr` for further details.
203+
* A method call expression. For example:
204+
* ```rust
205+
* x.foo(42);
206+
* x.foo::<u32, u64>(42);
207+
* ```
205208
*/
206-
final class CallExprBaseCfgNode extends Nodes::CallExprBaseCfgNode {
207-
private CallExprBaseChildMapping node;
209+
final class MethodCallExprCfgNode extends Nodes::MethodCallExprCfgNode {
210+
private MethodCallExprChildMapping node;
208211

209-
CallExprBaseCfgNode() { node = this.getAstNode() }
212+
MethodCallExprCfgNode() { node = this.getAstNode() }
210213

211214
/** Gets the `i`th argument of this call. */
212215
ExprCfgNode getArgument(int i) {
213216
any(ChildMapping mapping).hasCfgChild(node, node.getArgList().getArg(i), this, result)
214217
}
215218
}
216219

217-
/**
218-
* A method call expression. For example:
219-
* ```rust
220-
* x.foo(42);
221-
* x.foo::<u32, u64>(42);
222-
* ```
223-
*/
224-
final class MethodCallExprCfgNode extends CallExprBaseCfgNode, Nodes::MethodCallExprCfgNode { }
225-
226220
/**
227221
* A CFG node that calls a function.
228222
*
@@ -242,21 +236,31 @@ final class CallCfgNode extends ExprCfgNode {
242236
}
243237

244238
/** Gets the `i`th argument of this call, if any. */
245-
ExprCfgNode getPositionalArgument(int i) {
246-
any(ChildMapping mapping).hasCfgChild(node, node.getPositionalArgument(i), this, result)
239+
ExprCfgNode getArgument(int i) {
240+
any(ChildMapping mapping).hasCfgChild(node, node.getArgument(i), this, result)
247241
}
248242
}
249243

250244
/**
251-
* A function call expression. For example:
245+
* An expression with parenthesized arguments. For example:
252246
* ```rust
253247
* foo(42);
254248
* foo::<u32, u64>(42);
255249
* foo[0](42);
256250
* foo(1) = 4;
251+
* Option::Some(42);
257252
* ```
258253
*/
259-
final class CallExprCfgNode extends CallExprBaseCfgNode, Nodes::CallExprCfgNode { }
254+
final class CallExprCfgNode extends Nodes::CallExprCfgNode {
255+
private CallExprChildMapping node;
256+
257+
CallExprCfgNode() { node = this.getAstNode() }
258+
259+
/** 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)
262+
}
263+
}
260264

261265
/**
262266
* A FormatArgsExpr. For example:

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ class BreakExprTargetChildMapping extends ParentAstNode, Expr {
5757
override predicate relevantChild(AstNode child) { child.(BreakExpr).getTarget() = this }
5858
}
5959

60-
class CallExprBaseChildMapping extends ParentAstNode, CallExprBase {
61-
override predicate relevantChild(AstNode child) { child = this.getAnArg() }
60+
class CallExprChildMapping extends ParentAstNode, CallExpr {
61+
override predicate relevantChild(AstNode child) { child = this.getArgList().getAnArg() }
62+
}
63+
64+
class MethodCallExprChildMapping extends ParentAstNode, MethodCallExpr {
65+
override predicate relevantChild(AstNode child) { child = this.getArgList().getAnArg() }
6266
}
6367

6468
class StructExprChildMapping extends ParentAstNode, StructExpr {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ module ExprTrees {
512512

513513
class MethodCallExprTree extends StandardPostOrderTree, MethodCallExpr {
514514
override AstNode getChildNode(int i) {
515-
if i = 0 then result = this.getReceiver() else result = this.getArg(i - 1)
515+
if i = 0 then result = this.getReceiver() else result = this.getArgList().getArg(i - 1)
516516
}
517517
}
518518

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ newtype TContent =
269269
)]
270270
} or
271271
TFunctionCallReturnContent() or
272-
TFunctionCallArgumentContent(int pos) {
273-
pos in [0 .. any(CallExpr c).getArgList().getNumberOfArgs() - 1]
274-
} or
272+
TFunctionCallArgumentContent(int pos) { pos in [0 .. any(CallExpr c).getNumberOfArguments() - 1] } or
275273
TCapturedVariableContent(VariableCapture::CapturedVariable v) or
276274
TReferenceContent()

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ private import codeql.util.Boolean
88
private import codeql.dataflow.DataFlow
99
private import codeql.dataflow.internal.DataFlowImpl
1010
private import rust
11-
private import codeql.rust.elements.Call
1211
private import SsaImpl as SsaImpl
1312
private import codeql.rust.controlflow.internal.Scope as Scope
1413
private import codeql.rust.internal.PathResolution
@@ -57,7 +56,7 @@ final class DataFlowCallable extends TDataFlowCallable {
5756
}
5857

5958
final class DataFlowCall extends TDataFlowCall {
60-
/** Gets the underlying call in the CFG, if any. */
59+
/** Gets the underlying call, if any. */
6160
Call asCall() { this = TCall(result) }
6261

6362
predicate isSummaryCall(
@@ -132,7 +131,7 @@ final class ParameterPosition extends TParameterPosition {
132131
final class ArgumentPosition extends ParameterPosition {
133132
/** Gets the argument of `call` at this position, if any. */
134133
Expr getArgument(Call call) {
135-
result = call.getPositionalArgument(this.getPosition())
134+
result = call.getArgument(this.getPosition())
136135
or
137136
result = call.getReceiver() and this.isSelf()
138137
}
@@ -142,8 +141,6 @@ final class ArgumentPosition extends ParameterPosition {
142141
* Holds if `arg` is an argument of `call` at the position `pos`.
143142
*/
144143
predicate isArgumentForCall(Expr arg, Call call, ArgumentPosition pos) {
145-
// TODO: Handle index expressions as calls in data flow.
146-
not call instanceof IndexExpr and
147144
arg = pos.getArgument(call)
148145
}
149146

@@ -293,10 +290,8 @@ predicate lambdaCreationExpr(Expr creation) {
293290
* Holds if `call` is a lambda call of kind `kind` where `receiver` is the
294291
* invoked expression.
295292
*/
296-
predicate lambdaCallExpr(CallExpr call, LambdaCallKind kind, Expr receiver) {
297-
receiver = call.getFunction() and
298-
// All calls to complex expressions and local variable accesses are lambda call.
299-
(receiver instanceof PathExpr implies receiver = any(Variable v).getAnAccess()) and
293+
predicate lambdaCallExpr(ClosureCallExpr call, LambdaCallKind kind, Expr receiver) {
294+
receiver = call.getClosureExpr() and
300295
exists(kind)
301296
}
302297

@@ -664,12 +659,19 @@ module RustDataFlow implements InputSig<Location> {
664659
)
665660
}
666661

662+
pragma[nomagic]
663+
private TupleField getCallExprTupleField(CallExpr call, int pos) {
664+
result = call.(TupleStructExpr).getStruct().getTupleField(pos)
665+
or
666+
result = call.(TupleVariantExpr).getVariant().getTupleField(pos)
667+
}
668+
667669
pragma[nomagic]
668670
additional predicate storeContentStep(Node node1, Content c, Node node2) {
669671
exists(CallExpr call, int pos |
670-
node1.asExpr() = call.getArg(pragma[only_bind_into](pos)) and
672+
node1.asExpr() = call.getArgument(pragma[only_bind_into](pos)) and
671673
node2.asExpr() = call and
672-
c = TTupleFieldContent(call.getTupleField(pragma[only_bind_into](pos)))
674+
c = TTupleFieldContent(getCallExprTupleField(call, pragma[only_bind_into](pos)))
673675
)
674676
or
675677
exists(StructExpr re, string field |
@@ -824,11 +826,7 @@ module RustDataFlow implements InputSig<Location> {
824826
*/
825827
predicate lambdaCall(DataFlowCall call, LambdaCallKind kind, Node receiver) {
826828
(
827-
receiver.asExpr() = call.asCall().(CallExpr).getFunction() and
828-
// All calls to complex expressions and local variable accesses are lambda call.
829-
exists(Expr f | f = receiver.asExpr() |
830-
f instanceof PathExpr implies f = any(Variable v).getAnAccess()
831-
)
829+
receiver.asExpr() = call.asCall().(ClosureCallExpr).getClosureExpr()
832830
or
833831
call.isSummaryCall(_, receiver.(FlowSummaryNode).getSummaryNode())
834832
) and
@@ -992,9 +990,7 @@ private module Cached {
992990
newtype TDataFlowCall =
993991
TCall(Call call) {
994992
Stages::DataFlowStage::ref() and
995-
call.hasEnclosingCfgScope() and
996-
// TODO: Handle index expressions as calls in data flow.
997-
not call instanceof IndexExpr
993+
call.hasEnclosingCfgScope()
998994
} or
999995
TSummaryCall(
1000996
FlowSummaryImpl::Public::SummarizedCallable c, FlowSummaryImpl::Private::SummaryNode receiver

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ private import codeql.rust.dataflow.Ssa
1212
private import Content
1313

1414
module Input implements InputSig<Location, RustDataFlow> {
15-
private import codeql.rust.elements.internal.CallExprBaseImpl::Impl as CallExprBaseImpl
1615
private import codeql.rust.frameworks.stdlib.Stdlib
1716

1817
class SummarizedCallableBase = Function;
1918

2019
abstract private class SourceSinkBase extends AstNode {
2120
/** Gets the associated call. */
22-
abstract CallExprBase getCall();
21+
abstract ArgsExpr getCall();
2322

2423
/** Holds if the associated call resolves to `path`. */
2524
final predicate callResolvesTo(string path) {
26-
path = this.getCall().getStaticTarget().(Addressable).getCanonicalPath()
25+
path = this.getCall().getResolvedTarget().getCanonicalPath()
2726
}
2827
}
2928

@@ -44,7 +43,7 @@ module Input implements InputSig<Location, RustDataFlow> {
4443

4544
MethodCallExprNameRef() { this = call.getIdentifier() }
4645

47-
override MethodCallExpr getCall() { result = call }
46+
override ArgsExpr getCall() { result = call }
4847
}
4948

5049
RustDataFlow::ArgumentPosition callbackSelfParameterPosition() { result.isClosureSelf() }
@@ -53,9 +52,7 @@ module Input implements InputSig<Location, RustDataFlow> {
5352

5453
string encodeParameterPosition(ParameterPosition pos) { result = pos.toString() }
5554

56-
string encodeArgumentPosition(RustDataFlow::ArgumentPosition pos) {
57-
result = encodeParameterPosition(pos)
58-
}
55+
string encodeArgumentPosition(RustDataFlow::ArgumentPosition pos) { result = pos.toString() }
5956

6057
string encodeContent(ContentSet cs, string arg) {
6158
exists(Content c | cs = TSingletonContentSet(c) |
@@ -173,7 +170,7 @@ private module StepsInput implements Impl::Private::StepsInputSig {
173170
}
174171

175172
RustDataFlow::Node getSinkNode(Input::SinkBase sink, Impl::Private::SummaryComponent sc) {
176-
exists(CallExprBase call, Expr arg, ArgumentPosition pos |
173+
exists(ArgsExpr call, Expr arg, ArgumentPosition pos |
177174
result.asExpr() = arg and
178175
sc = Impl::Private::SummaryComponent::argument(pos) and
179176
call = sink.getCall() and

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ private import rust
4747
private import codeql.rust.dataflow.FlowSummary
4848
private import codeql.rust.dataflow.FlowSource
4949
private import codeql.rust.dataflow.FlowSink
50-
private import codeql.rust.elements.internal.CallExprBaseImpl::Impl as CallExprBaseImpl
5150

5251
/**
5352
* Holds if in a call to the function with canonical path `path`, the value referred

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,12 @@ final class SummaryArgumentNode extends FlowSummaryNode, ArgumentNode {
294294
* passed into the closure body at an invocation.
295295
*/
296296
final class ClosureArgumentNode extends ArgumentNode, ExprNode {
297-
private CallExpr call_;
297+
private Call call_;
298298

299299
ClosureArgumentNode() { lambdaCallExpr(call_, _, this.asExpr()) }
300300

301301
override predicate isArgumentOf(DataFlowCall call, RustDataFlow::ArgumentPosition pos) {
302-
call.asCall() = call_ and
303-
pos.isClosureSelf()
302+
call.asCall() = call_ and pos.isClosureSelf()
304303
}
305304
}
306305

@@ -527,7 +526,6 @@ newtype TNode =
527526
or
528527
e =
529528
[
530-
any(IndexExpr i).getBase(), //
531529
any(FieldExpr access).getContainer(), //
532530
any(TryExpr try).getExpr(), //
533531
any(AwaitExpr a).getExpr(), //

0 commit comments

Comments
 (0)