bytecodealliance / wasmtime

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

Another reading bytes difference. #8820

Closed Userzxcvbvnm closed 2 weeks ago

Userzxcvbvnm commented 2 weeks ago

This is related to https://github.com/bytecodealliance/wasmtime/issues/8819.

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_read_00012_Ep05V(int fd) {
    printf("Enter function fd_read_00012_Ep05V\n");

    char buf1[1];
    char buf2[1];
    struct iovec iov[2];
    ssize_t num_read;

    iov[0].iov_base = buf1;
    iov[0].iov_len = 1;
    iov[1].iov_base = buf2;
    iov[1].iov_len = 1;

    num_read = readv(fd, iov, 2);

    if (num_read == -1) {
        printf("Error reading from file descriptor\n");
    } else {
        printf("Read %ld bytes using readv\n", num_read);
    }
}

int main() {
    int fd = get_fd("subfile_1", O_RDONLY);
    if (fd == -1) {
        return 1;
    }

    fd_read_00012_Ep05V(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 subfile_1 exists, and the file size is 19 bytes.) wasmtime run --dir=. test.wasm

Expected Results

Print:

Get file descriptor of file subfile_1 succeed!
Enter function fd_read_00012_Ep05V
Read 2 bytes using readv

This is what WAMR and WasmEdge do.

Actual Results

wasmtime prints:

Get file descriptor of file subfile_1 succeed!
Enter function fd_read_00012_Ep05V
Read 1 bytes using readv

The buffer size is 2 bytes, which is smaller than the file size. Thus, reading 2bytes are expected. wasmtime only use one buffer although two are given.

Versions and Environment

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

pchickey commented 2 weeks ago

Dupe #8819