Stream: git-wasmtime

Topic: wasmtime / Issue #2515 fstat on stdout, stderr and stdin ...


view this post on Zulip Wasmtime GitHub notifications bot (Dec 16 2020 at 10:28):

abbec opened Issue #2515:

I am using this program to test:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <wasi/api.h>

int main(int argc, char **argv) {

        __wasi_fdstat_t statbuf;
        printf("__wasi_fd_fdstat_get for stdout: %d\n", __wasi_fd_fdstat_get(fileno(stdout), &statbuf));
        printf("rights for stdout: %llu\n", statbuf.fs_rights_base);
        printf("type for stdout: %d\n", statbuf.fs_filetype);
        printf("has filestat_get rights for stdout: %s\n", (statbuf.fs_rights_base & __WASI_RIGHTS_FD_FILESTAT_GET) != 0 ? "yes" : "no");

        __wasi_filestat_t filestat;
        int filestat_res = __wasi_fd_filestat_get(fileno(stdout), &filestat);
        printf("__wasi_fd_filestat_get for stdout: %d\n", filestat_res);
        if (filestat_res != 0) {
                printf("  E: __wasi_fd_filestat_get returned error: %d\n", filestat_res);
        }

        struct stat sb;
        int f = fstat(fileno(stdout), &sb);
        printf("fstat for stdout (fd: %d): %d\n", fileno(stdout), f);

        if (f < 0) {
                perror("  E: fstat returned error");
        }

        return 0;
}

Running it with wasmtime I get:

$ wasmtime fstat.wasm
__wasi_fd_fdstat_get for stdout: 0
rights for stdout: 136314954
type for stdout: 2
has filestat_get rights for stdout: yes
__wasi_fd_filestat_get for stdout: 8
  E: __wasi_fd_filestat_get returned error: 8
fstat for stdout (fd: 1): -1
  E: fstat returned error: Bad file descriptor

and running the same with Wasmer, I instead get:

$ wasmer fstat.wasm
__wasi_fd_fdstat_get for stdout: 0
rights for stdout: 136315089
type for stdout: 2
has filestat_get rights for stdout: yes
__wasi_fd_filestat_get for stdout: 0
fstat for stdout (fd: 1): 0

Expected outcome

Should be able to fstat stdout, right? We are trying to port a program that does that to WASI and we are running in to this.

view this post on Zulip Wasmtime GitHub notifications bot (Dec 16 2020 at 10:28):

abbec labeled Issue #2515:

I am using this program to test:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <wasi/api.h>

int main(int argc, char **argv) {

        __wasi_fdstat_t statbuf;
        printf("__wasi_fd_fdstat_get for stdout: %d\n", __wasi_fd_fdstat_get(fileno(stdout), &statbuf));
        printf("rights for stdout: %llu\n", statbuf.fs_rights_base);
        printf("type for stdout: %d\n", statbuf.fs_filetype);
        printf("has filestat_get rights for stdout: %s\n", (statbuf.fs_rights_base & __WASI_RIGHTS_FD_FILESTAT_GET) != 0 ? "yes" : "no");

        __wasi_filestat_t filestat;
        int filestat_res = __wasi_fd_filestat_get(fileno(stdout), &filestat);
        printf("__wasi_fd_filestat_get for stdout: %d\n", filestat_res);
        if (filestat_res != 0) {
                printf("  E: __wasi_fd_filestat_get returned error: %d\n", filestat_res);
        }

        struct stat sb;
        int f = fstat(fileno(stdout), &sb);
        printf("fstat for stdout (fd: %d): %d\n", fileno(stdout), f);

        if (f < 0) {
                perror("  E: fstat returned error");
        }

        return 0;
}

Running it with wasmtime I get:

$ wasmtime fstat.wasm
__wasi_fd_fdstat_get for stdout: 0
rights for stdout: 136314954
type for stdout: 2
has filestat_get rights for stdout: yes
__wasi_fd_filestat_get for stdout: 8
  E: __wasi_fd_filestat_get returned error: 8
fstat for stdout (fd: 1): -1
  E: fstat returned error: Bad file descriptor

and running the same with Wasmer, I instead get:

$ wasmer fstat.wasm
__wasi_fd_fdstat_get for stdout: 0
rights for stdout: 136315089
type for stdout: 2
has filestat_get rights for stdout: yes
__wasi_fd_filestat_get for stdout: 0
fstat for stdout (fd: 1): 0

Expected outcome

Should be able to fstat stdout, right? We are trying to port a program that does that to WASI and we are running in to this.

view this post on Zulip Wasmtime GitHub notifications bot (Dec 16 2020 at 10:59):

abbec commented on Issue #2515:

It seems to always hit this path: https://github.com/bytecodealliance/wasmtime/blob/5c1d728e3ae8ee7aa329e294999a2c3086b21676/crates/wasi-common/src/handle.rs#L123

view this post on Zulip Wasmtime GitHub notifications bot (Dec 16 2020 at 12:16):

abbec commented on Issue #2515:

Seems like Stdin, Stdout and Stderr are missing implementations for filestat_get in https://github.com/bytecodealliance/wasmtime/blob/main/crates/wasi-common/src/sys/stdio.rs?

view this post on Zulip Wasmtime GitHub notifications bot (Dec 16 2020 at 19:02):

pchickey closed Issue #2515:

I am using this program to test:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <wasi/api.h>

int main(int argc, char **argv) {

        __wasi_fdstat_t statbuf;
        printf("__wasi_fd_fdstat_get for stdout: %d\n", __wasi_fd_fdstat_get(fileno(stdout), &statbuf));
        printf("rights for stdout: %llu\n", statbuf.fs_rights_base);
        printf("type for stdout: %d\n", statbuf.fs_filetype);
        printf("has filestat_get rights for stdout: %s\n", (statbuf.fs_rights_base & __WASI_RIGHTS_FD_FILESTAT_GET) != 0 ? "yes" : "no");

        __wasi_filestat_t filestat;
        int filestat_res = __wasi_fd_filestat_get(fileno(stdout), &filestat);
        printf("__wasi_fd_filestat_get for stdout: %d\n", filestat_res);
        if (filestat_res != 0) {
                printf("  E: __wasi_fd_filestat_get returned error: %d\n", filestat_res);
        }

        struct stat sb;
        int f = fstat(fileno(stdout), &sb);
        printf("fstat for stdout (fd: %d): %d\n", fileno(stdout), f);

        if (f < 0) {
                perror("  E: fstat returned error");
        }

        return 0;
}

Running it with wasmtime I get:

$ wasmtime fstat.wasm
__wasi_fd_fdstat_get for stdout: 0
rights for stdout: 136314954
type for stdout: 2
has filestat_get rights for stdout: yes
__wasi_fd_filestat_get for stdout: 8
  E: __wasi_fd_filestat_get returned error: 8
fstat for stdout (fd: 1): -1
  E: fstat returned error: Bad file descriptor

and running the same with Wasmer, I instead get:

$ wasmer fstat.wasm
__wasi_fd_fdstat_get for stdout: 0
rights for stdout: 136315089
type for stdout: 2
has filestat_get rights for stdout: yes
__wasi_fd_filestat_get for stdout: 0
fstat for stdout (fd: 1): 0

Expected outcome

Should be able to fstat stdout, right? We are trying to port a program that does that to WASI and we are running in to this.

view this post on Zulip Wasmtime GitHub notifications bot (Dec 16 2020 at 19:05):

pchickey commented on Issue #2515:

Thanks for this report and the fix.

I'm currently working on a total rewrite of wasi-common in #2487. To make sure I don't introduce a regression on this issue, would you mind adding a unit test that stdio files give a reasonable filestat over in crates/test-programs/wasi-tests/src/bin?

view this post on Zulip Wasmtime GitHub notifications bot (Dec 17 2020 at 13:08):

sakarias88 commented on Issue #2515:

@pchickey
Sounds good. We will look into it.


Last updated: Nov 22 2024 at 17:03 UTC