Skip to content

Commit eea12e2

Browse files
committed
Rust: Refactor classes representing calls
1 parent 21e11ba commit eea12e2

File tree

63 files changed

+1018
-675
lines changed

Some content is hidden

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

63 files changed

+1018
-675
lines changed

rust/ql/.generated.list

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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 ParenArgsExprCfgNode extends Nodes::ParenArgsExprCfgNode {
255+
private ParenArgsExprChildMapping node;
256+
257+
ParenArgsExprCfgNode() { 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 ParenArgsExprChildMapping extends ParentAstNode, ParenArgsExpr {
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ module ExprTrees {
292292
}
293293
}
294294

295-
class CallExprTree extends StandardPostOrderTree instanceof CallExpr {
295+
class ParenArgsExprTree extends StandardPostOrderTree instanceof ParenArgsExpr {
296296
override AstNode getChildNode(int i) {
297-
i = 0 and result = super.getFunction()
297+
i = 0 and result = super.getBase()
298298
or
299299
result = super.getArgList().getArg(i - 1)
300300
}
@@ -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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ newtype TContent =
270270
} or
271271
TFunctionCallReturnContent() or
272272
TFunctionCallArgumentContent(int pos) {
273-
pos in [0 .. any(CallExpr c).getArgList().getNumberOfArgs() - 1]
273+
pos in [0 .. any(ParenArgsExpr c).getNumberOfArguments() - 1]
274274
} or
275275
TCapturedVariableContent(VariableCapture::CapturedVariable v) or
276276
TReferenceContent()

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

Lines changed: 9 additions & 20 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(ClosureCall call, LambdaCallKind kind, Expr receiver) {
294+
receiver = call.getClosureExpr() and
300295
exists(kind)
301296
}
302297

@@ -666,8 +661,8 @@ module RustDataFlow implements InputSig<Location> {
666661

667662
pragma[nomagic]
668663
additional predicate storeContentStep(Node node1, Content c, Node node2) {
669-
exists(CallExpr call, int pos |
670-
node1.asExpr() = call.getArg(pragma[only_bind_into](pos)) and
664+
exists(ParenArgsExpr call, int pos |
665+
node1.asExpr() = call.getArgument(pragma[only_bind_into](pos)) and
671666
node2.asExpr() = call and
672667
c = TTupleFieldContent(call.getTupleField(pragma[only_bind_into](pos)))
673668
)
@@ -818,7 +813,7 @@ module RustDataFlow implements InputSig<Location> {
818813
// pointer. Except if the path occurs directly in a call, then it's just a
819814
// call to the function and not a function being passed as data.
820815
resolvePath(e.(PathExpr).getPath()) = c.asCfgScope() and
821-
not any(CallExpr call).getFunction() = e
816+
not any(ParenArgsExpr call).getBase() = e
822817
)
823818
}
824819

@@ -828,11 +823,7 @@ module RustDataFlow implements InputSig<Location> {
828823
*/
829824
predicate lambdaCall(DataFlowCall call, LambdaCallKind kind, Node receiver) {
830825
(
831-
receiver.asExpr() = call.asCall().(CallExpr).getFunction() and
832-
// All calls to complex expressions and local variable accesses are lambda call.
833-
exists(Expr f | f = receiver.asExpr() |
834-
f instanceof PathExpr implies f = any(Variable v).getAnAccess()
835-
)
826+
receiver.asExpr() = call.asCall().(ClosureCall).getClosureExpr()
836827
or
837828
call.isSummaryCall(_, receiver.(FlowSummaryNode).getSummaryNode())
838829
) and
@@ -996,9 +987,7 @@ private module Cached {
996987
newtype TDataFlowCall =
997988
TCall(Call call) {
998989
Stages::DataFlowStage::ref() and
999-
call.hasEnclosingCfgScope() and
1000-
// TODO: Handle index expressions as calls in data flow.
1001-
not call instanceof IndexExpr
990+
call.hasEnclosingCfgScope()
1002991
} or
1003992
TSummaryCall(
1004993
FlowSummaryImpl::Public::SummarizedCallable c, FlowSummaryImpl::Private::SummaryNode receiver

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

Lines changed: 8 additions & 11 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

@@ -32,19 +31,19 @@ module Input implements InputSig<Location, RustDataFlow> {
3231
abstract class SinkBase extends SourceSinkBase { }
3332

3433
private class CallExprFunction extends SourceBase, SinkBase {
35-
private CallExpr call;
34+
private ParenArgsExpr call;
3635

37-
CallExprFunction() { this = call.getFunction() }
36+
CallExprFunction() { this = call.getBase() }
3837

39-
override CallExpr getCall() { result = call }
38+
override ParenArgsExpr getCall() { result = call }
4039
}
4140

4241
private class MethodCallExprNameRef extends SourceBase, SinkBase {
4342
private MethodCallExpr call;
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

0 commit comments

Comments
 (0)