Skip to content

Commit b55d5e0

Browse files
committed
wip
1 parent 7e8241d commit b55d5e0

File tree

10 files changed

+43
-56
lines changed

10 files changed

+43
-56
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ final class CallCfgNode extends ExprCfgNode {
232232

233233
/** Gets the receiver of this call if it is a method call. */
234234
ExprCfgNode getReceiver() {
235-
any(ChildMapping mapping).hasCfgChild(node, node.getReceiver(), this, result)
235+
any(ChildMapping mapping).hasCfgChild(node, node.(MethodCall).getReceiver(), this, result)
236236
}
237237

238238
/** Gets the `i`th argument of this call, if any. */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ final class ArgumentPosition extends ParameterPosition {
133133
Expr getArgument(Call call) {
134134
result = call.getPositionalArgument(this.getPosition())
135135
or
136-
result = call.getReceiver() and this.isSelf()
136+
result = call.(MethodCall).getReceiver() and this.isSelf()
137137
}
138138
}
139139

rust/ql/lib/codeql/rust/elements/Call.qll

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,7 @@ private import internal.CallExprImpl::Impl as CallExprImpl
88

99
final class Call = Impl::Call;
1010

11-
private predicate isGuaranteedMethodCall(ArgsExpr call) {
12-
call instanceof MethodCallExpr
13-
or
14-
call.(Operation).isOverloaded(_, _, _)
15-
or
16-
call instanceof IndexExpr
17-
}
18-
19-
/**
20-
* A call expression that targets a method.
21-
*
22-
* Either
23-
*
24-
* - a `CallExpr` where we can resolve the target as a method,
25-
* - a `MethodCallExpr`,
26-
* - an `Operation` that targets an overloadable operator, or
27-
* - an `IndexExpr`.
28-
*/
29-
final class MethodCall extends Call {
30-
MethodCall() {
31-
this.getStaticTarget() instanceof Method
32-
or
33-
isGuaranteedMethodCall(this)
34-
}
35-
36-
/** Gets the static target of this method call, if any. */
37-
Method getStaticTarget() { result = super.getStaticTarget() }
38-
}
11+
final class MethodCall = Impl::MethodCall;
3912

4013
/**
4114
* A call expression that targets a closure.

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ module Impl {
7272
)
7373
}
7474

75-
private predicate isMethodCall() { this.getResolvedTarget() instanceof Method }
75+
override Expr getPositionalArgument(int i) { result = super.getSyntacticArgument(i) }
76+
}
7677

77-
override Expr getPositionalArgument(int i) {
78-
if this.isMethodCall()
79-
then result = this.getSyntacticArgument(i + 1)
80-
else result = super.getSyntacticArgument(i)
81-
}
78+
class CallExprMethodCall extends CallExprCall, CallImpl::MethodCall {
79+
CallExprMethodCall() { this.getResolvedTarget() instanceof Method }
80+
81+
override Expr getPositionalArgument(int i) { result = this.getSyntacticArgument(i + 1) }
8282

83-
override Expr getReceiver() { this.isMethodCall() and result = super.getSyntacticArgument(0) }
83+
override Expr getReceiver() { result = super.getSyntacticArgument(0) }
8484
}
8585

8686
/**

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@ module Impl {
1616
* - an `IndexExpr`.
1717
*/
1818
abstract class Call extends ArgsExprImpl::ArgsExpr {
19-
/**
20-
* Gets the receiver of this call, if any.
21-
*
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-
* ```
29-
*/
30-
Expr getReceiver() { none() }
31-
3219
/**
3320
* Gets the `i`th positional argument of this call, if any.
3421
*
@@ -69,4 +56,31 @@ module Impl {
6956
)
7057
}
7158
}
59+
60+
/**
61+
* A method call.
62+
*
63+
* Either
64+
*
65+
* - a `CallExpr` where we can resolve the target as a method,
66+
* - a `MethodCallExpr`,
67+
* - an `Operation` that targets an overloadable operator, or
68+
* - an `IndexExpr`.
69+
*/
70+
abstract class MethodCall extends Call {
71+
/**
72+
* Gets the receiver of this method call.
73+
*
74+
* Examples:
75+
* ```rust
76+
* foo(42, "bar"); // no receiver
77+
* foo.bar(42); // `foo` is receiver
78+
* x + y; // `x` is receiver
79+
* x[y]; // `x` is receiver
80+
* ```
81+
*/
82+
Expr getReceiver() { none() }
83+
84+
override Method getStaticTarget() { result = super.getStaticTarget() }
85+
}
7286
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Impl {
2222
* list[42] = 1;
2323
* ```
2424
*/
25-
class IndexExpr extends Generated::IndexExpr, CallImpl::Call {
25+
class IndexExpr extends Generated::IndexExpr, CallImpl::MethodCall {
2626
override string toStringImpl() {
2727
result =
2828
this.getBase().toAbbreviatedString() + "[" + this.getIndex().toAbbreviatedString() + "]"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module Impl {
2727
* x.foo::<u32, u64>(42);
2828
* ```
2929
*/
30-
class MethodCallExpr extends Generated::MethodCallExpr, CallImpl::Call {
30+
class MethodCallExpr extends Generated::MethodCallExpr, CallImpl::MethodCall {
3131
private string toStringPart(int index) {
3232
index = 0 and
3333
result = this.getReceiver().toAbbreviatedString()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ module Impl {
137137
}
138138
}
139139

140-
private class CallOperation extends CallImpl::Call instanceof Operation {
141-
CallOperation() { super.isOverloaded(_, _, _) }
140+
private class OperationMethodCall extends CallImpl::MethodCall instanceof Operation {
141+
OperationMethodCall() { super.isOverloaded(_, _, _) }
142142

143143
override Expr getPositionalArgument(int i) { result = super.getOperand(i + 1) and i >= 0 }
144144

rust/ql/lib/codeql/rust/frameworks/rustcrypto/RustCrypto.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class StreamCipherInit extends Cryptography::CryptographicOperation::Range {
3030
["new", "new_from_slice", "new_with_eff_key_len", "new_from_slices"] and
3131
// extract the algorithm name from the type of `ce` or its receiver.
3232
exists(Type t, TypePath tp |
33-
t = inferType([call, call.getReceiver()], tp) and
33+
t = inferType([call, call.(MethodCall).getReceiver()], tp) and
3434
rawAlgorithmName = t.(StructType).getStruct().(Addressable).getCanonicalPath().splitAt("::")
3535
) and
3636
algorithmName = simplifyAlgorithmName(rawAlgorithmName) and

rust/ql/lib/codeql/rust/security/XssExtensions.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module Xss {
5555
HeuristicHtmlEncodingBarrier() {
5656
exists(Call fc |
5757
fc.getStaticTarget().getName().getText().regexpMatch(".*(escape|encode).*") and
58-
fc.getArgument(_) = this.asExpr()
58+
fc.getAPositionalArgument() = this.asExpr()
5959
)
6060
}
6161
}

0 commit comments

Comments
 (0)