Stream: git-wasmtime

Topic: wasmtime / issue #7318 Error setting file flags ?


view this post on Zulip Wasmtime GitHub notifications bot (Oct 21 2023 at 08:47):

orangeC23 added the bug label to Issue #7318.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 21 2023 at 08:47):

orangeC23 opened issue #7318:

Steps to Reproduce

(1) The cfile is :

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

int main() {
    const char* file_name = "Data/hello.txt";
    int open_style= O_RDWR;
    int fd = get_fd(file_name, open_style);
    fd_fdstat_set_flagsOAZrg1aDgM(fd);
    snapshot(fd);
    return 0;
}

int fd_fdstat_set_flagsOAZrg1aDgM (int fd) {
    int flag_value = O_DSYNC;
    return fd_fdstat_set_flags(fd, flag_value);
}

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

    return fd;
}

int fd_fdstat_set_flags(int fd, int flag_value){
    printf("Enter fd_fdstat_set_flags\n");

    int flags = fcntl(fd, F_GETFL);
    if (flags == -1) {
        perror("Error getting file flags");
        close(fd);
        return 1;
    }

    flags |= flag_value;

    int new_flag = fcntl(fd, F_SETFL, flags);
    if (new_flag == -1) {
        perror("Error setting file flags");
        close(fd);
        return 1;
    }
    printf("File status flags : %d\n", new_flag);

    printf("Leave fd_fdstat_set_flags\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);
    printf("File Permissions: %o \n", file_info.st_mode & ~S_IFMT);
    printf("File Owner UID: %d \n", file_info.st_uid);
    printf("File Group GID: %d \n", file_info.st_gid);


    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 test1.c -o test1.wasm
(3)exeute open.wasm
wasmtime run --dir=./Data test1.wasm
The permission of Data/hello.txt is 0700, user1 create the Data/hello.txt file before and user1 execute the Wasm file.

Expected Results

Other runtime print:

Enter fd_fdstat_set_flags
File status flags : 0
Leave fd_fdstat_set_flags
Enter snapshot
File Size: 30 bytes
File Permissions: 0
File Owner UID: 0
File Group GID: 0
Current offset: 0
Leave snapshot

And using gcc test1.c -o test1 and ./test1 to execute get the following result:

Enter fd_fdstat_set_flags
File status flags : 0
Leave fd_fdstat_set_flags
Enter snapshot
File Size: 30 bytes
File Permissions: 700
File Owner UID: 1002
File Group GID: 1002
Current offset: 0
Leave snapshot

Actual Results

wasmtime prints:

Enter fd_fdstat_set_flags
Error setting file flags: Invalid argument
Enter snapshot
Error getting file attributes: Bad file descriptor

Versions and Environment

wasmtime 13.0.0
Operating system: Ubuntu 20.04

Architecture: x86_64

view this post on Zulip Wasmtime GitHub notifications bot (Oct 21 2023 at 10:51):

bjorn3 commented on issue #7318:

Wasmtime only supports setting the nonblock and append flags using fd_fdstat_set_flags. The various sync flags can't be set anymore since https://github.com/bytecodealliance/wasmtime/pull/2487. I think it was a temporary workaround for cap-std not supporting them, so maybe this can be fixed now.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 21 2023 at 10:53):

orangeC23 commented on issue #7318:

Thanks for your reply.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 23 2023 at 14:35):

alexcrichton commented on issue #7318:

Yes can confirm what @bjorn3 said, so as this is expected behavior I'm going to close this.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 23 2023 at 14:35):

alexcrichton closed issue #7318:

Steps to Reproduce

(1) The cfile is :

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

int main() {
    const char* file_name = "Data/hello.txt";
    int open_style= O_RDWR;
    int fd = get_fd(file_name, open_style);
    fd_fdstat_set_flagsOAZrg1aDgM(fd);
    snapshot(fd);
    return 0;
}

int fd_fdstat_set_flagsOAZrg1aDgM (int fd) {
    int flag_value = O_DSYNC;
    return fd_fdstat_set_flags(fd, flag_value);
}

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

    return fd;
}

int fd_fdstat_set_flags(int fd, int flag_value){
    printf("Enter fd_fdstat_set_flags\n");

    int flags = fcntl(fd, F_GETFL);
    if (flags == -1) {
        perror("Error getting file flags");
        close(fd);
        return 1;
    }

    flags |= flag_value;

    int new_flag = fcntl(fd, F_SETFL, flags);
    if (new_flag == -1) {
        perror("Error setting file flags");
        close(fd);
        return 1;
    }
    printf("File status flags : %d\n", new_flag);

    printf("Leave fd_fdstat_set_flags\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);
    printf("File Permissions: %o \n", file_info.st_mode & ~S_IFMT);
    printf("File Owner UID: %d \n", file_info.st_uid);
    printf("File Group GID: %d \n", file_info.st_gid);


    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 test1.c -o test1.wasm
(3)exeute open.wasm
wasmtime run --dir=./Data test1.wasm
The permission of Data/hello.txt is 0700, user1 create the Data/hello.txt file before and user1 execute the Wasm file.

Expected Results

Other runtime print:

Enter fd_fdstat_set_flags
File status flags : 0
Leave fd_fdstat_set_flags
Enter snapshot
File Size: 30 bytes
File Permissions: 0
File Owner UID: 0
File Group GID: 0
Current offset: 0
Leave snapshot

And using gcc test1.c -o test1 and ./test1 to execute get the following result:

Enter fd_fdstat_set_flags
File status flags : 0
Leave fd_fdstat_set_flags
Enter snapshot
File Size: 30 bytes
File Permissions: 700
File Owner UID: 1002
File Group GID: 1002
Current offset: 0
Leave snapshot

Actual Results

wasmtime prints:

Enter fd_fdstat_set_flags
Error setting file flags: Invalid argument
Enter snapshot
Error getting file attributes: Bad file descriptor

Versions and Environment

wasmtime 13.0.0
Operating system: Ubuntu 20.04

Architecture: x86_64


Last updated: Dec 23 2024 at 12:05 UTC