Skip to content

Commit cb59795

Browse files
authored
Merge pull request #19134 from hvitved/rust/canonical-path
Rust: Compute canonical paths in QL
2 parents bb4c6a3 + 93c8507 commit cb59795

File tree

11 files changed

+464
-27
lines changed

11 files changed

+464
-27
lines changed

rust/ql/.generated.list

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `Addressable`.
43
*
@@ -12,10 +11,43 @@ private import codeql.rust.elements.internal.generated.Addressable
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
private import rust
15+
private import codeql.rust.internal.PathResolution
16+
17+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1518
/**
1619
* Something that can be addressed by a path.
1720
*
1821
* TODO: This does not yet include all possible cases.
1922
*/
20-
class Addressable extends Generated::Addressable { }
23+
class Addressable extends Generated::Addressable {
24+
/**
25+
* Gets the canonical path of this item, if any.
26+
*
27+
* The crate `c` is the root of the path.
28+
*
29+
* See [The Rust Reference][1] for more details.
30+
*
31+
* [1]: https://doc.rust-lang.org/reference/paths.html#canonical-paths
32+
*/
33+
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(_) }
43+
44+
/**
45+
* Holds if this item has a canonical path.
46+
*
47+
* See [The Rust Reference][1] for more details.
48+
*
49+
* [1]: https://doc.rust-lang.org/reference/paths.html#canonical-paths
50+
*/
51+
predicate hasCanonicalPath() { exists(this.getCanonicalPath()) }
52+
}
2153
}

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,7 @@ private class StartswithCall extends Path::SafeAccessCheck::Range, CfgNodes::Met
2828
* [1]: https://doc.rust-lang.org/std/option/enum.Option.html
2929
*/
3030
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-
}
31+
OptionEnum() { this.getCanonicalPath() = "core::option::Option" }
4132

4233
/** Gets the `Some` variant. */
4334
Variant getSome() { result = this.getVariant("Some") }
@@ -49,16 +40,7 @@ class OptionEnum extends Enum {
4940
* [1]: https://doc.rust-lang.org/stable/std/result/enum.Result.html
5041
*/
5142
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-
}
43+
ResultEnum() { this.getCanonicalPath() = "core::result::Result" }
6244

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

0 commit comments

Comments
 (0)