-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Python: Improve performance of FileNotClosed query by using basic block reachability #19641
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
joefarebrother
wants to merge
2
commits into
github:main
Choose a base branch
from
joefarebrother:python-qual-file-not-closed
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -50,29 +50,32 @@ class FileWrapperCall extends DataFlow::CallCfgNode { | |
|
||
/** A node where a file is closed. */ | ||
abstract class FileClose extends DataFlow::CfgNode { | ||
/** Holds if this file close will occur if an exception is thrown at `raises`. */ | ||
/** Holds if this file close will occur if an exception is raised at `raises`. */ | ||
predicate guardsExceptions(DataFlow::CfgNode raises) { | ||
cfgGetASuccessorStar(raises.asCfgNode().getAnExceptionalSuccessor(), this.asCfgNode()) | ||
// The close call occurs after an exception edge in the cfg (a catch or finally) | ||
bbReachableRefl(raises.asCfgNode().getBasicBlock().getAnExceptionalSuccessor(), | ||
this.asCfgNode().getBasicBlock()) | ||
or | ||
// The expression is after the close call. | ||
// This also covers the body of a `with` statement. | ||
cfgGetASuccessorStar(this.asCfgNode(), raises.asCfgNode()) | ||
// The exception is after the close call. | ||
// A full cfg reachability check is not in general feasible for performance, so we approximate it with: | ||
// - A basic block reachability check (here) that works if the expression and close call are in different basic blocks | ||
// - A check (in the `WithStatement` override of `guardsExceptions`) for the case where the exception call | ||
// is lexically contained in the body of a `with` statement that closes the file. | ||
// This may cause FPs in a case such as: | ||
joefarebrother marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// f.close() | ||
// f.write("...") | ||
// We presume this to not be very common. | ||
bbReachableStrict(this.asCfgNode().getBasicBlock(), raises.asCfgNode().getBasicBlock()) | ||
} | ||
} | ||
|
||
private predicate cfgGetASuccessor(ControlFlowNode src, ControlFlowNode sink) { | ||
sink = src.getASuccessor() | ||
} | ||
private predicate bbSuccessor(BasicBlock src, BasicBlock sink) { sink = src.getASuccessor() } | ||
|
||
pragma[inline] | ||
private predicate cfgGetASuccessorPlus(ControlFlowNode src, ControlFlowNode sink) = | ||
fastTC(cfgGetASuccessor/2)(src, sink) | ||
private predicate bbReachableStrict(BasicBlock src, BasicBlock sink) = | ||
fastTC(bbSuccessor/2)(src, sink) | ||
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. I think you should be able to use |
||
|
||
pragma[inline] | ||
private predicate cfgGetASuccessorStar(ControlFlowNode src, ControlFlowNode sink) { | ||
src = sink | ||
or | ||
cfgGetASuccessorPlus(src, sink) | ||
private predicate bbReachableRefl(BasicBlock src, BasicBlock sink) { | ||
bbReachableStrict(src, sink) or src = sink | ||
} | ||
|
||
/** A call to the `.close()` method of a file object. */ | ||
|
@@ -87,7 +90,16 @@ class OsCloseCall extends FileClose { | |
|
||
/** A `with` statement. */ | ||
class WithStatement extends FileClose { | ||
WithStatement() { this.asExpr() = any(With w).getContextExpr() } | ||
With w; | ||
|
||
WithStatement() { this.asExpr() = w.getContextExpr() } | ||
|
||
override predicate guardsExceptions(DataFlow::CfgNode raises) { | ||
super.guardsExceptions(raises) | ||
or | ||
// Check whether the exception is raised in the body of the with statement. | ||
raises.asExpr().getParent*() = w.getBody().getAnItem() | ||
} | ||
} | ||
|
||
/** Holds if an exception may be raised at `raises` if `file` is a file object. */ | ||
|
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
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.
In case they belong to the same basic block, there is no logic guaranteeing that
raises
comes beforethis
in that basic block.