From fc7697c0b9d1695b957ac6a1b48486eadbe38b4c Mon Sep 17 00:00:00 2001 From: "Claude (AI agent)" Date: Thu, 19 Nov 2026 13:09:02 +0000 Subject: [PATCH] fix(wasix1): copy nothing for an empty read in the scratch window 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 --- lib/wasix/wasix1/src/memory.rs | 7 +++++-- lib/wasix/wasix1/tests/guest.rs | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/wasix/wasix1/src/memory.rs b/lib/wasix/wasix1/src/memory.rs index 61dbf32dc9..de97bda7d9 100644 --- a/lib/wasix/wasix1/src/memory.rs +++ b/lib/wasix/wasix1/src/memory.rs @@ -135,8 +135,10 @@ impl<'a, S: 'static> CompatMemory<'a, S> { /// The window offsets `addr..addr + len`, or `None` if `addr` is below the window. Only /// what this call allocated is there: a range that starts past it faults, as an address - /// the guest's memory does not reach would. A read that starts inside an allocation and - /// runs past its end reads zeroes there, since the kernel reads strings in aligned chunks. + /// the guest's memory does not reach would. An empty range anywhere in the window is valid, + /// and an empty read copies nothing, as Linux's zero-length user copies never fault. A read + /// that starts inside an allocation and runs past its end reads zeroes there, since the + /// kernel reads strings in aligned chunks. fn scratch_range(addr: u32, len: usize, held: usize) -> Option> { if addr < SCRATCH_BASE { return None; @@ -161,6 +163,7 @@ impl UserMemory for CompatMemory<'_, S> { fn read<'a>(&'a self, addr: u32, buf: &'a mut [u8]) -> LocalFuture<'a, Result<(), Fault>> { let held = self.scratch.borrow().len(); match Self::scratch_range(addr, buf.len(), held) { + Some(Ok(_)) if buf.is_empty() => copied(Ok(())), Some(range) => { let result = range.map(|(start, end)| { let scratch = self.scratch.borrow(); diff --git a/lib/wasix/wasix1/tests/guest.rs b/lib/wasix/wasix1/tests/guest.rs index 444cf76fcb..053bfa80ba 100644 --- a/lib/wasix/wasix1/tests/guest.rs +++ b/lib/wasix/wasix1/tests/guest.rs @@ -70,6 +70,40 @@ fn hello_world() { assert_eq!(status, 7); } +/// Empty buffers are never read, even where they point into the compat layer's scratch window: +/// Linux's `writev(fd, iov, 0)` returns 0 without looking at `iov`, and an empty iovec's base is +/// never touched. Both used to panic the host. +#[test] +fn empty_iovec_array_in_scratch_window() { + let wat = r#" + (module + (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32))) + (import "wasi_snapshot_preview1" "fd_read" (func $fd_read (param i32 i32 i32 i32) (result i32))) + (import "wasi_snapshot_preview1" "proc_exit" (func $proc_exit (param i32))) + (memory (export "memory") 1) + ;; One empty iovec whose buffer lies in the window. + (data (i32.const 8) "\00\00\80\ff\00\00\00\00") + (func (export "_start") + (i32.store (i32.const 32) (i32.const -1)) + (if (call $fd_read (i32.const 0) (i32.const 8) (i32.const 1) (i32.const 32)) + (then (call $proc_exit (i32.const 5)))) + (if (i32.load (i32.const 32)) + (then (call $proc_exit (i32.const 6)))) + (i32.store (i32.const 32) (i32.const -1)) + (if (call $fd_write (i32.const 1) (i32.const 0xff800000) (i32.const 0) (i32.const 32)) + (then (call $proc_exit (i32.const 1)))) + (if (i32.load (i32.const 32)) + (then (call $proc_exit (i32.const 2)))) + (i32.store (i32.const 32) (i32.const -1)) + (if (call $fd_read (i32.const 0) (i32.const 0xff800000) (i32.const 0) (i32.const 32)) + (then (call $proc_exit (i32.const 3)))) + (if (i32.load (i32.const 32)) + (then (call $proc_exit (i32.const 4)))) + (call $proc_exit (i32.const 0)))) + "#; + assert_eq!(block_on(wat, &["empty-iovecs"]).0, 0); +} + /// Returning from `_start` is `exit(0)` under WASI. #[test] fn returning_from_start_exits_zero() {