Skip to content

Fix use-after-free when JS_NewContext runs out of memory - #1781

Merged
saghul merged 1 commit into
quickjs-ng:masterfrom
IamYipi:fix/new-context-oom
Oct 1, 2026
Merged

saghul merged 1 commit into
quickjs-ng:masterfrom
IamYipi:fix/new-context-oom

Conversation

@IamYipi

@IamYipi IamYipi commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Fixes #1181.

On current master the 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 allocates class_proto. When that allocation fails, the context is freed but stays on the list:

add_gc_object(rt, &ctx->header, JS_GC_OBJ_TYPE_JS_CONTEXT);

ctx->class_proto = js_malloc_rt(rt, sizeof(ctx->class_proto[0]) *
                                rt->class_count);
if (!ctx->class_proto) {
    js_free_rt(rt, ctx);    // ctx->header is still on rt->gc_obj_list
    return NULL;
}

The next add_gc_object() on that runtime writes through the freed node. Under ASAN that is a heap-use-after-free in __list_add, called from add_gc_object() in JS_NewContextRaw(); without sanitizers the next GC trips gc_scan: Assertion 'JS_REF_COUNT(p) > 0' failed.

The window is narrow: only limits where the JSContext itself still fits but class_proto does not. That is why stepping the limit by base/8, as the test in the issue does, walks past it.

The fix unlinks the context before freeing it.

Test

new_context_low_memory in api-test.c, based on the test from the issue. It reads the runtime's baseline with JS_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 its assert(ok > 0) fails on master. It uses new_runtime(), so a leak on any failure path aborts in JS_FreeRuntime().

build master this PR
ASAN + UBSan heap-use-after-free in add_gc_object() passes
-O2, no sanitizers gc_scan assertion passes

The whole api-test binary, 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 in JS_NewContext() and the JS_AddIntrinsic*() helpers look covered on master; this was the only failure the sweep found.

make test passes (0/117 errors), and so does make cxxtest.

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
saghul merged commit 8e14a39 into quickjs-ng:master Oct 1, 2026
125 of 128 checks passed
@IamYipi

IamYipi commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for reviewing and merging this, @saghul. It was a pleasure to contribute to QuickJS-ng.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash in JS_NewContext with low memory limit

2 participants