Skip to content

Open with O_RDONLY, but fd_write succeed. #4901

Open
@Userzxcvbvnm

Description

@Userzxcvbvnm

Describe the bug

This maybe related to #4892

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_00012_WOtrt(int fd) {
    printf("Enter function fd_write_00012_WOtrt\n");
    
    
    struct iovec iov[2];
    iov[0].iov_base = "v0AXE";
    iov[0].iov_len = 5;
    iov[1].iov_base = "v0AXE";
    iov[1].iov_len = 5;

    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("subdir_1/subdir_2/subfile_1", O_RDONLY);
    if (fd == -1) {
        return -1;
    }

    fd_write_00012_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, file subdir_1/subdir_2/subfile_1 exists.)
wasmer run --dir=. test.wasm

Expected behavior

print:

Get file descriptor of file subdir_1/subdir_2/subfile_1 succeed!
Enter function fd_write_00012_WOtrt
File current offset before write: 0
File current offset after write: 0
Write to file descriptor failed!

And this is what wasmtime, WAMR and WasmEdge do.

Actual behavior

wasmer print:

Get file descriptor of file subdir_1/subdir_2/subfile_1 succeed!
Enter function fd_write_00012_WOtrt
File current offset before write: 0
File current offset after write: 10
Write to file descriptor successful. Number of bytes written: 10

wasmer write new content into the file, opening with "O_RDONLY", rather than "O_WRONLY" or "O_RDWR".

Additional context

Ubuntu 20.04
x86_64
wasmer-4.2.2

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