-
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 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 62fb38d
Python: Rename `otherArgs` to `implicitArgumentNode`
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
| 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. | ||
| */ | ||
|
|
@@ -347,27 +339,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, _) | ||
| this = getCallArgApproximation() | ||
| or | ||
| // for potential summaries we allow all normal call arguments | ||
| normalCallArg(_, this, _) | ||
| 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() | ||
|
||
| } | ||
|
|
||
| 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 +456,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 +486,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
| 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 | |
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
5 changes: 5 additions & 0 deletions
5
python/ql/test/library-tests/dataflow/basic/maximalFlows.expected
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,7 +1,12 @@ | ||
| | test.py:1:1:1:21 | ControlFlowNode for FunctionExpr | test.py:0:0:0:0 | ModuleVariableNode in Module test for obfuscated_id | | ||
| | test.py:1:1:1:21 | ControlFlowNode for FunctionExpr | test.py:7:5:7:17 | ControlFlowNode for obfuscated_id | | ||
| | test.py:1:19:1:19 | ControlFlowNode for x | test.py:0:0:0:0 | ModuleVariableNode in Module test for b | | ||
| | test.py:1:19:1:19 | ControlFlowNode for x | test.py:4:10:4:10 | ControlFlowNode for z | | ||
| | test.py:1:19:1:19 | ControlFlowNode for x | test.py:7:1:7:1 | ControlFlowNode for b | | ||
| | test.py:6:5:6:6 | ControlFlowNode for IntegerLiteral | test.py:0:0:0:0 | ModuleVariableNode in Module test for a | | ||
| | test.py:6:5:6:6 | ControlFlowNode for IntegerLiteral | test.py:0:0:0:0 | ModuleVariableNode in Module test for b | | ||
| | test.py:6:5:6:6 | ControlFlowNode for IntegerLiteral | test.py:4:10:4:10 | ControlFlowNode for z | | ||
| | test.py:6:5:6:6 | ControlFlowNode for IntegerLiteral | test.py:7:1:7:1 | ControlFlowNode for b | | ||
| | test.py:6:5:6:6 | ControlFlowNode for IntegerLiteral | test.py:7:19:7:19 | ControlFlowNode for a | | ||
| | test.py:7:5:7:20 | ControlFlowNode for obfuscated_id() | test.py:0:0:0:0 | ModuleVariableNode in Module test for b | | ||
| | test.py:7:5:7:20 | ControlFlowNode for obfuscated_id() | test.py:7:1:7:1 | ControlFlowNode for b | |
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
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.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 considered doing a more fine-grained over-approximation (as we've done elsewhere), but in this case the benefit seemed limited.