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. Same bug as bellard/quickjs#527, fixed in bellard/quickjs#627. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Fix a use-after-free of the
Workermessage port inhandle_posted_message().Problem
handle_posted_message()readsport->on_message_funconly after callingJS_ReadObject()andJS_NewObject(), either of which can trigger the cycle GC. If the owningWorkeris reachable only through a self-cycle (for examplew.self_ref = w; w = null;), the cycle GC finalizes it andjs_worker_finalizer()freesport, making the later read a heap-use-after-free. ASan reports the read inhandle_posted_message(quickjs-libc.c:2738) and the free in the cycle GC.This matches the upstream bug reported in bellard/quickjs#527 and fixed in bellard/quickjs#627; the
quickjs-ngcode is identical and still unfixed.Fix
Duplicate the handler reference (
JS_DupValue(ctx, port->on_message_func)) before any allocation and release it at the end of the function, soportis never dereferenced afterward. The duplicated reference continues to protect the handler during invocation, and the existing comment aboutfuncfreeing the handler is merged into the updated comment.Testing
tests/test_worker_gc.js, a reduced reproducer from heap-use-after-free inhandle_posted_messagebellard/quickjs#527 picked up automatically bytests.conf.cmake -B build-asan -DCMAKE_BUILD_TYPE=Debug -DQJS_ENABLE_ASAN=ON),build-asan/qjs tests/test_worker_gc.jsfails on every run withheap-use-after-freeinhandle_posted_message, andbuild-asan/run-test262 -c tests.confaborts with the same report.tests/test_worker_gc.jspasses 6 of 6 runs andbuild-asan/run-test262 -c tests.confpasses, includingtests/test_worker.js.tests/test_std.js(second pipe handler, 100 ms timeout) failed in about one of three full ASan runs on a loaded machine both with and without this patch.AI-assisted: drafted with an AI coding assistant; reproduced and tested locally before submitting.