Fix exception ref-count leak in non-ref catch handlers.#12860
Open
cfallin wants to merge 1 commit intobytecodealliance:mainfrom
Open
Fix exception ref-count leak in non-ref catch handlers.#12860cfallin wants to merge 1 commit intobytecodealliance:mainfrom
cfallin wants to merge 1 commit intobytecodealliance:mainfrom
Conversation
When a Wasm `throw` instruction executes, the `throw_ref` libcall was cloning the GC ref (incrementing the refcount), but the catch handler never decremented it. This caused every caught exception to leak, leading to unbounded GC heap growth in throw/catch loops. Two fixes: 1. Remove the unnecessary `clone_gc_ref()` in `throw_ref`. The `throw`/`throw_ref` instructions consume the exnref operand, so ownership transfers naturally to `pending_exception` without cloning. 2. In `create_catch_block`, emit a `drop_gc_ref` call for non-ref catches (`Catch::One`, `Catch::All`) after field extraction. These catches consume the exnref without passing it to the branch target, so the refcount must be decremented. Also adds `Store::gc_heap_size()` / `StoreContext::gc_heap_size()` accessors and a `throw_catch_many_times` integration test that throws and catches 100K exceptions in a loop, asserting the GC heap stays within a single 64 KiB page.
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.
When a Wasm
throwinstruction executes, thethrow_reflibcall was cloning the GC ref (incrementing the refcount), but the catch handler never decremented it. This caused every caught exception to leak, leading to unbounded GC heap growth in throw/catch loops.Two fixes:
Remove the unnecessary
clone_gc_ref()inthrow_ref. Thethrow/throw_refinstructions consume the exnref operand, so ownership transfers naturally topending_exceptionwithout cloning.In
create_catch_block, emit adrop_gc_refcall for non-ref catches (Catch::One,Catch::All) after field extraction. These catches consume the exnref without passing it to the branch target, so the refcount must be decremented.Also adds
Store::gc_heap_size()/StoreContext::gc_heap_size()accessors and athrow_catch_many_timesintegration test that throws and catches 100K exceptions in a loop, asserting the GC heap stays within a single 64 KiB page.