Skip to content

Rust: Compute canonical paths in QL #19134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion rust/ql/.generated.list

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion rust/ql/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/ql/lib/codeql/files/FileSystem.qll
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Container = Impl::Container;

class Folder = Impl::Folder;

module Folder = Impl::Folder;

/** A file. */
class File extends Container, Impl::File {
/** Holds if this file was extracted from ordinary source code. */
Expand Down
22 changes: 22 additions & 0 deletions rust/ql/lib/codeql/rust/dataflow/internal/DataFlowConsistency.qll
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,25 @@ private module Input implements InputSig<Location, RustDataFlow> {
}

import MakeConsistency<Location, RustDataFlow, RustTaintTracking, Input>
private import codeql.rust.dataflow.internal.ModelsAsData

query predicate missingMadSummaryCanonicalPath(string crate, string path, Addressable a) {
summaryModel(crate, path, _, _, _, _, _) and
a.getCrateOrigin() = crate and
a.getExtendedCanonicalPath() = path and
not exists(a.getCanonicalPath())
}

query predicate missingMadSourceCanonicalPath(string crate, string path, Addressable a) {
sourceModel(crate, path, _, _, _, _) and
a.getCrateOrigin() = crate and
a.getExtendedCanonicalPath() = path and
not exists(a.getCanonicalPath())
}

query predicate missingMadSinkCanonicalPath(string crate, string path, Addressable a) {
sinkModel(crate, path, _, _, _, _) and
a.getCrateOrigin() = crate and
a.getExtendedCanonicalPath() = path and
not exists(a.getCanonicalPath())
}
36 changes: 34 additions & 2 deletions rust/ql/lib/codeql/rust/elements/internal/AddressableImpl.qll
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// generated by codegen, remove this comment if you wish to edit this file
/**
* This module provides a hand-modifiable wrapper around the generated class `Addressable`.
*
Expand All @@ -12,10 +11,43 @@ private import codeql.rust.elements.internal.generated.Addressable
* be referenced directly.
*/
module Impl {
private import rust
private import codeql.rust.internal.PathResolution

// the following QLdoc is generated: if you need to edit it, do it in the schema file
/**
* Something that can be addressed by a path.
*
* TODO: This does not yet include all possible cases.
*/
class Addressable extends Generated::Addressable { }
class Addressable extends Generated::Addressable {
/**
* Gets the canonical path of this item, if any.
*
* The crate `c` is the root of the path.
*
* See [The Rust Reference][1] for more details.
*
* [1]: https://doc.rust-lang.org/reference/paths.html#canonical-paths
*/
string getCanonicalPath(Crate c) { result = this.(ItemNode).getCanonicalPath(c) }

/**
* Gets the canonical path of this item, if any.
*
* See [The Rust Reference][1] for more details.
*
* [1]: https://doc.rust-lang.org/reference/paths.html#canonical-paths
*/
string getCanonicalPath() { result = this.getCanonicalPath(_) }

/**
* Holds if this item has a canonical path.
*
* See [The Rust Reference][1] for more details.
*
* [1]: https://doc.rust-lang.org/reference/paths.html#canonical-paths
*/
predicate hasCanonicalPath() { exists(this.getCanonicalPath()) }
}
}
22 changes: 2 additions & 20 deletions rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,7 @@ private class StartswithCall extends Path::SafeAccessCheck::Range, CfgNodes::Met
* [1]: https://doc.rust-lang.org/std/option/enum.Option.html
*/
class OptionEnum extends Enum {
OptionEnum() {
// todo: replace with canonical path, once calculated in QL
exists(Crate core, Module m |
core.getName() = "core" and
m = core.getModule().getItemList().getAnItem() and
m.getName().getText() = "option" and
this = m.getItemList().getAnItem() and
this.getName().getText() = "Option"
)
}
OptionEnum() { this.getCanonicalPath() = "core::option::Option" }

/** Gets the `Some` variant. */
Variant getSome() { result = this.getVariant("Some") }
Expand All @@ -49,16 +40,7 @@ class OptionEnum extends Enum {
* [1]: https://doc.rust-lang.org/stable/std/result/enum.Result.html
*/
class ResultEnum extends Enum {
ResultEnum() {
// todo: replace with canonical path, once calculated in QL
exists(Crate core, Module m |
core.getName() = "core" and
m = core.getModule().getItemList().getAnItem() and
m.getName().getText() = "result" and
this = m.getItemList().getAnItem() and
this.getName().getText() = "Result"
)
}
ResultEnum() { this.getCanonicalPath() = "core::result::Result" }

/** Gets the `Ok` variant. */
Variant getOk() { result = this.getVariant("Ok") }
Expand Down
Loading
Loading