Skip to content

Python: fix shared CFG exception-handler reachability - #22380

Open
yoff wants to merge 2 commits into
github:mainfrom
yoff:yoff-fix-shared-cfg-ssa-regressions
Open

Python: fix shared CFG exception-handler reachability#22380
yoff wants to merge 2 commits into
github:mainfrom
yoff:yoff-fix-shared-cfg-ssa-regressions

Conversation

@yoff

@yoff yoff commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes exception-handler reachability in the shared Python CFG introduced by #21921 and now present on main. Testing the dataflow switch-over in #21925 surfaced the bug.

Leaf expressions that may throw, including bare yield and plain imports, were not always connected to typed exception-handler entries. The fix uses the canonical shared-CFG injects mapping when starting exceptional completion, so these expressions reach the appropriate catch entry.

Tests

Adds focused inline coverage for:

  • bare yield reaching a GeneratorExit handler;
  • a plain import reaching an ImportError handler.

DCA impact

The switch-over DCA in #38556 exposed effects of the missing exception edges in mypy, CPython, and youtube-dl. Restoring these edges recovers the affected catch reachability and downstream flow.

Validation

  • codeql test run python/ql/test/library-tests/ControlFlow/shared-cfg-exceptions
  • codeql query format --check-only python/ql/test/library-tests/ControlFlow/shared-cfg-exceptions/ExceptionReachabilityTest.ql

Copilot AI balanced review requested due to automatic review settings August 19, 2026 11:10
@yoff
yoff requested a review from a team as a code owner August 19, 2026 11:10
@yoff yoff added the no-change-note-required This PR does not need a change note label Aug 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes shared Python CFG/SSA regressions affecting exception-handler reachability and module-export phi uses.

Changes:

  • Routes exceptional completion through canonical CFG nodes.
  • Preserves synthetic normal-exit uses for generic SSA definitions.
  • Adds regression tests for both fixes.
Show a summary per file
File Description
python/ql/lib/semmle/python/controlflow/internal/AstNodeImpl.qll Uses canonical CFG-node mapping for exception flow.
python/ql/lib/semmle/python/dataflow/new/internal/SsaImpl.qll Supports phi definitions when resolving uses.
python/ql/test/library-tests/ControlFlow/shared-cfg-exceptions/test.py Adds yield and import exception cases.
python/ql/test/library-tests/ControlFlow/shared-cfg-exceptions/ExceptionReachabilityTest.ql Checks typed-handler reachability.
python/ql/test/library-tests/ControlFlow/shared-cfg-exceptions/ExceptionReachabilityTest.expected Records expected exception results.
python/ql/test/library-tests/dataflow-new-ssa/test.py Adds conditional module-export coverage.
python/ql/test/library-tests/dataflow-new-ssa/SsaTest.ql Checks synthetic exit uses for phi definitions.

Review details

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 6/7 changed files
  • Comments generated: 0
  • Review effort level: Balanced

@yoff yoff added the Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish label Aug 19, 2026
@yoff
yoff force-pushed the yoff-fix-shared-cfg-ssa-regressions branch from 9055769 to 94aa7e2 Compare August 19, 2026 11:24
@yoff
yoff force-pushed the yoff/python-shared-cfg-dataflow-flip branch from 0394945 to c69ba9a Compare August 19, 2026 15:39
@yoff
yoff force-pushed the yoff-fix-shared-cfg-ssa-regressions branch from 94aa7e2 to a93d631 Compare August 19, 2026 15:42
@yoff yoff changed the title Python: fix shared CFG and SSA correctness regressions Python: fix shared CFG exception-handler reachability Aug 19, 2026
yoff and others added 2 commits August 20, 2026 08:54
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@yoff
yoff force-pushed the yoff-fix-shared-cfg-ssa-regressions branch from a93d631 to 3dfc77e Compare August 20, 2026 06:55
@yoff
yoff requested review from a team as code owners August 20, 2026 06:55
@yoff
yoff changed the base branch from yoff/python-shared-cfg-dataflow-flip to main August 20, 2026 06:56
or
mayThrow(ast) and
n.isIn(ast) and
n.injects(ast) and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, I think perhaps you need to add the relevant nodes to postOrInOrder instead. Otherwise, if these are leaf nodes, then you end up without a separate after-node, which means that unconditional exceptions cannot be represented.
(The fact that we have always = false may save us for now, but flipping that boolean wouldn't work, so it seems a bit too brittle.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess perhaps we could add a consistency check to verify that beginAbruptCompletion always sit on isIn or isAdditional nodes. I believe that's an implicit assumption by the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish no-change-note-required This PR does not need a change note Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants