Repository navigation
Conversation
…_message The cycle GC can run during JS_ReadObject() or JS_NewObject() and finalize a Worker that is only reachable through a self-cycle, which frees its message handler. handle_posted_message() then read port->on_message_func from freed memory. Take the reference to the handler function before any allocation and do not touch port afterwards. Add tests/test_worker_gc.js as a regression test. Fixes bellard#527 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
bellard
pushed a commit
that referenced
this pull request
Nov 28, 2026
Owner
|
applied |
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.
Fixes #527
Problem
handle_posted_message()readsport->on_message_funcafter callingJS_ReadObject()andJS_NewObject(), either of which can trigger the cycle GC. If the owningWorkeris only reachable through a self-cycle (for example,w.self_ref = w; w = null;), the GC finalizes it andjs_worker_finalizer()freesport, causing a heap-use-after-free on the subsequent read. ASan reports the read atquickjs-libc.c:2460, with the free injs_free_portviagc_free_cyclesandjs_trigger_gc.Fix
Duplicate the handler reference (
JS_DupValue(ctx, port->on_message_func)) before any allocation and release it at the end of the function.portis not dereferenced after that point. The duplicated reference still protects the handler call iffuncfrees the handler, and the existing comment aboutfuncfreeing the handler is merged into the new comment.Testing
tests/test_worker_gc.js, a reduced reproducer from the issue that runs as part ofmake test.make CONFIG_ASAN=y), the new test and the issue's PoC fail withheap-use-after-freeinhandle_posted_message.make CONFIG_ASAN=y testexits 0, includingtests/test_worker.js.AI-assisted: drafted with an AI coding assistant; reproduced and tested locally before submitting.