wasmerio / wasmer

🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten
https://wasmer.io
MIT License
18.58k stars 795 forks source link

`fdflags::append` not respected if file offset changed through another fd #5128

Open yagehu opened 5 days ago

yagehu commented 5 days ago

Describe the bug

Open a file twice: once with fdflags::append flag (fd0) and another time without (fd1). fd_write some non-empty buffer, say "abc" to fd1, changing the file offset to 3. Now, fd_write to fd0. It should write from offset 3 due to the append flag. Instead, it writes from offset 0.

wasmer -vV; rustc -vV
wasmer 4.3.7 (a4b8667 2024-10-02)
binary: wasmer-cli
commit-hash: a4b86673d4c2bbf631b2751859b8d6fd4314a38a
commit-date: 2024-10-02
host: x86_64-apple-darwin
compiler: singlepass,cranelift
rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: x86_64-apple-darwin
release: 1.74.1
LLVM version: 17.0.4

Steps to reproduce

Compile this snippet with wasi-sdk and run:

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

int main(void) {
    int fd0 = open("dir/a", O_CREAT | O_EXCL | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
    if (fd0 == -1) perror("open fd0");

    int fd1 = open("dir/a", O_WRONLY);
    if (fd1 == -1) perror("open fd1");

    write(fd1, "abc", 3);
    write(fd0, "d", 1);

    return 0;
}
clang double-append-write.c
wasmer run --mapdir dir:. a.out

Expected behavior

The file should have content abcd.

Actual behavior

The file has content dbc.

xdoardo commented 4 days ago

Thanks for reporting, we'll look into it.