wasmer
wasmer copied to clipboard
Error running python 3.11 repl in Rust host
First off - thank you for this great effort!
I'm using the wasi-3.11 python.wasm artifact published here in my Rust host app (which is basically just the wasi example in this repo) and I am running into this runtime error:
Error while importing "wasi_snapshot_preview1"."sock_accept": unknown import. Expected Function(FunctionType { params: [I32, I32, I32], results: [I32] })
Here is the relevant portion of my host app:
let mut store = Store::default();
let module = Module::new(&store, wasm_bytes)?;
let load_dur = Instant::now() - load_start;
println!(" loaded in {:.3}", load_dur.as_secs_f32());
assert!(wasmer_wasi::is_wasi_module(&module), "module is not WASI");
fn say_hello_world() {
println!("Hello, from the Rust host app!");
}
let mut import_object = imports! {
"env" => {
"say_hello" => Function::new_typed(&mut store, say_hello_world),
},
};
println!("creating WASI env");
let wasi_env = WasiState::new("hello")
.finalize(&mut store)?;
import_object.extend(wasi_env.import_object(&mut store, &module)?.into_iter());
println!(" created");
println!("setting up instance and memory");
let instance = Instance::new(&mut store, &module, &import_object)?;
let memory = instance.exports.get_memory("memory")?;
wasi_env.data_mut(&mut store).set_memory(memory.clone());
// ...
My guess is that creating the Instance is failing because of the missing import, though I'm having trouble tracking down which rabbit hole to descend into to debug the situation. Do y'all have any helpful pointers?
❯ wasmer -V
wasmer 2.3.0
❯ rustc -V
rustc 1.65.0-nightly (02654a084 2022-08-30)
❯ uname -m
arm64
This I believe should be supported already, so we need to investigate what's going on. Thanks for creating the issue @schell
~~I think sock_accept is already implemented here.~~
It is similar but takes four args. sock_accept for snapshot_preview1 should takes two args fd flags.
https://github.com/wasmerio/wasmer/blob/3225852547ebfbb13d0a1583fcc24cd812ec09ab/lib/wasi/src/syscalls/mod.rs#L5168..L5241
maybe relating. https://github.com/wasmerio/wasmer/pull/2842 https://github.com/WebAssembly/WASI/pull/458
Same here with php wasi form wasmlab.

Still the same here on Wasmer 3.3.0 whenever I attempt to listen to a socket via Hyper or Rouille:
│ 1: failed to instantiate WASI module
│ 2: Instantiation failed
╰─▶ 3: Error while importing "wasi_snapshot_preview1"."sock_accept": unknown import. Expected Function(FunctionType { params: [I32, I32, I32], results: [I32] })
I have the same problems when execute python-3.11.3.wasm from https://github.com/vmware-labs/webassembly-language-runtimes/releases/tag/python%2F3.11.3%2B20230428-7d1b259.
See also https://github.com/vmware-labs/webassembly-language-runtimes/issues/106.
D:\Downloads>wasmer --version
wasmer 3.3.0
D:\Downloads>
D:\Downloads>wasmer run python-3.11.3.wasm
error: failed to run `python-3.11.3.wasm`
│ 1: failed to instantiate WASI module
│ 2: Instantiation failed
╰─▶ 3: Error while importing "wasi_snapshot_preview1"."sock_accept": unknown import. Expected Function(FunctionType { params: [I32, I32, I32], results: [I32] })
D:\Downloads>
This fixes the issue https://github.com/wasmerio/wasmer/pull/3971
This fixes the issue #3971
Thanks, I try to build from master branch and tested, it worked now.
D:\Downloads>D:\apps\wasmer_4.0\bin\wasmer.exe run python-3.11.3.wasm -- -V
Python 3.11.3
D:\Downloads>D:\apps\wasmer_4.0\bin\wasmer.exe run python-3.11.3.wasm -- --help
usage: python-3.11.3.wasm [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options (and corresponding environment variables):
-b : issue warnings about str(bytes_instance), str(bytearray_instance)
and comparing bytes/bytearray with str. (-bb: issue errors)
-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d : turn on parser debugging output (for experts only, only works on
debug builds); also PYTHONDEBUG=x
-E : ignore PYTHON* environment variables (such as PYTHONPATH)
-h : print this help message and exit (also -? or --help)
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-I : isolate Python from the user's environment (implies -E and -s)
-m mod : run library module as a script (terminates option list)
-O : remove assert and __debug__-dependent statements; add .opt-1 before
.pyc extension; also PYTHONOPTIMIZE=x
-OO : do -O changes and also discard docstrings; add .opt-2 before
.pyc extension
-P : don't prepend a potentially unsafe path to sys.path
-q : don't print version and copyright messages on interactive startup
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE
-S : don't imply 'import site' on initialization
-u : force the stdout and stderr streams to be unbuffered;
this option has no effect on stdin; also PYTHONUNBUFFERED=x
-v : verbose (trace import statements); also PYTHONVERBOSE=x
can be supplied multiple times to increase verbosity
-V : print the Python version number and exit (also --version)
when given twice, print more information about the build
-W arg : warning control; arg is action:message:category:module:lineno
also PYTHONWARNINGS=arg
-x : skip first line of source, allowing use of non-Unix forms of #!cmd
-X opt : set implementation-specific option
--check-hash-based-pycs always|default|never:
control how Python invalidates hash-based .pyc files
--help-env : print help about Python environment variables and exit
--help-xoptions : print help about implementation-specific -X options and exit
--help-all : print complete help information and exit
Arguments:
file : program read from script file
- : program read from stdin (default; interactive mode if a tty)
arg ...: arguments passed to program in sys.argv[1:]
D:\Downloads>
D:\Downloads>D:\apps\wasmer_4.0\bin\wasmer.exe run python-3.11.3.wasm
Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 28 2023, 09:49:53) [Clang 15.0.7 ] on wasi
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> dir(os)
['DirEntry', 'EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_LOCK', 'F_OK', 'F_TEST', 'F_TLOCK', 'F_ULOCK', 'GenericAlias', 'Mapping', 'MutableMapping', 'NGROUPS_MAX', 'O_ACCMODE', 'O_APPEND', 'O_CLOEXEC', 'O_CREAT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_EXEC', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_RSYNC', 'O_SEARCH', 'O_SYNC', 'O_TRUNC', 'O_TTY_INIT', 'O_WRONLY', 'POSIX_FADV_DONTNEED', 'POSIX_FADV_NOREUSE', 'POSIX_FADV_NORMAL', 'POSIX_FADV_RANDOM', 'POSIX_FADV_SEQUENTIAL', 'POSIX_FADV_WILLNEED', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_methods', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_walk', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'confstr', 'confstr_names', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'environ', 'environb', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execvp', 'execvpe', 'extsep', 'fdatasync', 'fdopen', 'fpathconf', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_blocking', 'get_exec_path', 'get_inheritable', 'getcwd', 'getcwdb', 'getenv', 'getenvb', 'getpid', 'isatty', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathconf', 'pathconf_names', 'pathsep', 'popen', 'posix_fadvise', 'posix_fallocate', 'pread', 'preadv', 'putenv', 'pwrite', 'pwritev', 'read', 'readlink', 'readv', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sched_yield', 'sep', 'set_blocking', 'set_inheritable', 'st', 'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'sysconf', 'sysconf_names', 'terminal_size', 'times', 'times_result', 'truncate', 'uname', 'uname_result', 'unlink', 'unsetenv', 'urandom', 'utime', 'walk', 'write', 'writev']
>>> os.getcwd()
'/'
>>> os.listdir()
[]
>>>