-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Prepare dataflow for local annotations #21138
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
Open
tausbn
wants to merge
8
commits into
main
Choose a base branch
from
tausbn/python-prepare-for-overlay-annotations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−65
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4543c66
Python: Prepare `LocalSourceNode` for locality
tausbn 30ce406
Python: Remove global restriction on `ModuleVariableNode`
tausbn ac5a744
Python: Fix tests
tausbn 7fccc23
Python: Make `ExtractedArgumentNode` local
tausbn 6113d4b
Python: Fix test issues
tausbn 3f71812
Python: Make capturing closure arguments synthetic and non-global
tausbn fb6175d
Python: Fix consistency test failures
tausbn 958c798
Python: Accept dataflow test changes
tausbn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,15 +76,7 @@ newtype TNode = | |
| node.getNode() = any(Comp c).getIterable() | ||
| } or | ||
| /** A node representing a global (module-level) variable in a specific module. */ | ||
| TModuleVariableNode(Module m, GlobalVariable v) { | ||
| v.getScope() = m and | ||
| ( | ||
| v.escapes() | ||
| or | ||
| isAccessedThroughImportStar(m) and | ||
| ImportStar::globalNameDefinedInModule(v.getId(), m) | ||
| ) | ||
| } or | ||
| TModuleVariableNode(Module m, GlobalVariable v) { v.getScope() = m } or | ||
| /** | ||
| * A synthetic node representing that an iterable sequence flows to consumer. | ||
| */ | ||
|
|
@@ -129,6 +121,20 @@ newtype TNode = | |
| f = any(VariableCapture::CapturedVariable v).getACapturingScope() and | ||
| exists(TFunction(f)) | ||
| } or | ||
| /** | ||
| * A synthetic node representing the values of the variables captured | ||
| * by the callable being called. | ||
| */ | ||
| TSynthCapturedVariablesArgumentNode(ControlFlowNode callable) { | ||
| callable = any(CallNode c).getFunction() | ||
| } or | ||
| /** | ||
| * A synthetic node representing the values of the variables captured | ||
| * by the callable being called, after the output has been computed. | ||
| */ | ||
| TSynthCapturedVariablesArgumentPostUpdateNode(ControlFlowNode callable) { | ||
| callable = any(CallNode c).getFunction() | ||
| } or | ||
| /** A synthetic node representing the values of variables captured by a comprehension. */ | ||
| TSynthCompCapturedVariablesArgumentNode(Comp comp) { | ||
| comp.getFunction() = any(VariableCapture::CapturedVariable v).getACapturingScope() | ||
|
|
@@ -347,27 +353,51 @@ abstract class ArgumentNode extends Node { | |
| final ExtractedDataFlowCall getCall() { this.argumentOf(result, _) } | ||
| } | ||
|
|
||
| /** Gets an overapproximation of the argument nodes that are included in `getCallArg`. */ | ||
| Node getCallArgApproximation() { | ||
| // pre-update nodes for calls | ||
| result = any(CallCfgNode c).(PostUpdateNode).getPreUpdateNode() | ||
| or | ||
| // self parameters in methods | ||
| exists(Class c | result.asExpr() = c.getAMethod().getArg(0)) | ||
| or | ||
| // the object part of an attribute expression (which might be a bound method) | ||
| result.asCfgNode() = any(AttrNode a).getObject() | ||
| or | ||
| // the function part of any call | ||
| result.asCfgNode() = any(CallNode c).getFunction() | ||
| } | ||
|
|
||
| /** Gets the extracted argument nodes that do not rely on `getCallArg`. */ | ||
| private Node otherArgs() { | ||
| // for potential summaries we allow all normal call arguments | ||
| normalCallArg(_, result, _) | ||
| or | ||
| // and self arguments | ||
| result.asCfgNode() = any(CallNode c).getFunction().(AttrNode).getObject() | ||
| or | ||
| // for comprehensions, we allow the synthetic `iterable` argument | ||
| result.asExpr() = any(Comp c).getIterable() | ||
| } | ||
|
|
||
| /** | ||
| * A data flow node that represents a call argument found in the source code. | ||
| */ | ||
| class ExtractedArgumentNode extends ArgumentNode { | ||
| ExtractedArgumentNode() { | ||
| // for resolved calls, we need to allow all argument nodes | ||
| getCallArg(_, _, _, this, _) | ||
| or | ||
| // for potential summaries we allow all normal call arguments | ||
| normalCallArg(_, this, _) | ||
| this = getCallArgApproximation() | ||
| or | ||
| // and self arguments | ||
| this.asCfgNode() = any(CallNode c).getFunction().(AttrNode).getObject() | ||
| or | ||
| // for comprehensions, we allow the synthetic `iterable` argument | ||
| this.asExpr() = any(Comp c).getIterable() | ||
| this = otherArgs() | ||
|
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. Perhaps call this one |
||
| } | ||
|
|
||
| final override predicate argumentOf(DataFlowCall call, ArgumentPosition pos) { | ||
| this = call.getArgument(pos) and | ||
| call instanceof ExtractedDataFlowCall | ||
| call instanceof ExtractedDataFlowCall and | ||
| ( | ||
| this = otherArgs() | ||
| or | ||
| this = getCallArgApproximation() and getCallArg(_, _, _, this, _) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -440,13 +470,17 @@ class ModuleVariableNode extends Node, TModuleVariableNode { | |
|
|
||
| /** Gets a node that reads this variable. */ | ||
| Node getARead() { | ||
| result.asCfgNode() = var.getALoad().getAFlowNode() and | ||
| // Ignore reads that happen when the module is imported. These are only executed once. | ||
| not result.getScope() = mod | ||
| result = this.getALocalRead() | ||
| or | ||
| this = import_star_read(result) | ||
| } | ||
|
|
||
| /** Gets a node that reads this variable, excluding reads that happen through `from ... import *`. */ | ||
| Node getALocalRead() { | ||
| result.asCfgNode() = var.getALoad().getAFlowNode() and | ||
| not result.getScope() = mod | ||
| } | ||
|
|
||
| /** Gets an `EssaNode` that corresponds to an assignment of this global variable. */ | ||
| Node getAWrite() { | ||
| any(EssaNodeDefinition def).definedBy(var, result.asCfgNode().(DefinitionNode)) | ||
|
|
@@ -466,8 +500,6 @@ class ModuleVariableNode extends Node, TModuleVariableNode { | |
| override Location getLocation() { result = mod.getLocation() } | ||
| } | ||
|
|
||
| private predicate isAccessedThroughImportStar(Module m) { m = ImportStar::getStarImported(_) } | ||
|
|
||
| private ModuleVariableNode import_star_read(Node n) { | ||
| resolved_import_star_module(result.getModule(), result.getVariable().getId(), n) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| | test.py:5:9:5:16 | ControlFlowNode for __init__ | test.py:4:1:4:20 | ControlFlowNode for ClassExpr | __init__ | test.py:5:5:5:28 | ControlFlowNode for FunctionExpr | | ||
| | test.py:6:9:6:16 | ControlFlowNode for Attribute | test.py:6:9:6:12 | ControlFlowNode for self | foo | test.py:6:20:6:22 | ControlFlowNode for foo | | ||
| | test.py:9:1:9:9 | ControlFlowNode for Attribute | test.py:0:0:0:0 | ModuleVariableNode in Module test for myobj | foo | test.py:9:13:9:17 | ControlFlowNode for StringLiteral | | ||
| | test.py:9:1:9:9 | ControlFlowNode for Attribute | test.py:9:1:9:5 | ControlFlowNode for myobj | foo | test.py:9:13:9:17 | ControlFlowNode for StringLiteral | | ||
| | test.py:12:1:12:25 | ControlFlowNode for setattr() | test.py:12:9:12:13 | ControlFlowNode for myobj | foo | test.py:12:23:12:24 | ControlFlowNode for IntegerLiteral | |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I suppose
v.escapes()could be kept, but the other branch would have to be over-approximated. Well, if it is not too expensive to just have all of them, then that is fine.