Skip to content

Commit 3f19279

Browse files
committed
Rust: Adjust argument position when call expression is for method
1 parent a1069b1 commit 3f19279

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

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

+23-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import codeql.dataflow.internal.DataFlowImpl
99
private import rust
1010
private import SsaImpl as SsaImpl
1111
private import codeql.rust.controlflow.internal.Scope as Scope
12+
private import codeql.rust.elements.internal.PathResolution
1213
private import codeql.rust.controlflow.ControlFlowGraph
1314
private import codeql.rust.controlflow.CfgNodes
1415
private import codeql.rust.dataflow.Ssa
@@ -120,12 +121,30 @@ final class ParameterPosition extends TParameterPosition {
120121
}
121122
}
122123

124+
/** Holds if `call` invokes a qualified path that resolves to a method. */
125+
private predicate callToMethod(CallExpr call) {
126+
exists(Path path |
127+
path = call.getFunction().(PathExpr).getPath() and
128+
path.hasQualifier() and
129+
resolvePath(path).(Function).getParamList().hasSelfParam()
130+
)
131+
}
132+
123133
/** Holds if `arg` is an argument of `call` at the position `pos`. */
124134
private predicate isArgumentForCall(ExprCfgNode arg, CallExprBaseCfgNode call, ParameterPosition pos) {
125-
arg = call.getArgument(pos.getPosition())
126-
or
127-
// The self argument in a method call.
128-
arg = call.(MethodCallExprCfgNode).getReceiver() and pos.isSelf()
135+
if callToMethod(call.(CallExprCfgNode).getCallExpr())
136+
then (
137+
// The first argument is for the `self` parameter
138+
arg = call.getArgument(0) and pos.isSelf()
139+
or
140+
// Succeeding arguments are shifted left
141+
arg = call.getArgument(pos.getPosition() + 1)
142+
) else (
143+
// The self argument in a method call.
144+
arg = call.(MethodCallExprCfgNode).getReceiver() and pos.isSelf()
145+
or
146+
arg = call.getArgument(pos.getPosition())
147+
)
129148
}
130149

131150
/**

rust/ql/test/library-tests/dataflow/global/inline-flow.expected

+42
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ edges
5151
| main.rs:134:24:134:33 | source(...) | main.rs:134:13:134:34 | ...::new(...) [MyInt] | provenance | |
5252
| main.rs:135:9:135:26 | MyInt {...} [MyInt] | main.rs:135:24:135:24 | m | provenance | |
5353
| main.rs:135:24:135:24 | m | main.rs:136:10:136:10 | m | provenance | |
54+
| main.rs:175:18:175:21 | SelfParam [MyInt] | main.rs:175:48:177:5 | { ... } [MyInt] | provenance | |
55+
| main.rs:179:26:179:37 | ...: MyInt [MyInt] | main.rs:179:49:181:5 | { ... } [MyInt] | provenance | |
56+
| main.rs:185:9:185:9 | a [MyInt] | main.rs:187:49:187:49 | a [MyInt] | provenance | |
57+
| main.rs:185:13:185:38 | MyInt {...} [MyInt] | main.rs:185:9:185:9 | a [MyInt] | provenance | |
58+
| main.rs:185:28:185:36 | source(...) | main.rs:185:13:185:38 | MyInt {...} [MyInt] | provenance | |
59+
| main.rs:187:9:187:26 | MyInt {...} [MyInt] | main.rs:187:24:187:24 | c | provenance | |
60+
| main.rs:187:24:187:24 | c | main.rs:188:10:188:10 | c | provenance | |
61+
| main.rs:187:30:187:53 | ...::take_self(...) [MyInt] | main.rs:187:9:187:26 | MyInt {...} [MyInt] | provenance | |
62+
| main.rs:187:49:187:49 | a [MyInt] | main.rs:175:18:175:21 | SelfParam [MyInt] | provenance | |
63+
| main.rs:187:49:187:49 | a [MyInt] | main.rs:187:30:187:53 | ...::take_self(...) [MyInt] | provenance | |
64+
| main.rs:191:9:191:9 | b [MyInt] | main.rs:192:54:192:54 | b [MyInt] | provenance | |
65+
| main.rs:191:13:191:39 | MyInt {...} [MyInt] | main.rs:191:9:191:9 | b [MyInt] | provenance | |
66+
| main.rs:191:28:191:37 | source(...) | main.rs:191:13:191:39 | MyInt {...} [MyInt] | provenance | |
67+
| main.rs:192:9:192:26 | MyInt {...} [MyInt] | main.rs:192:24:192:24 | c | provenance | |
68+
| main.rs:192:24:192:24 | c | main.rs:193:10:193:10 | c | provenance | |
69+
| main.rs:192:30:192:55 | ...::take_second(...) [MyInt] | main.rs:192:9:192:26 | MyInt {...} [MyInt] | provenance | |
70+
| main.rs:192:54:192:54 | b [MyInt] | main.rs:179:26:179:37 | ...: MyInt [MyInt] | provenance | |
71+
| main.rs:192:54:192:54 | b [MyInt] | main.rs:192:30:192:55 | ...::take_second(...) [MyInt] | provenance | |
5472
| main.rs:202:9:202:9 | a | main.rs:203:10:203:10 | a | provenance | |
5573
| main.rs:202:13:202:21 | source(...) | main.rs:202:9:202:9 | a | provenance | |
5674
| main.rs:212:13:212:13 | c | main.rs:213:14:213:14 | c | provenance | |
@@ -122,6 +140,26 @@ nodes
122140
| main.rs:135:9:135:26 | MyInt {...} [MyInt] | semmle.label | MyInt {...} [MyInt] |
123141
| main.rs:135:24:135:24 | m | semmle.label | m |
124142
| main.rs:136:10:136:10 | m | semmle.label | m |
143+
| main.rs:175:18:175:21 | SelfParam [MyInt] | semmle.label | SelfParam [MyInt] |
144+
| main.rs:175:48:177:5 | { ... } [MyInt] | semmle.label | { ... } [MyInt] |
145+
| main.rs:179:26:179:37 | ...: MyInt [MyInt] | semmle.label | ...: MyInt [MyInt] |
146+
| main.rs:179:49:181:5 | { ... } [MyInt] | semmle.label | { ... } [MyInt] |
147+
| main.rs:185:9:185:9 | a [MyInt] | semmle.label | a [MyInt] |
148+
| main.rs:185:13:185:38 | MyInt {...} [MyInt] | semmle.label | MyInt {...} [MyInt] |
149+
| main.rs:185:28:185:36 | source(...) | semmle.label | source(...) |
150+
| main.rs:187:9:187:26 | MyInt {...} [MyInt] | semmle.label | MyInt {...} [MyInt] |
151+
| main.rs:187:24:187:24 | c | semmle.label | c |
152+
| main.rs:187:30:187:53 | ...::take_self(...) [MyInt] | semmle.label | ...::take_self(...) [MyInt] |
153+
| main.rs:187:49:187:49 | a [MyInt] | semmle.label | a [MyInt] |
154+
| main.rs:188:10:188:10 | c | semmle.label | c |
155+
| main.rs:191:9:191:9 | b [MyInt] | semmle.label | b [MyInt] |
156+
| main.rs:191:13:191:39 | MyInt {...} [MyInt] | semmle.label | MyInt {...} [MyInt] |
157+
| main.rs:191:28:191:37 | source(...) | semmle.label | source(...) |
158+
| main.rs:192:9:192:26 | MyInt {...} [MyInt] | semmle.label | MyInt {...} [MyInt] |
159+
| main.rs:192:24:192:24 | c | semmle.label | c |
160+
| main.rs:192:30:192:55 | ...::take_second(...) [MyInt] | semmle.label | ...::take_second(...) [MyInt] |
161+
| main.rs:192:54:192:54 | b [MyInt] | semmle.label | b [MyInt] |
162+
| main.rs:193:10:193:10 | c | semmle.label | c |
125163
| main.rs:202:9:202:9 | a | semmle.label | a |
126164
| main.rs:202:13:202:21 | source(...) | semmle.label | source(...) |
127165
| main.rs:203:10:203:10 | a | semmle.label | a |
@@ -142,6 +180,8 @@ subpaths
142180
| main.rs:55:26:55:26 | a | main.rs:51:21:51:26 | ...: i64 | main.rs:51:36:53:5 | { ... } | main.rs:55:13:55:27 | pass_through(...) |
143181
| main.rs:103:29:103:29 | a | main.rs:79:28:79:33 | ...: i64 | main.rs:79:43:85:5 | { ... } | main.rs:103:13:103:30 | mn.data_through(...) |
144182
| main.rs:134:24:134:33 | source(...) | main.rs:128:12:128:17 | ...: i64 | main.rs:128:28:130:5 | { ... } [MyInt] | main.rs:134:13:134:34 | ...::new(...) [MyInt] |
183+
| main.rs:187:49:187:49 | a [MyInt] | main.rs:175:18:175:21 | SelfParam [MyInt] | main.rs:175:48:177:5 | { ... } [MyInt] | main.rs:187:30:187:53 | ...::take_self(...) [MyInt] |
184+
| main.rs:192:54:192:54 | b [MyInt] | main.rs:179:26:179:37 | ...: MyInt [MyInt] | main.rs:179:49:181:5 | { ... } [MyInt] | main.rs:192:30:192:55 | ...::take_second(...) [MyInt] |
145185
| main.rs:237:16:237:25 | source(...) | main.rs:228:25:228:30 | ...: i64 | main.rs:228:12:228:22 | ...: ... [Return] [&ref] | main.rs:237:13:237:13 | [post] m [&ref] |
146186
testFailures
147187
#select
@@ -154,6 +194,8 @@ testFailures
154194
| main.rs:91:10:91:10 | a | main.rs:75:13:75:21 | source(...) | main.rs:91:10:91:10 | a | $@ | main.rs:75:13:75:21 | source(...) | source(...) |
155195
| main.rs:104:10:104:10 | b | main.rs:102:13:102:21 | source(...) | main.rs:104:10:104:10 | b | $@ | main.rs:102:13:102:21 | source(...) | source(...) |
156196
| main.rs:136:10:136:10 | m | main.rs:134:24:134:33 | source(...) | main.rs:136:10:136:10 | m | $@ | main.rs:134:24:134:33 | source(...) | source(...) |
197+
| main.rs:188:10:188:10 | c | main.rs:185:28:185:36 | source(...) | main.rs:188:10:188:10 | c | $@ | main.rs:185:28:185:36 | source(...) | source(...) |
198+
| main.rs:193:10:193:10 | c | main.rs:191:28:191:37 | source(...) | main.rs:193:10:193:10 | c | $@ | main.rs:191:28:191:37 | source(...) | source(...) |
157199
| main.rs:203:10:203:10 | a | main.rs:202:13:202:21 | source(...) | main.rs:203:10:203:10 | a | $@ | main.rs:202:13:202:21 | source(...) | source(...) |
158200
| main.rs:213:14:213:14 | c | main.rs:212:17:212:25 | source(...) | main.rs:213:14:213:14 | c | $@ | main.rs:212:17:212:25 | source(...) | source(...) |
159201
| main.rs:238:10:238:11 | * ... | main.rs:237:16:237:25 | source(...) | main.rs:238:10:238:11 | * ... | $@ | main.rs:237:16:237:25 | source(...) | source(...) |

rust/ql/test/library-tests/dataflow/global/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ fn data_through_trait_method_called_as_function() {
185185
let a = MyInt { value: source(8) };
186186
let b = MyInt { value: 2 };
187187
let MyInt { value: c } = MyTrait::take_self(a, b);
188-
sink(c); // $ MISSING: hasValueFlow=8
188+
sink(c); // $ hasValueFlow=8
189189

190190
let a = MyInt { value: 0 };
191191
let b = MyInt { value: source(37) };
192192
let MyInt { value: c } = MyTrait::take_second(a, b);
193-
sink(c); // $ MISSING: hasValueFlow=37
193+
sink(c); // $ hasValueFlow=37
194194

195195
let a = MyInt { value: 0 };
196196
let b = MyInt { value: source(38) };

0 commit comments

Comments
 (0)