wasi-preview1-component-adapter: fd_write does not invalidate cached InputStream, causing stale reads
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 72/100
調査の方向性
crates/wasi-preview1-component-adapter/src/descriptors.rs と get_read_stream の実装から始め、次に crates/wasi-preview1-component-adapter/src/lib.rs の fd_write 周辺で逐次書き込みを追ってください。Issue にあるスタンドアロンの C プログラムとアダプターコマンドを使って、古い内容が返る2回目の読み取りを再現してください。書き込み後の2回目の読み取りで c が返され、直接の P1 の動作が期待どおりであれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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時間
- マージ済み PR(30日)
- 238
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
bytecodealliance/wasmtime のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
bytecodealliance/wasmtime#15536 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
bytecodealliance/wasmtime#15455 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug wasm-proposal:exceptions wasmtime:debugging
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
bytecodealliance/wasmtime#14102 ·
メンテナーはふだん 1 日以内に返信
-
wasm-proposal:gc
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
bytecodealliance/wasmtime#13808 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
`Func::call` aborts on OOM when Wasm traps (with name section) or throws対応中かも @cfallin が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 30/100
bytecodealliance/wasmtime#15529 ·
メンテナーはふだん 1 日以内に返信
bytecodealliance/wasmtime の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
valkey-io/valkey-glide#7975 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
marklauter/tsong#110 ·
-
docs enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
wasm-bindgen/wasm-bindgen#5503 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
vortex-data/vortex#12633 ·
メンテナーはふだん 1 日以内に返信