wasi-preview1-component-adapter: fd_write does not invalidate cached InputStream, causing stale reads

未关闭 适合新手
#15,296 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@dpfens 已经在做这个了。

开始于 2026年11月20日。

  • #15393 来自 @dpfens —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust, wasm
领域
backend

调研方向

从 crates/wasi-preview1-component-adapter/src/descriptors.rs 和 get_read_stream 的实现开始,然后跟踪 crates/wasi-preview1-component-adapter/src/lib.rs 中 fd_write 附近的顺序写入。使用 issue 中的独立 C 程序和适配器命令重现第二次读取到过期内容的情况。完成标准是写入后第二次读取返回 c,同时直接 P1 的行为仍符合预期。

由索引模型根据 Issue 内容生成。

描述

wasi

wasi-preview1-component-adapter: fd_write does not invalidate cached InputStream, causing stale reads

Summary

After read → write → read on one O_RDWR descriptor, the second read returns the byte just written at offset 1 ('A') rather than the byte at the new shared offset 2 ('c'). The process exits normally and the on-disk file is correct; only the returned buffer is stale.

Confirmed with the standalone C program below, without any custom test driver or trace format. Each lane reproduced the same output 5/5 times in fresh directories on Wasmtime 46.0.0 (Release and Debug), 47.0.2, and 50.0.1. Wasmtime 50.0.1 was the latest upstream release at verification time.

Minimal reproducer (repro.c)

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(void) {
    char first = 0, second = 0;
    int fd = open("file.txt", O_CREAT | O_TRUNC | O_WRONLY, 0644);
    write(fd, "abcdefghijklmnop", 16);
    close(fd);
    fd = open("file.txt", O_RDWR);
    errno = 0;
    ssize_t read1 = read(fd, &first, 1);
    printf("read #1: rv=%zd errno=%d byte=%c\n", read1, errno, first);
    errno = 0;
    ssize_t written = write(fd, "A", 1);
    printf("write: rv=%zd errno=%d (%s)\n", written, errno, strerror(errno));
    errno = 0;
    ssize_t read2 = read(fd, &second, 1);
    printf("read #2: rv=%zd errno=%d byte=%c (expected c, stale value A)\n",
           read2, errno, second);
    close(fd);
    return 0;
}

Exact commands

Run in a new empty directory. The first command downloads the official v50.0.1 command adapter; clang, wasm-tools, and wasmtime must be on the stated paths or adjusted to local installations.

curl -fLO https://github.com/bytecodealliance/wasmtime/releases/download/v50.0.1/wasi_snapshot_preview1.command.wasm
/opt/wasi-sdk/bin/clang --target=wasm32-wasip1 -std=c17 -O2 -Wall -Wextra -Werror repro_rc4_stale_read_after_write.c -o repro_p1.wasm
wasm-tools component new repro_p1.wasm --adapt wasi_snapshot_preview1=wasi_snapshot_preview1.command.wasm -o repro_adapter.wasm
wasmtime run --dir . repro_p1.wasm; wasmtime run --dir . repro_adapter.wasm

Expected vs. actual output (Wasmtime 50.0.1)

Direct P1 (expected):

read #1: rv=1 errno=0 byte=a
write: rv=1 errno=0 (Success)
read #2: rv=1 errno=0 byte=c (expected c, stale value A)

Adapter P2 (actual):

read #1: rv=1 errno=0 byte=a
write: rv=1 errno=0 (Success)
read #2: rv=1 errno=0 byte=A (expected c, stale value A)

Root cause

get_read_stream creates an InputStream at the then-current file.position and caches it (descriptors.rs:44-66). fd_write later advances file.position after a successful sequential write (lib.rs:1747-1762) but does not invalidate streams.input. The next read therefore reuses the host stream's old offset. The same code remains in v50.0.1.

Suggested fix and causal evidence

After a successful sequential file write advances the P1 position, invalidate the cached input stream (for example, drop(streams.input.take())) so a later read is created at the updated position. A minimal v46 patch changed the second read from 'A' to 'c' 3/3; reverting restored 'A' 3/3.

主要语言
Rust
星标
18.9k
派生
2k
平均合并
1 天 6 小时
30 天内合并 PR
238

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bytecodealliance/wasmtime 的其他 Issue

查看 bytecodealliance/wasmtime 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。