-
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -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 `gaurdsExceptions`) for the case where the exception call | ||
joefarebrother marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 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. That requires extra predicates to restrict the source and sink, right? |
||
|
||
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. */ | ||
|
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.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.
raises
must come beforethis
regardless because we're checking the that exceptional successor ofraises
reachesthis
on the basic block level.