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

Open
#919 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, wasm

Research direction

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().

Written by the indexing model from the issue text.

Description

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.

Dominant language
C
Stars
1k
Forks
268
Avg merge
1d 3h
Merged PRs (30d)
20

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from WebAssembly/wasi-libc

All issues in WebAssembly/wasi-libc

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.