bytecodealliance / wasmtime

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

File access mode difference. #8816

Closed Userzxcvbvnm closed 3 months ago

Userzxcvbvnm commented 3 months ago

Test Case

The c test case is:


#include <stdio.h>
#include <fcntl.h>
#include <unistd.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_fdstat_get_00002_yBbmO(int fd) {
    printf("Enter function fd_fdstat_get_00002_yBbmO\n");

    int flags = fcntl(fd, F_GETFL);
    if (flags == -1) {
        printf("Error getting file descriptor flags\n");
        return;
    }

    printf("File descriptor flags: %d\n", flags);

    int access_mode = flags & O_ACCMODE;

    switch(access_mode) {
        case O_RDONLY:
            printf("File access mode: O_RDONLY\n");
            break;
        case O_WRONLY:
            printf("File access mode: O_WRONLY\n");
            break;
        case O_RDWR:
            printf("File access mode: O_RDWR\n");
            break;
        case O_NONBLOCK:
            printf("File access mode: O_NONBLOCK\n");
            break;
        case O_SYNC:
            printf("File access mode: O_SYNC\n");
            break;
        case O_DSYNC:
            printf("File access mode: O_DSYNC\n");
            break;
        default:
            printf("File access mode: Unknown\n");
            break;
    }
}

int main() {
    int fd = get_fd("subdir_3/subfile_2", O_WRONLY);
    if (fd == -1) {
        return -1;
    }

    fd_fdstat_get_00002_yBbmO(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_3/subfile_2 exists.) wasmtime run --dir=. test.wasm

Expected Results

Print:

Get file descriptor of file subdir_3/subfile_2 succeed!
Enter function fd_fdstat_get_00002_yBbmO
File descriptor flags: 268435456
File access mode: O_WRONLY

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

Actual Results

wasmtime prints:

Get file descriptor of file subdir_3/subfile_2 succeed!
Enter function fd_fdstat_get_00002_yBbmO
File descriptor flags: 335544320
File access mode: O_RDWR

Versions and Environment

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