wasmerio / wasmer

🚀 The leading Wasm Runtime supporting WASIX and WASI
https://wasmer.io
MIT License
19.06k stars 817 forks source link

Access mode difference ? #4270

Open orangeC23 opened 1 year ago

orangeC23 commented 1 year ago

Steps to reproduce

(1)The c file is :

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/uio.h>

int main() {
    const char* file_name = "Data/mydir";
    int open_style= O_RDONLY;
    int fd = get_fd(file_name, open_style);
    fd_prestat_dir_name6MppH24h5k(fd);
    snapshot(fd);
    return 0;
}

int fd_prestat_dir_name6MppH24h5k (int fd) {
    return fd_prestat_dir_name(fd);
}

int get_fd(const char* file_name, int open_style){
    // Open a file for reading
    int fd = open(file_name, open_style);
    if (fd == -1) {
        perror("Failed to open the file");
        return 1;
    }

    return fd;
}

int fd_prestat_dir_name(int fd) {
    printf("Enter fd_prestat_dir_name\n");

    int flags = fcntl(fd, F_GETFL);

    if (flags == -1) {
        perror("Error getting file descriptor flags");
        return 1;
    }

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

    if (flags & O_RDONLY){
        printf("Access Mode: O_RDONLY\n");
    }
    if (flags & O_WRONLY){
        printf("Access Mode: O_WRONLY\n");
    }
    if (flags & O_RDWR){
        printf("Access Mode: O_RDWR\n");
    }
    if (flags & O_APPEND){
        printf("Access Mode: O_APPEND\n");
    }
    if (flags & O_NONBLOCK){
        printf("Access Mode: O_NONBLOCK\n");
    }
    if (flags & O_SYNC){
        printf("Access Mode: O_SYNC\n");
    }
    if (flags & O_DSYNC){
        printf("Access Mode: O_DSYNC\n");
    }
    if (flags & O_RSYNC){
        printf("Access Mode: O_RSYNC\n");
    }

    printf("Leave fd_prestat_dir_name\n");
    return fd;
}

int snapshot(int myfd){   
    printf("Enter snapshot\n");

    struct stat file_info;
    if (fstat(myfd, &file_info) == -1) {
        perror("Error getting file attributes");
        close(myfd);
        return 1;
    }

    printf("File Size: %lld bytes \n", (long long)file_info.st_size);

    off_t cur_offset = lseek(myfd, 0, SEEK_CUR);
    if (cur_offset == -1) {
        perror("Error getting current offset");
    }
    printf("Current offset: %lld \n", (long long)cur_offset);

    if (close(myfd) == -1) {
        perror("Error closing file");
        return 1;
    }

    printf("Leave snapshot\n");
}

(2) compile the c file into wasm: ./wasi-sdk-16.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-16.0/share/wasi-sysroot test.c -o test.wasm (3)exeute open.wasm wasmer run --dir=./Data test.wasm The permission of Data/mydir is 0700, user1 create the Data/mydir file and user1 execute the Wasm file.

Expected behavior

wasmtime, wazero, wamr, wasmedge print:

Enter fd_prestat_dir_name
flags: 67108864
Access Mode: O_RDONLY
Access Mode: O_RDWR
Leave fd_prestat_dir_name
Enter snapshot
File Size: 4096 bytes 
Error getting current offset: Bad file descriptor
Current offset: -1 
Leave snapshot

Actual behavior

wasmer prints:

Enter fd_prestat_dir_name
flags: 335544320
Access Mode: O_RDONLY
Access Mode: O_WRONLY
Access Mode: O_RDWR
Leave fd_prestat_dir_name
Enter snapshot
File Size: 4096 bytes 
Current offset: 0 
Leave snapshot

I'm not sure whether the difference between wasmer and other runtimes could pose a bug or only difference? Could you please provide some suggestion? Thanks a lot !

version

wasmer 4.2.2

ubuntu 20.04

Arshia001 commented 1 year ago

probably related to #4259 and #4265.

stale[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.