Skip to content
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

Rust: Flow through captured variables #18270

Merged
merged 9 commits into from
Dec 16, 2024

Conversation

paldepind
Copy link
Contributor

No description provided.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Dec 11, 2024
1000 + i
}

fn sink(s: i64) {

Check notice

Code scanning / CodeQL

Unused variable Note test

Variable 's' is not used.
@paldepind paldepind force-pushed the rust-captured-variables branch from 2f56b3e to 215922c Compare December 11, 2024 14:25
@paldepind paldepind force-pushed the rust-captured-variables branch from 215922c to 94b037f Compare December 11, 2024 14:44
@paldepind paldepind marked this pull request as ready for review December 11, 2024 18:25
@paldepind
Copy link
Contributor Author

The QLDoc checker is complaining, but it doesn't seem to be directly about the added code and otherwise the CI is happy.

@paldepind
Copy link
Contributor Author

Thanks @intrigus-lgtm. I've applied the suggestions :)

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

Great work!

rust/ql/lib/codeql/rust/dataflow/Ssa.qll Outdated Show resolved Hide resolved
* ```rust
* fn capture_mut() {
* let mut y = 0;
* (0..5).for_each(|| {
Copy link
Contributor

Choose a reason for hiding this comment

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

|| -> |x|?

*
* a definition for `y` is inserted at the call to `for_each`.
*/
class CapturedCallDefinition extends Definition, SsaImpl::UncertainWriteDefinition {
Copy link
Contributor

Choose a reason for hiding this comment

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

Presumably this class was only introduced to improve the toString representation? I suggest moving it into SsaImpl or making it private.

@@ -101,11 +101,19 @@ final class ParameterPosition extends TParameterPosition {
/** Holds if this position represents the `self` position. */
predicate isSelf() { this = TSelfParameterPosition() }

/**
* Holds if this position represents a reference to a lambda itself. Only
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 we should use the closure terminology in Rust instead of lambda.

@@ -402,6 +445,37 @@ module Node {
final override string toString() { result = PostUpdateNode.super.toString() }
}

private class CapturePostUpdateNode extends PostUpdateNode, CaptureNode {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Move this down after the class CaptureNode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about moving CaptureNode up above PostUpdateNode instead? Having all the extensions of PostUpdateNode follow right after the abstract class makes it easier to see what it's domain is.

or
node instanceof Node::ClosureParameterNode
or
node instanceof Node::ClosureArgumentNode
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should hide ClosureArgumentNodes

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

One last nit.

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll Outdated Show resolved Hide resolved
@paldepind
Copy link
Contributor Author

I've now added the consistency checks for captured variables. I've fixed an inconsistency, which I think occurred because async blocks capture variables (due to how we've defined capture) but was not considered a lambda expression. So I've added a case for async blocks in lambdaCreationExpr (never mind what I said about closures with async blocks, that might be a problem, but was not the cause of the inconsistency)

I've not added metric queries for the new consistency checks.

@paldepind
Copy link
Contributor Author

DCA shows new data flow inconsistencies. I'll investigate.

@paldepind
Copy link
Contributor Author

@hvitved Do you think it would be ok to merge the PR as-is and accept the additional data flow inconsistencies?

The inconsistencies doesn't seem trivial to fix and are of the form "Node steps to itself" and occur for some phi nodes at the entries to loops when there are both captured variables and continue statements.

Here's one example:

pub fn foo(cond: bool, names: Vec<Option<String>>) {
  let default_pattern = "hello";
  for name in names { // INCONSISTENCY: phi(default_pattern) | Node steps to itself
    if cond {
      let _ = name.unwrap_or_else(|| default_pattern.to_string());
      continue;
    }
  }
}

@paldepind
Copy link
Contributor Author

I've added a new test that reproduces the inconsistency and added an internal issue to track it.

@paldepind paldepind merged commit 3171752 into github:main Dec 16, 2024
14 of 15 checks passed
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.

3 participants