Skip to content

fd_write bug. #4868

Open
Open
@Userzxcvbvnm

Description

@Userzxcvbvnm

Describe the bug

Fail to write the array content into the file, although print success.

Steps to reproduce

(1)The test case is :


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/uio.h>
#include <fcntl.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_write_00059_WOtrt(int fd) {
    printf("Enter function fd_write_00059_WOtrt\n");
    
    
    struct iovec iov[2];
    iov[0].iov_base = "m0vkA83GGZ5hRl0xjLtMalXvPLfW9I1hk1ECqM1TIVrkFekPWyqjNlXyNDxRSeWrm3N8NGsOGB39K7ZCqbPq8SYwwILhM84HEMHd8Qokt5mkRN4VW3fNKfvIrWOWGZVTJQj9bIg7Y61O2nnc6LEsw5uYcKErHuyoo4pOQO1j0JL8TXjToclqaKZI9bdthrUjqB";
    iov[0].iov_len = 194;
    iov[1].iov_base = "m0vkA83GGZ5hRl0xjLtMalXvPLfW9I1hk1ECqM1TIVrkFekPWyqjNlXyNDxRSeWrm3N8NGsOGB39K7ZCqbPq8SYwwILhM84HEMHd8Qokt5mkRN4VW3fNKfvIrWOWGZVTJQj9bIg7Y61O2nnc6LEsw5uYcKErHuyoo4pOQO1j0JL8TXjToclqaKZI9bdthrUjqB";
    iov[1].iov_len = 194;

    off_t offset = lseek(fd, 0, SEEK_CUR);
    printf("File current offset before write: %lld\n", (long long)offset);
    ssize_t numBytes = writev(fd, iov, 2);
    offset = lseek(fd, 0, SEEK_CUR);
    printf("File current offset after write: %lld\n", (long long)offset);


    if (numBytes == -1) {
        printf("Write to file descriptor failed!\n");
    } else {
        printf("Write to file descriptor successful. Number of bytes written: %zd\n", numBytes);
    }
}

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

    fd_write_00059_WOtrt(fd);

    closebyfd(fd);

    return 0;
}



(2)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

(3)Running wasm:
(Before run the Wasm file, subfile_3 exists and the file size is 28, the file content is 'oFLZbyIeYRq3x9k60Ep9uSUR1oNW')
wasmer run --dir=. test.wasm

Expected behavior

print:

Get file descriptor of file subfile_3 succeed!
Enter function fd_write_00059_WOtrt
File current offset before write: 0
File current offset after write: 388
Write to file descriptor successful. Number of bytes written: 388

And the file content change to : 'm0vkA83GGZ5hRl0xjLtMalXvPLfW9I1hk1ECqM1TIVrkFekPWyqjNlXyNDxRSeWrm3N8NGsOGB39K7ZCqbPq8SYwwILhM84HEMHd8Qokt5mkRN4VW3fNKfvIrWOWGZVTJQj9bIg7Y61O2nnc6LEsw5uYcKErHuyoo4pOQO1j0JL8TXjToclqaKZI9bdthrUjqBm0vkA83GGZ5hRl0xjLtMalXvPLfW9I1hk1ECqM1TIVrkFekPWyqjNlXyNDxRSeWrm3N8NGsOGB39K7ZCqbPq8SYwwILhM84HEMHd8Qokt5mkRN4VW3fNKfvIrWOWGZVTJQj9bIg7Y61O2nnc6LEsw5uYcKErHuyoo4pOQO1j0JL8TXjToclqaKZI9bdthrUjqB'

This is what WAMR and WasmEdge do.

Actual behavior

wasmer print:

Get file descriptor of file subfile_3 succeed!
Enter function fd_write_00059_WOtrt
File current offset before write: 0
File current offset after write: 388
Write to file descriptor successful. Number of bytes written: 388

wasmer print the same log, however, the file content do not change, the file content is still: 'oFLZbyIeYRq3x9k60Ep9uSUR1oNW'

Additional context

Ubuntu 20.04
x86_64
wasmer-4.3.1

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpriority-highHigh priority issue📦 lib-vfsAbout wasmer-vfs

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions