Skip to content

Commit 774fa8d

Browse files
committed
wip
1 parent 1475463 commit 774fa8d

File tree

5 files changed

+71
-26
lines changed

5 files changed

+71
-26
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,25 @@ private module Input implements InputSig<Location, RustDataFlow> {
2929
}
3030

3131
import MakeConsistency<Location, RustDataFlow, RustTaintTracking, Input>
32+
private import codeql.rust.dataflow.internal.ModelsAsData
33+
34+
query predicate missingMadSummaryCanonicalPath(string crate, string path, Addressable a) {
35+
summaryModel(crate, path, _, _, _, _, _) and
36+
a.getCrateOrigin() = crate and
37+
a.getExtendedCanonicalPath() = path and
38+
not exists(a.getCanonicalPath())
39+
}
40+
41+
query predicate missingMadSourceCanonicalPath(string crate, string path, Addressable a) {
42+
sourceModel(crate, path, _, _, _, _) and
43+
a.getCrateOrigin() = crate and
44+
a.getExtendedCanonicalPath() = path and
45+
not exists(a.getCanonicalPath())
46+
}
47+
48+
query predicate missingMadSinkCanonicalPath(string crate, string path, Addressable a) {
49+
sinkModel(crate, path, _, _, _, _) and
50+
a.getCrateOrigin() = crate and
51+
a.getExtendedCanonicalPath() = path and
52+
not exists(a.getCanonicalPath())
53+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,21 @@ module Impl {
2424
/**
2525
* Gets the canonical path of this item, if any.
2626
*
27+
* The crate `c` is the root of the path.
28+
*
2729
* See [The Rust Reference][1] for more details.
2830
*
2931
* [1]: https://doc.rust-lang.org/reference/paths.html#canonical-paths
3032
*/
3133
string getCanonicalPath(Crate c) { result = this.(ItemNode).getCanonicalPath(c) }
34+
35+
/**
36+
* Gets the canonical path of this item, if any.
37+
*
38+
* See [The Rust Reference][1] for more details.
39+
*
40+
* [1]: https://doc.rust-lang.org/reference/paths.html#canonical-paths
41+
*/
42+
string getCanonicalPath() { result = this.getCanonicalPath(_) }
3243
}
3344
}

rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@ private class StartswithCall extends Path::SafeAccessCheck::Range, CfgNodes::Met
2222
}
2323
}
2424

25+
private import codeql.rust.internal.PathResolution
26+
private import codeql.rust.elements.internal.generated.ParentChild
27+
2528
/**
2629
* The [`Option` enum][1].
2730
*
2831
* [1]: https://doc.rust-lang.org/std/option/enum.Option.html
2932
*/
3033
class OptionEnum extends Enum {
31-
OptionEnum() {
32-
// todo: replace with canonical path, once calculated in QL
33-
exists(Crate core, Module m |
34-
core.getName() = "core" and
35-
m = core.getModule().getItemList().getAnItem() and
36-
m.getName().getText() = "option" and
37-
this = m.getItemList().getAnItem() and
38-
this.getName().getText() = "Option"
39-
)
40-
}
34+
OptionEnum() { this.getCanonicalPath() = "core::option::Option" }
4135

4236
/** Gets the `Some` variant. */
4337
Variant getSome() { result = this.getVariant("Some") }
@@ -49,16 +43,7 @@ class OptionEnum extends Enum {
4943
* [1]: https://doc.rust-lang.org/stable/std/result/enum.Result.html
5044
*/
5145
class ResultEnum extends Enum {
52-
ResultEnum() {
53-
// todo: replace with canonical path, once calculated in QL
54-
exists(Crate core, Module m |
55-
core.getName() = "core" and
56-
m = core.getModule().getItemList().getAnItem() and
57-
m.getName().getText() = "result" and
58-
this = m.getItemList().getAnItem() and
59-
this.getName().getText() = "Result"
60-
)
61-
}
46+
ResultEnum() { this.getCanonicalPath() = "core::result::Result" }
6247

6348
/** Gets the `Ok` variant. */
6449
Variant getOk() { result = this.getVariant("Ok") }

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,13 @@ class CrateItemNode extends ItemNode instanceof Crate {
360360

361361
override predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) {
362362
this.hasCanonicalPath(c) and
363-
exists(ModuleLikeNode m | m = this.getModuleNode() |
364-
child = m
365-
or
366-
child.getImmediateParent() = m.(SourceFile) and
363+
exists(ModuleLikeNode m |
364+
child.getImmediateParent() = m and
367365
not m = child.(SourceFileItemNode).getSuper()
366+
|
367+
m = super.getModule() // the special `crate` root module inserted by the extractor
368+
or
369+
m = super.getSourceFile()
368370
)
369371
}
370372

@@ -650,6 +652,16 @@ private class MacroCallItemNode extends AssocItemNode instanceof MacroCall {
650652

651653
override Visibility getVisibility() { none() }
652654

655+
override predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) {
656+
any(ItemNode parent).providesCanonicalPathPrefixFor(c, this) and
657+
child.getImmediateParent() = this
658+
}
659+
660+
override string getCanonicalPathPrefixFor(Crate c, ItemNode child) {
661+
result = this.getCanonicalPathPrefix(c) and
662+
this.providesCanonicalPathPrefixFor(c, child)
663+
}
664+
653665
override predicate hasCanonicalPath(Crate c) { none() }
654666

655667
override string getCanonicalPath(Crate c) { none() }
@@ -1415,8 +1427,8 @@ private module Debug {
14151427
private Locatable getRelevantLocatable() {
14161428
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
14171429
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
1418-
filepath.matches("%/test_logging.rs") and
1419-
startline = 163
1430+
filepath.matches("%/clean/types.rs") and
1431+
startline = [5, 350]
14201432
)
14211433
}
14221434

@@ -1452,4 +1464,14 @@ private module Debug {
14521464
m = getRelevantLocatable() and
14531465
fileImport(m, f)
14541466
}
1467+
1468+
predicate debugPreludeEdge(SourceFile f, string name, ItemNode i) {
1469+
preludeEdge(f, name, i) and
1470+
f = getRelevantLocatable()
1471+
}
1472+
1473+
string debugGetCanonicalPath(ItemNode i, Crate c) {
1474+
result = i.getCanonicalPath(c) and
1475+
i = getRelevantLocatable()
1476+
}
14551477
}

rust/ql/src/queries/telemetry/RustAnalyzerComparison.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,8 @@ private module QlCallGraph implements CompareSig<CallGraph> {
145145
}
146146

147147
module CallGraphCompare = Compare<CallGraph, RustAnalyzerCallGraph, QlCallGraph>;
148+
149+
private predicate qlMissingCanonicalPath(Addressable a, string path) {
150+
path = a.getExtendedCanonicalPath() and
151+
not exists(a.getCanonicalPath(_))
152+
}

0 commit comments

Comments
 (0)