Skip to content

fix(wasix1): copy nothing for an empty read in the scratch window - #3

Open
russellw wants to merge 1 commit into
wasmerio:mainfrom
russellw:fix/wasix1-empty-scratch-read
Open

russellw wants to merge 1 commit into
wasmerio:mainfrom
russellw:fix/wasix1-empty-scratch-read

Conversation

@russellw

@russellw russellw commented Nov 19, 2026 •

Copy link
Copy Markdown

Description

A v1 guest could make the host panic with an empty read in the compat layer's scratch window. This PR makes that read succeed without copying anything.

The bug

CompatMemory (lib/wasix/wasix1/src/memory.rs) serves addresses from SCRATCH_BASE (0xFF00_0000) up out of a per-call scratch buffer of held bytes. scratch_range treats an empty range anywhere in the window as valid. read then did this:

let copied = end.min(held);
buf.split_at_mut(copied - start)   // start > held: underflow

When an empty read starts past held, copied - start underflows:

  • Debug builds panic with "attempt to subtract with overflow".
  • Release builds pass split_at_mut an out-of-range index, and it panics instead.

The runtime catches host panics (host_panicked), so the host process survives. The guest, though, is killed with SIGSEGV for a call that should have returned success.

How a guest reaches it

  • fd_write or fd_read with iovs_len = 0 and iovs pointing into the window. The shim passes the iovec pointer through. The kernel's read_iovecs (wlk/crates/kernel/src/fs/io.rs) then reads vlen * 16 bytes, which is an empty read when vlen == 0.
  • An empty iovec whose base is in the window.

On Linux none of these can fault. A zero-length copy_from_user never faults, and writev(fd, iov, 0) returns 0 without looking at iov.

The fix

An empty read in the window now copies nothing and succeeds (one line). The doc comment on scratch_range now says so.

I fixed it in CompatMemory rather than having the kernel skip zero-length reads. An empty read is harmless against every other UserMemory implementation, so the defect is in this one's arithmetic.

write is left unchanged. It still returns Fault for an empty write past held. That is inconsistent with read, but it can't crash, and I found no guest path that reaches it.

Test

empty_iovec_array_in_scratch_window is in lib/wasix/wasix1/tests/guest.rs, written as a WAT guest like its neighbour hello_world. It covers the three paths above, and checks that each call succeeds with 0 bytes transferred.

  • Without the fix, it fails on a host panic at memory.rs, attempt to subtract with overflow.
  • With the fix, it passes.

Checks

  • cargo fmt --check -p wasixv2-wasix1: clean.
  • cargo clippy -p wasixv2-wasix1 --all-targets --locked -- -D clippy::all, with the lint-packages RUSTFLAGS: clean. I ran clippy on this crate only, not the full workspace make lint.
  • cargo test -p wasixv2-wasix1: all pass.
    • The guest tests need RUST_MIN_STACK=67108864 in a debug build on my machine. The existing hello_world overflows the default 2 MiB test-thread stack without it too, so this is not caused by this change.

AI disclosure

Claude (an AI agent) found this bug, wrote the fix and the test, and made the commit. Russell Wallace reviewed and approved them, and is responsible for the change.

🤖 Generated with Claude Code

CompatMemory serves addresses from SCRATCH_BASE up out of a per-call
scratch buffer of `held` bytes. scratch_range accepts an empty range
anywhere in the window, but read then split the buffer at
`end.min(held) - start`, which underflows when the empty range starts
past `held`. The host panicked ("attempt to subtract with overflow" in
debug, an out-of-range split_at_mut in release), and the runtime killed
the guest with SIGSEGV.

A v1 guest reaches it with fd_write or fd_read given iovs_len = 0 and an
iovs pointer in the window, since the kernel's read_iovecs reads
`vlen * 16` bytes even when vlen is 0, or with an empty iovec whose base
is in the window. Linux never faults a zero-length user copy, and
writev(fd, iov, 0) returns 0 without looking at iov.

Return success without copying for an empty read in the window.

Authored and committed by Claude (AI agent).

Co-Authored-By: Claude Opus 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