Skip to content

Commit 217d9e3

Browse files
committed
wip2
1 parent 8f2d27f commit 217d9e3

File tree

4 files changed

+87
-9
lines changed

4 files changed

+87
-9
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,21 @@ module Impl {
7474
}
7575

7676
/** Holds if the call expression dispatches to a method. */
77-
private predicate callIsMethodCall(CallExpr call, Path qualifier, string methodName) {
77+
private predicate callIsMethodCall(CallExpr call, Path qualifier, string methodName, boolean isRef) {
7878
exists(Path path, Function f |
7979
path = call.getFunction().(PathExpr).getPath() and
8080
f = resolvePath(path) and
81-
f.getParamList().hasSelfParam() and
8281
qualifier = path.getQualifier() and
83-
path.getSegment().getIdentifier().getText() = methodName
82+
path.getSegment().getIdentifier().getText() = methodName and
83+
exists(SelfParam self |
84+
self = f.getParamList().getSelfParam() and
85+
if self.isRef() then isRef = true else isRef = false
86+
)
8487
)
8588
}
8689

8790
private class CallExprCall extends Call instanceof CallExpr {
88-
CallExprCall() { not callIsMethodCall(this, _, _) }
91+
CallExprCall() { not callIsMethodCall(this, _, _, _) }
8992

9093
override string getMethodName() { none() }
9194

@@ -98,11 +101,19 @@ module Impl {
98101
}
99102
}
100103

101-
private class CallExprMethodCall extends Call instanceof CallExpr {
104+
class CallExprMethodCall extends Call instanceof CallExpr {
102105
Path qualifier;
103106
string methodName;
107+
boolean isRef;
108+
109+
CallExprMethodCall() { callIsMethodCall(this, qualifier, methodName, isRef) }
104110

105-
CallExprMethodCall() { callIsMethodCall(this, qualifier, methodName) }
111+
/**
112+
* Holds if this call has an explicit borrow for the `self` argument,
113+
* which is needed for `&self` parameters when method are called using
114+
* function call syntax.
115+
*/
116+
predicate hasExplicitSelfBorrow() { isRef = true }
106117

107118
override string getMethodName() { result = methodName }
108119

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,14 @@ final class MethodCall extends Call {
12651265
t0.(StructType).asItemNode() instanceof StringStruct and
12661266
result.(StructType).asItemNode() instanceof Builtins::Str
12671267
)
1268-
else result = this.getReceiverTypeAt(path)
1268+
else
1269+
if this.(CallImpl::CallExprMethodCall).hasExplicitSelfBorrow()
1270+
then
1271+
exists(TypePath path0 |
1272+
result = this.getReceiverTypeAt(path0) and
1273+
path0.isCons(TRefTypeParameter(), path)
1274+
)
1275+
else result = this.getReceiverTypeAt(path)
12691276
}
12701277
}
12711278

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ edges
112112
| main.rs:222:9:222:35 | MyInt {...} [MyInt] | main.rs:220:42:223:5 | { ... } [MyInt] | provenance | |
113113
| main.rs:222:24:222:27 | self [MyInt] | main.rs:222:24:222:33 | self.value | provenance | |
114114
| main.rs:222:24:222:33 | self.value | main.rs:222:9:222:35 | MyInt {...} [MyInt] | provenance | |
115+
| main.rs:227:30:227:39 | ...: MyInt [MyInt] | main.rs:228:25:228:27 | rhs [MyInt] | provenance | |
116+
| main.rs:228:10:228:14 | [post] * ... [MyInt] | main.rs:228:11:228:14 | [post] self [&ref, MyInt] | provenance | |
117+
| main.rs:228:11:228:14 | [post] self [&ref, MyInt] | main.rs:227:19:227:27 | SelfParam [Return] [&ref, MyInt] | provenance | |
118+
| main.rs:228:25:228:27 | rhs [MyInt] | main.rs:228:25:228:33 | rhs.value | provenance | |
119+
| main.rs:228:25:228:33 | rhs.value | main.rs:228:10:228:14 | [post] * ... [MyInt] | provenance | |
120+
| main.rs:235:14:235:18 | SelfParam [&ref, MyInt] | main.rs:236:12:236:15 | self [&ref, MyInt] | provenance | |
121+
| main.rs:236:9:236:22 | &... [&ref] | main.rs:235:38:237:5 | { ... } [&ref] | provenance | |
122+
| main.rs:236:10:236:22 | ... .value | main.rs:236:9:236:22 | &... [&ref] | provenance | |
123+
| main.rs:236:11:236:15 | * ... [MyInt] | main.rs:236:10:236:22 | ... .value | provenance | |
124+
| main.rs:236:12:236:15 | self [&ref, MyInt] | main.rs:236:11:236:15 | * ... [MyInt] | provenance | |
115125
| main.rs:242:9:242:9 | a [MyInt] | main.rs:244:13:244:13 | a [MyInt] | provenance | |
116126
| main.rs:242:13:242:38 | MyInt {...} [MyInt] | main.rs:242:9:242:9 | a [MyInt] | provenance | |
117127
| main.rs:242:28:242:36 | source(...) | main.rs:242:13:242:38 | MyInt {...} [MyInt] | provenance | |
@@ -127,6 +137,23 @@ edges
127137
| main.rs:254:9:254:9 | d [MyInt] | main.rs:255:10:255:10 | d [MyInt] | provenance | |
128138
| main.rs:254:13:254:20 | a.add(...) [MyInt] | main.rs:254:9:254:9 | d [MyInt] | provenance | |
129139
| main.rs:255:10:255:10 | d [MyInt] | main.rs:255:10:255:16 | d.value | provenance | |
140+
| main.rs:259:9:259:9 | b [MyInt] | main.rs:261:35:261:35 | b [MyInt] | provenance | |
141+
| main.rs:259:13:259:39 | MyInt {...} [MyInt] | main.rs:259:9:259:9 | b [MyInt] | provenance | |
142+
| main.rs:259:28:259:37 | source(...) | main.rs:259:13:259:39 | MyInt {...} [MyInt] | provenance | |
143+
| main.rs:261:27:261:32 | [post] &mut a [&ref, MyInt] | main.rs:261:32:261:32 | [post] a [MyInt] | provenance | |
144+
| main.rs:261:32:261:32 | [post] a [MyInt] | main.rs:262:10:262:10 | a [MyInt] | provenance | |
145+
| main.rs:261:35:261:35 | b [MyInt] | main.rs:227:30:227:39 | ...: MyInt [MyInt] | provenance | |
146+
| main.rs:261:35:261:35 | b [MyInt] | main.rs:261:27:261:32 | [post] &mut a [&ref, MyInt] | provenance | |
147+
| main.rs:262:10:262:10 | a [MyInt] | main.rs:262:10:262:16 | a.value | provenance | |
148+
| main.rs:270:9:270:9 | a [MyInt] | main.rs:272:28:272:28 | a [MyInt] | provenance | |
149+
| main.rs:270:13:270:39 | MyInt {...} [MyInt] | main.rs:270:9:270:9 | a [MyInt] | provenance | |
150+
| main.rs:270:28:270:37 | source(...) | main.rs:270:13:270:39 | MyInt {...} [MyInt] | provenance | |
151+
| main.rs:272:9:272:9 | c | main.rs:273:10:273:10 | c | provenance | |
152+
| main.rs:272:13:272:29 | * ... | main.rs:272:9:272:9 | c | provenance | |
153+
| main.rs:272:14:272:29 | ...::deref(...) [&ref] | main.rs:272:13:272:29 | * ... | provenance | |
154+
| main.rs:272:27:272:28 | &a [&ref, MyInt] | main.rs:235:14:235:18 | SelfParam [&ref, MyInt] | provenance | |
155+
| main.rs:272:27:272:28 | &a [&ref, MyInt] | main.rs:272:14:272:29 | ...::deref(...) [&ref] | provenance | |
156+
| main.rs:272:28:272:28 | a [MyInt] | main.rs:272:27:272:28 | &a [&ref, MyInt] | provenance | |
130157
| main.rs:289:18:289:21 | SelfParam [MyInt] | main.rs:289:48:291:5 | { ... } [MyInt] | provenance | |
131158
| main.rs:293:26:293:37 | ...: MyInt [MyInt] | main.rs:293:49:295:5 | { ... } [MyInt] | provenance | |
132159
| main.rs:299:9:299:9 | a [MyInt] | main.rs:301:50:301:50 | a [MyInt] | provenance | |
@@ -278,6 +305,18 @@ nodes
278305
| main.rs:222:9:222:35 | MyInt {...} [MyInt] | semmle.label | MyInt {...} [MyInt] |
279306
| main.rs:222:24:222:27 | self [MyInt] | semmle.label | self [MyInt] |
280307
| main.rs:222:24:222:33 | self.value | semmle.label | self.value |
308+
| main.rs:227:19:227:27 | SelfParam [Return] [&ref, MyInt] | semmle.label | SelfParam [Return] [&ref, MyInt] |
309+
| main.rs:227:30:227:39 | ...: MyInt [MyInt] | semmle.label | ...: MyInt [MyInt] |
310+
| main.rs:228:10:228:14 | [post] * ... [MyInt] | semmle.label | [post] * ... [MyInt] |
311+
| main.rs:228:11:228:14 | [post] self [&ref, MyInt] | semmle.label | [post] self [&ref, MyInt] |
312+
| main.rs:228:25:228:27 | rhs [MyInt] | semmle.label | rhs [MyInt] |
313+
| main.rs:228:25:228:33 | rhs.value | semmle.label | rhs.value |
314+
| main.rs:235:14:235:18 | SelfParam [&ref, MyInt] | semmle.label | SelfParam [&ref, MyInt] |
315+
| main.rs:235:38:237:5 | { ... } [&ref] | semmle.label | { ... } [&ref] |
316+
| main.rs:236:9:236:22 | &... [&ref] | semmle.label | &... [&ref] |
317+
| main.rs:236:10:236:22 | ... .value | semmle.label | ... .value |
318+
| main.rs:236:11:236:15 | * ... [MyInt] | semmle.label | * ... [MyInt] |
319+
| main.rs:236:12:236:15 | self [&ref, MyInt] | semmle.label | self [&ref, MyInt] |
281320
| main.rs:242:9:242:9 | a [MyInt] | semmle.label | a [MyInt] |
282321
| main.rs:242:13:242:38 | MyInt {...} [MyInt] | semmle.label | MyInt {...} [MyInt] |
283322
| main.rs:242:28:242:36 | source(...) | semmle.label | source(...) |
@@ -293,6 +332,23 @@ nodes
293332
| main.rs:254:13:254:20 | a.add(...) [MyInt] | semmle.label | a.add(...) [MyInt] |
294333
| main.rs:255:10:255:10 | d [MyInt] | semmle.label | d [MyInt] |
295334
| main.rs:255:10:255:16 | d.value | semmle.label | d.value |
335+
| main.rs:259:9:259:9 | b [MyInt] | semmle.label | b [MyInt] |
336+
| main.rs:259:13:259:39 | MyInt {...} [MyInt] | semmle.label | MyInt {...} [MyInt] |
337+
| main.rs:259:28:259:37 | source(...) | semmle.label | source(...) |
338+
| main.rs:261:27:261:32 | [post] &mut a [&ref, MyInt] | semmle.label | [post] &mut a [&ref, MyInt] |
339+
| main.rs:261:32:261:32 | [post] a [MyInt] | semmle.label | [post] a [MyInt] |
340+
| main.rs:261:35:261:35 | b [MyInt] | semmle.label | b [MyInt] |
341+
| main.rs:262:10:262:10 | a [MyInt] | semmle.label | a [MyInt] |
342+
| main.rs:262:10:262:16 | a.value | semmle.label | a.value |
343+
| main.rs:270:9:270:9 | a [MyInt] | semmle.label | a [MyInt] |
344+
| main.rs:270:13:270:39 | MyInt {...} [MyInt] | semmle.label | MyInt {...} [MyInt] |
345+
| main.rs:270:28:270:37 | source(...) | semmle.label | source(...) |
346+
| main.rs:272:9:272:9 | c | semmle.label | c |
347+
| main.rs:272:13:272:29 | * ... | semmle.label | * ... |
348+
| main.rs:272:14:272:29 | ...::deref(...) [&ref] | semmle.label | ...::deref(...) [&ref] |
349+
| main.rs:272:27:272:28 | &a [&ref, MyInt] | semmle.label | &a [&ref, MyInt] |
350+
| main.rs:272:28:272:28 | a [MyInt] | semmle.label | a [MyInt] |
351+
| main.rs:273:10:273:10 | c | semmle.label | c |
296352
| main.rs:289:18:289:21 | SelfParam [MyInt] | semmle.label | SelfParam [MyInt] |
297353
| main.rs:289:48:291:5 | { ... } [MyInt] | semmle.label | { ... } [MyInt] |
298354
| main.rs:293:26:293:37 | ...: MyInt [MyInt] | semmle.label | ...: MyInt [MyInt] |
@@ -339,11 +395,11 @@ subpaths
339395
| main.rs:212:24:212:33 | source(...) | main.rs:206:12:206:17 | ...: i64 | main.rs:206:28:208:5 | { ... } [MyInt] | main.rs:212:13:212:34 | ...::new(...) [MyInt] |
340396
| main.rs:244:13:244:13 | a [MyInt] | main.rs:220:12:220:15 | SelfParam [MyInt] | main.rs:220:42:223:5 | { ... } [MyInt] | main.rs:244:13:244:17 | ... + ... [MyInt] |
341397
| main.rs:252:9:252:9 | a [MyInt] | main.rs:220:12:220:15 | SelfParam [MyInt] | main.rs:220:42:223:5 | { ... } [MyInt] | main.rs:254:13:254:20 | a.add(...) [MyInt] |
398+
| main.rs:261:35:261:35 | b [MyInt] | main.rs:227:30:227:39 | ...: MyInt [MyInt] | main.rs:227:19:227:27 | SelfParam [Return] [&ref, MyInt] | main.rs:261:27:261:32 | [post] &mut a [&ref, MyInt] |
399+
| main.rs:272:27:272:28 | &a [&ref, MyInt] | main.rs:235:14:235:18 | SelfParam [&ref, MyInt] | main.rs:235:38:237:5 | { ... } [&ref] | main.rs:272:14:272:29 | ...::deref(...) [&ref] |
342400
| main.rs:301:50:301:50 | a [MyInt] | main.rs:289:18:289:21 | SelfParam [MyInt] | main.rs:289:48:291:5 | { ... } [MyInt] | main.rs:301:30:301:54 | ...::take_self(...) [MyInt] |
343401
| main.rs:306:55:306:55 | b [MyInt] | main.rs:293:26:293:37 | ...: MyInt [MyInt] | main.rs:293:49:295:5 | { ... } [MyInt] | main.rs:306:30:306:56 | ...::take_second(...) [MyInt] |
344402
testFailures
345-
| main.rs:262:20:262:39 | //... | Missing result: hasValueFlow=34 |
346-
| main.rs:273:14:273:33 | //... | Missing result: hasValueFlow=27 |
347403
#select
348404
| main.rs:18:10:18:10 | a | main.rs:13:5:13:13 | source(...) | main.rs:18:10:18:10 | a | $@ | main.rs:13:5:13:13 | source(...) | source(...) |
349405
| main.rs:39:10:39:21 | a.get_data() | main.rs:38:23:38:31 | source(...) | main.rs:39:10:39:21 | a.get_data() | $@ | main.rs:38:23:38:31 | source(...) | source(...) |
@@ -366,6 +422,8 @@ testFailures
366422
| main.rs:214:10:214:10 | m | main.rs:212:24:212:33 | source(...) | main.rs:214:10:214:10 | m | $@ | main.rs:212:24:212:33 | source(...) | source(...) |
367423
| main.rs:245:10:245:16 | c.value | main.rs:242:28:242:36 | source(...) | main.rs:245:10:245:16 | c.value | $@ | main.rs:242:28:242:36 | source(...) | source(...) |
368424
| main.rs:255:10:255:16 | d.value | main.rs:252:28:252:36 | source(...) | main.rs:255:10:255:16 | d.value | $@ | main.rs:252:28:252:36 | source(...) | source(...) |
425+
| main.rs:262:10:262:16 | a.value | main.rs:259:28:259:37 | source(...) | main.rs:262:10:262:16 | a.value | $@ | main.rs:259:28:259:37 | source(...) | source(...) |
426+
| main.rs:273:10:273:10 | c | main.rs:270:28:270:37 | source(...) | main.rs:273:10:273:10 | c | $@ | main.rs:270:28:270:37 | source(...) | source(...) |
369427
| main.rs:302:10:302:10 | c | main.rs:299:28:299:36 | source(...) | main.rs:302:10:302:10 | c | $@ | main.rs:299:28:299:36 | source(...) | source(...) |
370428
| main.rs:307:10:307:10 | c | main.rs:305:28:305:37 | source(...) | main.rs:307:10:307:10 | c | $@ | main.rs:305:28:305:37 | source(...) | source(...) |
371429
| main.rs:317:10:317:10 | a | main.rs:316:13:316:21 | source(...) | main.rs:317:10:317:10 | a | $@ | main.rs:316:13:316:21 | source(...) | source(...) |

rust/ql/test/library-tests/dataflow/global/viableCallable.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@
7171
| main.rs:254:13:254:20 | a.add(...) | main.rs:220:5:223:5 | fn add |
7272
| main.rs:255:5:255:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
7373
| main.rs:259:28:259:37 | source(...) | main.rs:1:1:3:1 | fn source |
74+
| main.rs:261:5:261:36 | ...::mul_assign(...) | main.rs:227:5:229:5 | fn mul_assign |
7475
| main.rs:262:5:262:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
7576
| main.rs:265:28:265:37 | source(...) | main.rs:1:1:3:1 | fn source |
7677
| main.rs:266:5:266:10 | ... *= ... | main.rs:227:5:229:5 | fn mul_assign |
7778
| main.rs:267:5:267:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
7879
| main.rs:270:28:270:37 | source(...) | main.rs:1:1:3:1 | fn source |
80+
| main.rs:272:14:272:29 | ...::deref(...) | main.rs:235:5:237:5 | fn deref |
7981
| main.rs:273:5:273:11 | sink(...) | main.rs:5:1:7:1 | fn sink |
8082
| main.rs:275:28:275:37 | source(...) | main.rs:1:1:3:1 | fn source |
8183
| main.rs:276:13:276:14 | * ... | main.rs:235:5:237:5 | fn deref |

0 commit comments

Comments
 (0)