Report a dead target as empty from napi_get_reference_value - #67
Merged
Conversation
QuickJS frees reference cycles in two passes, so a finalizer can still see other members of its own cycle as zombies -- that is what JS_IsLiveObject exists for. A weak reference slot pointing at such an object has not been cleared yet, because reset_weak_ref only runs from that object's own free_object. napi_get_reference_value would therefore dup_inner() a zombie, handing the caller a fresh reference to memory the collector is about to release; whichever scope owns the resulting napi_value then frees it again and the process trips a heap-use-after-free. Report such a target as empty instead, which is what callers already handle for a collected weak reference. The check is deliberately restricted to objects. JS_IsLiveObject reports every non-object -- strings, symbols, bigints -- as not live, so applying it unconditionally makes every reference to one read back as empty (51 node:crypto tests fail that way). Co-Authored-By: Claude Opus 5 (1M context) <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.
Problem
QuickJS frees reference cycles in two passes, so a finalizer can still see other members of its own cycle as zombies — that is exactly what
JS_IsLiveObjectexists for. A weak reference slot pointing at such an object has not been cleared yet, becausereset_weak_refonly runs from that object's ownfree_object.napi_get_reference_valuetherefore callsdup_inner()on a zombie and hands the caller a fresh reference to memory the collector is about to release. Whichever scope owns the resultingnapi_valuefrees it again on close, and the process trips a heap-use-after-free.Found while fixing a TLS memory leak in edgejs (WAX-609). That leak had the side effect of keeping
TLSWrapwrappers permanently alive, which is what kept this latent — as soon as the wrappers could actually be collected,TlsWrapFinalizereached back throughnapi_get_reference_valueand resurrected a zombie:Fix
Report such a target as empty, which is what callers already handle for a collected weak reference.
Note on the
JS_IsObjectguardThe check is deliberately restricted to objects.
JS_IsLiveObjectreturns false for every non-object — strings, symbols, bigints — so applying it unconditionally makes every reference to one read back as empty. I hit exactly that: the first version of this patch failed 51node:cryptotests because every string reference vanished.Verification
Built against edgejs (
EDGE_NAPI_PROVIDER=quickjs):node:tls+node:https+node:crypto— 433/433, matching baseline.-fsanitize=address) clean on the four tests that previously aborted withcorrupted double-linked list.This is defence-in-depth and is independent of the edgejs-side fix: edgejs is green and ASAN-clean with or without it. It guards any addon whose finalizer reaches back into the object graph.
🤖 Generated with Claude Code