Repository navigation
A return through a finally releases the scopes the finally reads after it runs; nested finallys all run - #598
Merged
Conversation
…r it runs
A `return` from a `try` body or catch clause released the whole frame and
then ran the finally's return copy, which could still read those scopes:
under -mm=rc `let s = ...; try { return x; } finally { log(s); }` read a
freed string. The return now stops at the scope around the `try`
(GenContext::finallyGuardScope), and the finally ends with return-only
releases (RETURN_ONLY_ATTR_NAME) for the rest, which TryOpLowering keeps
in the return copy alone.
A return through two nested finallys also skipped the outer one in every
memory model: the inner return copy's ReturnOp now takes over the cleanup
the outer TryOp gave the original returns.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… inner finally Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What was wrong
Under
-mm=rc, areturnfrom atrybody or catch clause released the whole frame (mlirGenScopeExit(FullStack)) and only then ran the copy of thefinallythat TryOpLowering routes it through. That copy can still read every scope around thetry:read a freed
s(p4r ###########;instead ofp4r p4;).Separately, in every memory model, a
returnthrough two nestedfinallys ran only the inner one: the outer TryOp lowers first and points the returns at its finally copy, then the inner TryOp re-points them at its own, whose closingReturnOpwent straight to the function's return block.Fix
trywith afinallygives its body and catch clause a guard context (GenContext::finallyGuardScope, a self-pointer so copies never inherit it). Areturnreleases up to there and stops. Thefinallyends withRETURN_ONLY_ATTR_NAMEreleases for the scopes from there out to the next guard (mlirGenReleaseOwnedOnReturn), the counterpart of the unwind-only releases from An exception out of a try/finally, a nested block in a try, or a throw releases the scopes it leaves #596.ReturnOptakes over the cleanup an outer TryOp had set on the original returns, so a return chains through every enclosingfinally. The outer guard then releases the outer scopes, once.Not changed:
usingdisposal on the return path (mlirGenDisposable(FullStack)) still runs before the finally, andbreak/continuethrough a finally still release the loop-body scope first. These are the same kind of bug and are left as follow-ups.Tests
00try_return_through_finally.ts(compile + JIT + TSLANG_CORPUS): return from body, bare return, from a catch, from a nested block, through two finallys, from a try in a catch, from a loop, from the finally itself, plus normal completion, an exception, and an empty inner finally. Each case reads its values again afterchurn().00try_unwind_outer_locals.ts'sreturnThroughFinallynow readssin its finally, which it avoided because of this bug.--emit=mlir-affine --verify-ownershipis quiet under rc and own.ctest -C Release -j 8 --timeout 300: 100% passed (4150).🤖 Generated with Claude Code