Skip to content

Keep lazily initialized builtins usable after a failed first access - #1783

Open
IamYipi wants to merge 1 commit into
quickjs-ng:masterfrom
IamYipi:fix/autoinit-retry
Open

IamYipi wants to merge 1 commit into
quickjs-ng:masterfrom
IamYipi:fix/autoinit-retry

Conversation

@IamYipi

@IamYipi IamYipi commented Sep 30, 2026

Copy link
Copy Markdown

Builtin C functions (JS_DEF_CFUNC entries) are defined as auto-init properties and created on their first access. When that setup fails, JS_AutoInitProperty() has already turned the property into a plain data property holding undefined, so it stays undefined for the rest of the context's life:

val = func(realm, p, prop, pr->u.init.opaque);
js_autoinit_free(ctx->rt, pr);
prs->flags &= ~JS_PROP_TMASK;
pr->u.value = JS_UNDEFINED;
if (JS_IsException(val))
    return -1;

With a memory limit, a first access that runs out of memory therefore breaks the builtin in that context even after memory is available again. For JSON.parse:

first access of JSON.parse under the limit  ->  InternalError: out of memory   (expected)
later, without the limit: typeof JSON.parse ->  "undefined"                     (expected "function")

On master, setting the limit a few bytes above the current usage and raising it 4 bytes at a time, 39 of 1025 fresh contexts ended up like that.

This returns before converting the property when func fails, so the next access runs the setup again. func does not modify the object's properties, so there is nothing to undo.

Test

autoinit_after_failure in api-test.c compiles JSON.parse first, then runs it with the limit a few bytes above the current usage, raised 8 bytes at a time until it succeeds, and checks after each attempt that typeof JSON.parse is "function". Compiling beforehand keeps the parser out of the low-memory part, so only the property access is exercised. The test fails on master with and without sanitizers and passes with this change; it uses new_runtime(), so these paths are also checked for leaks.

make test (0/117 errors), make cxxtest and the whole api-test pass.

Builtin C functions are defined as auto-init properties and created on
their first access. When that setup failed, JS_AutoInitProperty() had
already turned the property into a plain data property holding
undefined, so the builtin stayed undefined for the rest of the
context's life: after an out-of-memory error on the first access of
JSON.parse, typeof JSON.parse was "undefined" even once memory was
available again.

Check for the exception before converting the property, so that the
next access runs the setup again. The setup function does not modify
the object's properties, so there is nothing to undo.

This branch has not been deployed

No deployments
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.

1 participant