Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fd_datasync failure. #3533

Open
Userzxcvbvnm opened this issue Jun 15, 2024 · 2 comments
Open

fd_datasync failure. #3533

Userzxcvbvnm opened this issue Jun 15, 2024 · 2 comments

Comments

@Userzxcvbvnm
Copy link

Subject of the issue

fd_datasync fails.
I'm not sure whether this is a bug.

Test case


#include <stdio.h>
#include <stdlib.h>
#include <unistd.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_datasync_00001_TKDa3(int fd) {
    printf("Enter function fd_datasync_00001_TKDa3\n");
    
    if (fdatasync(fd) == -1) {
        printf("Failed to synchronize data to disk\n");
        return;
    }
    
    printf("Data synchronized to disk successfully\n");
}

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

    fd_datasync_00001_TKDa3(fd);

    closebyfd(fd);

    return 0;
}

Your environment

Ubuntu 20.04
x86_64
WAMR 1.3.2

Steps to reproduce

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, subdir_1/subfile_2 exists.)
iwasm --dir=. test.wasm

Expected behavior

Get file descriptor of file subdir_1/subfile_2 succeed!
Enter function fd_datasync_00001_TKDa3
Data synchronized to disk successfully

This is what wasmtime and WasmEdge do.

Actual behavior

Get file descriptor of file subdir_1/subfile_2 succeed!
Enter function fd_datasync_00001_TKDa3
Failed to synchronize data to disk
@TianlongLiang
Copy link
Contributor

Technically, this is not a bug. If we take a look at the system call manual for fdatasync: https://linux.die.net/man/2/fdatasync it states that On some UNIX systems (but not Linux), fd must be a writable file descriptor. WAMR's implementation chosed a rather strict limitation to ensure consistent behavior across different platforms. So if you change the O_RDONLY to O_RDWR or O_WRONLY, everything will be fine

@yamt
Copy link
Collaborator

yamt commented Jun 27, 2024

wasi-libc doesn't request DATASYNC rights for O_RDONLY.
https://github.com/WebAssembly/wasi-libc/blob/320bbbcced68ce8e564b0dc4c8f80a5a5ad21a9c/libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c#L27-L42

This is what wasmtime and WasmEdge do.

wasmtime removed the most of their implementation of wasi rights while ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants