Skip to content

Fix stale getArrayBuffer view and host leaks on failure paths (follow-up to #248) - #276

Open
jackkav wants to merge 4 commits into
justjake:mainfrom
jackkav:fix-views-and-leaks
Open

jackkav wants to merge 4 commits into
justjake:mainfrom
jackkav:fix-views-and-leaks

Conversation

@jackkav

@jackkav jackkav commented Oct 16, 2026 •

Copy link
Copy Markdown

Layer: TypeScript wrapper
Review just this PR: jackkav/quickjs-emscripten@fix-memory-growth-views...fix-views-and-leaks
Stack: #248 → #276 → #277 → (#278 | #279 → #280 → #281 → #282). Issues: #274, #275.

What

  • getArrayBuffer returned a HEAPU8.subarray view that heap growth detaches (same class as Fix WASM memory view invalidation when memory grows #248). Now returns a copy.
  • consume*CharPointer: free the C string even if UTF8ToString throws.
  • newArrayBuffer: free the host buffer if QuickJS fails to create the ArrayBuffer (it only takes ownership on success).

Test: 3 new tests + 5 for #248 itself; each fails without its fix. Leak tests spy on _malloc/_free, and every test's afterEach disposal runs the runtime's no-leak assertion (and the sanitizer check on debug builds).
Verified on: All variants in the test matrix, TS-only so published 0.32.0 wasm is representative; the same tests also pass on freshly built wasm at the top of the stack (751 passed, 0 failed).
Behavior change: getArrayBuffer result is a copy (dispose() is a no-op). newArrayBuffer throws the QuickJS error instead of returning an exception-tagged handle.
Overlap: Builds on #248 (includes its commits rebased on main, plus regression tests for it). #207 does not touch these code paths.

🤖 Generated with Claude Code

justjake and others added 4 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>
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