bytecodealliance / wasmtime

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

fd_renumber difference. #8831

Open Userzxcvbvnm opened 2 weeks ago

Userzxcvbvnm commented 2 weeks ago

Test Case

The c test case is:

#include <stdio.h>
#include <stdlib.h>
#include <wasi/api.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);
    }
}

int fd_renumber_00001_1x8fd(int fd) {
    printf("Enter function fd_renumber_00001_1x8fd.\n");

    int new_fd = get_fd("subdir_1/subdir_2/subfile_1", O_RDONLY); 
    __wasi_errno_t result = __wasi_fd_renumber(fd, new_fd);

    if (result != 0) {
        printf("Error %d while renumbering file descriptors.\n", result);
        return result;
    } else {
        printf("Successful file descriptors %d.\n", result);
        printf("Leave fd_renumber_00001.\n");
        closebyfd(new_fd);
        return result;
    }

}

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

    fd_renumber_00001_1x8fd(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 softfile_3, subdir_1/subdir_2/subfile_1 and subdir_2/subdir_1/subfile_1 exists, and softfile_3 is softlink file points to file subdir_2/subdir_1/subfile_1. Before run the Wasm file, change the permission of subdir_1/subdir_2/subfile_1 into 0200(-w-------) . ) wasmtime run --dir=. test.wasm

Expected Results

Print:

Get file descriptor of file softfile_3 succeed!
Enter function fd_renumber_00001_1x8fd.
Get file descriptor of file subdir_1/subdir_2/subfile_1 failed!
Error 8 while renumbering file descriptors.

This is what WAMR and WasmEdge do.

Actual Results

wasmtime prints:

Get file descriptor of file softfile_3 succeed!
Enter function fd_renumber_00001_1x8fd.
Get file descriptor of file subdir_1/subdir_2/subfile_1 failed!
Successful file descriptors 0.
Leave fd_renumber_00001.

Since the permission of subdir_1/subdir_2/subfile_1 is 0200, which only contains write permission, and subdir_1/subdir_2/subfile_1 is opened with O_RDONLY, getting the file descriptor of subdir_1/subdir_2/subfile_1 failure is expected. However, getting the file descriptor of subdir_1/subdir_2/subfile_1 fails, but renumbering file descriptors succeed.

Versions and Environment

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

pchickey commented 2 weeks ago

Wasmtime considers the entire space of u32 numbers as valid file descriptors for preview 1, so the renumber to -1 is a renumber to 4294967295. I don't think this is a bug. It is still possible to show the call to open failed because the WASI fd_open interface (implements open inside libc) has a distinct way to return the failure vs successfully opening fd 4294967295.