-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: Handle functions as data flow lambdas #20547
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Improve data flow through functions being passed as function pointers. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -295,13 +295,10 @@ module LocalFlow { | |||||||||||||||||||||
| class LambdaCallKind = Unit; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** Holds if `creation` is an expression that creates a lambda of kind `kind`. */ | ||||||||||||||||||||||
| predicate lambdaCreationExpr(Expr creation, LambdaCallKind kind) { | ||||||||||||||||||||||
| ( | ||||||||||||||||||||||
| creation instanceof ClosureExpr | ||||||||||||||||||||||
| or | ||||||||||||||||||||||
| creation instanceof Scope::AsyncBlockScope | ||||||||||||||||||||||
| ) and | ||||||||||||||||||||||
| exists(kind) | ||||||||||||||||||||||
| predicate lambdaCreationExpr(Expr creation) { | ||||||||||||||||||||||
| creation instanceof ClosureExpr | ||||||||||||||||||||||
| or | ||||||||||||||||||||||
| creation instanceof Scope::AsyncBlockScope | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
|
|
@@ -810,8 +807,15 @@ module RustDataFlow implements InputSig<Location> { | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** Holds if `creation` is an expression that creates a lambda of kind `kind` for `c`. */ | ||||||||||||||||||||||
| predicate lambdaCreation(Node creation, LambdaCallKind kind, DataFlowCallable c) { | ||||||||||||||||||||||
| exists(Expr e | | ||||||||||||||||||||||
| e = creation.asExpr().getExpr() and lambdaCreationExpr(e, kind) and e = c.asCfgScope() | ||||||||||||||||||||||
| exists(kind) and | ||||||||||||||||||||||
| exists(Expr e | e = creation.asExpr().getExpr() | | ||||||||||||||||||||||
| lambdaCreationExpr(e) and e = c.asCfgScope() | ||||||||||||||||||||||
| or | ||||||||||||||||||||||
| // A path expression, that resolves to a function, evaluates to a function | ||||||||||||||||||||||
| // pointer. Except if the path occurs directly in a call, then it's just a | ||||||||||||||||||||||
| // call to the function and not a function being passed as data. | ||||||||||||||||||||||
|
Comment on lines
+814
to
+816
|
||||||||||||||||||||||
| // A path expression, that resolves to a function, evaluates to a function | |
| // pointer. Except if the path occurs directly in a call, then it's just a | |
| // call to the function and not a function being passed as data. | |
| // A path expression that resolves to a function evaluates to a function | |
| // pointer. Except if the path occurs directly in a call, then it's just a | |
| // call to the function and not a function being passed as data. | |
| // `resolvePath` returns the entity (such as a function or variable) to which | |
| // the path expression refers. Here, we check if the resolved entity is the same | |
| // as `c.asCfgScope()`, which represents the callable's scope. See the definition | |
| // of `resolvePath` in `codeql.rust.internal.PathResolution` for details. |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| models | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you change the dir name to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before the tests only covered closures. Now they also covers functions, which are not closures. So I though that "closure" wasn't wide enough, and that using the data flow library terminology was the best match for what's common across the tests. |
||
| edges | ||
| | main.rs:10:20:10:52 | if cond {...} else {...} | main.rs:11:10:11:16 | f(...) | provenance | | | ||
| | main.rs:10:30:10:39 | source(...) | main.rs:10:20:10:52 | if cond {...} else {...} | provenance | | | ||
| | main.rs:15:20:15:23 | ... | main.rs:17:18:17:21 | data | provenance | | | ||
| | main.rs:22:9:22:9 | a | main.rs:23:13:23:13 | a | provenance | | | ||
| | main.rs:22:13:22:22 | source(...) | main.rs:22:9:22:9 | a | provenance | | | ||
| | main.rs:23:13:23:13 | a | main.rs:15:20:15:23 | ... | provenance | | | ||
| | main.rs:27:20:27:23 | ... | main.rs:27:26:27:52 | if cond {...} else {...} | provenance | | | ||
| | main.rs:28:9:28:9 | a | main.rs:29:21:29:21 | a | provenance | | | ||
| | main.rs:28:13:28:22 | source(...) | main.rs:28:9:28:9 | a | provenance | | | ||
| | main.rs:29:9:29:9 | b | main.rs:30:10:30:10 | b | provenance | | | ||
| | main.rs:29:13:29:22 | f(...) | main.rs:29:9:29:9 | b | provenance | | | ||
| | main.rs:29:21:29:21 | a | main.rs:27:20:27:23 | ... | provenance | | | ||
| | main.rs:29:21:29:21 | a | main.rs:29:13:29:22 | f(...) | provenance | | | ||
| | main.rs:37:16:37:25 | source(...) | main.rs:39:5:39:5 | [post] f [captured capt] | provenance | | | ||
| | main.rs:39:5:39:5 | [post] f [captured capt] | main.rs:40:10:40:13 | capt | provenance | | | ||
| | main.rs:39:5:39:5 | [post] f [captured capt] | main.rs:44:5:44:5 | g [captured capt] | provenance | | | ||
| | main.rs:44:5:44:5 | g [captured capt] | main.rs:42:14:42:17 | capt | provenance | | | ||
| | main.rs:47:29:49:1 | { ... } | main.rs:57:10:57:12 | f(...) | provenance | | | ||
| | main.rs:48:5:48:14 | source(...) | main.rs:47:29:49:1 | { ... } | provenance | | | ||
| | main.rs:51:17:51:25 | ...: i64 | main.rs:52:10:52:13 | data | provenance | | | ||
| | main.rs:62:9:62:9 | a | main.rs:63:7:63:7 | a | provenance | | | ||
| | main.rs:62:13:62:22 | source(...) | main.rs:62:9:62:9 | a | provenance | | | ||
| | main.rs:63:7:63:7 | a | main.rs:51:17:51:25 | ...: i64 | provenance | | | ||
| | main.rs:66:24:66:32 | ...: i64 | main.rs:66:42:72:1 | { ... } | provenance | | | ||
| | main.rs:76:9:76:9 | a | main.rs:77:21:77:21 | a | provenance | | | ||
| | main.rs:76:13:76:22 | source(...) | main.rs:76:9:76:9 | a | provenance | | | ||
| | main.rs:77:9:77:9 | b | main.rs:78:10:78:10 | b | provenance | | | ||
| | main.rs:77:13:77:22 | f(...) | main.rs:77:9:77:9 | b | provenance | | | ||
| | main.rs:77:21:77:21 | a | main.rs:66:24:66:32 | ...: i64 | provenance | | | ||
| | main.rs:77:21:77:21 | a | main.rs:77:13:77:22 | f(...) | provenance | | | ||
| nodes | ||
| | main.rs:10:20:10:52 | if cond {...} else {...} | semmle.label | if cond {...} else {...} | | ||
| | main.rs:10:30:10:39 | source(...) | semmle.label | source(...) | | ||
| | main.rs:11:10:11:16 | f(...) | semmle.label | f(...) | | ||
| | main.rs:15:20:15:23 | ... | semmle.label | ... | | ||
| | main.rs:17:18:17:21 | data | semmle.label | data | | ||
| | main.rs:22:9:22:9 | a | semmle.label | a | | ||
| | main.rs:22:13:22:22 | source(...) | semmle.label | source(...) | | ||
| | main.rs:23:13:23:13 | a | semmle.label | a | | ||
| | main.rs:27:20:27:23 | ... | semmle.label | ... | | ||
| | main.rs:27:26:27:52 | if cond {...} else {...} | semmle.label | if cond {...} else {...} | | ||
| | main.rs:28:9:28:9 | a | semmle.label | a | | ||
| | main.rs:28:13:28:22 | source(...) | semmle.label | source(...) | | ||
| | main.rs:29:9:29:9 | b | semmle.label | b | | ||
| | main.rs:29:13:29:22 | f(...) | semmle.label | f(...) | | ||
| | main.rs:29:21:29:21 | a | semmle.label | a | | ||
| | main.rs:30:10:30:10 | b | semmle.label | b | | ||
| | main.rs:37:16:37:25 | source(...) | semmle.label | source(...) | | ||
| | main.rs:39:5:39:5 | [post] f [captured capt] | semmle.label | [post] f [captured capt] | | ||
| | main.rs:40:10:40:13 | capt | semmle.label | capt | | ||
| | main.rs:42:14:42:17 | capt | semmle.label | capt | | ||
| | main.rs:44:5:44:5 | g [captured capt] | semmle.label | g [captured capt] | | ||
| | main.rs:47:29:49:1 | { ... } | semmle.label | { ... } | | ||
| | main.rs:48:5:48:14 | source(...) | semmle.label | source(...) | | ||
| | main.rs:51:17:51:25 | ...: i64 | semmle.label | ...: i64 | | ||
| | main.rs:52:10:52:13 | data | semmle.label | data | | ||
| | main.rs:57:10:57:12 | f(...) | semmle.label | f(...) | | ||
| | main.rs:62:9:62:9 | a | semmle.label | a | | ||
| | main.rs:62:13:62:22 | source(...) | semmle.label | source(...) | | ||
| | main.rs:63:7:63:7 | a | semmle.label | a | | ||
| | main.rs:66:24:66:32 | ...: i64 | semmle.label | ...: i64 | | ||
| | main.rs:66:42:72:1 | { ... } | semmle.label | { ... } | | ||
| | main.rs:76:9:76:9 | a | semmle.label | a | | ||
| | main.rs:76:13:76:22 | source(...) | semmle.label | source(...) | | ||
| | main.rs:77:9:77:9 | b | semmle.label | b | | ||
| | main.rs:77:13:77:22 | f(...) | semmle.label | f(...) | | ||
| | main.rs:77:21:77:21 | a | semmle.label | a | | ||
| | main.rs:78:10:78:10 | b | semmle.label | b | | ||
| subpaths | ||
| | main.rs:29:21:29:21 | a | main.rs:27:20:27:23 | ... | main.rs:27:26:27:52 | if cond {...} else {...} | main.rs:29:13:29:22 | f(...) | | ||
| | main.rs:77:21:77:21 | a | main.rs:66:24:66:32 | ...: i64 | main.rs:66:42:72:1 | { ... } | main.rs:77:13:77:22 | f(...) | | ||
| testFailures | ||
| #select | ||
| | main.rs:11:10:11:16 | f(...) | main.rs:10:30:10:39 | source(...) | main.rs:11:10:11:16 | f(...) | $@ | main.rs:10:30:10:39 | source(...) | source(...) | | ||
| | main.rs:17:18:17:21 | data | main.rs:22:13:22:22 | source(...) | main.rs:17:18:17:21 | data | $@ | main.rs:22:13:22:22 | source(...) | source(...) | | ||
| | main.rs:30:10:30:10 | b | main.rs:28:13:28:22 | source(...) | main.rs:30:10:30:10 | b | $@ | main.rs:28:13:28:22 | source(...) | source(...) | | ||
| | main.rs:40:10:40:13 | capt | main.rs:37:16:37:25 | source(...) | main.rs:40:10:40:13 | capt | $@ | main.rs:37:16:37:25 | source(...) | source(...) | | ||
| | main.rs:42:14:42:17 | capt | main.rs:37:16:37:25 | source(...) | main.rs:42:14:42:17 | capt | $@ | main.rs:37:16:37:25 | source(...) | source(...) | | ||
| | main.rs:52:10:52:13 | data | main.rs:62:13:62:22 | source(...) | main.rs:52:10:52:13 | data | $@ | main.rs:62:13:62:22 | source(...) | source(...) | | ||
| | main.rs:57:10:57:12 | f(...) | main.rs:48:5:48:14 | source(...) | main.rs:57:10:57:12 | f(...) | $@ | main.rs:48:5:48:14 | source(...) | source(...) | | ||
| | main.rs:78:10:78:10 | b | main.rs:76:13:76:22 | source(...) | main.rs:78:10:78:10 | b | $@ | main.rs:76:13:76:22 | source(...) | source(...) | | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,30 +6,25 @@ fn sink(s: i64) { | |||||||||||||||||||||||||||||||||||||
| println!("{}", s); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| fn closure_flow_out() { | ||||||||||||||||||||||||||||||||||||||
| let f = |cond| if cond { source(92) } else { 0 }; | ||||||||||||||||||||||||||||||||||||||
| sink(f(true)); // $ hasValueFlow=92 | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| fn closure_flow_in() { | ||||||||||||||||||||||||||||||||||||||
| let f = |cond, data| | ||||||||||||||||||||||||||||||||||||||
| let f = |cond, data| { | ||||||||||||||||||||||||||||||||||||||
| if cond { | ||||||||||||||||||||||||||||||||||||||
| sink(data); // $ hasValueFlow=87 | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| sink(0) | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+21
|
||||||||||||||||||||||||||||||||||||||
| let f = |cond, data| { | |
| if cond { | |
| sink(data); // $ hasValueFlow=87 | |
| } else { | |
| sink(0) | |
| }; | |
| } | |
| }; | |
| let f = |cond, data| sink(if cond { data } else { 0 }); // $ hasValueFlow=87 |
Copilot
AI
Sep 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The closure formatting is inconsistent with the rest of the file. Consider using the same single-line format as closure_flow_through() for consistency, or apply consistent multi-line formatting throughout.
| let f = |cond, data| { | |
| if cond { | |
| sink(data); // $ hasValueFlow=87 | |
| } else { | |
| sink(0) | |
| }; | |
| } | |
| }; | |
| let f = |cond, data| if cond { sink(data); /* $ hasValueFlow=87 */ } else { sink(0) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropped the
kindparameter as it was only a nuisance for most callers.