wasip2: enabling O_APPEND with fcntl reuses the old positional output stream and overwrites the file

Aperta
#919 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
70/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, wasm

Direzione di ricerca

Start by reading libc-bottom-half/sources/file.c, especially file_fcntl_setfl() and the stream cleanup path in file_close_streams(). Run the supplied reproducer with both wasm32-wasip1 and wasm32-wasip2 using the listed wasi-sdk and wasmtime commands. Done when the P2 run appends Z to the initial file contents after F_SETFL enables O_APPEND, without the extra lseek().

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

wasip2: enabling O_APPEND with fcntl reuses the old positional output stream and overwrites the file

Summary

Calling fcntl(fd, F_SETFL, O_APPEND) on wasm32-wasip2 updates file->oflag but does not close the existing positional output stream. The next write therefore overwrites the previous position instead of appending to the end of the file.

Minimal reproducer

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

int main(void) {
    char contents[32] = {0};
    int fd = open("f", O_RDWR);
    write(fd, "A", 1);
    int flags = fcntl(fd, F_SETFL, O_APPEND);
    write(fd, "Z", 1);
    int in = open("f", O_RDONLY);
    ssize_t size = read(in, contents, sizeof(contents) - 1);
    printf("flags=%d size=%zd content=%s\n", flags, size, contents);
    close(in);
    close(fd);
    return 0;
}

Use an initial file f containing abcdefghijklmnop, then compile and run with the same P1 and P2 commands:

/opt/wasi-sdk/bin/clang --target=wasm32-wasip1 -O2 repro.c -o repro-p1.wasm
/opt/wasi-sdk/bin/clang --target=wasm32-wasip2 -O2 repro.c -o repro-p2.wasm
wasmtime run --dir . repro-p1.wasm
wasmtime run --dir . repro-p2.wasm

Expected vs. actual output

Direct P1:

content=AbcdefghijklmnopZ

P2-libc:

content=AZcdefghijklmnop

Root cause

libc-bottom-half/sources/file.c:361-368 implements file_fcntl_setfl(). It updates the O_NONBLOCK and O_APPEND bits in file->oflag without calling file_close_streams().

The output stream was created before O_APPEND was enabled and remains in positional-write mode, so it writes Z at offset 1 rather than at EOF.

Suggested fix and causal validation

When the append bit changes, close the cached input/output streams (and associated pollables) so that the next write creates a stream using the new append mode.

The minimal patch produced:

  • Unpatched: 3/3 executions generated AZcdefghijklmnop.
  • Patched: 3/3 executions generated AbcdefghijklmnopZ.
  • Reverted: 3/3 executions generated AZcdefghijklmnop, and the reverted libc.a SHA-256 matched the unpatched library.

Adding lseek(fd, 0, SEEK_SET) before the second write masks the problem because it closes the old stream; the short sequence without that extra operation exposes it directly.

Lingua principale
C
Stelle
1k
Fork
268
Merge medio
1g 3h
PR unite (30g)
20

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di WebAssembly/wasi-libc

Tutte le issue di WebAssembly/wasi-libc

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.