Skip to content

Rust: Path resolution improvements #19051

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

Merged
merged 12 commits into from
Mar 24, 2025

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Mar 18, 2025

This PR implements various improvements to our path resolution logic, most notably the ability to resolve paths across crates. Commit-by-commit review is strongly encouraged.

DCA shows that, while we maintain performance, we gain an additional 132% true positive call edges (up 448,902 from 193,392) and an additional 94% true positive resolved paths (up 345,075 from 177,539), all numbers computed across the entire DCA suite.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Mar 18, 2025
@@ -831,3 +887,44 @@
name != "_"
)
}

/** Provides predicates for debugging the path resolution implementation. */
private module Debug {

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.
/** Provides predicates for debugging the path resolution implementation. */
private module Debug {
private Locatable getRelevantLocatable() {
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |

Check warning

Code scanning / CodeQL

Omittable 'exists' variable Warning

This exists variable can be omitted by using a don't-care expression
in this argument
.
/** Provides predicates for debugging the path resolution implementation. */
private module Debug {
private Locatable getRelevantLocatable() {
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |

Check warning

Code scanning / CodeQL

Omittable 'exists' variable Warning

This exists variable can be omitted by using a don't-care expression
in this argument
.
/** Provides predicates for debugging the path resolution implementation. */
private module Debug {
private Locatable getRelevantLocatable() {
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |

Check warning

Code scanning / CodeQL

Omittable 'exists' variable Warning

This exists variable can be omitted by using a don't-care expression
in this argument
.

predicate same(Source s, Target t) {
t = RustAnalyzer::resolve(s) and
t = Ql::resolve(s)

Check warning

Code scanning / CodeQL

Redundant assignment. Warning

The variable t
has previously been assigned
the same value
.
@hvitved hvitved force-pushed the rust/path-resolution-cross-crate branch 4 times, most recently from 87770c5 to f12cce9 Compare March 19, 2025 12:34
@hvitved hvitved force-pushed the rust/path-resolution-cross-crate branch 2 times, most recently from dc4a355 to 9ede9a9 Compare March 19, 2025 14:10
@@ -59,6 +59,19 @@

predicate qlUniqueCount(int c) { c = count(Source s | qlUnique(s)) }

// debug predicates to find missing targets in QL implementation
private module Debug {

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.
@hvitved hvitved force-pushed the rust/path-resolution-cross-crate branch 2 times, most recently from f9255d3 to f1ae6a3 Compare March 20, 2025 12:42
@hvitved hvitved force-pushed the rust/path-resolution-cross-crate branch from f1ae6a3 to 3142dbb Compare March 20, 2025 13:16
@hvitved hvitved marked this pull request as ready for review March 21, 2025 08:02
@hvitved hvitved requested a review from paldepind March 21, 2025 08:02
Copy link
Contributor

@paldepind paldepind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

Comment on lines 861 to 865
* According to
*
* https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/second-edition/ch07-02-controlling-visibility-with-pub.html#privacy-rules
*
* this is either `itemParent` itself or any (transitive) child of `itemParent`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found the same thing in the official Rust reference. We could also use a reference-style Markdown link.

Suggested change
* According to
*
* https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/second-edition/ch07-02-controlling-visibility-with-pub.html#privacy-rules
*
* this is either `itemParent` itself or any (transitive) child of `itemParent`.
* According to [The Rust Reference][1] this is either `itemParent` itself or any (transitive) child of `itemParent`.
*
* [1]: https://doc.rust-lang.org/reference/visibility-and-privacy.html#r-vis.access

Comment on lines 215 to 218
exists(ModuleItemNode mod |
fileImport(mod, this) and
result = mod.getASuccessor("super")
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
exists(ModuleItemNode mod |
fileImport(mod, this) and
result = mod.getASuccessor("super")
)
result = any(ModuleItemNode mod | fileImport(mod, this)).getASuccessor("super")

}

/**
* Gets a source file that belongs to this crate, if any.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's a strong convention, but I've noticed that on our AST classes the getAFoo use a phrasing that starts with "Gets any of the foos ...".

Suggested change
* Gets a source file that belongs to this crate, if any.
* Gets any of the source files that belongs to this crate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't like the Gets any ... style, and it also seems to conflict with https://github.com/github/codeql/blob/main/docs/qldoc-style-guide.md#predicates-with-result.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this part of the rule:

Use "if any" if the item is usually unique but might be missing.

That seems to conflict with how "if any" is used here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the "if any" bit can be removed.

// filepath.matches("%/compile.rs") and
// startline = 1986
// filepath.matches("%/build_steps/mod.rs") and
// startline = 17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

@hvitved hvitved requested a review from paldepind March 24, 2025 09:54
@hvitved
Copy link
Contributor Author

hvitved commented Mar 24, 2025

I believe the query-tests/security/CWE-312/CleartextLogging.qlref test failure is unrelated to this PR, I also cannot reproduce locally.

Copy link
Contributor

@paldepind paldepind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Not sure what's up with the CI though.

@hvitved hvitved merged commit 0f1aee0 into github:main Mar 24, 2025
15 of 16 checks passed
@hvitved hvitved deleted the rust/path-resolution-cross-crate branch March 24, 2025 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants