Skip to content

Commit 1eac6aa

Browse files
committed
Rust: Use extended canonical paths to resolve calls in data flow
1 parent fb62e7a commit 1eac6aa

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

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

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,55 @@ module RustDataFlow implements InputSig<Location> {
402402

403403
final class ReturnKind = ReturnKindAlias;
404404

405+
private import codeql.util.Option
406+
407+
private class CrateOrigin extends string {
408+
CrateOrigin() {
409+
this = [any(Item i).getCrateOrigin(), any(Resolvable r).getResolvedCrateOrigin()]
410+
}
411+
}
412+
413+
private class CrateOriginOption = Option<CrateOrigin>::Option;
414+
415+
pragma[nomagic]
416+
private predicate hasExtendedCanonicalPath(
417+
DataFlowCallable c, CrateOriginOption crate, string path
418+
) {
419+
exists(Item i |
420+
i = c.asCfgScope() and
421+
path = i.getExtendedCanonicalPath()
422+
|
423+
crate.asSome() = i.getCrateOrigin()
424+
or
425+
crate.isNone() and
426+
not i.hasCrateOrigin()
427+
)
428+
}
429+
430+
pragma[nomagic]
431+
private predicate resolvesExtendedCanonicalPath(
432+
DataFlowCall c, CrateOriginOption crate, string path
433+
) {
434+
exists(Resolvable r |
435+
path = r.getResolvedPath() and
436+
(
437+
r = c.asMethodCallExprCfgNode().getExpr()
438+
or
439+
r = c.asCallExprCfgNode().getExpr().(PathExprCfgNode).getPath()
440+
)
441+
|
442+
crate.asSome() = r.getResolvedCrateOrigin()
443+
or
444+
crate.isNone() and
445+
not r.hasResolvedCrateOrigin()
446+
)
447+
}
448+
405449
/** Gets a viable implementation of the target of the given `Call`. */
406-
DataFlowCallable viableCallable(DataFlowCall c) {
407-
exists(Function f, string name | result.asCfgScope() = f and name = f.getName().toString() |
408-
if f.getParamList().hasSelfParam()
409-
then name = c.asMethodCallExprCfgNode().getNameRef().getText()
410-
else
411-
name = c.asCallExprCfgNode().getExpr().getExpr().(PathExpr).getPath().getPart().toString()
450+
DataFlowCallable viableCallable(DataFlowCall call) {
451+
exists(string path, CrateOriginOption crate |
452+
hasExtendedCanonicalPath(result, crate, path) and
453+
resolvesExtendedCanonicalPath(call, crate, path)
412454
)
413455
}
414456

shared/util/codeql/util/Option.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/** A type with `toString`. */
44
private signature class TypeWithToString {
5+
bindingset[this]
56
string toString();
67
}
78

0 commit comments

Comments
 (0)