Skip to content

Commit 143d7e2

Browse files
committed
Rust: Use extended canonical paths to resolve calls in data flow
1 parent faabc99 commit 143d7e2

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
@@ -399,13 +399,55 @@ module RustDataFlow implements InputSig<Location> {
399399

400400
final class ReturnKind = ReturnKindAlias;
401401

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

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)