fix(wasm): zero-initialize WASM page buffers to prevent memory corruption#12
Merged
Merged
Conversation
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
…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
5e198fa to
5b2067c
Compare
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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:wasmrebuilds 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 typecheckbun run build:wasm— rebuilt locally with Zig 0.15.2; 416 KB outputbun test— 333 tests pass (2 new regression tests), 0 failbun run build:libRisk
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".