wasi-preview1-component-adapter: enabling O_APPEND reuses a positional OutputStream and overwrites data

オープン
#15,298 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
71/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust, wasm

調査の方向性

Read crates/wasi-preview1-component-adapter/src/lib.rs around fd_fdstat_set_flags (lines 961–984) and descriptors.rs around get_write_stream (lines 72–99). Reproduce with the standalone C program and adapter commands in the issue, then check how changing O_APPEND affects the cached output stream. Done when the adapter appends the final Z and produces AbcdefghijklmnopZ.

索引モデルが issue の本文から書いたものです。

説明

wasi

wasi-preview1-component-adapter: enabling O_APPEND reuses a positional OutputStream and overwrites data

Summary

After one positional write, fcntl(fd, F_SETFL, O_APPEND) succeeds, but the next write still uses the old positional stream. It overwrites offset 1 instead of appending, producing AZcdefghijklmnop rather than AbcdefghijklmnopZ.

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>

static void show_file(void) {
    char content[18] = {0};
    int fd = open("file.txt", O_RDONLY);
    ssize_t size = read(fd, content, 17);
    printf("file: rv=%zd content=%s\n", size, content);
    close(fd);
}

int main(void) {
    int fd = open("file.txt", O_CREAT | O_TRUNC | O_WRONLY, 0644);
    write(fd, "abcdefghijklmnop", 16);
    close(fd);
    fd = open("file.txt", O_RDWR);
    printf("write #1: rv=%zd\n", write(fd, "A", 1));
    errno = 0;
    int flags = fcntl(fd, F_SETFL, O_APPEND);
    printf("fcntl(F_SETFL, O_APPEND): rv=%d errno=%d (%s)\n",
           flags, errno, strerror(errno));
    printf("write #2: rv=%zd\n", write(fd, "Z", 1));
    close(fd);
    show_file();
    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_rc5_dynamic_append_overwrite.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):

write #1: rv=1
fcntl(F_SETFL, O_APPEND): rv=0 errno=0 (Success)
write #2: rv=1
file: rv=17 content=AbcdefghijklmnopZ

Adapter P2 (actual):

write #1: rv=1
fcntl(F_SETFL, O_APPEND): rv=0 errno=0 (Success)
write #2: rv=1
file: rv=16 content=AZcdefghijklmnop

Root cause

fd_fdstat_set_flags updates file.append and file.blocking_mode without invalidating the cached output stream (lib.rs:961-984). get_write_stream chooses append versus positional mode only when creating the stream (descriptors.rs:72-99). Consequently, changing the flag does not change the already cached host stream's mode. The same code remains in v50.0.1.

Suggested fix and causal evidence

When APPEND actually changes, invalidate the cached OutputStream so the next write uses append_via_stream(). A minimal v46 patch invalidated output only on an APPEND transition: the content divergence changed 3/3 → 0/3 → 3/3 across unpatched/patched/reverted, and unpatched/reverted binaries were identical.

主要言語
Rust
スター
18.9k
フォーク
2k
平均マージ
1日 6時間
マージ済み PR(30日)
238

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bytecodealliance/wasmtime のほかの issue

bytecodealliance/wasmtime の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。