wasmer
wasmer copied to clipboard
C-API's wasi_get_imports fails when there are also non-WASI imports.
Describe the bug
Using the C-API, wasi_get_imports(store, module, wasi_env, &imports);
returns false if the WASM module also contains imports that are not from wasi.
echo "`wasmer -V` | `rustc -V` | `uname -m`"
outputs: wasmer 2.2.0 | rustc 1.56.1 (59eed8a2a 2021-11-01) | x86_64
Steps to reproduce
-
From the project root, compile the C-API tests. I did that with
make test-capi
, which also runs them. -
cd lib/c-api/examples/
-
export WASMER_CAPI_CONFIG=cranelift-dylib
-
Backup existing wasm file used for test:
mv assets/qjs.wasm assets/qjs_backup.wasm
-
Compile this C code with emscripten:
#include "emscripten.h"
#include <stdio.h>
extern void host();
int main() {
printf("hello wasi!");
host();
return 0;
}
emcc -O3 imp.c -o assets/qjs.wasm -DNDEBUG -s ERROR_ON_UNDEFINED_SYMBOLS=0
This emits a warning about an undefined symbol, because this is an import that will be provided by the embedding code. The generated wasm module contains three imports - two from wasi, and one other import:
wabt/bin/wasm2wat assets/qjs.wasm | grep import
(import "env" "host" (func (;0;) (type 2)))
(import "wasi_snapshot_preview1" "proc_exit" (func (;1;) (type 3)))
(import "wasi_snapshot_preview1" "fd_write" (func (;2;) (type 5)))
- Run test:
./wasi
. - See error:
> Error getting WASI imports!
Error len: `38`
Error str: `Failed to resolve import "env" "host"`
- Restore original wasm file:
mv assets/qjs_backup.wasm assets/qjs.wasm
Expected behavior
I would expect the wasi imports to be extracted also when non-wasi imports exist. (And I expect the test to fail later, on module instantiation, if the code is not changed to provide the import, or to just succeed if the test code is changed to provide the import)
Actual behavior
WASI imports extraction fails so the code fails even if the rest of the test is changed to provide the non-wasi import.
Additional context
An embedding code can provide some additional, more specific API for guest wasm code beyond WASI, but currently this is impossible to do for guest code that uses WASI, because wasi_get_imports
fails (and returns false) whenever there are additional imports.