Skip to content

QuickJS can crash while recovering from the memory limit: the Memory limit test segfaults on musl x86_64 under emulation #32

Open

Description

@marcobambini

The Memory limit test crashes the process on musl x86_64 when x86_64 is emulated on an arm64 host. Filing it so it is not lost: nothing that ships is affected — the CI job linux-musl-x86_64 build + test, which runs on real x86_64 hardware, passes — but the fragility underneath is real, it is in the bundled QuickJS rather than in the extension, and this environment reproduces it reliably.

Where it crashes

In test_aggregate_near_limit(0, free_kb), the loop in test_memory_limit that fills the 64 MB default limit with 1 KB ArrayBuffers, catches the "out of memory" error, then releases free_kb of it:

js_eval('globalThis.keep = []; try { for (;;) keep.push(new ArrayBuffer(1024)); } catch (e) {} keep.length -= 14; 0')
Segmentation fault

12 tests pass, then SIGSEGV. The exact free_kb it dies at moves with the heap: 11 on 1.4.1, 14 on current main.

What is and is not needed

musl glibc
arm64, native 16/16 16/16
x86_64, emulated with Rosetta crash 16/16
x86_64, emulated with qemu-user crash 16/16
x86_64, real (CI runner) 16/16 16/16

All three of musl, x86_64 and emulation are needed. Both emulators were checked because the first guess was that Rosetta was at fault; qemu crashes at the same statement, and says so itself:

qemu: uncaught target signal 11 (Segmentation fault) - core dumped

Checked with qemu by disabling Docker Desktop's rosetta and rosetta-wrapper binfmt entries so that qemu-x86_64 was the only enabled x86_64 handler. The container reports a 4 KB page size either way, so page-size rounding is not the difference.

What it is not

  • Not a stack overflow. AddressSanitizer labels it stack-overflow, but it still crashes with ulimit -s 65536, and the ASan report shows a shallow frame, so that label is misleading — ASan on musl is not well supported.
  • Not reproducible in isolation. The same js_eval on its own, at free_kb 10, 12, 14 and 16, survives. It needs the heap as the 12 preceding tests leave it, which fits the test's own comment that "how much an object costs is up to the allocator of the platform, which rounds allocations up by more once the heap of the process is warm".
  • Not introduced recently. It reproduces on 1.4.1 unchanged and on every branch since.

Reading

The test deliberately runs the runtime out of memory and then asks it to create an aggregate context, which the code already knows is the dangerous moment — src/sqlitejs.c:1176:

QuickJS cannot always recover from running out of memory while it creates a context: it can crash.

and src/sqlitejs.c:611 for the same reason at init. The workaround there is to create the context with the limit lifted. So the likely reading is that musl's allocator puts the out-of-memory boundary somewhere this recovery path still goes wrong, while glibc's rounding does not land there and real x86_64 musl lays the heap out differently again. The bug would then be in libs/quickjs.c (QuickJS 0.12.1), in the out-of-memory path, and the emulators merely produce a heap layout that exposes it.

To reproduce

On an arm64 macOS host with Docker:

docker run --rm -t --platform linux/amd64 -v "$PWD:/w" -w /w alpine:latest sh -c \
  'apk add --no-cache gcc make sqlite musl-dev linux-headers && make && make dist/main && ./dist/main'

Where this could go

  • Narrow it to the failing allocation inside QuickJS and see whether it is already fixed upstream in a release after 0.12.1, which would make this a bump rather than a patch.
  • If it is still there upstream, it is worth reporting there with this reproduction.
  • Either way it is low priority: no shipping platform is in the crashing column.

This issue originated with AI: Claude, Anthropic's AI model, found it while testing pull requests #22, #23 and #24 across platforms, and wrote this report.

Activity

  1. added
    bug
    on Nov 13, 2026
  2. marcobambini commented on Nov 13, 2026

    @marcobambini
    MemberAuthor

    Checked upstream. Not demonstrably fixed, but there is a lot of movement in these paths and a bump is the way to settle it.

    Bundled here is quickjs-ng 0.12.1 (released 2026-02-12). Upstream is at v0.18.0 (2026-11-01) — six minor releases ahead.

    The nearest fix is the neighbouring bug, not this one

    quickjs-ng#1781, "Fix use-after-free when JS_NewContext runs out of memory", merged 2026-10-01 and in v0.18.0, +35/-0 in JS_NewContextRaw.

    That is precisely the fragility this repository already documents and works around:

    QuickJS cannot always recover from running out of memory while it creates a context: it can crash.
    — src/sqlitejs.c:1176, and :611 for the same reason at init

    So the workaround there — creating the aggregate context with the memory limit lifted — could eventually be dropped. But it is not this crash's path: here the process dies inside the js_eval that fills the limit and catches the error, before any aggregate context is created.

    Other out-of-memory fixes landed since 0.12.1, none of which names this path either:

    release fix
    v0.13.0 prevent malloc_size underflow (#1372)
    v0.16.0 use-after-free when creating the promise resolving functions OOMs (#1613)
    v0.16.0 integer overflow in the TypedArray sort index allocation (#1611)
    v0.18.0 infinite loop on allocation failure in unicode_script() (#1829)

    A bump would change documented behavior here

    Worth knowing before anyone reaches for it, because this is the part that bites:

    quickjs-ng#1810, "Make OOM exceptions uncatchable by JS code", in v0.18.0, touching JS_ThrowOutOfMemory. It came out of #1773, where an embedder asked for opt-in terminal memory-limit semantics; the opt-in proposal (#1779) was not merged and the unconditional change was taken instead.

    That contradicts what js_set_memory_limit() promises today — "JavaScript that catches the error receives null instead when the limit leaves no room even for the error" — and the Memory limit test is built on try { for (;;) keep.push(new ArrayBuffer(1024)); } catch (e) {}, which would stop doing what it says.

    Two smaller ones in the same area:

    • v0.14.0 makes malloc(0) always return NULL (#1427), which meets the custom allocator in src/sqlitejs.c that accounts every allocation against the limit.
    • v0.16.0 adds an arena allocator and builds releases with mimalloc; v0.18.0 switches the usable-size hook to mi_usable_size (#1788). js_mem_usable_size(), which the memory limit is measured with, lives in exactly that area.

    How to settle it

    libs/quickjs.c here is a single 83k-line amalgamation with no upstream includes left, and the releases ship only binaries — but upstream has amalgam.js in the tree, which is presumably how this file was produced. So: regenerate from v0.18.0, build, and run the suite in the environment above. This crash is then the acceptance test, and either it goes away or we have a reproduction worth sending upstream.

    That is its own piece of work, given the three behavior changes, rather than something to fold into this issue. Leaving this at P3 until then.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions