bytecodealliance / wasmtime

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

write success, but not expected. #8818

Closed Userzxcvbvnm closed 3 months ago

Userzxcvbvnm commented 3 months 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_00067_nEEKq(int fd) {
    printf("Enter function fd_pwrite_00067_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 = "";
    iov[0].iov_len = 0;

    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, 9);
    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_1/subdir_2/subdir_3/subfile_3", O_RDONLY);
    if (fd == -1) {
        return 1;
    }

    fd_pwrite_00067_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_1/subdir_2/subdir_3/subfile_3 exists.) wasmtime run --dir=. test.wasm

Expected Results

Print:

Get file descriptor of file subdir_2/subdir_1/subdir_2/subdir_3/subfile_3 succeed!
Enter function fd_pwrite_00067_nEEKq
Current file size before: 87
pwritev failed
Current file size after: 87

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

Actual Results

wasmtime prints:

Get file descriptor of file subdir_2/subdir_1/subdir_2/subdir_3/subfile_3 succeed!
Enter function fd_pwrite_00067_nEEKq
Current file size before: 87
pwritev successful. 0 bytes written
Current file size after: 87

The file is opened with O_RDONLY. Maybe write successful message is not expected.

Versions and Environment

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

alexcrichton commented 3 months ago

Thanks for the detailed report!

(and for the others too)