Stream: git-wasmtime

Topic: wasmtime / issue #8817 File fd_pwrite bug


view this post on Zulip Wasmtime GitHub notifications bot (Jun 17 2024 at 08:51):

Userzxcvbvnm added the bug label to Issue #8817.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 17 2024 at 08:51):

Userzxcvbvnm opened issue #8817:

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

view this post on Zulip Wasmtime GitHub notifications bot (Jun 17 2024 at 21:39):

alexcrichton closed issue #8817:

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


Last updated: Nov 22 2024 at 17:03 UTC