Stream: git-wasmtime

Topic: wasmtime / issue #9127 Error while opening file in emscri...


view this post on Zulip Wasmtime GitHub notifications bot (Aug 14 2024 at 13:48):

himakshi25 added the bug label to Issue #9127.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 14 2024 at 13:48):

himakshi25 opened issue #9127:

Created a wasm of a simple c program with command: emcc main.c -o file.wasm.
Program is trying to open a file present in test directory.

// main.c
#include <stdio.h>

int main() {
    printf("Hello world!\n");
    FILE *file = fopen("test/hello_world_file.txt", "rb");
    if (!file) {
        printf("cannot open file\n");
        return 1;
    }
    while (!feof(file)) {
        char c = fgetc(file);
        if (c != EOF) {
            putchar(c);
        }
    }
    fclose (file);
    return 0;
}

On running file.wasm with wasmtime with command wasmtime file.wasm got the below error

Hello world!
cannot open file

Also, tried giving directory as well in wasmtime but didn't work and got same error.
command wasmtime --dir=test file.wasm

Is there any way to allow wasmtimes to access or mount local directory for file read and write?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 14 2024 at 13:48):

himakshi25 edited issue #9127:

Created a wasm of a simple c program with command: emcc main.c -o file.wasm.
Program is trying to open a file present in test directory.

// main.c
#include <stdio.h>

int main() {
    printf("Hello world!\n");
    FILE *file = fopen("test/hello_world_file.txt", "rb");
    if (!file) {
        printf("cannot open file\n");
        return 1;
    }
    while (!feof(file)) {
        char c = fgetc(file);
        if (c != EOF) {
            putchar(c);
        }
    }
    fclose (file);
    return 0;
}

On running file.wasm with wasmtime with command wasmtime file.wasm got the below error

Hello world!
cannot open file

Also, tried giving directory as well in wasmtime but didn't work and got same error.
command wasmtime --dir=test file.wasm

Is there any way to allow wasmtime to access or mount local directory for file read and write?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 14 2024 at 13:54):

bjorn3 commented on issue #9127:

Unless you enabled the PURE_WASI mode, Emscripten compiled modules are not guaranteed to be WASI compatible. I don't know if that is the reason it failed here, but it is something to keep in mind.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 14 2024 at 14:47):

alexcrichton commented on issue #9127:

Have you tried --dir . perhaps? By default guests have no access to the filesystem so wasmtime file.wasm won't work. The --dir test argument I don't think will work because that makes test the root of the filesystem where the program wants to open a directory called test so the root needs to be one level higher. If that doesn't work casn you upload the wasm binary here?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 15 2024 at 19:41):

himakshi25 commented on issue #9127:

Yeah, I tried without PURE_WASI mode and this might be the possible reason, Sharing file below to confirm.
wasm.zip

As suggested, tried with PURE_WASI mode.
command: emcc main.cpp -o file.wasm -s PURE_WASI
Getting same error, sharing the wasm binary as well.

Hello world!
cannot open file

wasm_pure_wasi.zip

Could you please help to know if I am missing any other argument here?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 15 2024 at 19:41):

himakshi25 edited a comment on issue #9127:

@bjorn3 Yeah, I tried without PURE_WASI mode and this might be the possible reason, Sharing file below to confirm.
wasm.zip

As suggested, tried with PURE_WASI mode.
command: emcc main.cpp -o file.wasm -s PURE_WASI
Getting same error, sharing the wasm binary as well.

Hello world!
cannot open file

wasm_pure_wasi.zip

Could you please help to know if I am missing any other argument here?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 15 2024 at 21:13):

alexcrichton commented on issue #9127:

I think this is an issue with a flag needing to be passed to emscripten or similar. The wasm binaries here have these imports:

  (import "wasi_snapshot_preview1" "proc_exit" (func (;0;) (type 3)))
  (import "wasi_snapshot_preview1" "fd_write" (func (;1;) (type 7)))
  (import "wasi_snapshot_preview1" "fd_read" (func (;2;) (type 7)))
  (import "wasi_snapshot_preview1" "fd_close" (func (;3;) (type 0)))
  (import "wasi_snapshot_preview1" "fd_seek" (func (;4;) (type 12)))

but none of those are the import to open a host file. This means that the wasm binary is not even trying to open a host file. This makes me think the issue is with the binary itself (e.g. configuration of emscripten) rather than Wasmtime.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 19 2024 at 09:17):

rggaur789 commented on issue #9127:

Hi @alexcrichton ,
I have tried the below simple c++ code

#include <iostream>

int main() {
    std::cout << "Hello World!\n";
    return 0;
}

with command emcc main.cpp -o file.wasm -s PURE_WASI

and wasmtime file.wasm

but I am getting

Error: failed to run main module `file.wasm`

Caused by:
    0: failed to instantiate "file.wasm"
    1: unknown import: `env::_tzset_js` has not been defined

I am using emscripten and wasmtime latest versions. Can you please help what I am doing wrong here?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 19 2024 at 09:17):

rggaur789 edited a comment on issue #9127:

Hi @alexcrichton ,
I have tried the below simple c++ code

#include <iostream>

int main() {
    std::cout << "Hello World!\n";
    return 0;
}

with command emcc main.cpp -o file.wasm -s PURE_WASI

and wasmtime file.wasm

but I am getting

Error: failed to run main module `file.wasm`

Caused by:
    0: failed to instantiate "file.wasm"
    1: unknown import: `env::_tzset_js` has not been defined

I am using emscripten and wasmtime latest versions. Can you please help what I am doing wrong here?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 19 2024 at 09:18):

rggaur789 edited a comment on issue #9127:

Hi @alexcrichton / @bjorn3 ,
I have tried the below simple c++ code

#include <iostream>

int main() {
    std::cout << "Hello World!\n";
    return 0;
}

with command emcc main.cpp -o file.wasm -s PURE_WASI

and wasmtime file.wasm

but I am getting

Error: failed to run main module `file.wasm`

Caused by:
    0: failed to instantiate "file.wasm"
    1: unknown import: `env::_tzset_js` has not been defined

I am using emscripten and wasmtime latest versions. Can you please help what I am doing wrong here?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 19 2024 at 09:20):

bjorn3 commented on issue #9127:

It may be the case that the PURE_WASI mode of Emscripten is broken. It is marked as experimental anyway.

You may have more luck using wasi-sdk instead of Emscripten: https://github.com/webassembly/wasi-sdk

view this post on Zulip Wasmtime GitHub notifications bot (Aug 19 2024 at 09:21):

rggaur789 commented on issue #9127:

@bjorn3 I have tried with -s STANDALONE_WASM also but the result is same.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 19 2024 at 12:13):

rggaur789 edited a comment on issue #9127:

@bjorn3 I have tried with -s STANDALONE_WASM also but the result is same,
also in wasi-sdk c++ exception is not supported so it will not be compatible with our uses cases

view this post on Zulip Wasmtime GitHub notifications bot (Aug 19 2024 at 14:45):

alexcrichton commented on issue #9127:

@rggaur789 you're asking questions here that require a deeper knowledge of Emscripten than I think many of us have. I'd recommend raising questions with the Emscripten side of things for answers

view this post on Zulip Wasmtime GitHub notifications bot (Aug 20 2024 at 07:57):

himakshi25 commented on issue #9127:

Thanks @bjorn3 @alexcrichton for inputs.
One related query,
I also tried by using --embed-file of emscripten https://emscripten.org/docs/porting/files/packaging_files.html
command : emcc file.cpp -o file.wasm --embed-file test/hello_world_file.txt -s STANDALONE_WASM
and while executing with wasmtime getting error

Error: failed to run main module `file.wasm`

Caused by:
    0: failed to instantiate "file.wasm"
    1: unknown import: `env::_emscripten_fs_load_embedded_files` has not been defined

embed_file.zip

Is the wasmtime not supporting this emscripten file packaging feature or is there any plan to support this.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 20 2024 at 09:07):

bjorn3 commented on issue #9127:

Wasmtime doesn't support Emscripten's ABI. Said ABI is not even stable anyway AFAIK. It seems --embed-file depends on Emscripten's ABI and as such is incompatible with STANDALONE_WASM.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 20 2024 at 09:44):

himakshi25 closed issue #9127:

Created a wasm of a simple c program with command: emcc main.c -o file.wasm.
Program is trying to open a file present in test directory.

// main.c
#include <stdio.h>

int main() {
    printf("Hello world!\n");
    FILE *file = fopen("test/hello_world_file.txt", "rb");
    if (!file) {
        printf("cannot open file\n");
        return 1;
    }
    while (!feof(file)) {
        char c = fgetc(file);
        if (c != EOF) {
            putchar(c);
        }
    }
    fclose (file);
    return 0;
}

On running file.wasm with wasmtime with command wasmtime file.wasm got the below error

Hello world!
cannot open file

Also, tried giving directory as well in wasmtime but didn't work and got same error.
command wasmtime --dir=test file.wasm

Is there any way to allow wasmtime to access or mount local directory for file read and write?


Last updated: Dec 23 2024 at 12:05 UTC