Skip to content

Commit d10c145

Browse files
committed
wip5
1 parent 1370921 commit d10c145

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,18 +1161,32 @@ private module MethodCallResolution {
11611161
)
11621162
}
11631163

1164-
pragma[nomagic]
1165-
private predicate traitMethodInfo(string name, int arity, Trait trait) {
1166-
exists(ImplItemNode i |
1167-
methodInfo(_, name, arity, i, _, _, _) and
1168-
trait = i.resolveTraitTy()
1169-
)
1164+
/**
1165+
* todo
1166+
*
1167+
* Holds if method `f` with the name `name` and the arity `arity` exists in
1168+
* `i`, and the type of the `self` parameter is `selfType`.
1169+
*
1170+
* `rootTypePath` points to the type `rootType` inside `selfType`, which is
1171+
* the (possibly `&`-stripped) root type of `selfType`.
1172+
*/
1173+
pragma[inline]
1174+
predicate methodInfoMatch(
1175+
Function f, string name, int arity, ImplOrTraitItemNode i, FunctionType selfType,
1176+
TypePath rootTypePath, Type rootType
1177+
) {
1178+
methodInfo(f, name, arity, i, selfType, rootTypePath, rootType)
1179+
or
1180+
methodInfo(f, name, arity, i, selfType, rootTypePath, TTypeParamTypeParameter(_))
11701181
}
11711182

11721183
pragma[nomagic]
11731184
private predicate methodCallTraitCandidate(Element mc, Trait trait) {
11741185
exists(string name, int arity | mc.(MethodCall).isMethodCall(name, arity) |
1175-
traitMethodInfo(name, arity, trait)
1186+
exists(ImplItemNode i |
1187+
methodInfo(_, name, arity, i, _, _, _) and
1188+
trait = i.resolveTraitTy()
1189+
)
11761190
or
11771191
methodInfo(_, name, arity, trait, _, _, _)
11781192
)
@@ -1209,7 +1223,7 @@ private module MethodCallResolution {
12091223
) {
12101224
exists(string name, int arity |
12111225
mc.isMethodCall(name, arity) and
1212-
methodInfo(_, name, arity, i, self, rootTypePath, rootType)
1226+
methodInfoMatch(_, name, arity, i, self, rootTypePath, rootType)
12131227
|
12141228
i =
12151229
any(Impl impl |
@@ -1472,7 +1486,7 @@ private module MethodCallResolution {
14721486
this.isMethodCall(_, rootTypePath, rootType, name, arity) and
14731487
isRefStrippedRoot(rootTypePath, rootType) and
14741488
forall(Impl i |
1475-
methodInfo(_, name, arity, i, _, rootTypePath, rootType) and
1489+
methodInfoMatch(_, name, arity, i, _, rootTypePath, rootType) and
14761490
not i.hasTrait()
14771491
|
14781492
this.isNotInherentCandidate(i)
@@ -1883,7 +1897,7 @@ private module FunctionCallResolution {
18831897
exists(TypePath rootTypePath, Type rootType |
18841898
f = call.getPathResolutionResolvedFunctionOrImplementation(resolved) and
18851899
trait = call.(Call).getTrait() and
1886-
MethodCallResolution::methodInfo(f, _, _, i, self, rootTypePath, rootType) and
1900+
MethodCallResolution::methodInfoMatch(f, _, _, i, self, rootTypePath, rootType) and
18871901
call.getTypeAt(rootTypePath) = rootType
18881902
)
18891903
}
@@ -2178,12 +2192,12 @@ private module OperationResolution {
21782192
private module OperationIsInstantiationOfInput implements
21792193
IsInstantiationOfInputSig<Op, FunctionType>
21802194
{
2181-
pragma[nomagic]
2182-
private predicate methodInfo(
2195+
pragma[inline]
2196+
private predicate methodInfoMatch(
21832197
TypeAbstraction abs, FunctionType constraint, Trait trait, string name, int arity,
21842198
TypePath rootTypePath, Type rootType
21852199
) {
2186-
MethodCallResolution::methodInfo(_, name, arity, abs, constraint, rootTypePath, rootType) and
2200+
MethodCallResolution::methodInfoMatch(_, name, arity, abs, constraint, rootTypePath, rootType) and
21872201
(
21882202
trait = abs.(ImplItemNode).resolveTraitTy()
21892203
or
@@ -2195,7 +2209,7 @@ private module OperationResolution {
21952209
predicate potentialInstantiationOf(Op op, TypeAbstraction abs, FunctionType constraint) {
21962210
exists(Trait trait, string name, int arity, TypePath rootTypePath, Type rootType |
21972211
op.isOperation(rootTypePath, rootType, trait, name, arity) and
2198-
methodInfo(abs, constraint, trait, name, arity, rootTypePath, rootType)
2212+
methodInfoMatch(abs, constraint, trait, name, arity, rootTypePath, rootType)
21992213
)
22002214
}
22012215

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
multipleCallTargets
2+
| main.rs:322:14:322:33 | ... .cmp(...) |
3+
| main.rs:334:9:334:28 | ... .cmp(...) |
24
| main.rs:362:14:362:30 | ... .lt(...) |

rust/ql/test/library-tests/dataflow/sources/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ multipleCallTargets
1111
| test.rs:179:30:179:68 | ...::_print(...) |
1212
| test.rs:188:26:188:105 | ...::_print(...) |
1313
| test.rs:229:22:229:72 | ... .read_to_string(...) |
14+
| test.rs:639:26:639:43 | file1.chain(...) |
15+
| test.rs:647:26:647:40 | file1.take(...) |
1416
| test.rs:697:18:697:38 | ...::_print(...) |
1517
| test.rs:702:18:702:45 | ...::_print(...) |
1618
| test.rs:720:38:720:42 | ...::_print(...) |

rust/ql/test/library-tests/dataflow/sources/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,15 +638,15 @@ async fn test_tokio_file() -> std::io::Result<()> {
638638
let file2 = tokio::fs::File::open("another_file.txt").await?; // $ Alert[rust/summary/taint-sources]
639639
let mut reader = file1.chain(file2);
640640
reader.read_to_string(&mut buffer).await?;
641-
sink(&buffer); // $ MISSING: hasTaintFlow="file.txt" hasTaintFlow="another_file.txt" -- we cannot resolve the `chain` and `read_to_string` calls above, which comes from `impl<R: AsyncRead + ?Sized> AsyncReadExt for R {}` in `async_read_ext.rs`
641+
sink(&buffer); // $ hasTaintFlow="file.txt" hasTaintFlow="another_file.txt"
642642
}
643643

644644
{
645645
let mut buffer = String::new();
646646
let file1 = tokio::fs::File::open("file.txt").await?; // $ Alert[rust/summary/taint-sources]
647647
let mut reader = file1.take(100);
648648
reader.read_to_string(&mut buffer).await?;
649-
sink(&buffer); // $ MISSING: hasTaintFlow="file.txt" -- we cannot resolve the `take` and `read_to_string` calls above, which comes from `impl<R: AsyncRead + ?Sized> AsyncReadExt for R {}` in `async_read_ext.rs`
649+
sink(&buffer); // $ hasTaintFlow="file.txt"
650650
}
651651

652652
Ok(())

rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ multipleCallTargets
55
| dereference.rs:184:17:184:30 | ... .foo() |
66
| dereference.rs:186:17:186:25 | S.bar(...) |
77
| dereference.rs:187:17:187:29 | S.bar(...) |
8+
| main.rs:1803:13:1803:63 | ... .partial_cmp(...) |
89
| main.rs:2318:9:2318:34 | ...::my_from2(...) |
910
| main.rs:2319:9:2319:33 | ...::my_from2(...) |
1011
| main.rs:2320:9:2320:38 | ...::my_from2(...) |

0 commit comments

Comments
 (0)