Skip to content

Java: Add AnnotatedExitNodes to the CFG. #19885

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions java/ql/lib/semmle/code/java/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
*/

import java
private import codeql.util.Boolean
private import Completion
private import controlflow.internal.Preconditions
private import controlflow.internal.SwitchCases
Expand All @@ -100,6 +101,7 @@ module ControlFlow {
private newtype TNode =
TExprNode(Expr e) { hasControlFlow(e) } or
TStmtNode(Stmt s) or
TAnnotatedExitNode(Callable c, Boolean normal) { exists(c.getBody()) } or
TExitNode(Callable c) { exists(c.getBody()) } or
TAssertThrowNode(AssertStmt s)

Expand Down Expand Up @@ -189,6 +191,38 @@ module ControlFlow {
override Location getLocation() { result = s.getLocation() }
}

/** A control flow node indicating the normal or exceptional termination of a callable. */
class AnnotatedExitNode extends Node, TAnnotatedExitNode {
Callable c;
boolean normal;

AnnotatedExitNode() { this = TAnnotatedExitNode(c, normal) }

override Callable getEnclosingCallable() { result = c }

override ExprParent getAstNode() { result = c }

/** Gets a textual representation of this element. */
override string toString() {
normal = true and result = "Normal Exit"
or
normal = false and result = "Exceptional Exit"
}

/** Gets the source location for this element. */
override Location getLocation() { result = c.getLocation() }
}

/** A control flow node indicating normal termination of a callable. */
class NormalExitNode extends AnnotatedExitNode {
NormalExitNode() { this = TAnnotatedExitNode(_, true) }
}

/** A control flow node indicating exceptional termination of a callable. */
class ExceptionalExitNode extends AnnotatedExitNode {
ExceptionalExitNode() { this = TAnnotatedExitNode(_, false) }
}

/** A control flow node indicating the termination of a callable. */
class ExitNode extends Node, TExitNode {
Callable c;
Expand Down Expand Up @@ -1264,11 +1298,17 @@ private module ControlFlowGraphImpl {
*/
cached
Node succ(Node n, Completion completion) {
// After executing the callable body, the final node is the exit node.
// After executing the callable body, the final nodes are first the
// annotated exit node and then the final exit node.
exists(Callable c | last(c.getBody(), n, completion) |
result.(ExitNode).getEnclosingCallable() = c
if completion instanceof ThrowCompletion
then result.(ExceptionalExitNode).getEnclosingCallable() = c
else result.(NormalExitNode).getEnclosingCallable() = c
)
or
completion = NormalCompletion() and
n.(AnnotatedExitNode).getEnclosingCallable() = result.(ExitNode).getEnclosingCallable()
or
// Logic expressions and conditional expressions execute in AST pre-order.
completion = NormalCompletion() and
(
Expand Down
14 changes: 2 additions & 12 deletions java/ql/lib/semmle/code/java/security/PathSanitizer.qll
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,10 @@ private module ValidationMethod<DataFlow::guardChecksSig/3 validationGuard> {
* Holds if `m` validates its `arg`th parameter by using `validationGuard`.
*/
private predicate validationMethod(Method m, int arg) {
exists(
Guard g, SsaImplicitInit var, ControlFlow::ExitNode exit, ControlFlowNode normexit,
boolean branch
|
exists(Guard g, SsaImplicitInit var, ControlFlow::NormalExitNode normexit, boolean branch |
validationGuard(g, var.getAUse(), branch) and
var.isParameterDefinition(m.getParameter(arg)) and
exit.getEnclosingCallable() = m and
normexit.getANormalSuccessor() = exit and
1 = strictcount(ControlFlowNode n | n.getANormalSuccessor() = exit)
|
exists(ConditionNode conditionNode |
g = conditionNode.getCondition() and conditionNode.getABranchSuccessor(branch) = exit
)
or
normexit.getEnclosingCallable() = m and
g.controls(normexit.getBasicBlock(), branch)
)
}
Expand Down