Skip to content

Commit 44015db

Browse files
committed
wip
1 parent 22b674e commit 44015db

File tree

5 files changed

+66
-23
lines changed

5 files changed

+66
-23
lines changed

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

+22
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

+11
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

+5-20
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

+23-3
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ 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() |
363+
exists(ModuleLikeNode m | m = [this.getModuleNode(), super.getModule().(ModuleLikeNode)] |
364364
child = m
365365
or
366366
child.getImmediateParent() = m.(SourceFile) and
@@ -648,6 +648,16 @@ private class MacroCallItemNode extends AssocItemNode instanceof MacroCall {
648648

649649
override Visibility getVisibility() { none() }
650650

651+
override predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) {
652+
any(ItemNode parent).providesCanonicalPathPrefixFor(c, this) and
653+
child.getImmediateParent() = this
654+
}
655+
656+
override string getCanonicalPathPrefixFor(Crate c, ItemNode child) {
657+
result = this.getCanonicalPathPrefix(c) and
658+
this.providesCanonicalPathPrefixFor(c, child)
659+
}
660+
651661
override predicate hasCanonicalPath(Crate c) { none() }
652662

653663
override string getCanonicalPath(Crate c) { none() }
@@ -1413,8 +1423,8 @@ private module Debug {
14131423
private Locatable getRelevantLocatable() {
14141424
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
14151425
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
1416-
filepath.matches("%/test_logging.rs") and
1417-
startline = 163
1426+
filepath.matches("%/clean/types.rs") and
1427+
startline = [5, 350]
14181428
)
14191429
}
14201430

@@ -1450,4 +1460,14 @@ private module Debug {
14501460
m = getRelevantLocatable() and
14511461
fileImport(m, f)
14521462
}
1463+
1464+
predicate debugPreludeEdge(SourceFile f, string name, ItemNode i) {
1465+
preludeEdge(f, name, i) and
1466+
f = getRelevantLocatable()
1467+
}
1468+
1469+
string debugGetCanonicalPath(ItemNode i, Crate c) {
1470+
result = i.getCanonicalPath(c) and
1471+
i = getRelevantLocatable()
1472+
}
14531473
}

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

+5
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)