Skip to content

fix(wasm): zero-initialize WASM page buffers to prevent memory corruption#12

Merged
diegosouzapw merged 1 commit into
mainfrom
fix/port-pr-142-zero-init-wasm-buffers
May 23, 2026
Merged

fix(wasm): zero-initialize WASM page buffers to prevent memory corruption#12
diegosouzapw merged 1 commit into
mainfrom
fix/port-pr-142-zero-init-wasm-buffers

Conversation

@diegosouzapw

Copy link
Copy Markdown
Owner

Summary

Ports upstream PR #142: zero-init WASM page buffers explicitly so the Ghostty terminal does not read stale bytes from freshly-grown pages.

Changes are in two files:

  • patches/ghostty-wasm-api.patch — adds explicit @memset(..., 0) in the Zig-side page buffer constructors (CI / bun run build:wasm rebuilds the wasm binary).
  • lib/terminal.test.ts — two new regression tests that exercise the previously-corrupted render shape.

Attribution

Thanks to @sauyon for the original implementation.

Test plan

  • bun run fmt && bun run lint && bun run typecheck
  • bun run build:wasm — rebuilt locally with Zig 0.15.2; 416 KB output
  • bun test — 333 tests pass (2 new regression tests), 0 fail
  • bun run build:lib

Risk

Low — the change is additive (zero-fill on creation). No API surface affected. Memory corruption fix; the worst-case is "harder-to-reproduce bugs reduce in frequency".

@gemini-code-assist

Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

…tion

Ghostty allocates page buffers via the wasm allocator (which calls
wasm_alloc -> memory.grow). New memory pages on grow are zero on most
runtimes but the cell layout depends on this being EXPLICITLY true:
some cell fields are inspected before the first write, and a stray
non-zero byte can corrupt the screen state in ways that surface much
later (wrong colors, stuck cursor, dropped grapheme clusters).

Patches/ghostty-wasm-api.patch is updated so the underlying buffer
arrays are explicitly memset-to-zero at construction. Adds two TS-side
regression tests that exercise the corrupted-render shape.

Co-authored-by: Sauyon Lee <git@sjle.co>
Inspired-by: coder#142
@diegosouzapw diegosouzapw force-pushed the fix/port-pr-142-zero-init-wasm-buffers branch from 5e198fa to 5b2067c Compare May 23, 2026 16:57
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
27.6% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@diegosouzapw diegosouzapw merged commit 74aa7f3 into main May 23, 2026
1 of 2 checks passed
@diegosouzapw diegosouzapw deleted the fix/port-pr-142-zero-init-wasm-buffers branch May 23, 2026 16:59
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