Repository navigation
Fix use-after-free when JS_NewContext runs out of memory - #1781
Merged
Merged
Conversation
JS_NewContextRaw() puts the new context on the runtime's GC object list before it allocates class_proto. When that allocation fails, the context is freed but stays on the list, so the next add_gc_object() on the same runtime writes through freed memory, and a later GC or JS_FreeRuntime() walks it. Unlink the context before freeing it. The new api-test steps the memory limit across JS_NewContext's first allocations in small steps, then up to where contexts fit, and checks that every attempt either fails cleanly or succeeds.
saghul
approved these changes
Sep 30, 2026
Contributor
Author
|
Thank you for reviewing and merging this, @saghul. It was a pleasure to contribute to QuickJS-ng. |
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 #1181.
On current
masterthe crash from the issue no longer reproduces with the loop given there, but one failure is left, and it is a use-after-free.JS_NewContextRaw()puts the new context on the runtime's GC object list, then allocatesclass_proto. When that allocation fails, the context is freed but stays on the list:The next
add_gc_object()on that runtime writes through the freed node. Under ASAN that is aheap-use-after-freein__list_add, called fromadd_gc_object()inJS_NewContextRaw(); without sanitizers the next GC tripsgc_scan: Assertion 'JS_REF_COUNT(p) > 0' failed.The window is narrow: only limits where the
JSContextitself still fits butclass_protodoes not. That is why stepping the limit bybase/8, as the test in the issue does, walks past it.The fix unlinks the context before freeing it.
Test
new_context_low_memoryinapi-test.c, based on the test from the issue. It reads the runtime's baseline withJS_ComputeMemoryUsage(), raises the limit by 8 bytes at a time across the first 8 KB, so that each of the first allocations gets its turn to fail, then by 1 KB until four contexts fit. Anchoring on the measured baseline keeps it valid as the context's footprint changes: with the fixed range from the issue, no context fits below 128 KB any more, so itsassert(ok > 0)fails onmaster. It usesnew_runtime(), so a leak on any failure path aborts inJS_FreeRuntime().masterheap-use-after-freeinadd_gc_object()-O2, no sanitizersgc_scanassertionThe whole
api-testbinary, new test included, runs in 19 ms in a release build.I also swept every limit from 1 byte to 300 KB, one byte at a time, under ASAN, UBSan and LeakSanitizer: 307,200 calls. With this change each
JS_NewContext()either fails cleanly or succeeds (from 263,464 bytes up), with no reports. So the other out-of-memory paths inJS_NewContext()and theJS_AddIntrinsic*()helpers look covered onmaster; this was the only failure the sweep found.make testpasses (0/117 errors), and so doesmake cxxtest.