bytecodealliance / wasmtime

A fast and secure runtime for WebAssembly
https://wasmtime.dev/
Apache License 2.0
14.82k stars 1.23k forks source link

File fd_pwrite bug #8817

Closed Userzxcvbvnm closed 2 weeks ago

Userzxcvbvnm commented 2 weeks ago

Test Case

The c test case is:


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/uio.h>

int get_fd(const char *filename, int flags) {
    int fd = open(filename, flags);

    if (fd == -1) {
        printf("Get file descriptor of file %s failed!\n", filename);
        return -1;
    } else {
        printf("Get file descriptor of file %s succeed!\n", filename);
        return fd;
    }
}

void closebyfd(int fd) {
    if (close(fd) == -1) {
        printf("Close the file %d by descriptor failed!\n", fd);
    }
}

void fd_pwrite_00019_nEEKq(int fd) {
    printf("Enter function fd_pwrite_00019_nEEKq\n");
    off_t size = lseek(fd, 0, SEEK_END); 
    printf("Current file size before: %ld\n", size);

    struct iovec iov[1];
    iov[0].iov_base = "JBr3HJK";
    iov[0].iov_len = 7;

    off_t offset = lseek(fd, 0, SEEK_CUR);
    if (offset == -1) {
        printf("Failed to get current offset\n");
        return;
    }

    ssize_t bytes_written = pwritev(fd, iov, 1, 0);
    if (bytes_written == -1) {
        printf("pwritev failed\n");
    } else {
        printf("pwritev successful. %zd bytes written\n", bytes_written);
    }
    size = lseek(fd, 0, SEEK_END); 
    printf("Current file size after: %ld\n", size);

}

int main() {
    int fd = get_fd("subdir_2/subdir_2/subfile_3", O_WRONLY | O_APPEND);
    if (fd == -1) {
        return 1;
    }

    fd_pwrite_00019_nEEKq(fd);

    closebyfd(fd);

    return 0;
}

Steps to Reproduce

(1)compile to wasm:./wasi-sdk-21.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-21.0/share/wasi-sysroot test.c -o test.wasm

(2)Running wasm: (Before run the Wasm file, file subdir_2/subdir_2/subfile_3 exists, and the file size is 20 bytes.) wasmtime run --dir=. test.wasm

Expected Results

Print:

Get file descriptor of file subdir_2/subdir_2/subfile_3 succeed!
Enter function fd_pwrite_00019_nEEKq
Current file size before: 20
pwritev successful. 7 bytes written
Current file size after: 27

This is what WAMR, WasmEdge and Linux native code do.

Actual Results

wasmtime prints:

Get file descriptor of file subdir_2/subdir_2/subfile_3 succeed!
Enter function fd_pwrite_00019_nEEKq
Current file size before: 20
pwritev successful. 7 bytes written
Current file size after: 20

wasmtime do not write the content into the file although the file is opened with O_APPEND.

Versions and Environment

Wasmtime version or commit: 19.0.2 Operating system: Ubuntu 20.04 Architecture: x86_64