Skip to content

Java: Update the CFG for assert statements to make them proper guards. #19733

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 4 commits into from
Jun 13, 2025

Conversation

aschackmull
Copy link
Contributor

This upgrades the CFG for Java assert statements to be proper guards and includes the message part, which was previously a CFG orphan, in the CFG. That is, the false edge of the condition now leads to an exception edge rather than simply ignoring the value.
For things like nullness, this is definitely what we want, and in general it's probably fine to assume that asserts are evaluated (even though they technically can be disabled).

@github-actions github-actions bot added the Java label Jun 11, 2025
@aschackmull aschackmull marked this pull request as ready for review June 12, 2025 09:04
@Copilot Copilot AI review requested due to automatic review settings June 12, 2025 09:04
@aschackmull aschackmull requested a review from a team as a code owner June 12, 2025 09:04
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refines how Java assert statements are represented in the control‐flow graph, treating them as true guards and incorporating the optional message expression into the flow.

  • Expanded the assertion framework predicates to include AssertStmt nodes.
  • Removed direct handling of AssertStmt in nullness flow in favor of the updated CFG.
  • Enhanced ControlFlowGraph.qll to generate throw and normal edges for asserts, update boolean contexts, and record thrown types.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
java/ql/lib/semmle/code/java/frameworks/Assertions.qll Added AssertStmt cases to the assert‐failure guard
java/ql/lib/semmle/code/java/dataflow/Nullness.qll Removed direct AssertStmt null‐guard branch
java/ql/lib/semmle/code/java/ControlFlowGraph.qll Integrated assert into CFG edges and completions
java/ql/lib/change-notes/2025-06-12-assert-cfg.md Documented CFG change for assert analysis
Comments suppressed due to low confidence (2)

java/ql/lib/semmle/code/java/dataflow/Nullness.qll:143

  • Removing the direct AssertStmt null‐guard branch may break nullness inference for plain assert statements. Ensure that the new CFG paths fully cover this case or reintroduce null‐guard logic for AssertStmt.
result.asStmt().(AssertStmt).getExpr() = nullGuard(v, true, false)

java/ql/lib/semmle/code/java/ControlFlowGraph.qll:1119

  • It would be beneficial to add unit tests that verify both normal and exceptional CFG edges for assert statements (with and without messages), to ensure the new logic behaves as expected.
exists(AssertStmt assertstmt | assertstmt = n |

@aschackmull
Copy link
Contributor Author

I've spot-checked a bunch of the changed results. The removed results were FPs, and the new results were typically redundant checks - e.g. both asserting a condition and then also checking it in an if statement.

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion, otherwise LGTM.

Co-authored-by: Tom Hvitved <hvitved@github.com>
@aschackmull aschackmull merged commit 8838104 into github:main Jun 13, 2025
16 checks passed
@aschackmull aschackmull deleted the java/assert-cfg branch June 13, 2025 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants