Node-API (Chakra): fix heap corruption when an ObjectWrap constructor throws#199
Merged
bkaradzic-microsoft merged 2 commits intoJun 17, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a ChakraCore Node-API (N-API) use-after-free that can occur when an ObjectWrap-based constructor throws, by proactively detaching any remaining wrap finalizer so it can’t run later during GC and corrupt the heap.
Changes:
- In Chakra’s N-API callback trampoline, detect pending exceptions after a construct call and
napi_remove_wrap()onthiswhile preserving the exception. - Make
napi_remove_wrap()tolerate anullptrout-param so it can be called for cleanup-only scenarios. - Add a regression test that repeatedly throws from
TextDecoder’s wrapped constructor and then performs allocations/decoding to help surface corruption.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Core/Node-API/Source/js_native_api_chakra.cc | Preserves pending exceptions across a wrap-detach cleanup on constructor-throw; makes napi_remove_wrap accept nullptr out-param. |
| Tests/UnitTests/Scripts/tests.ts | Adds a regression test that repeatedly triggers the constructor-throw path for wrapped constructors. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
… throws When a napi ObjectWrap subclass constructor throws (e.g. TextDecoder given an unsupported encoding), the C++ instance is destroyed during stack unwinding, but the wrap finalizer registered by napi_wrap() stays attached to the JS `this` object. A later GC then runs the finalizer on the freed native instance -> use-after-free / double free, surfacing as non-deterministic heap corruption (STATUS_HEAP_CORRUPTION 0xC0000374 / access violation) crashes far from the throw site. Root cause: the addon-api ~ObjectWrap() is supposed to detach the wrap on construction failure, but it calls napi_get_reference_value() on the wrap's weak (refcount 0) reference to obtain `this`, and the Chakra backend returns null for every refcount-0 reference (it cannot track weak-reference liveness with the in-box jsrt API). So ~ObjectWrap() sees an empty object and skips napi_remove_wrap(), leaving the dangling finalizer attached. Fix: in the Chakra construct-call trampoline, if the callback leaves a pending JS exception, detach any wrap left on `this` via napi_remove_wrap() (preserving the pending exception) so the finalizer cannot run on the freed instance. Also guard napi_remove_wrap() against a null `result` out-param (it is now called with nullptr, matching upstream which guards this). Adds a regression test that throws from a wrapped constructor many times and then exercises the heap; without the fix this reliably corrupts the heap on Chakra (Win32 x64/x86). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
a6f8390 to
34e8302
Compare
bghgary
approved these changes
Jun 17, 2026
Co-authored-by: Gary Hsu <bghgary@users.noreply.github.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
Fixes a heap corruption in the Chakra Node-API implementation when an
ObjectWrap-based constructor throws.Split out of #198 as an independent fix, per review.
Problem
When a wrapped (
napi_wrap) constructor throws, the C++ObjectWrapinstance is destroyed by stack unwinding inside the callback, but the wrap finalizer registered bynapi_wrap()stays attached tothis.~ObjectWrap()cannot detach it, becausenapi_get_reference_value()returns null for the wrap's weak (refcount 0) reference. A later GC then runs the finalizer on the freed native instance — a use-after-free that surfaces as non-deterministicSTATUS_HEAP_CORRUPTION(0xC0000374).Fix
Core/Node-API/Source/js_native_api_chakra.cc:ExternalCallback, after a construct call that left a pending JS exception, detach the wrap (napi_remove_wrap) before returning, then re-set the exception so it is preserved across the cleanup.napi_remove_wrapagainst anullptrresultout-param (the finalizer-cleanup call above passesnullptr), so it no longer dereferences a null pointer.Test
Adds a regression test (
Tests/UnitTests/Scripts/tests.ts) that throws from a wrapped constructor 100× to create many dangling wraps, then allocates/decodes to exercise the heap and surface any corruption within the run. It usesTextDecoderas a convenient throwing wrapped constructor; the test passes onmain's existingTextDecoderbehavior and is independent of the TextDecoder label change in #198.Verification
Reliably reproduced as
STATUS_HEAP_CORRUPTIONon Chakra (Win32 x64/x86) before the fix; passes after.