Skip to content

Validate runtime limits to 32 bits, unsigned pointer arrays (part of #275) - #278

Open
jackkav wants to merge 8 commits into
justjake:mainfrom
jackkav:validate-limits-and-unsigned-ptrs
Open

jackkav wants to merge 8 commits into
justjake:mainfrom
jackkav:validate-limits-and-unsigned-ptrs

Conversation

@jackkav

@jackkav jackkav commented Oct 16, 2026 •

Copy link
Copy Markdown

Layer: TypeScript wrapper (argument validation and docs). It does not change how the engine counts memory; that gap is listed in #275
Review just this PR: jackkav/quickjs-emscripten@fix-oom-and-hostref...validate-limits-and-unsigned-ptrs
Stack: #248 → #276 → #277 → (#278 | #279 → #280 → #281 → #282). Issues: #274, #275.

What

  • setMemoryLimit / setMaxStackSize throw RangeError for NaN, Infinity and ≥ 2^32 instead of wrapping in the 32-bit size_t.
  • Pointer arrays are Uint32Array (resolves the old TODO); heap addresses ≥ 2 GiB no longer read as negative.
  • Docs: don't call getWasmMemory().grow() yourself.

Test: Argument-validation and unsigned-pointer tests fail without the change. One further test is a regression guard (passes on main too): a valid 1 MiB limit still rejects an 8 MiB allocation and the runtime disposes cleanly.
Verified on: All variants in the test matrix (sync/async × release/debug × quickjs/quickjs-ng), TS-only so the published 0.32.0 wasm is representative: all pass. (This PR is a sibling of #279, so it is not part of the freshly-built-wasm run of the C stack.)
Behavior change: setMemoryLimit/setMaxStackSize now throw RangeError for values that used to wrap silently.
Overlap: Not a memory-accounting change, so it doesn't overlap #207's malloc_usable_size work (which is where the under-enforced limit in #275 would be addressed). Sibling of #279, both on #277.

🤖 Generated with Claude Code

justjake and others added 7 commits October 15, 2026 20:35
When Emscripten's WASM memory grows (due to -sALLOW_MEMORY_GROWTH), all
existing TypedArray views become detached because the underlying
ArrayBuffer is replaced. This caused bugs where reading from views after
FFI calls returned undefined values if memory had grown.

The fix introduces RefreshableTypedArray, a wrapper that lazily recreates
the TypedArray view when HEAPU8.buffer changes. This is a simple reference
comparison that only triggers view recreation when actually needed.

Affected call sites:
- runtime.ts: executePendingJobs - reads ctxPtrOut after QTS_ExecutePendingJob
- context.ts: newPromise - reads resolve/reject handles after QTS_NewPromiseCapability
- context.ts: getLength - reads uint32Out after QTS_GetLength
- context.ts: getOwnPropertyNames - reads outPtr and uint32Out after QTS_GetOwnPropertyNames

Also fixed: getOwnPropertyNames was using HEAP8.buffer instead of HEAPU8.buffer

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- getArrayBuffer returned a HEAPU8.subarray view that is detached by the next
  WASM memory growth; return a copy instead.
- consumeJSCharPointer / consumeHeapCharPointer leaked the C string when
  UTF8ToString threw (e.g. strings longer than V8's max string length).
- newArrayBuffer leaked the host buffer (and returned an exception-tagged
  handle) when QuickJS failed to create the ArrayBuffer. It now frees the
  buffer and throws.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…reelist

- ModuleMemory helpers now throw on a null _malloc instead of writing at address 0.
- getOwnPropertyNames freed a plain-malloc'd array with js_free (QTS_FreeVoidPointer),
  unbalancing QuickJS allocation counters; free it with the module's free.
- HostRefMap: FIFO freelist used Array#shift (quadratic when draining); use a head index.
  Deleting an already-deleted id in a non-empty group no longer queues it for reuse twice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- setMemoryLimit / setMaxStackSize silently wrapped NaN, Infinity and values >= 2^32
  in the 32-bit size_t; they now throw RangeError. Fix the max stack size error message.
- Pointer arrays used Int32Array, which reads heap addresses >= 2 GiB as negative; use Uint32Array.
- Document that getWasmMemory().grow() must not be called by users.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

2 participants