From 42fa74ffb3b6400f5a7765374cfcf4a75eb18593 Mon Sep 17 00:00:00 2001 From: Johnathan Sharratt Date: Sat, 14 May 2022 23:58:04 +0200 Subject: [PATCH] Implemented multithreading and fixed a number of bugs with WASIX --- examples/wasi.rs | 5 +- examples/wasi_pipes.rs | 5 +- lib/c-api/examples/early-exit.c | 10 +- lib/c-api/examples/wasi.c | 18 +- lib/c-api/src/wasm_c_api/unstable/wasi.rs | 5 +- .../src/wasm_c_api/wasi/capture_files.rs | 93 -- lib/c-api/src/wasm_c_api/wasi/mod.rs | 53 +- lib/cli/src/commands/run/wasi.rs | 12 +- lib/cli/src/commands/wasmer_create_exe_main.c | 12 +- lib/engine-staticlib/README.md | 12 +- lib/vbus/src/lib.rs | 37 +- lib/vfs/src/lib.rs | 23 +- lib/vm/src/instance/mod.rs | 2 +- lib/wasi-types/src/bus.rs | 59 +- lib/wasi/src/lib.rs | 955 +++++++------- lib/wasi/src/macros.rs | 31 + lib/wasi/src/runtime.rs | 21 +- lib/wasi/src/state/builder.rs | 1 + lib/wasi/src/state/guard.rs | 16 +- lib/wasi/src/state/mod.rs | 131 +- lib/wasi/src/state/types.rs | 51 + lib/wasi/src/syscalls/legacy/snapshot0.rs | 29 +- lib/wasi/src/syscalls/mod.rs | 1155 ++++++++++------- lib/wasi/src/syscalls/wasi.rs | 186 ++- lib/wasi/src/syscalls/wasix32.rs | 557 ++++---- lib/wasi/src/syscalls/wasix64.rs | 557 ++++---- lib/wasi/src/utils.rs | 12 + lib/wasi/tests/stdio.rs | 19 +- .../tests/staticlib_engine_test_c_source.c | 12 +- tests/lib/wast/src/wasi_wast.rs | 7 +- 30 files changed, 2187 insertions(+), 1899 deletions(-) delete mode 100644 lib/c-api/src/wasm_c_api/wasi/capture_files.rs diff --git a/examples/wasi.rs b/examples/wasi.rs index d886a2acc87..3d24c5af60b 100644 --- a/examples/wasi.rs +++ b/examples/wasi.rs @@ -40,7 +40,7 @@ fn main() -> Result<(), Box> { println!("Creating `WasiEnv`..."); // First, we create the `WasiEnv` - let wasi_env = WasiState::new("hello") + let mut wasi_env = WasiState::new("hello") // .args(&["world"]) // .env("KEY", "Value") .finalize()?; @@ -48,8 +48,7 @@ fn main() -> Result<(), Box> { println!("Instantiating module with WASI imports..."); // Then, we get the import object related to our WASI // and attach it to the Wasm instance. - let mut wasi_thread = wasi_env.new_thread(); - let import_object = wasi_thread.import_object(&module)?; + let import_object = wasi_env.import_object(&module)?; let instance = Instance::new(&module, &import_object)?; println!("Call WASI `_start` function..."); diff --git a/examples/wasi_pipes.rs b/examples/wasi_pipes.rs index b198843caf1..fa4cad6973b 100644 --- a/examples/wasi_pipes.rs +++ b/examples/wasi_pipes.rs @@ -39,7 +39,7 @@ fn main() -> Result<(), Box> { // First, we create the `WasiEnv` with the stdio pipes let mut input = Pipe::new(); let mut output = Pipe::new(); - let wasi_env = WasiState::new("hello") + let mut wasi_env = WasiState::new("hello") .stdin(Box::new(input.clone())) .stdout(Box::new(output.clone())) .finalize()?; @@ -47,8 +47,7 @@ fn main() -> Result<(), Box> { println!("Instantiating module with WASI imports..."); // Then, we get the import object related to our WASI // and attach it to the Wasm instance. - let mut wasi_thread = wasi_env.new_thread(); - let import_object = wasi_thread.import_object(&module)?; + let import_object = wasi_env.import_object(&module)?; let instance = Instance::new(&module, &import_object)?; let msg = "racecar go zoom"; diff --git a/lib/c-api/examples/early-exit.c b/lib/c-api/examples/early-exit.c index b4479fc6143..d2410ca8baf 100644 --- a/lib/c-api/examples/early-exit.c +++ b/lib/c-api/examples/early-exit.c @@ -10,10 +10,12 @@ // Use the last_error API to retrieve error messages void print_wasmer_error() { int error_len = wasmer_last_error_length(); - printf("Error len: `%d`\n", error_len); - char *error_str = malloc(error_len); - wasmer_last_error_message(error_str, error_len); - printf("Error str: `%s`\n", error_str); + if (error_len > 0) { + printf("Error len: `%d`\n", error_len); + char *error_str = malloc(error_len); + wasmer_last_error_message(error_str, error_len); + printf("Error str: `%s`\n", error_str); + } } void print_frame(wasm_frame_t* frame) { diff --git a/lib/c-api/examples/wasi.c b/lib/c-api/examples/wasi.c index e3047912dca..941a769135e 100644 --- a/lib/c-api/examples/wasi.c +++ b/lib/c-api/examples/wasi.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -12,13 +13,16 @@ void print_wasmer_error() { int error_len = wasmer_last_error_length(); - printf("Error len: `%d`\n", error_len); - char *error_str = malloc(error_len); - wasmer_last_error_message(error_str, error_len); - printf("Error str: `%s`\n", error_str); + if (error_len > 0) { + printf("Error len: `%d`\n", error_len); + char *error_str = malloc(error_len); + wasmer_last_error_message(error_str, error_len); + printf("Error str: `%s`\n", error_str); + } } int main(int argc, const char* argv[]) { + // Initialize. printf("Initializing...\n"); wasm_engine_t* engine = wasm_engine_new(); @@ -119,6 +123,7 @@ int main(int argc, const char* argv[]) { printf("> Error calling function!\n"); return 1; } + printf("Call completed\n"); { FILE *memory_stream; @@ -137,6 +142,11 @@ int main(int argc, const char* argv[]) { do { data_read_size = wasi_env_read_stdout(wasi_env, buffer, BUF_SIZE); + if (data_read_size == -1) { + printf("failed to read stdout: %s\n", strerror(errno)); + print_wasmer_error(); + return -1; + } if (data_read_size > 0) { stdout_size += data_read_size; diff --git a/lib/c-api/src/wasm_c_api/unstable/wasi.rs b/lib/c-api/src/wasm_c_api/unstable/wasi.rs index 6970f722a63..f2d1b944713 100644 --- a/lib/c-api/src/wasm_c_api/unstable/wasi.rs +++ b/lib/c-api/src/wasm_c_api/unstable/wasi.rs @@ -6,7 +6,7 @@ use super::super::{ wasi::wasi_env_t, }; use wasmer_api::{Exportable, Extern}; -use wasmer_wasi::{generate_import_object_from_thread, get_wasi_version}; +use wasmer_wasi::{generate_import_object_from_env, get_wasi_version}; /// Unstable non-standard type wrapping `wasm_extern_t` with the /// addition of two `wasm_name_t` respectively for the module name and @@ -170,8 +170,7 @@ fn wasi_get_unordered_imports_inner( let version = c_try!(get_wasi_version(&module.inner, false) .ok_or("could not detect a WASI version on the given module")); - let thread = wasi_env.inner.new_thread(); - let import_object = generate_import_object_from_thread(store, thread, version); + let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version); imports.set_buffer( import_object diff --git a/lib/c-api/src/wasm_c_api/wasi/capture_files.rs b/lib/c-api/src/wasm_c_api/wasi/capture_files.rs deleted file mode 100644 index 85ae72dd1e3..00000000000 --- a/lib/c-api/src/wasm_c_api/wasi/capture_files.rs +++ /dev/null @@ -1,93 +0,0 @@ -//! Default implementations for capturing the stdout/stderr output of a WASI program. - -use std::collections::VecDeque; -use std::io::{self, Read, Seek, Write}; -use wasmer_wasi::{WasiFile, WasiFsError}; - -/// For capturing stdout/stderr. Stores all output in a string. -#[derive(Debug)] -pub struct OutputCapturer { - pub(crate) buffer: VecDeque, -} - -impl OutputCapturer { - pub fn new() -> Self { - Self { - buffer: VecDeque::new(), - } - } -} - -impl WasiFile for OutputCapturer { - fn last_accessed(&self) -> u64 { - 0 - } - fn last_modified(&self) -> u64 { - 0 - } - fn created_time(&self) -> u64 { - 0 - } - fn size(&self) -> u64 { - 0 - } - fn set_len(&mut self, _len: u64) -> Result<(), WasiFsError> { - Ok(()) - } - fn unlink(&mut self) -> Result<(), WasiFsError> { - Ok(()) - } - fn bytes_available(&self) -> Result { - // return an arbitrary amount - Ok(1024) - } -} - -// fail when reading or Seeking -impl Read for OutputCapturer { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { - Err(io::Error::new( - io::ErrorKind::Other, - "can not read from capturing stdout", - )) - } - fn read_to_end(&mut self, _buf: &mut Vec) -> std::io::Result { - Err(std::io::Error::new( - std::io::ErrorKind::Other, - "can not read from capturing stdout", - )) - } - fn read_to_string(&mut self, _buf: &mut String) -> io::Result { - Err(io::Error::new( - io::ErrorKind::Other, - "can not read from capturing stdout", - )) - } - fn read_exact(&mut self, _buf: &mut [u8]) -> io::Result<()> { - Err(io::Error::new( - io::ErrorKind::Other, - "can not read from capturing stdout", - )) - } -} -impl Seek for OutputCapturer { - fn seek(&mut self, _pos: io::SeekFrom) -> io::Result { - Err(io::Error::new( - io::ErrorKind::Other, - "can not seek capturing stdout", - )) - } -} -impl Write for OutputCapturer { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.buffer.extend(buf); - Ok(buf.len()) - } - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } - fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - self.buffer.extend(buf); - Ok(()) - } -} diff --git a/lib/c-api/src/wasm_c_api/wasi/mod.rs b/lib/c-api/src/wasm_c_api/wasi/mod.rs index 11e2f800207..8c5244a9c9f 100644 --- a/lib/c-api/src/wasm_c_api/wasi/mod.rs +++ b/lib/c-api/src/wasm_c_api/wasi/mod.rs @@ -2,8 +2,6 @@ //! //! This API will be superseded by a standard WASI API when/if such a standard is created. -mod capture_files; - pub use super::unstable::wasi::wasi_get_unordered_imports; use super::{ externals::{wasm_extern_vec_t, wasm_func_t}, @@ -12,15 +10,14 @@ use super::{ store::wasm_store_t, }; use crate::error::update_last_error; -use std::cmp::min; use std::convert::TryFrom; use std::ffi::CStr; use std::os::raw::c_char; use std::slice; use wasmer_api::{Exportable, Extern}; use wasmer_wasi::{ - generate_import_object_from_thread, get_wasi_version, WasiEnv, WasiFile, WasiState, - WasiStateBuilder, WasiVersion, + generate_import_object_from_env, get_wasi_version, WasiEnv, WasiFile, WasiState, + WasiStateBuilder, WasiVersion, Pipe, }; #[derive(Debug)] @@ -177,13 +174,13 @@ pub extern "C" fn wasi_env_new(mut config: Box) -> Option isize { let inner_buffer = slice::from_raw_parts_mut(buffer as *mut _, buffer_len as usize); - let mut state = env.inner.state(); + let state = env.inner.state(); - let stdout = if let Ok(stdout) = state.fs.stdout_mut() { + if let Ok(mut stdout) = state.stdout() { if let Some(stdout) = stdout.as_mut() { - stdout + read_inner(stdout, inner_buffer) } else { update_last_error("could not find a file handle for `stdout`"); return -1; } } else { + update_last_error("could not find a file handle for `stdout`"); return -1; - }; - read_inner(stdout, inner_buffer) + } } #[no_mangle] @@ -228,11 +225,10 @@ pub unsafe extern "C" fn wasi_env_read_stderr( buffer_len: usize, ) -> isize { let inner_buffer = slice::from_raw_parts_mut(buffer as *mut _, buffer_len as usize); - let mut state = env.inner.state(); - let inodes = state.inodes.write().unwrap(); - let stderr = if let Ok(stderr) = inodes.stderr_mut(&state.fs.fd_map) { + let state = env.inner.state(); + if let Ok(mut stderr) = state.stderr() { if let Some(stderr) = stderr.as_mut() { - stderr + read_inner(stderr, inner_buffer) } else { update_last_error("could not find a file handle for `stderr`"); return -1; @@ -240,24 +236,16 @@ pub unsafe extern "C" fn wasi_env_read_stderr( } else { update_last_error("could not find a file handle for `stderr`"); return -1; - }; - read_inner(stderr, inner_buffer) + } } -fn read_inner(wasi_file: &mut Box, inner_buffer: &mut [u8]) -> isize { - if let Some(oc) = wasi_file.downcast_mut::() { - let total_to_read = min(inner_buffer.len(), oc.buffer.len()); - - for (address, value) in inner_buffer - .iter_mut() - .zip(oc.buffer.drain(..total_to_read)) - { - *address = value; +fn read_inner(wasi_file: &mut Box, inner_buffer: &mut [u8]) -> isize { + match wasi_file.read(inner_buffer) { + Ok(a) => a as isize, + Err(err) => { + update_last_error(format!("failed to read wasi_file: {}", err)); + -1 } - - total_to_read as isize - } else { - -1 } } @@ -356,8 +344,7 @@ fn wasi_get_imports_inner( let version = c_try!(get_wasi_version(&module.inner, false) .ok_or("could not detect a WASI version on the given module")); - let mut thread = wasi_env.inner.new_thread(); - let import_object = generate_import_object_from_thread(store, thread, version); + let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version); imports.set_buffer(c_try!(module .inner diff --git a/lib/cli/src/commands/run/wasi.rs b/lib/cli/src/commands/run/wasi.rs index 6513c24b179..531298140e9 100644 --- a/lib/cli/src/commands/run/wasi.rs +++ b/lib/cli/src/commands/run/wasi.rs @@ -3,7 +3,7 @@ use anyhow::Result; use std::collections::BTreeSet; use std::path::PathBuf; use wasmer::{Instance, Module, RuntimeError, Val}; -use wasmer_wasi::{get_wasi_versions, WasiError, WasiState, WasiVersion}; +use wasmer_wasi::{get_wasi_versions, is_wasix_module, WasiError, WasiState, WasiVersion}; use structopt::StructOpt; @@ -96,9 +96,13 @@ impl Wasi { } } - let wasi_env = wasi_state_builder.finalize()?; - let mut wasi_thread = wasi_env.new_thread(); - let import_object = wasi_thread.import_object_for_all_wasi_versions(&module)?; + let mut wasi_env = wasi_state_builder.finalize()?; + wasi_env.state.fs.is_wasix.store( + is_wasix_module(&module), + std::sync::atomic::Ordering::Release, + ); + + let import_object = wasi_env.import_object_for_all_wasi_versions(&module)?; let instance = Instance::new(&module, &import_object)?; Ok(instance) } diff --git a/lib/cli/src/commands/wasmer_create_exe_main.c b/lib/cli/src/commands/wasmer_create_exe_main.c index c77fcab9dd7..3661ffa9d29 100644 --- a/lib/cli/src/commands/wasmer_create_exe_main.c +++ b/lib/cli/src/commands/wasmer_create_exe_main.c @@ -12,11 +12,13 @@ static void print_wasmer_error() { int error_len = wasmer_last_error_length(); - printf("Error len: `%d`\n", error_len); - char *error_str = (char *)malloc(error_len); - wasmer_last_error_message(error_str, error_len); - printf("%s\n", error_str); - free(error_str); + if (error_len > 0) { + printf("Error len: `%d`\n", error_len); + char *error_str = (char *)malloc(error_len); + wasmer_last_error_message(error_str, error_len); + printf("%s\n", error_str); + free(error_str); + } } #ifdef WASI diff --git a/lib/engine-staticlib/README.md b/lib/engine-staticlib/README.md index 9c1cc0dbd74..e8566f0137b 100644 --- a/lib/engine-staticlib/README.md +++ b/lib/engine-staticlib/README.md @@ -40,11 +40,13 @@ Now let's create a program to link with this static object file. static void print_wasmer_error() { int error_len = wasmer_last_error_length(); - printf("Error len: `%d`\n", error_len); - char* error_str = (char*) malloc(error_len); - wasmer_last_error_message(error_str, error_len); - printf("Error str: `%s`\n", error_str); - free(error_str); + if (error_len > 0) { + printf("Error len: `%d`\n", error_len); + char* error_str = (char*) malloc(error_len); + wasmer_last_error_message(error_str, error_len); + printf("Error str: `%s`\n", error_str); + free(error_str); + } } int main() { diff --git a/lib/vbus/src/lib.rs b/lib/vbus/src/lib.rs index 4f3f23d9b3c..0852a3e57be 100644 --- a/lib/vbus/src/lib.rs +++ b/lib/vbus/src/lib.rs @@ -10,11 +10,19 @@ pub type Result = std::result::Result; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[repr(transparent)] -pub struct BusDescriptor(usize); +pub struct CallDescriptor(u32); -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -#[repr(transparent)] -pub struct CallDescriptor(usize); +impl CallDescriptor { + pub fn raw(&self) -> u32 { + self.0 + } +} + +impl From for CallDescriptor { + fn from(a: u32) -> Self { + Self(a) + } +} pub trait VirtualBus: fmt::Debug + Send + Sync + 'static { /// Starts a new WAPM sub process @@ -31,6 +39,7 @@ pub trait VirtualBusSpawner { #[derive(Debug, Clone)] pub struct SpawnOptionsConfig { + reuse: bool, chroot: bool, args: Vec, preopen: Vec, @@ -43,6 +52,10 @@ pub struct SpawnOptionsConfig { } impl SpawnOptionsConfig { + pub const fn reuse(&self) -> bool { + self.reuse + } + pub const fn chroot(&self) -> bool { self.chroot } @@ -90,6 +103,7 @@ impl SpawnOptions { Self { spawner, conf: SpawnOptionsConfig { + reuse: false, chroot: false, args: Vec::new(), preopen: Vec::new(), @@ -107,6 +121,16 @@ impl SpawnOptions { self } + pub fn reuse(&mut self, reuse: bool) -> &mut Self { + self.conf.reuse = reuse; + self + } + + pub fn chroot(&mut self, chroot: bool) -> &mut Self { + self.conf.chroot = chroot; + self + } + pub fn args(&mut self, args: Vec) -> &mut Self { self.conf.args = args; self @@ -155,8 +179,6 @@ impl SpawnOptions { #[derive(Debug)] pub struct BusSpawnedProcess { - /// Handle of the instance - pub handle: BusDescriptor, /// Reference to the spawned instance pub inst: Box, } @@ -330,6 +352,9 @@ pub enum BusError { /// Invocation has failed #[error("invocation has failed")] InvokeFailed, + /// Already consumed + #[error("already consumed")] + AlreadyConsumed, /// Memory access violation #[error("memory access violation")] MemoryAccessViolation, diff --git a/lib/vfs/src/lib.rs b/lib/vfs/src/lib.rs index 927440cd212..65e725215df 100644 --- a/lib/vfs/src/lib.rs +++ b/lib/vfs/src/lib.rs @@ -22,6 +22,18 @@ pub type Result = std::result::Result; #[repr(transparent)] pub struct FileDescriptor(usize); +impl From for FileDescriptor { + fn from(a: u32) -> Self { + Self(a as usize) + } +} + +impl Into for FileDescriptor { + fn into(self) -> u32 { + self.0 as u32 + } +} + pub trait FileSystem: fmt::Debug + Send + Sync + 'static + Upcastable { fn read_dir(&self, path: &Path) -> Result; fn create_dir(&self, path: &Path) -> Result<()>; @@ -238,17 +250,6 @@ impl Upcastable for T { } } -impl dyn VirtualFile + 'static { - #[inline] - pub fn downcast_ref(&'_ self) -> Option<&'_ T> { - self.upcast_any_ref().downcast_ref::() - } - #[inline] - pub fn downcast_mut(&'_ mut self) -> Option<&'_ mut T> { - self.upcast_any_mut().downcast_mut::() - } -} - /// Determines the mode that stdio handlers will operate in #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum StdioMode { diff --git a/lib/vm/src/instance/mod.rs b/lib/vm/src/instance/mod.rs index 837bb3bfcf9..14fc16f45c5 100644 --- a/lib/vm/src/instance/mod.rs +++ b/lib/vm/src/instance/mod.rs @@ -859,7 +859,7 @@ impl Instance { /// /// This is more or less a public facade of the private `Instance`, /// providing useful higher-level API. -#[derive(Debug, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct InstanceHandle { /// The [`InstanceRef`]. See its documentation to learn more. instance: InstanceRef, diff --git a/lib/wasi-types/src/bus.rs b/lib/wasi-types/src/bus.rs index bdbd7bcfa6d..ff8bea2ecb3 100644 --- a/lib/wasi-types/src/bus.rs +++ b/lib/wasi-types/src/bus.rs @@ -2,11 +2,6 @@ use super::*; use wasmer_derive::ValueType; use wasmer_types::MemorySize; -pub type __wasi_busdatatype_t = u8; -pub const __WASI_BUS_DATA_TYPE_CALL: __wasi_busdatatype_t = 0; -pub const __WASI_BUS_DATA_TYPE_CALLBACK: __wasi_busdatatype_t = 1; -pub const __WASI_BUS_DATA_TYPE_REPLY: __wasi_busdatatype_t = 2; - pub type __wasi_busdataformat_t = u8; pub const __WASI_BUS_DATA_FORMAT_RAW: __wasi_busdataformat_t = 0; pub const __WASI_BUS_DATA_FORMAT_BINCODE: __wasi_busdataformat_t = 1; @@ -14,11 +9,15 @@ pub const __WASI_BUS_DATA_FORMAT_MESSAGE_PACK: __wasi_busdataformat_t = 2; pub const __WASI_BUS_DATA_FORMAT_JSON: __wasi_busdataformat_t = 3; pub const __WASI_BUS_DATA_FORMAT_YAML: __wasi_busdataformat_t = 4; pub const __WASI_BUS_DATA_FORMAT_XML: __wasi_busdataformat_t = 5; +pub const __WASI_BUS_DATA_FORMAT_RKYV: __wasi_busdataformat_t = 6; pub type __wasi_buseventtype_t = u8; -pub const __WASI_BUS_EVENT_TYPE_EXIT: __wasi_buseventtype_t = 0; -pub const __WASI_BUS_EVENT_TYPE_DATA: __wasi_buseventtype_t = 1; -pub const __WASI_BUS_EVENT_TYPE_FAULT: __wasi_buseventtype_t = 2; +pub const __WASI_BUS_EVENT_TYPE_NOOP: __wasi_buseventtype_t = 0; +pub const __WASI_BUS_EVENT_TYPE_EXIT: __wasi_buseventtype_t = 1; +pub const __WASI_BUS_EVENT_TYPE_CALL: __wasi_buseventtype_t = 2; +pub const __WASI_BUS_EVENT_TYPE_RESULT: __wasi_buseventtype_t = 3; +pub const __WASI_BUS_EVENT_TYPE_FAULT: __wasi_buseventtype_t = 4; +pub const __WASI_BUS_EVENT_TYPE_CLOSE: __wasi_buseventtype_t = 5; pub type __wasi_bid_t = u32; @@ -31,6 +30,13 @@ pub struct __wasi_option_bid_t { pub type __wasi_cid_t = u8; +#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)] +#[repr(C)] +pub struct __wasi_option_fd_t { + pub tag: __wasi_option_t, + pub fd: __wasi_fd_t, +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)] #[repr(C)] pub struct __wasi_option_cid_t { @@ -41,25 +47,37 @@ pub struct __wasi_option_cid_t { #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)] #[repr(C)] pub struct __wasi_bus_handles_t { - pub handle: __wasi_bid_t, - pub stdin: __wasi_fd_t, - pub stdout: __wasi_fd_t, - pub stderr: __wasi_fd_t, + pub bid: __wasi_bid_t, + pub stdin: __wasi_option_fd_t, + pub stdout: __wasi_option_fd_t, + pub stderr: __wasi_option_fd_t, } #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)] #[repr(C)] pub struct __wasi_busevent_exit_t { + pub bid: __wasi_bid_t, pub rval: __wasi_exitcode_t, } #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)] #[repr(C)] -pub struct __wasi_busevent_data_t { - pub ty: __wasi_busdatatype_t, - pub format: __wasi_busdataformat_t, +pub struct __wasi_busevent_call_t { + pub parent: __wasi_option_cid_t, pub cid: __wasi_cid_t, + pub format: __wasi_busdataformat_t, + pub topic_ptr: M::Offset, pub topic_len: M::Offset, + pub buf_ptr: M::Offset, + pub buf_len: M::Offset, +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)] +#[repr(C)] +pub struct __wasi_busevent_result_t { + pub format: __wasi_busdataformat_t, + pub cid: __wasi_cid_t, + pub buf_ptr: M::Offset, pub buf_len: M::Offset, } @@ -70,12 +88,21 @@ pub struct __wasi_busevent_fault_t { pub err: __bus_errno_t, } +#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)] +#[repr(C)] +pub struct __wasi_busevent_close_t { + pub cid: __wasi_cid_t, +} + #[derive(Copy, Clone)] #[repr(C)] pub union __wasi_busevent_u { + pub noop: u8, pub exit: __wasi_busevent_exit_t, - pub data: __wasi_busevent_data_t, + pub call: __wasi_busevent_call_t, + pub result: __wasi_busevent_result_t, pub fault: __wasi_busevent_fault_t, + pub close: __wasi_busevent_close_t, } #[derive(Copy, Clone)] diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index 5ff475d8b1d..71375e7725a 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -47,7 +47,9 @@ pub use crate::state::{ WasiStateCreationError, ALL_RIGHTS, VIRTUAL_ROOT_FD, }; pub use crate::syscalls::types; -pub use crate::utils::{get_wasi_version, get_wasi_versions, is_wasi_module, WasiVersion}; +pub use crate::utils::{ + get_wasi_version, get_wasi_versions, is_wasi_module, is_wasix_module, WasiVersion, +}; pub use wasmer_vbus::{UnsupportedVirtualBus, VirtualBus}; #[deprecated(since = "2.1.0", note = "Please use `wasmer_vfs::FsError`")] pub use wasmer_vfs::FsError as WasiFsError; @@ -62,13 +64,13 @@ use std::ops::Deref; use thiserror::Error; use wasmer::{ imports, Function, Imports, LazyInit, Memory, Memory32, MemoryAccessError, MemorySize, Module, - Store, WasmerEnv, + Store, WasmerEnv, TypedFunction, }; pub use runtime::{ - PlugableRuntimeImplementation, WasiRuntimeImplementation, WasiThreadError, WasiTtyState, + PluggableRuntimeImplementation, WasiRuntimeImplementation, WasiThreadError, WasiTtyState, }; -use std::sync::{Arc, RwLockReadGuard, RwLockWriteGuard}; +use std::sync::{mpsc, Arc, Mutex, RwLockReadGuard, RwLockWriteGuard}; use std::time::Duration; /// This is returned in `RuntimeError`. @@ -92,39 +94,201 @@ impl From for WasiThreadId { } impl Into for WasiThreadId { fn into(self) -> u32 { - self.0 + self.0 as u32 + } +} + +/// Represents the ID of a sub-process +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct WasiBusProcessId(u32); + +impl From for WasiBusProcessId { + fn from(id: u32) -> Self { + Self(id) + } +} +impl Into for WasiBusProcessId { + fn into(self) -> u32 { + self.0 as u32 } } -/// WASI processes can have multiple threads attached to the same environment -#[derive(Debug, Clone, WasmerEnv)] +#[cfg(target_family = "wasm")] +#[link(wasm_import_module = "__wbindgen_thread_xform__")] +extern "C" { + fn __wbindgen_thread_id() -> u32; +} + +#[derive(Debug, Clone)] pub struct WasiThread { /// ID of this thread + #[allow(dead_code)] id: WasiThreadId, - /// Provides access to the WASI environment - env: WasiEnv, + /// Signalers used to tell joiners that the thread has exited + exit: Arc>>>, + /// Event to wait on for the thread to join + join: Arc>>, +} + +impl WasiThread { + /// Waits for the thread to exit (false = timeout) + pub fn join(&self, timeout: Duration) -> bool { + let guard = self.join.lock().unwrap(); + match guard.recv_timeout(timeout) { + Ok(_) => true, + Err(mpsc::RecvTimeoutError::Disconnected) => true, + Err(mpsc::RecvTimeoutError::Timeout) => false, + } + } +} + +/// The environment provided to the WASI imports. +#[derive(Derivative, Clone, WasmerEnv)] +#[derivative(Debug)] +pub struct WasiEnv { + /// ID of this thread (zero is the main thread) + id: WasiThreadId, + /// Represents a reference to the memory #[wasmer(export)] memory: LazyInit, + /// If the module has it then map the thread start + #[derivative(Debug = "ignore")] + #[wasmer(export(optional = true, name = "_thread_start"))] + thread_start: LazyInit>, + #[derivative(Debug = "ignore")] + #[wasmer(export(optional = true, name = "_reactor_work"))] + reactor_work: LazyInit>, + #[derivative(Debug = "ignore")] + #[wasmer(export(optional = true, name = "_reactor_finish"))] + reactor_finish: LazyInit>, + #[derivative(Debug = "ignore")] + #[wasmer(export(optional = true, name = "_malloc"))] + malloc: LazyInit>, + #[derivative(Debug = "ignore")] + #[wasmer(export(optional = true, name = "_free"))] + free: LazyInit>, + /// Shared state of the WASI system. Manages all the data that the + /// executing WASI program can see. + pub state: Arc, + /// Implementation of the WASI runtime. + pub(crate) runtime: Arc, } -/// The WASI thread dereferences into the WASI environment -impl Deref for WasiThread { - type Target = WasiEnv; +impl WasiEnv { + pub fn new(state: WasiState) -> Self { + Self { + id: 0u32.into(), + state: Arc::new(state), + memory: LazyInit::new(), + thread_start: LazyInit::new(), + reactor_work: LazyInit::new(), + reactor_finish: LazyInit::new(), + malloc: LazyInit::new(), + free: LazyInit::new(), + runtime: Arc::new(PluggableRuntimeImplementation::default()), + } + } - fn deref(&self) -> &WasiEnv { - &self.env + /// Returns a copy of the current runtime implementation for this environment + pub fn runtime<'a>(&'a self) -> &'a (dyn WasiRuntimeImplementation) { + self.runtime.deref() } -} -impl WasiThread { - /// Returns the unique ID of this thread - pub fn thread_id(&self) -> WasiThreadId { + /// Overrides the runtime implementation for this environment + pub fn set_runtime(&mut self, runtime: R) + where + R: WasiRuntimeImplementation + Send + Sync + 'static, + { + self.runtime = Arc::new(runtime); + } + + /// Returns the current thread ID + pub fn current_thread_id(&self) -> WasiThreadId { self.id } + /// Creates a new thread only this wasi environment + pub fn new_thread(&self) -> WasiThread { + let (tx, rx) = mpsc::channel(); + + let mut guard = self.state.threading.lock().unwrap(); + + guard.thread_seed += 1; + let next_id: WasiThreadId = guard.thread_seed.into(); + + let thread = WasiThread { + id: next_id, + exit: Arc::new(Mutex::new(Some(tx))), + join: Arc::new(Mutex::new(rx)), + }; + + guard.threads.insert(thread.id, thread.clone()); + thread + } + + /// Get the WASI state + /// + /// Be careful when using this in host functions that call into Wasm: + /// if the lock is held and the Wasm calls into a host function that tries + /// to lock this mutex, the program will deadlock. + pub fn state(&self) -> &WasiState { + self.state.deref() + } + + /// Get a reference to the memory + pub fn memory(&self) -> &Memory { + self.memory + .get_ref() + .expect("Memory should be set on `WasiEnv` first") + } + + /// Copy the lazy reference so that when it's initialized during the + /// export phase, all the other references get a copy of it + pub fn memory_clone(&self) -> LazyInit { + self.memory.clone() + } + + /// Get an `Imports` for a specific version of WASI detected in the module. + pub fn import_object(&mut self, module: &Module) -> Result { + let wasi_version = get_wasi_version(module, false).ok_or(WasiError::UnknownWasiVersion)?; + Ok(generate_import_object_from_env( + module.store(), + self.clone(), + wasi_version, + )) + } + + /// Like `import_object` but containing all the WASI versions detected in + /// the module. + pub fn import_object_for_all_wasi_versions( + &mut self, + module: &Module, + ) -> Result { + let wasi_versions = + get_wasi_versions(module, false).ok_or(WasiError::UnknownWasiVersion)?; + + let mut resolver = Imports::new(); + for version in wasi_versions.iter() { + let new_import_object = + generate_import_object_from_env(module.store(), self.clone(), *version); + for ((n, m), e) in new_import_object.into_iter() { + resolver.define(&n, &m, e); + } + } + + if is_wasix_module(module) { + self.state + .fs + .is_wasix + .store(true, std::sync::atomic::Ordering::Release); + } + + Ok(resolver) + } + // Yields execution pub fn yield_now(&self) -> Result<(), WasiError> { - self.env.runtime.yield_now(self.id)?; + self.runtime.yield_now(self.id)?; Ok(()) } @@ -158,26 +322,13 @@ impl WasiThread { /// Accesses the virtual networking implementation pub fn net<'a>(&'a self) -> &'a (dyn VirtualNetworking) { - self.env.runtime.networking() + self.runtime.networking() } /// Accesses the virtual bus implementation pub fn bus<'a>(&'a self) -> &'a (dyn VirtualBus) { - self.env.runtime.bus() - } - - /// Get a reference to the memory - pub fn memory(&self) -> &Memory { - self.memory_ref() - .expect("Memory should be set on `WasiEnv` first") + self.runtime.bus() } - - // Copy the lazy reference so that when its initialized during the - // export phase that all the other references get a copy of it - pub fn memory_clone(&self) -> LazyInit { - self.memory.clone() - } - pub(crate) fn get_memory_and_wasi_state(&self, _mem_index: u32) -> (&Memory, &WasiState) { let memory = self.memory(); let state = self.state.deref(); @@ -203,460 +354,361 @@ impl WasiThread { let inodes = state.inodes.write().unwrap(); (memory, state, inodes) } - - /// Get an `Imports` for a specific version of WASI detected in the module. - pub fn import_object(&mut self, module: &Module) -> Result { - let wasi_version = get_wasi_version(module, false).ok_or(WasiError::UnknownWasiVersion)?; - Ok(generate_import_object_from_thread( - module.store(), - self.clone(), - wasi_version, - )) - } - - /// Like `import_object` but containing all the WASI versions detected in - /// the module. - pub fn import_object_for_all_wasi_versions( - &mut self, - module: &Module, - ) -> Result { - let wasi_versions = - get_wasi_versions(module, false).ok_or(WasiError::UnknownWasiVersion)?; - - let mut resolver = Imports::new(); - for version in wasi_versions.iter() { - let new_import_object = - generate_import_object_from_thread(module.store(), self.clone(), *version); - for ((n, m), e) in new_import_object.into_iter() { - resolver.define(&n, &m, e); - } - } - Ok(resolver) - } -} - -/// The environment provided to the WASI imports. -#[derive(Derivative, Clone)] -#[derivative(Debug)] -pub struct WasiEnv { - /// Represents a reference to the memory - memory: LazyInit, - /// Shared state of the WASI system. Manages all the data that the - /// executing WASI program can see. - pub state: Arc, - /// Implementation of the WASI runtime. - pub(crate) runtime: Arc, -} - -impl WasiEnv { - pub fn new(state: WasiState) -> Self { - Self { - state: Arc::new(state), - memory: LazyInit::new(), - runtime: Arc::new(PlugableRuntimeImplementation::default()), - } - } - - /// Returns a copy of the current runtime implementation for this environment - pub fn runtime<'a>(&'a self) -> &'a (dyn WasiRuntimeImplementation) { - self.runtime.deref() - } - - /// Overrides the runtime implementation for this environment - pub fn set_runtime(&mut self, runtime: R) - where - R: WasiRuntimeImplementation + Send + Sync + 'static, - { - self.runtime = Arc::new(runtime); - } - - /// Creates a new thread only this wasi environment - pub fn new_thread(&self) -> WasiThread { - WasiThread { - id: self.runtime.thread_generate_id(), - env: self.clone(), - memory: self.memory_clone(), - } - } - - /// Get the WASI state - /// - /// Be careful when using this in host functions that call into Wasm: - /// if the lock is held and the Wasm calls into a host function that tries - /// to lock this mutex, the program will deadlock. - pub fn state(&self) -> &WasiState { - self.state.deref() - } - - /// Get a reference to the memory - pub fn memory(&self) -> &Memory { - self.memory - .get_ref() - .expect("Memory should be set on `WasiEnv` first") - } - - /// Copy the lazy reference so that when it's initialized during the - /// export phase, all the other references get a copy of it - pub fn memory_clone(&self) -> LazyInit { - self.memory.clone() - } } /// Create an [`Imports`] with an existing [`WasiEnv`]. `WasiEnv` /// needs a [`WasiState`], that can be constructed from a /// [`WasiStateBuilder`](state::WasiStateBuilder). -pub fn generate_import_object_from_thread( +pub fn generate_import_object_from_env( store: &Store, - thread: WasiThread, + env: WasiEnv, version: WasiVersion, ) -> Imports { match version { - WasiVersion::Snapshot0 => generate_import_object_snapshot0(store, thread), - WasiVersion::Wasix32v1 => generate_import_object_wasix32_v1(store, thread), - WasiVersion::Wasix64v1 => generate_import_object_wasix64_v1(store, thread), + WasiVersion::Snapshot0 => generate_import_object_snapshot0(store, env), + WasiVersion::Wasix32v1 => generate_import_object_wasix32_v1(store, env), + WasiVersion::Wasix64v1 => generate_import_object_wasix64_v1(store, env), WasiVersion::Snapshot1 | WasiVersion::Latest => { - generate_import_object_snapshot1(store, thread) + generate_import_object_snapshot1(store, env) } } } /// Combines a state generating function with the import list for legacy WASI -fn generate_import_object_snapshot0(store: &Store, thread: WasiThread) -> Imports { +fn generate_import_object_snapshot0(store: &Store, env: WasiEnv) -> Imports { use self::wasi::*; imports! { "wasi_unstable" => { - "args_get" => Function::new_native_with_env(store, thread.clone(), args_get), - "args_sizes_get" => Function::new_native_with_env(store, thread.clone(), args_sizes_get), - "clock_res_get" => Function::new_native_with_env(store, thread.clone(), clock_res_get), - "clock_time_get" => Function::new_native_with_env(store, thread.clone(), clock_time_get), - "environ_get" => Function::new_native_with_env(store, thread.clone(), environ_get), - "environ_sizes_get" => Function::new_native_with_env(store, thread.clone(), environ_sizes_get), - "fd_advise" => Function::new_native_with_env(store, thread.clone(), fd_advise), - "fd_allocate" => Function::new_native_with_env(store, thread.clone(), fd_allocate), - "fd_close" => Function::new_native_with_env(store, thread.clone(), fd_close), - "fd_datasync" => Function::new_native_with_env(store, thread.clone(), fd_datasync), - "fd_fdstat_get" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_get), - "fd_fdstat_set_flags" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native_with_env(store, thread.clone(), legacy::snapshot0::fd_filestat_get), - "fd_filestat_set_size" => Function::new_native_with_env(store, thread.clone(), fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native_with_env(store, thread.clone(), fd_filestat_set_times), - "fd_pread" => Function::new_native_with_env(store, thread.clone(), fd_pread), - "fd_prestat_get" => Function::new_native_with_env(store, thread.clone(), fd_prestat_get), - "fd_prestat_dir_name" => Function::new_native_with_env(store, thread.clone(), fd_prestat_dir_name), - "fd_pwrite" => Function::new_native_with_env(store, thread.clone(), fd_pwrite), - "fd_read" => Function::new_native_with_env(store, thread.clone(), fd_read), - "fd_readdir" => Function::new_native_with_env(store, thread.clone(), fd_readdir), - "fd_renumber" => Function::new_native_with_env(store, thread.clone(), fd_renumber), - "fd_seek" => Function::new_native_with_env(store, thread.clone(), legacy::snapshot0::fd_seek), - "fd_sync" => Function::new_native_with_env(store, thread.clone(), fd_sync), - "fd_tell" => Function::new_native_with_env(store, thread.clone(), fd_tell), - "fd_write" => Function::new_native_with_env(store, thread.clone(), fd_write), - "path_create_directory" => Function::new_native_with_env(store, thread.clone(), path_create_directory), - "path_filestat_get" => Function::new_native_with_env(store, thread.clone(), legacy::snapshot0::path_filestat_get), - "path_filestat_set_times" => Function::new_native_with_env(store, thread.clone(), path_filestat_set_times), - "path_link" => Function::new_native_with_env(store, thread.clone(), path_link), - "path_open" => Function::new_native_with_env(store, thread.clone(), path_open), - "path_readlink" => Function::new_native_with_env(store, thread.clone(), path_readlink), - "path_remove_directory" => Function::new_native_with_env(store, thread.clone(), path_remove_directory), - "path_rename" => Function::new_native_with_env(store, thread.clone(), path_rename), - "path_symlink" => Function::new_native_with_env(store, thread.clone(), path_symlink), - "path_unlink_file" => Function::new_native_with_env(store, thread.clone(), path_unlink_file), - "poll_oneoff" => Function::new_native_with_env(store, thread.clone(), legacy::snapshot0::poll_oneoff), - "proc_exit" => Function::new_native_with_env(store, thread.clone(), proc_exit), - "proc_raise" => Function::new_native_with_env(store, thread.clone(), proc_raise), - "random_get" => Function::new_native_with_env(store, thread.clone(), random_get), - "sched_yield" => Function::new_native_with_env(store, thread.clone(), sched_yield), - "sock_recv" => Function::new_native_with_env(store, thread.clone(), sock_recv), - "sock_send" => Function::new_native_with_env(store, thread.clone(), sock_send), - "sock_shutdown" => Function::new_native_with_env(store, thread, sock_shutdown), + "args_get" => Function::new_native_with_env(store, env.clone(), args_get), + "args_sizes_get" => Function::new_native_with_env(store, env.clone(), args_sizes_get), + "clock_res_get" => Function::new_native_with_env(store, env.clone(), clock_res_get), + "clock_time_get" => Function::new_native_with_env(store, env.clone(), clock_time_get), + "environ_get" => Function::new_native_with_env(store, env.clone(), environ_get), + "environ_sizes_get" => Function::new_native_with_env(store, env.clone(), environ_sizes_get), + "fd_advise" => Function::new_native_with_env(store, env.clone(), fd_advise), + "fd_allocate" => Function::new_native_with_env(store, env.clone(), fd_allocate), + "fd_close" => Function::new_native_with_env(store, env.clone(), fd_close), + "fd_datasync" => Function::new_native_with_env(store, env.clone(), fd_datasync), + "fd_fdstat_get" => Function::new_native_with_env(store, env.clone(), fd_fdstat_get), + "fd_fdstat_set_flags" => Function::new_native_with_env(store, env.clone(), fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_native_with_env(store, env.clone(), fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_native_with_env(store, env.clone(), legacy::snapshot0::fd_filestat_get), + "fd_filestat_set_size" => Function::new_native_with_env(store, env.clone(), fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_native_with_env(store, env.clone(), fd_filestat_set_times), + "fd_pread" => Function::new_native_with_env(store, env.clone(), fd_pread), + "fd_prestat_get" => Function::new_native_with_env(store, env.clone(), fd_prestat_get), + "fd_prestat_dir_name" => Function::new_native_with_env(store, env.clone(), fd_prestat_dir_name), + "fd_pwrite" => Function::new_native_with_env(store, env.clone(), fd_pwrite), + "fd_read" => Function::new_native_with_env(store, env.clone(), fd_read), + "fd_readdir" => Function::new_native_with_env(store, env.clone(), fd_readdir), + "fd_renumber" => Function::new_native_with_env(store, env.clone(), fd_renumber), + "fd_seek" => Function::new_native_with_env(store, env.clone(), legacy::snapshot0::fd_seek), + "fd_sync" => Function::new_native_with_env(store, env.clone(), fd_sync), + "fd_tell" => Function::new_native_with_env(store, env.clone(), fd_tell), + "fd_write" => Function::new_native_with_env(store, env.clone(), fd_write), + "path_create_directory" => Function::new_native_with_env(store, env.clone(), path_create_directory), + "path_filestat_get" => Function::new_native_with_env(store, env.clone(), legacy::snapshot0::path_filestat_get), + "path_filestat_set_times" => Function::new_native_with_env(store, env.clone(), path_filestat_set_times), + "path_link" => Function::new_native_with_env(store, env.clone(), path_link), + "path_open" => Function::new_native_with_env(store, env.clone(), path_open), + "path_readlink" => Function::new_native_with_env(store, env.clone(), path_readlink), + "path_remove_directory" => Function::new_native_with_env(store, env.clone(), path_remove_directory), + "path_rename" => Function::new_native_with_env(store, env.clone(), path_rename), + "path_symlink" => Function::new_native_with_env(store, env.clone(), path_symlink), + "path_unlink_file" => Function::new_native_with_env(store, env.clone(), path_unlink_file), + "poll_oneoff" => Function::new_native_with_env(store, env.clone(), legacy::snapshot0::poll_oneoff), + "proc_exit" => Function::new_native_with_env(store, env.clone(), proc_exit), + "proc_raise" => Function::new_native_with_env(store, env.clone(), proc_raise), + "random_get" => Function::new_native_with_env(store, env.clone(), random_get), + "sched_yield" => Function::new_native_with_env(store, env.clone(), sched_yield), + "sock_recv" => Function::new_native_with_env(store, env.clone(), sock_recv), + "sock_send" => Function::new_native_with_env(store, env.clone(), sock_send), + "sock_shutdown" => Function::new_native_with_env(store, env, sock_shutdown), }, } } /// Combines a state generating function with the import list for snapshot 1 -fn generate_import_object_snapshot1(store: &Store, thread: WasiThread) -> Imports { +fn generate_import_object_snapshot1(store: &Store, env: WasiEnv) -> Imports { use self::wasi::*; imports! { "wasi_snapshot_preview1" => { - "args_get" => Function::new_native_with_env(store, thread.clone(), args_get), - "args_sizes_get" => Function::new_native_with_env(store, thread.clone(), args_sizes_get), - "clock_res_get" => Function::new_native_with_env(store, thread.clone(), clock_res_get), - "clock_time_get" => Function::new_native_with_env(store, thread.clone(), clock_time_get), - "environ_get" => Function::new_native_with_env(store, thread.clone(), environ_get), - "environ_sizes_get" => Function::new_native_with_env(store, thread.clone(), environ_sizes_get), - "fd_advise" => Function::new_native_with_env(store, thread.clone(), fd_advise), - "fd_allocate" => Function::new_native_with_env(store, thread.clone(), fd_allocate), - "fd_close" => Function::new_native_with_env(store, thread.clone(), fd_close), - "fd_datasync" => Function::new_native_with_env(store, thread.clone(), fd_datasync), - "fd_fdstat_get" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_get), - "fd_fdstat_set_flags" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native_with_env(store, thread.clone(), fd_filestat_get), - "fd_filestat_set_size" => Function::new_native_with_env(store, thread.clone(), fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native_with_env(store, thread.clone(), fd_filestat_set_times), - "fd_pread" => Function::new_native_with_env(store, thread.clone(), fd_pread), - "fd_prestat_get" => Function::new_native_with_env(store, thread.clone(), fd_prestat_get), - "fd_prestat_dir_name" => Function::new_native_with_env(store, thread.clone(), fd_prestat_dir_name), - "fd_pwrite" => Function::new_native_with_env(store, thread.clone(), fd_pwrite), - "fd_read" => Function::new_native_with_env(store, thread.clone(), fd_read), - "fd_readdir" => Function::new_native_with_env(store, thread.clone(), fd_readdir), - "fd_renumber" => Function::new_native_with_env(store, thread.clone(), fd_renumber), - "fd_seek" => Function::new_native_with_env(store, thread.clone(), fd_seek), - "fd_sync" => Function::new_native_with_env(store, thread.clone(), fd_sync), - "fd_tell" => Function::new_native_with_env(store, thread.clone(), fd_tell), - "fd_write" => Function::new_native_with_env(store, thread.clone(), fd_write), - "path_create_directory" => Function::new_native_with_env(store, thread.clone(), path_create_directory), - "path_filestat_get" => Function::new_native_with_env(store, thread.clone(), path_filestat_get), - "path_filestat_set_times" => Function::new_native_with_env(store, thread.clone(), path_filestat_set_times), - "path_link" => Function::new_native_with_env(store, thread.clone(), path_link), - "path_open" => Function::new_native_with_env(store, thread.clone(), path_open), - "path_readlink" => Function::new_native_with_env(store, thread.clone(), path_readlink), - "path_remove_directory" => Function::new_native_with_env(store, thread.clone(), path_remove_directory), - "path_rename" => Function::new_native_with_env(store, thread.clone(), path_rename), - "path_symlink" => Function::new_native_with_env(store, thread.clone(), path_symlink), - "path_unlink_file" => Function::new_native_with_env(store, thread.clone(), path_unlink_file), - "poll_oneoff" => Function::new_native_with_env(store, thread.clone(), poll_oneoff), - "proc_exit" => Function::new_native_with_env(store, thread.clone(), proc_exit), - "proc_raise" => Function::new_native_with_env(store, thread.clone(), proc_raise), - "random_get" => Function::new_native_with_env(store, thread.clone(), random_get), - "sched_yield" => Function::new_native_with_env(store, thread.clone(), sched_yield), - "sock_recv" => Function::new_native_with_env(store, thread.clone(), sock_recv), - "sock_send" => Function::new_native_with_env(store, thread.clone(), sock_send), - "sock_shutdown" => Function::new_native_with_env(store, thread, sock_shutdown), + "args_get" => Function::new_native_with_env(store, env.clone(), args_get), + "args_sizes_get" => Function::new_native_with_env(store, env.clone(), args_sizes_get), + "clock_res_get" => Function::new_native_with_env(store, env.clone(), clock_res_get), + "clock_time_get" => Function::new_native_with_env(store, env.clone(), clock_time_get), + "environ_get" => Function::new_native_with_env(store, env.clone(), environ_get), + "environ_sizes_get" => Function::new_native_with_env(store, env.clone(), environ_sizes_get), + "fd_advise" => Function::new_native_with_env(store, env.clone(), fd_advise), + "fd_allocate" => Function::new_native_with_env(store, env.clone(), fd_allocate), + "fd_close" => Function::new_native_with_env(store, env.clone(), fd_close), + "fd_datasync" => Function::new_native_with_env(store, env.clone(), fd_datasync), + "fd_fdstat_get" => Function::new_native_with_env(store, env.clone(), fd_fdstat_get), + "fd_fdstat_set_flags" => Function::new_native_with_env(store, env.clone(), fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_native_with_env(store, env.clone(), fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_native_with_env(store, env.clone(), fd_filestat_get), + "fd_filestat_set_size" => Function::new_native_with_env(store, env.clone(), fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_native_with_env(store, env.clone(), fd_filestat_set_times), + "fd_pread" => Function::new_native_with_env(store, env.clone(), fd_pread), + "fd_prestat_get" => Function::new_native_with_env(store, env.clone(), fd_prestat_get), + "fd_prestat_dir_name" => Function::new_native_with_env(store, env.clone(), fd_prestat_dir_name), + "fd_pwrite" => Function::new_native_with_env(store, env.clone(), fd_pwrite), + "fd_read" => Function::new_native_with_env(store, env.clone(), fd_read), + "fd_readdir" => Function::new_native_with_env(store, env.clone(), fd_readdir), + "fd_renumber" => Function::new_native_with_env(store, env.clone(), fd_renumber), + "fd_seek" => Function::new_native_with_env(store, env.clone(), fd_seek), + "fd_sync" => Function::new_native_with_env(store, env.clone(), fd_sync), + "fd_tell" => Function::new_native_with_env(store, env.clone(), fd_tell), + "fd_write" => Function::new_native_with_env(store, env.clone(), fd_write), + "path_create_directory" => Function::new_native_with_env(store, env.clone(), path_create_directory), + "path_filestat_get" => Function::new_native_with_env(store, env.clone(), path_filestat_get), + "path_filestat_set_times" => Function::new_native_with_env(store, env.clone(), path_filestat_set_times), + "path_link" => Function::new_native_with_env(store, env.clone(), path_link), + "path_open" => Function::new_native_with_env(store, env.clone(), path_open), + "path_readlink" => Function::new_native_with_env(store, env.clone(), path_readlink), + "path_remove_directory" => Function::new_native_with_env(store, env.clone(), path_remove_directory), + "path_rename" => Function::new_native_with_env(store, env.clone(), path_rename), + "path_symlink" => Function::new_native_with_env(store, env.clone(), path_symlink), + "path_unlink_file" => Function::new_native_with_env(store, env.clone(), path_unlink_file), + "poll_oneoff" => Function::new_native_with_env(store, env.clone(), poll_oneoff), + "proc_exit" => Function::new_native_with_env(store, env.clone(), proc_exit), + "proc_raise" => Function::new_native_with_env(store, env.clone(), proc_raise), + "random_get" => Function::new_native_with_env(store, env.clone(), random_get), + "sched_yield" => Function::new_native_with_env(store, env.clone(), sched_yield), + "sock_recv" => Function::new_native_with_env(store, env.clone(), sock_recv), + "sock_send" => Function::new_native_with_env(store, env.clone(), sock_send), + "sock_shutdown" => Function::new_native_with_env(store, env, sock_shutdown), } } } /// Combines a state generating function with the import list for snapshot 1 -fn generate_import_object_wasix32_v1(store: &Store, thread: WasiThread) -> Imports { +fn generate_import_object_wasix32_v1(store: &Store, env: WasiEnv) -> Imports { use self::wasix32::*; imports! { "wasix_32v1" => { - "args_get" => Function::new_native_with_env(store, thread.clone(), args_get), - "args_sizes_get" => Function::new_native_with_env(store, thread.clone(), args_sizes_get), - "clock_res_get" => Function::new_native_with_env(store, thread.clone(), clock_res_get), - "clock_time_get" => Function::new_native_with_env(store, thread.clone(), clock_time_get), - "environ_get" => Function::new_native_with_env(store, thread.clone(), environ_get), - "environ_sizes_get" => Function::new_native_with_env(store, thread.clone(), environ_sizes_get), - "fd_advise" => Function::new_native_with_env(store, thread.clone(), fd_advise), - "fd_allocate" => Function::new_native_with_env(store, thread.clone(), fd_allocate), - "fd_close" => Function::new_native_with_env(store, thread.clone(), fd_close), - "fd_datasync" => Function::new_native_with_env(store, thread.clone(), fd_datasync), - "fd_fdstat_get" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_get), - "fd_fdstat_set_flags" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native_with_env(store, thread.clone(), fd_filestat_get), - "fd_filestat_set_size" => Function::new_native_with_env(store, thread.clone(), fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native_with_env(store, thread.clone(), fd_filestat_set_times), - "fd_pread" => Function::new_native_with_env(store, thread.clone(), fd_pread), - "fd_prestat_get" => Function::new_native_with_env(store, thread.clone(), fd_prestat_get), - "fd_prestat_dir_name" => Function::new_native_with_env(store, thread.clone(), fd_prestat_dir_name), - "fd_pwrite" => Function::new_native_with_env(store, thread.clone(), fd_pwrite), - "fd_read" => Function::new_native_with_env(store, thread.clone(), fd_read), - "fd_readdir" => Function::new_native_with_env(store, thread.clone(), fd_readdir), - "fd_renumber" => Function::new_native_with_env(store, thread.clone(), fd_renumber), - "fd_dup" => Function::new_native_with_env(store, thread.clone(), fd_dup), - "fd_event" => Function::new_native_with_env(store, thread.clone(), fd_event), - "fd_seek" => Function::new_native_with_env(store, thread.clone(), fd_seek), - "fd_sync" => Function::new_native_with_env(store, thread.clone(), fd_sync), - "fd_tell" => Function::new_native_with_env(store, thread.clone(), fd_tell), - "fd_write" => Function::new_native_with_env(store, thread.clone(), fd_write), - "fd_pipe" => Function::new_native_with_env(store, thread.clone(), fd_pipe), - "path_create_directory" => Function::new_native_with_env(store, thread.clone(), path_create_directory), - "path_filestat_get" => Function::new_native_with_env(store, thread.clone(), path_filestat_get), - "path_filestat_set_times" => Function::new_native_with_env(store, thread.clone(), path_filestat_set_times), - "path_link" => Function::new_native_with_env(store, thread.clone(), path_link), - "path_open" => Function::new_native_with_env(store, thread.clone(), path_open), - "path_readlink" => Function::new_native_with_env(store, thread.clone(), path_readlink), - "path_remove_directory" => Function::new_native_with_env(store, thread.clone(), path_remove_directory), - "path_rename" => Function::new_native_with_env(store, thread.clone(), path_rename), - "path_symlink" => Function::new_native_with_env(store, thread.clone(), path_symlink), - "path_unlink_file" => Function::new_native_with_env(store, thread.clone(), path_unlink_file), - "poll_oneoff" => Function::new_native_with_env(store, thread.clone(), poll_oneoff), - "proc_exit" => Function::new_native_with_env(store, thread.clone(), proc_exit), - "proc_raise" => Function::new_native_with_env(store, thread.clone(), proc_raise), - "random_get" => Function::new_native_with_env(store, thread.clone(), random_get), - "tty_get" => Function::new_native_with_env(store, thread.clone(), tty_get), - "tty_set" => Function::new_native_with_env(store, thread.clone(), tty_set), - "getcwd" => Function::new_native_with_env(store, thread.clone(), getcwd), - "chdir" => Function::new_native_with_env(store, thread.clone(), chdir), - "thread_spawn" => Function::new_native_with_env(store, thread.clone(), thread_spawn), - "thread_sleep" => Function::new_native_with_env(store, thread.clone(), thread_sleep), - "thread_id" => Function::new_native_with_env(store, thread.clone(), thread_id), - "thread_join" => Function::new_native_with_env(store, thread.clone(), thread_join), - "thread_parallelism" => Function::new_native_with_env(store, thread.clone(), thread_parallelism), - "thread_exit" => Function::new_native_with_env(store, thread.clone(), thread_exit), - "sched_yield" => Function::new_native_with_env(store, thread.clone(), sched_yield), - "getpid" => Function::new_native_with_env(store, thread.clone(), getpid), - "bus_spawn_local" => Function::new_native_with_env(store, thread.clone(), bus_spawn_local), - "bus_spawn_remote" => Function::new_native_with_env(store, thread.clone(), bus_spawn_remote), - "bus_close" => Function::new_native_with_env(store, thread.clone(), bus_close), - "bus_invoke" => Function::new_native_with_env(store, thread.clone(), bus_invoke), - "bus_fault" => Function::new_native_with_env(store, thread.clone(), bus_fault), - "bus_drop" => Function::new_native_with_env(store, thread.clone(), bus_drop), - "bus_reply" => Function::new_native_with_env(store, thread.clone(), bus_reply), - "bus_callback" => Function::new_native_with_env(store, thread.clone(), bus_callback), - "bus_listen" => Function::new_native_with_env(store, thread.clone(), bus_listen), - "bus_poll" => Function::new_native_with_env(store, thread.clone(), bus_poll), - "bus_poll_data" => Function::new_native_with_env(store, thread.clone(), bus_poll_data), - "ws_connect" => Function::new_native_with_env(store, thread.clone(), ws_connect), - "http_request" => Function::new_native_with_env(store, thread.clone(), http_request), - "http_status" => Function::new_native_with_env(store, thread.clone(), http_status), - "port_bridge" => Function::new_native_with_env(store, thread.clone(), port_bridge), - "port_unbridge" => Function::new_native_with_env(store, thread.clone(), port_unbridge), - "port_dhcp_acquire" => Function::new_native_with_env(store, thread.clone(), port_dhcp_acquire), - "port_addr_add" => Function::new_native_with_env(store, thread.clone(), port_addr_add), - "port_addr_remove" => Function::new_native_with_env(store, thread.clone(), port_addr_remove), - "port_addr_clear" => Function::new_native_with_env(store, thread.clone(), port_addr_clear), - "port_addr_list" => Function::new_native_with_env(store, thread.clone(), port_addr_list), - "port_mac" => Function::new_native_with_env(store, thread.clone(), port_mac), - "port_gateway_set" => Function::new_native_with_env(store, thread.clone(), port_gateway_set), - "port_route_add" => Function::new_native_with_env(store, thread.clone(), port_route_add), - "port_route_remove" => Function::new_native_with_env(store, thread.clone(), port_route_remove), - "port_route_clear" => Function::new_native_with_env(store, thread.clone(), port_route_clear), - "port_route_list" => Function::new_native_with_env(store, thread.clone(), port_route_list), - "sock_status" => Function::new_native_with_env(store, thread.clone(), sock_status), - "sock_addr_local" => Function::new_native_with_env(store, thread.clone(), sock_addr_local), - "sock_addr_peer" => Function::new_native_with_env(store, thread.clone(), sock_addr_peer), - "sock_open" => Function::new_native_with_env(store, thread.clone(), sock_open), - "sock_set_opt_flag" => Function::new_native_with_env(store, thread.clone(), sock_set_opt_flag), - "sock_get_opt_flag" => Function::new_native_with_env(store, thread.clone(), sock_get_opt_flag), - "sock_set_opt_time" => Function::new_native_with_env(store, thread.clone(), sock_set_opt_time), - "sock_get_opt_time" => Function::new_native_with_env(store, thread.clone(), sock_get_opt_time), - "sock_set_opt_size" => Function::new_native_with_env(store, thread.clone(), sock_set_opt_size), - "sock_get_opt_size" => Function::new_native_with_env(store, thread.clone(), sock_get_opt_size), - "sock_join_multicast_v4" => Function::new_native_with_env(store, thread.clone(), sock_join_multicast_v4), - "sock_leave_multicast_v4" => Function::new_native_with_env(store, thread.clone(), sock_leave_multicast_v4), - "sock_join_multicast_v6" => Function::new_native_with_env(store, thread.clone(), sock_join_multicast_v6), - "sock_leave_multicast_v6" => Function::new_native_with_env(store, thread.clone(), sock_leave_multicast_v6), - "sock_bind" => Function::new_native_with_env(store, thread.clone(), sock_bind), - "sock_listen" => Function::new_native_with_env(store, thread.clone(), sock_listen), - "sock_accept" => Function::new_native_with_env(store, thread.clone(), sock_accept), - "sock_connect" => Function::new_native_with_env(store, thread.clone(), sock_connect), - "sock_recv" => Function::new_native_with_env(store, thread.clone(), sock_recv), - "sock_recv_from" => Function::new_native_with_env(store, thread.clone(), sock_recv_from), - "sock_send" => Function::new_native_with_env(store, thread.clone(), sock_send), - "sock_send_to" => Function::new_native_with_env(store, thread.clone(), sock_send_to), - "sock_send_file" => Function::new_native_with_env(store, thread.clone(), sock_send_file), - "sock_shutdown" => Function::new_native_with_env(store, thread.clone(), sock_shutdown), - "resolve" => Function::new_native_with_env(store, thread.clone(), resolve), + "args_get" => Function::new_native_with_env(store, env.clone(), args_get), + "args_sizes_get" => Function::new_native_with_env(store, env.clone(), args_sizes_get), + "clock_res_get" => Function::new_native_with_env(store, env.clone(), clock_res_get), + "clock_time_get" => Function::new_native_with_env(store, env.clone(), clock_time_get), + "environ_get" => Function::new_native_with_env(store, env.clone(), environ_get), + "environ_sizes_get" => Function::new_native_with_env(store, env.clone(), environ_sizes_get), + "fd_advise" => Function::new_native_with_env(store, env.clone(), fd_advise), + "fd_allocate" => Function::new_native_with_env(store, env.clone(), fd_allocate), + "fd_close" => Function::new_native_with_env(store, env.clone(), fd_close), + "fd_datasync" => Function::new_native_with_env(store, env.clone(), fd_datasync), + "fd_fdstat_get" => Function::new_native_with_env(store, env.clone(), fd_fdstat_get), + "fd_fdstat_set_flags" => Function::new_native_with_env(store, env.clone(), fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_native_with_env(store, env.clone(), fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_native_with_env(store, env.clone(), fd_filestat_get), + "fd_filestat_set_size" => Function::new_native_with_env(store, env.clone(), fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_native_with_env(store, env.clone(), fd_filestat_set_times), + "fd_pread" => Function::new_native_with_env(store, env.clone(), fd_pread), + "fd_prestat_get" => Function::new_native_with_env(store, env.clone(), fd_prestat_get), + "fd_prestat_dir_name" => Function::new_native_with_env(store, env.clone(), fd_prestat_dir_name), + "fd_pwrite" => Function::new_native_with_env(store, env.clone(), fd_pwrite), + "fd_read" => Function::new_native_with_env(store, env.clone(), fd_read), + "fd_readdir" => Function::new_native_with_env(store, env.clone(), fd_readdir), + "fd_renumber" => Function::new_native_with_env(store, env.clone(), fd_renumber), + "fd_dup" => Function::new_native_with_env(store, env.clone(), fd_dup), + "fd_event" => Function::new_native_with_env(store, env.clone(), fd_event), + "fd_seek" => Function::new_native_with_env(store, env.clone(), fd_seek), + "fd_sync" => Function::new_native_with_env(store, env.clone(), fd_sync), + "fd_tell" => Function::new_native_with_env(store, env.clone(), fd_tell), + "fd_write" => Function::new_native_with_env(store, env.clone(), fd_write), + "fd_pipe" => Function::new_native_with_env(store, env.clone(), fd_pipe), + "path_create_directory" => Function::new_native_with_env(store, env.clone(), path_create_directory), + "path_filestat_get" => Function::new_native_with_env(store, env.clone(), path_filestat_get), + "path_filestat_set_times" => Function::new_native_with_env(store, env.clone(), path_filestat_set_times), + "path_link" => Function::new_native_with_env(store, env.clone(), path_link), + "path_open" => Function::new_native_with_env(store, env.clone(), path_open), + "path_readlink" => Function::new_native_with_env(store, env.clone(), path_readlink), + "path_remove_directory" => Function::new_native_with_env(store, env.clone(), path_remove_directory), + "path_rename" => Function::new_native_with_env(store, env.clone(), path_rename), + "path_symlink" => Function::new_native_with_env(store, env.clone(), path_symlink), + "path_unlink_file" => Function::new_native_with_env(store, env.clone(), path_unlink_file), + "poll_oneoff" => Function::new_native_with_env(store, env.clone(), poll_oneoff), + "proc_exit" => Function::new_native_with_env(store, env.clone(), proc_exit), + "proc_raise" => Function::new_native_with_env(store, env.clone(), proc_raise), + "random_get" => Function::new_native_with_env(store, env.clone(), random_get), + "tty_get" => Function::new_native_with_env(store, env.clone(), tty_get), + "tty_set" => Function::new_native_with_env(store, env.clone(), tty_set), + "getcwd" => Function::new_native_with_env(store, env.clone(), getcwd), + "chdir" => Function::new_native_with_env(store, env.clone(), chdir), + "thread_spawn" => Function::new_native_with_env(store, env.clone(), thread_spawn), + "thread_sleep" => Function::new_native_with_env(store, env.clone(), thread_sleep), + "thread_id" => Function::new_native_with_env(store, env.clone(), thread_id), + "thread_join" => Function::new_native_with_env(store, env.clone(), thread_join), + "thread_parallelism" => Function::new_native_with_env(store, env.clone(), thread_parallelism), + "thread_exit" => Function::new_native_with_env(store, env.clone(), thread_exit), + "sched_yield" => Function::new_native_with_env(store, env.clone(), sched_yield), + "getpid" => Function::new_native_with_env(store, env.clone(), getpid), + "process_spawn" => Function::new_native_with_env(store, env.clone(), process_spawn), + "bus_open_local" => Function::new_native_with_env(store, env.clone(), bus_open_local), + "bus_open_remote" => Function::new_native_with_env(store, env.clone(), bus_open_remote), + "bus_close" => Function::new_native_with_env(store, env.clone(), bus_close), + "bus_call" => Function::new_native_with_env(store, env.clone(), bus_call), + "bus_subcall" => Function::new_native_with_env(store, env.clone(), bus_subcall), + "bus_poll" => Function::new_native_with_env(store, env.clone(), bus_poll), + "call_reply" => Function::new_native_with_env(store, env.clone(), call_reply), + "call_fault" => Function::new_native_with_env(store, env.clone(), call_fault), + "call_close" => Function::new_native_with_env(store, env.clone(), call_close), + "ws_connect" => Function::new_native_with_env(store, env.clone(), ws_connect), + "http_request" => Function::new_native_with_env(store, env.clone(), http_request), + "http_status" => Function::new_native_with_env(store, env.clone(), http_status), + "port_bridge" => Function::new_native_with_env(store, env.clone(), port_bridge), + "port_unbridge" => Function::new_native_with_env(store, env.clone(), port_unbridge), + "port_dhcp_acquire" => Function::new_native_with_env(store, env.clone(), port_dhcp_acquire), + "port_addr_add" => Function::new_native_with_env(store, env.clone(), port_addr_add), + "port_addr_remove" => Function::new_native_with_env(store, env.clone(), port_addr_remove), + "port_addr_clear" => Function::new_native_with_env(store, env.clone(), port_addr_clear), + "port_addr_list" => Function::new_native_with_env(store, env.clone(), port_addr_list), + "port_mac" => Function::new_native_with_env(store, env.clone(), port_mac), + "port_gateway_set" => Function::new_native_with_env(store, env.clone(), port_gateway_set), + "port_route_add" => Function::new_native_with_env(store, env.clone(), port_route_add), + "port_route_remove" => Function::new_native_with_env(store, env.clone(), port_route_remove), + "port_route_clear" => Function::new_native_with_env(store, env.clone(), port_route_clear), + "port_route_list" => Function::new_native_with_env(store, env.clone(), port_route_list), + "sock_status" => Function::new_native_with_env(store, env.clone(), sock_status), + "sock_addr_local" => Function::new_native_with_env(store, env.clone(), sock_addr_local), + "sock_addr_peer" => Function::new_native_with_env(store, env.clone(), sock_addr_peer), + "sock_open" => Function::new_native_with_env(store, env.clone(), sock_open), + "sock_set_opt_flag" => Function::new_native_with_env(store, env.clone(), sock_set_opt_flag), + "sock_get_opt_flag" => Function::new_native_with_env(store, env.clone(), sock_get_opt_flag), + "sock_set_opt_time" => Function::new_native_with_env(store, env.clone(), sock_set_opt_time), + "sock_get_opt_time" => Function::new_native_with_env(store, env.clone(), sock_get_opt_time), + "sock_set_opt_size" => Function::new_native_with_env(store, env.clone(), sock_set_opt_size), + "sock_get_opt_size" => Function::new_native_with_env(store, env.clone(), sock_get_opt_size), + "sock_join_multicast_v4" => Function::new_native_with_env(store, env.clone(), sock_join_multicast_v4), + "sock_leave_multicast_v4" => Function::new_native_with_env(store, env.clone(), sock_leave_multicast_v4), + "sock_join_multicast_v6" => Function::new_native_with_env(store, env.clone(), sock_join_multicast_v6), + "sock_leave_multicast_v6" => Function::new_native_with_env(store, env.clone(), sock_leave_multicast_v6), + "sock_bind" => Function::new_native_with_env(store, env.clone(), sock_bind), + "sock_listen" => Function::new_native_with_env(store, env.clone(), sock_listen), + "sock_accept" => Function::new_native_with_env(store, env.clone(), sock_accept), + "sock_connect" => Function::new_native_with_env(store, env.clone(), sock_connect), + "sock_recv" => Function::new_native_with_env(store, env.clone(), sock_recv), + "sock_recv_from" => Function::new_native_with_env(store, env.clone(), sock_recv_from), + "sock_send" => Function::new_native_with_env(store, env.clone(), sock_send), + "sock_send_to" => Function::new_native_with_env(store, env.clone(), sock_send_to), + "sock_send_file" => Function::new_native_with_env(store, env.clone(), sock_send_file), + "sock_shutdown" => Function::new_native_with_env(store, env.clone(), sock_shutdown), + "resolve" => Function::new_native_with_env(store, env.clone(), resolve), } } } -fn generate_import_object_wasix64_v1(store: &Store, thread: WasiThread) -> Imports { +fn generate_import_object_wasix64_v1(store: &Store, env: WasiEnv) -> Imports { use self::wasix64::*; imports! { "wasix_64v1" => { - "args_get" => Function::new_native_with_env(store, thread.clone(), args_get), - "args_sizes_get" => Function::new_native_with_env(store, thread.clone(), args_sizes_get), - "clock_res_get" => Function::new_native_with_env(store, thread.clone(), clock_res_get), - "clock_time_get" => Function::new_native_with_env(store, thread.clone(), clock_time_get), - "environ_get" => Function::new_native_with_env(store, thread.clone(), environ_get), - "environ_sizes_get" => Function::new_native_with_env(store, thread.clone(), environ_sizes_get), - "fd_advise" => Function::new_native_with_env(store, thread.clone(), fd_advise), - "fd_allocate" => Function::new_native_with_env(store, thread.clone(), fd_allocate), - "fd_close" => Function::new_native_with_env(store, thread.clone(), fd_close), - "fd_datasync" => Function::new_native_with_env(store, thread.clone(), fd_datasync), - "fd_fdstat_get" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_get), - "fd_fdstat_set_flags" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native_with_env(store, thread.clone(), fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native_with_env(store, thread.clone(), fd_filestat_get), - "fd_filestat_set_size" => Function::new_native_with_env(store, thread.clone(), fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native_with_env(store, thread.clone(), fd_filestat_set_times), - "fd_pread" => Function::new_native_with_env(store, thread.clone(), fd_pread), - "fd_prestat_get" => Function::new_native_with_env(store, thread.clone(), fd_prestat_get), - "fd_prestat_dir_name" => Function::new_native_with_env(store, thread.clone(), fd_prestat_dir_name), - "fd_pwrite" => Function::new_native_with_env(store, thread.clone(), fd_pwrite), - "fd_read" => Function::new_native_with_env(store, thread.clone(), fd_read), - "fd_readdir" => Function::new_native_with_env(store, thread.clone(), fd_readdir), - "fd_renumber" => Function::new_native_with_env(store, thread.clone(), fd_renumber), - "fd_dup" => Function::new_native_with_env(store, thread.clone(), fd_dup), - "fd_event" => Function::new_native_with_env(store, thread.clone(), fd_event), - "fd_seek" => Function::new_native_with_env(store, thread.clone(), fd_seek), - "fd_sync" => Function::new_native_with_env(store, thread.clone(), fd_sync), - "fd_tell" => Function::new_native_with_env(store, thread.clone(), fd_tell), - "fd_write" => Function::new_native_with_env(store, thread.clone(), fd_write), - "fd_pipe" => Function::new_native_with_env(store, thread.clone(), fd_pipe), - "path_create_directory" => Function::new_native_with_env(store, thread.clone(), path_create_directory), - "path_filestat_get" => Function::new_native_with_env(store, thread.clone(), path_filestat_get), - "path_filestat_set_times" => Function::new_native_with_env(store, thread.clone(), path_filestat_set_times), - "path_link" => Function::new_native_with_env(store, thread.clone(), path_link), - "path_open" => Function::new_native_with_env(store, thread.clone(), path_open), - "path_readlink" => Function::new_native_with_env(store, thread.clone(), path_readlink), - "path_remove_directory" => Function::new_native_with_env(store, thread.clone(), path_remove_directory), - "path_rename" => Function::new_native_with_env(store, thread.clone(), path_rename), - "path_symlink" => Function::new_native_with_env(store, thread.clone(), path_symlink), - "path_unlink_file" => Function::new_native_with_env(store, thread.clone(), path_unlink_file), - "poll_oneoff" => Function::new_native_with_env(store, thread.clone(), poll_oneoff), - "proc_exit" => Function::new_native_with_env(store, thread.clone(), proc_exit), - "proc_raise" => Function::new_native_with_env(store, thread.clone(), proc_raise), - "random_get" => Function::new_native_with_env(store, thread.clone(), random_get), - "tty_get" => Function::new_native_with_env(store, thread.clone(), tty_get), - "tty_set" => Function::new_native_with_env(store, thread.clone(), tty_set), - "getcwd" => Function::new_native_with_env(store, thread.clone(), getcwd), - "chdir" => Function::new_native_with_env(store, thread.clone(), chdir), - "thread_spawn" => Function::new_native_with_env(store, thread.clone(), thread_spawn), - "thread_sleep" => Function::new_native_with_env(store, thread.clone(), thread_sleep), - "thread_id" => Function::new_native_with_env(store, thread.clone(), thread_id), - "thread_join" => Function::new_native_with_env(store, thread.clone(), thread_join), - "thread_parallelism" => Function::new_native_with_env(store, thread.clone(), thread_parallelism), - "thread_exit" => Function::new_native_with_env(store, thread.clone(), thread_exit), - "sched_yield" => Function::new_native_with_env(store, thread.clone(), sched_yield), - "getpid" => Function::new_native_with_env(store, thread.clone(), getpid), - "bus_spawn_local" => Function::new_native_with_env(store, thread.clone(), bus_spawn_local), - "bus_spawn_remote" => Function::new_native_with_env(store, thread.clone(), bus_spawn_remote), - "bus_close" => Function::new_native_with_env(store, thread.clone(), bus_close), - "bus_invoke" => Function::new_native_with_env(store, thread.clone(), bus_invoke), - "bus_fault" => Function::new_native_with_env(store, thread.clone(), bus_fault), - "bus_drop" => Function::new_native_with_env(store, thread.clone(), bus_drop), - "bus_reply" => Function::new_native_with_env(store, thread.clone(), bus_reply), - "bus_callback" => Function::new_native_with_env(store, thread.clone(), bus_callback), - "bus_listen" => Function::new_native_with_env(store, thread.clone(), bus_listen), - "bus_poll" => Function::new_native_with_env(store, thread.clone(), bus_poll), - "bus_poll_data" => Function::new_native_with_env(store, thread.clone(), bus_poll_data), - "ws_connect" => Function::new_native_with_env(store, thread.clone(), ws_connect), - "http_request" => Function::new_native_with_env(store, thread.clone(), http_request), - "http_status" => Function::new_native_with_env(store, thread.clone(), http_status), - "port_bridge" => Function::new_native_with_env(store, thread.clone(), port_bridge), - "port_unbridge" => Function::new_native_with_env(store, thread.clone(), port_unbridge), - "port_dhcp_acquire" => Function::new_native_with_env(store, thread.clone(), port_dhcp_acquire), - "port_addr_add" => Function::new_native_with_env(store, thread.clone(), port_addr_add), - "port_addr_remove" => Function::new_native_with_env(store, thread.clone(), port_addr_remove), - "port_addr_clear" => Function::new_native_with_env(store, thread.clone(), port_addr_clear), - "port_addr_list" => Function::new_native_with_env(store, thread.clone(), port_addr_list), - "port_mac" => Function::new_native_with_env(store, thread.clone(), port_mac), - "port_gateway_set" => Function::new_native_with_env(store, thread.clone(), port_gateway_set), - "port_route_add" => Function::new_native_with_env(store, thread.clone(), port_route_add), - "port_route_remove" => Function::new_native_with_env(store, thread.clone(), port_route_remove), - "port_route_clear" => Function::new_native_with_env(store, thread.clone(), port_route_clear), - "port_route_list" => Function::new_native_with_env(store, thread.clone(), port_route_list), - "sock_status" => Function::new_native_with_env(store, thread.clone(), sock_status), - "sock_addr_local" => Function::new_native_with_env(store, thread.clone(), sock_addr_local), - "sock_addr_peer" => Function::new_native_with_env(store, thread.clone(), sock_addr_peer), - "sock_open" => Function::new_native_with_env(store, thread.clone(), sock_open), - "sock_set_opt_flag" => Function::new_native_with_env(store, thread.clone(), sock_set_opt_flag), - "sock_get_opt_flag" => Function::new_native_with_env(store, thread.clone(), sock_get_opt_flag), - "sock_set_opt_time" => Function::new_native_with_env(store, thread.clone(), sock_set_opt_time), - "sock_get_opt_time" => Function::new_native_with_env(store, thread.clone(), sock_get_opt_time), - "sock_set_opt_size" => Function::new_native_with_env(store, thread.clone(), sock_set_opt_size), - "sock_get_opt_size" => Function::new_native_with_env(store, thread.clone(), sock_get_opt_size), - "sock_join_multicast_v4" => Function::new_native_with_env(store, thread.clone(), sock_join_multicast_v4), - "sock_leave_multicast_v4" => Function::new_native_with_env(store, thread.clone(), sock_leave_multicast_v4), - "sock_join_multicast_v6" => Function::new_native_with_env(store, thread.clone(), sock_join_multicast_v6), - "sock_leave_multicast_v6" => Function::new_native_with_env(store, thread.clone(), sock_leave_multicast_v6), - "sock_bind" => Function::new_native_with_env(store, thread.clone(), sock_bind), - "sock_listen" => Function::new_native_with_env(store, thread.clone(), sock_listen), - "sock_accept" => Function::new_native_with_env(store, thread.clone(), sock_accept), - "sock_connect" => Function::new_native_with_env(store, thread.clone(), sock_connect), - "sock_recv" => Function::new_native_with_env(store, thread.clone(), sock_recv), - "sock_recv_from" => Function::new_native_with_env(store, thread.clone(), sock_recv_from), - "sock_send" => Function::new_native_with_env(store, thread.clone(), sock_send), - "sock_send_to" => Function::new_native_with_env(store, thread.clone(), sock_send_to), - "sock_send_file" => Function::new_native_with_env(store, thread.clone(), sock_send_file), - "sock_shutdown" => Function::new_native_with_env(store, thread.clone(), sock_shutdown), - "resolve" => Function::new_native_with_env(store, thread.clone(), resolve), + "args_get" => Function::new_native_with_env(store, env.clone(), args_get), + "args_sizes_get" => Function::new_native_with_env(store, env.clone(), args_sizes_get), + "clock_res_get" => Function::new_native_with_env(store, env.clone(), clock_res_get), + "clock_time_get" => Function::new_native_with_env(store, env.clone(), clock_time_get), + "environ_get" => Function::new_native_with_env(store, env.clone(), environ_get), + "environ_sizes_get" => Function::new_native_with_env(store, env.clone(), environ_sizes_get), + "fd_advise" => Function::new_native_with_env(store, env.clone(), fd_advise), + "fd_allocate" => Function::new_native_with_env(store, env.clone(), fd_allocate), + "fd_close" => Function::new_native_with_env(store, env.clone(), fd_close), + "fd_datasync" => Function::new_native_with_env(store, env.clone(), fd_datasync), + "fd_fdstat_get" => Function::new_native_with_env(store, env.clone(), fd_fdstat_get), + "fd_fdstat_set_flags" => Function::new_native_with_env(store, env.clone(), fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_native_with_env(store, env.clone(), fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_native_with_env(store, env.clone(), fd_filestat_get), + "fd_filestat_set_size" => Function::new_native_with_env(store, env.clone(), fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_native_with_env(store, env.clone(), fd_filestat_set_times), + "fd_pread" => Function::new_native_with_env(store, env.clone(), fd_pread), + "fd_prestat_get" => Function::new_native_with_env(store, env.clone(), fd_prestat_get), + "fd_prestat_dir_name" => Function::new_native_with_env(store, env.clone(), fd_prestat_dir_name), + "fd_pwrite" => Function::new_native_with_env(store, env.clone(), fd_pwrite), + "fd_read" => Function::new_native_with_env(store, env.clone(), fd_read), + "fd_readdir" => Function::new_native_with_env(store, env.clone(), fd_readdir), + "fd_renumber" => Function::new_native_with_env(store, env.clone(), fd_renumber), + "fd_dup" => Function::new_native_with_env(store, env.clone(), fd_dup), + "fd_event" => Function::new_native_with_env(store, env.clone(), fd_event), + "fd_seek" => Function::new_native_with_env(store, env.clone(), fd_seek), + "fd_sync" => Function::new_native_with_env(store, env.clone(), fd_sync), + "fd_tell" => Function::new_native_with_env(store, env.clone(), fd_tell), + "fd_write" => Function::new_native_with_env(store, env.clone(), fd_write), + "fd_pipe" => Function::new_native_with_env(store, env.clone(), fd_pipe), + "path_create_directory" => Function::new_native_with_env(store, env.clone(), path_create_directory), + "path_filestat_get" => Function::new_native_with_env(store, env.clone(), path_filestat_get), + "path_filestat_set_times" => Function::new_native_with_env(store, env.clone(), path_filestat_set_times), + "path_link" => Function::new_native_with_env(store, env.clone(), path_link), + "path_open" => Function::new_native_with_env(store, env.clone(), path_open), + "path_readlink" => Function::new_native_with_env(store, env.clone(), path_readlink), + "path_remove_directory" => Function::new_native_with_env(store, env.clone(), path_remove_directory), + "path_rename" => Function::new_native_with_env(store, env.clone(), path_rename), + "path_symlink" => Function::new_native_with_env(store, env.clone(), path_symlink), + "path_unlink_file" => Function::new_native_with_env(store, env.clone(), path_unlink_file), + "poll_oneoff" => Function::new_native_with_env(store, env.clone(), poll_oneoff), + "proc_exit" => Function::new_native_with_env(store, env.clone(), proc_exit), + "proc_raise" => Function::new_native_with_env(store, env.clone(), proc_raise), + "random_get" => Function::new_native_with_env(store, env.clone(), random_get), + "tty_get" => Function::new_native_with_env(store, env.clone(), tty_get), + "tty_set" => Function::new_native_with_env(store, env.clone(), tty_set), + "getcwd" => Function::new_native_with_env(store, env.clone(), getcwd), + "chdir" => Function::new_native_with_env(store, env.clone(), chdir), + "thread_spawn" => Function::new_native_with_env(store, env.clone(), thread_spawn), + "thread_sleep" => Function::new_native_with_env(store, env.clone(), thread_sleep), + "thread_id" => Function::new_native_with_env(store, env.clone(), thread_id), + "thread_join" => Function::new_native_with_env(store, env.clone(), thread_join), + "thread_parallelism" => Function::new_native_with_env(store, env.clone(), thread_parallelism), + "thread_exit" => Function::new_native_with_env(store, env.clone(), thread_exit), + "sched_yield" => Function::new_native_with_env(store, env.clone(), sched_yield), + "getpid" => Function::new_native_with_env(store, env.clone(), getpid), + "process_spawn" => Function::new_native_with_env(store, env.clone(), process_spawn), + "bus_open_local" => Function::new_native_with_env(store, env.clone(), bus_open_local), + "bus_open_remote" => Function::new_native_with_env(store, env.clone(), bus_open_remote), + "bus_close" => Function::new_native_with_env(store, env.clone(), bus_close), + "bus_call" => Function::new_native_with_env(store, env.clone(), bus_call), + "bus_subcall" => Function::new_native_with_env(store, env.clone(), bus_subcall), + "bus_poll" => Function::new_native_with_env(store, env.clone(), bus_poll), + "call_reply" => Function::new_native_with_env(store, env.clone(), call_reply), + "call_fault" => Function::new_native_with_env(store, env.clone(), call_fault), + "call_close" => Function::new_native_with_env(store, env.clone(), call_close), + "ws_connect" => Function::new_native_with_env(store, env.clone(), ws_connect), + "http_request" => Function::new_native_with_env(store, env.clone(), http_request), + "http_status" => Function::new_native_with_env(store, env.clone(), http_status), + "port_bridge" => Function::new_native_with_env(store, env.clone(), port_bridge), + "port_unbridge" => Function::new_native_with_env(store, env.clone(), port_unbridge), + "port_dhcp_acquire" => Function::new_native_with_env(store, env.clone(), port_dhcp_acquire), + "port_addr_add" => Function::new_native_with_env(store, env.clone(), port_addr_add), + "port_addr_remove" => Function::new_native_with_env(store, env.clone(), port_addr_remove), + "port_addr_clear" => Function::new_native_with_env(store, env.clone(), port_addr_clear), + "port_addr_list" => Function::new_native_with_env(store, env.clone(), port_addr_list), + "port_mac" => Function::new_native_with_env(store, env.clone(), port_mac), + "port_gateway_set" => Function::new_native_with_env(store, env.clone(), port_gateway_set), + "port_route_add" => Function::new_native_with_env(store, env.clone(), port_route_add), + "port_route_remove" => Function::new_native_with_env(store, env.clone(), port_route_remove), + "port_route_clear" => Function::new_native_with_env(store, env.clone(), port_route_clear), + "port_route_list" => Function::new_native_with_env(store, env.clone(), port_route_list), + "sock_status" => Function::new_native_with_env(store, env.clone(), sock_status), + "sock_addr_local" => Function::new_native_with_env(store, env.clone(), sock_addr_local), + "sock_addr_peer" => Function::new_native_with_env(store, env.clone(), sock_addr_peer), + "sock_open" => Function::new_native_with_env(store, env.clone(), sock_open), + "sock_set_opt_flag" => Function::new_native_with_env(store, env.clone(), sock_set_opt_flag), + "sock_get_opt_flag" => Function::new_native_with_env(store, env.clone(), sock_get_opt_flag), + "sock_set_opt_time" => Function::new_native_with_env(store, env.clone(), sock_set_opt_time), + "sock_get_opt_time" => Function::new_native_with_env(store, env.clone(), sock_get_opt_time), + "sock_set_opt_size" => Function::new_native_with_env(store, env.clone(), sock_set_opt_size), + "sock_get_opt_size" => Function::new_native_with_env(store, env.clone(), sock_get_opt_size), + "sock_join_multicast_v4" => Function::new_native_with_env(store, env.clone(), sock_join_multicast_v4), + "sock_leave_multicast_v4" => Function::new_native_with_env(store, env.clone(), sock_leave_multicast_v4), + "sock_join_multicast_v6" => Function::new_native_with_env(store, env.clone(), sock_join_multicast_v6), + "sock_leave_multicast_v6" => Function::new_native_with_env(store, env.clone(), sock_leave_multicast_v6), + "sock_bind" => Function::new_native_with_env(store, env.clone(), sock_bind), + "sock_listen" => Function::new_native_with_env(store, env.clone(), sock_listen), + "sock_accept" => Function::new_native_with_env(store, env.clone(), sock_accept), + "sock_connect" => Function::new_native_with_env(store, env.clone(), sock_connect), + "sock_recv" => Function::new_native_with_env(store, env.clone(), sock_recv), + "sock_recv_from" => Function::new_native_with_env(store, env.clone(), sock_recv_from), + "sock_send" => Function::new_native_with_env(store, env.clone(), sock_send), + "sock_send_to" => Function::new_native_with_env(store, env.clone(), sock_send_to), + "sock_send_file" => Function::new_native_with_env(store, env.clone(), sock_send_file), + "sock_shutdown" => Function::new_native_with_env(store, env.clone(), sock_shutdown), + "resolve" => Function::new_native_with_env(store, env.clone(), resolve), } } } @@ -669,3 +721,12 @@ fn mem_error_to_wasi(err: MemoryAccessError) -> types::__wasi_errno_t { _ => types::__WASI_EINVAL, } } + +fn mem_error_to_bus(err: MemoryAccessError) -> types::__bus_errno_t { + match err { + MemoryAccessError::HeapOutOfBounds => types::__BUS_EMEMVIOLATION, + MemoryAccessError::Overflow => types::__BUS_EMEMVIOLATION, + MemoryAccessError::NonUtf8String => types::__BUS_EBADREQUEST, + _ => types::__BUS_EUNKNOWN, + } +} diff --git a/lib/wasi/src/macros.rs b/lib/wasi/src/macros.rs index 259fa572ca7..b9ec4245597 100644 --- a/lib/wasi/src/macros.rs +++ b/lib/wasi/src/macros.rs @@ -53,6 +53,24 @@ macro_rules! wasi_try_ok { }}; } +/// Like the `try!` macro or `?` syntax: returns the value if the computation +/// succeeded or returns the error value. +macro_rules! wasi_try_bus { + ($expr:expr) => {{ + let res: Result<_, crate::syscalls::types::__bus_errno_t> = $expr; + match res { + Ok(val) => { + tracing::trace!("wasi::wasi_try_bus::val: {:?}", val); + val + } + Err(err) => { + tracing::debug!("wasi::wasi_try_bus::err: {:?}", err); + return err; + } + } + }}; +} + /// Like `wasi_try` but converts a `MemoryAccessError` to a __wasi_errno_t`. macro_rules! wasi_try_mem { ($expr:expr) => {{ @@ -60,6 +78,13 @@ macro_rules! wasi_try_mem { }}; } +/// Like `wasi_try` but converts a `MemoryAccessError` to a __bus_errno_t`. +macro_rules! wasi_try_mem_bus { + ($expr:expr) => {{ + wasi_try_bus!($expr.map_err($crate::mem_error_to_bus)) + }}; +} + /// Like `wasi_try` but converts a `MemoryAccessError` to a __wasi_errno_t`. macro_rules! wasi_try_mem_ok { ($expr:expr) => {{ @@ -77,3 +102,9 @@ macro_rules! get_input_str { wasi_try_mem!($data.read_utf8_string($memory, $len)) }}; } + +macro_rules! get_input_str_bus { + ($memory:expr, $data:expr, $len:expr) => {{ + wasi_try_mem_bus!($data.read_utf8_string($memory, $len)) + }}; +} diff --git a/lib/wasi/src/runtime.rs b/lib/wasi/src/runtime.rs index ae196747e9d..e24f5c52f55 100644 --- a/lib/wasi/src/runtime.rs +++ b/lib/wasi/src/runtime.rs @@ -76,10 +76,8 @@ pub trait WasiRuntimeImplementation: fmt::Debug + Sync { /// Spawns a new thread by invoking the fn thread_spawn( &self, - _method: &str, - _user_data: u64, - _reactor: bool, - ) -> Result { + _callback: Box, + ) -> Result<(), WasiThreadError> { Err(WasiThreadError::Unsupported) } @@ -88,11 +86,6 @@ pub trait WasiRuntimeImplementation: fmt::Debug + Sync { Err(WasiThreadError::Unsupported) } - // Joins the specified thread with this thread - which effectively waits for it to exit - fn thread_join(&self, _other: WasiThreadId) -> Result<(), WasiThreadError> { - Err(WasiThreadError::Unsupported) - } - /// Invokes whenever a WASM thread goes idle. In some runtimes (like singlethreaded /// execution environments) they will need to do asynchronous work whenever the main /// thread goes idle and this is the place to hook for that. @@ -108,13 +101,13 @@ pub trait WasiRuntimeImplementation: fmt::Debug + Sync { } #[derive(Debug)] -pub struct PlugableRuntimeImplementation { +pub struct PluggableRuntimeImplementation { pub bus: Box, pub networking: Box, pub thread_id_seed: AtomicU32, } -impl PlugableRuntimeImplementation { +impl PluggableRuntimeImplementation { pub fn set_bus_implementation(&mut self, bus: I) where I: VirtualBus + Sync, @@ -130,7 +123,7 @@ impl PlugableRuntimeImplementation { } } -impl Default for PlugableRuntimeImplementation { +impl Default for PluggableRuntimeImplementation { fn default() -> Self { Self { #[cfg(not(feature = "host-vnet"))] @@ -143,13 +136,15 @@ impl Default for PlugableRuntimeImplementation { } } -impl WasiRuntimeImplementation for PlugableRuntimeImplementation { +impl WasiRuntimeImplementation for PluggableRuntimeImplementation { fn bus<'a>(&'a self) -> &'a (dyn VirtualBus) { self.bus.deref() } + fn networking<'a>(&'a self) -> &'a (dyn VirtualNetworking) { self.networking.deref() } + fn thread_generate_id(&self) -> WasiThreadId { self.thread_id_seed.fetch_add(1, Ordering::Relaxed).into() } diff --git a/lib/wasi/src/state/builder.rs b/lib/wasi/src/state/builder.rs index a576baee119..73efed3b5f9 100644 --- a/lib/wasi/src/state/builder.rs +++ b/lib/wasi/src/state/builder.rs @@ -470,6 +470,7 @@ impl WasiStateBuilder { fs: wasi_fs, inodes: Arc::new(inodes), args: self.args.clone(), + threading: Default::default(), envs: self .envs .iter() diff --git a/lib/wasi/src/state/guard.rs b/lib/wasi/src/state/guard.rs index 17ec9164a5a..8e21647de35 100644 --- a/lib/wasi/src/state/guard.rs +++ b/lib/wasi/src/state/guard.rs @@ -1,6 +1,6 @@ use std::{sync::{ RwLockReadGuard, RwLockWriteGuard -}, io::Seek}; +}, io::{Seek, Read}}; use super::*; #[derive(Debug)] @@ -50,17 +50,19 @@ pub(crate) struct WasiStateFileGuard { impl WasiStateFileGuard { - pub fn new(state: &WasiState, fd: __wasi_fd_t) -> Result { + pub fn new(state: &WasiState, fd: __wasi_fd_t) -> Result, FsError> { let inodes = state.inodes.read().unwrap(); let fd_map = state.fs.fd_map.read().unwrap(); if let Some(fd) = fd_map.get(&fd) { let guard = inodes.arena[fd.inode].read(); if let Kind::File { .. } = guard.deref() { Ok( - Self { - inodes: state.inodes.clone(), - inode: fd.inode - } + Some( + Self { + inodes: state.inodes.clone(), + inode: fd.inode + } + ) ) } else { // Our public API should ensure that this is not possible @@ -68,7 +70,7 @@ impl WasiStateFileGuard } } else { - Err(FsError::NoDevice) + Ok(None) } } diff --git a/lib/wasi/src/state/mod.rs b/lib/wasi/src/state/mod.rs index 1b89c88b1d5..3ca24694c19 100644 --- a/lib/wasi/src/state/mod.rs +++ b/lib/wasi/src/state/mod.rs @@ -28,13 +28,16 @@ pub use self::types::*; pub use self::guard::*; use crate::syscalls::types::*; use crate::utils::map_io_err; +use crate::WasiBusProcessId; +use crate::WasiThread; +use crate::WasiThreadId; use generational_arena::Arena; pub use generational_arena::Index as Inode; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +use std::borrow::Cow; use std::collections::HashMap; use std::collections::VecDeque; -use std::io::Read; use std::sync::mpsc; use std::sync::Arc; use std::{ @@ -43,11 +46,12 @@ use std::{ ops::{Deref, DerefMut}, path::{Path, PathBuf}, sync::{ - atomic::{AtomicU32, AtomicU64, Ordering}, + atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard, }, }; use tracing::{debug, trace}; +use wasmer_vbus::BusSpawnedProcess; use wasmer_vfs::{FileSystem, FsError, OpenOptions, VirtualFile}; @@ -326,6 +330,7 @@ pub struct WasiFs { pub next_fd: AtomicU32, inode_counter: AtomicU64, pub current_dir: Mutex, + pub is_wasix: AtomicBool, #[cfg_attr(feature = "enable-serde", serde(skip, default = "default_fs_backing"))] pub fs_backing: Box, } @@ -577,6 +582,7 @@ impl WasiFs { next_fd: AtomicU32::new(3), inode_counter: AtomicU64::new(1024), current_dir: Mutex::new("/".to_string()), + is_wasix: AtomicBool::new(false), fs_backing, }; wasi_fs.create_stdin(inodes); @@ -830,22 +836,25 @@ impl WasiFs { pub fn get_current_dir( &self, inodes: &mut WasiInodes, + base: __wasi_fd_t, ) -> Result<(Inode, String), __wasi_errno_t> { - self.get_current_dir_inner(inodes, 0) + self.get_current_dir_inner(inodes, base, 0) } - fn get_current_dir_inner( + pub(crate) fn get_current_dir_inner( &self, inodes: &mut WasiInodes, + base: __wasi_fd_t, symlink_count: u32, ) -> Result<(Inode, String), __wasi_errno_t> { let current_dir = { let guard = self.current_dir.lock().unwrap(); guard.clone() }; + let cur_inode = self.get_fd_inode(base)?; let inode = self.get_inode_at_path_inner( inodes, - VIRTUAL_ROOT_FD, + cur_inode, current_dir.as_str(), symlink_count, true, @@ -869,7 +878,7 @@ impl WasiFs { fn get_inode_at_path_inner( &self, inodes: &mut WasiInodes, - base: __wasi_fd_t, + mut cur_inode: generational_arena::Index, path: &str, mut symlink_count: u32, follow_symlinks: bool, @@ -879,23 +888,8 @@ impl WasiFs { } let path: &Path = Path::new(path); - let mut cur_inode = self.get_fd_inode(base)?; - if path.to_str().unwrap() == "/" && base == VIRTUAL_ROOT_FD { - return Ok(cur_inode); - } let n_components = path.components().count(); - // If this is the start of the path search and it starts with the - // current directory marker then we need to resolve the current - // path to the inode before we do the search - if path.components().next().map(|c| c.as_os_str().to_str()).flatten() == Some(".") - && base == VIRTUAL_ROOT_FD // File queries will occur on the root first - && symlink_count == 0 - // The resolving of the current path only should happen once and not after symbolic links are followed - { - (cur_inode, _) = self.get_current_dir_inner(inodes, symlink_count + 1)?; - } - // TODO: rights checks 'path_iter: for (i, component) in path.components().enumerate() { // used to terminate symlink resolution properly @@ -1093,6 +1087,8 @@ impl WasiFs { relative_path, } => { let new_base_dir = *base_po_dir; + let new_base_inode = self.get_fd_inode(new_base_dir)?; + // allocate to reborrow mutabily to recur let new_path = { /*if let Kind::Root { .. } = self.inodes[base_po_dir].kind { @@ -1109,7 +1105,7 @@ impl WasiFs { drop(guard); let symlink_inode = self.get_inode_at_path_inner( inodes, - new_base_dir, + new_base_inode, &new_path, symlink_count + 1, follow_symlinks, @@ -1239,7 +1235,15 @@ impl WasiFs { path: &str, follow_symlinks: bool, ) -> Result { - self.get_inode_at_path_inner(inodes, base, path, 0, follow_symlinks) + let start_inode; + if path.starts_with("/") == false && self.is_wasix.load(Ordering::Acquire) { + let (cur_inode, _) = self.get_current_dir(inodes, base)?; + start_inode = cur_inode; + } else { + start_inode = self.get_fd_inode(base)?; + } + + self.get_inode_at_path_inner(inodes, start_inode, path, 0, follow_symlinks) } /// Returns the parent Dir or Root that the file at a given path is in and the file name @@ -1365,19 +1369,23 @@ impl WasiFs { let inode_val = &inodes.arena[inode]; if inode_val.is_preopened { - Ok(__wasi_prestat_t { - pr_type: __WASI_PREOPENTYPE_DIR, - u: PrestatEnum::Dir { - // REVIEW: - pr_name_len: inode_val.name.len() as u32 + 1, - } - .untagged(), - }) + Ok(self.prestat_fd_inner(inode_val)) } else { Err(__WASI_EBADF) } } + pub(crate) fn prestat_fd_inner(&self, inode_val: &InodeVal) -> __wasi_prestat_t { + __wasi_prestat_t { + pr_type: __WASI_PREOPENTYPE_DIR, + u: PrestatEnum::Dir { + // REVIEW: + pr_name_len: inode_val.name.len() as u32 + 1, + } + .untagged(), + } + } + pub fn flush(&self, inodes: &WasiInodes, fd: __wasi_fd_t) -> Result<(), __wasi_errno_t> { match fd { __WASI_STDIN_FILENO => (), @@ -1794,6 +1802,20 @@ impl WasiState { } } +/// Structures used for the threading and sub-processes +/// +/// These internal implementation details are hidden away from the +/// consumer who should instead implement the vbus trait on the runtime +#[derive(Debug, Default)] +#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +pub(crate) struct WasiStateThreading { + pub threads: HashMap, + pub thread_seed: u32, + pub processes: HashMap, + pub process_reuse: HashMap, WasiBusProcessId>, + pub process_seed: u32, +} + /// Top level data type containing all* the state with which WASI can /// interact. /// @@ -1827,6 +1849,7 @@ impl WasiState { pub struct WasiState { pub fs: WasiFs, pub inodes: Arc>, + pub(crate) threading: Mutex, pub args: Vec>, pub envs: Vec>, } @@ -1854,32 +1877,68 @@ impl WasiState { /// Get the `VirtualFile` object at stdout pub fn stdout( &self, - ) -> Result, FsError> { + ) -> Result>, FsError> { self.std_dev_get(__WASI_STDOUT_FILENO) } + #[deprecated( + since = "3.0.0", + note = "stdout_mut() is no longer needed - just use stdout() instead" + )] + pub fn stdout_mut( + &self + ) -> Result>, FsError> { + self.stdout() + } + /// Get the `VirtualFile` object at stderr pub fn stderr( &self, - ) -> Result, FsError> { + ) -> Result>, FsError> { self.std_dev_get(__WASI_STDERR_FILENO) } + #[deprecated( + since = "3.0.0", + note = "stderr_mut() is no longer needed - just use stderr() instead" + )] + pub fn stderr_mut( + &self + ) -> Result>, FsError> { + self.stderr() + } + /// Get the `VirtualFile` object at stdin pub fn stdin( &self, - ) -> Result, FsError> { + ) -> Result>, FsError> { self.std_dev_get(__WASI_STDIN_FILENO) } + #[deprecated( + since = "3.0.0", + note = "stdin_mut() is no longer needed - just use stdin() instead" + )] + pub fn stdin_mut( + &self + ) -> Result>, FsError> { + self.stdin() + } + /// Internal helper function to get a standard device handle. /// Expects one of `__WASI_STDIN_FILENO`, `__WASI_STDOUT_FILENO`, `__WASI_STDERR_FILENO`. fn std_dev_get( &self, fd: __wasi_fd_t, - ) -> Result, FsError> { + ) -> Result>, FsError> { + let ret = WasiStateFileGuard::new(self, fd)? + .map(|a| { + let ret = Box::new(a); + let ret: Box = ret; + ret + }); Ok( - Box::new(WasiStateFileGuard::new(self, fd)?) + ret ) } } diff --git a/lib/wasi/src/state/types.rs b/lib/wasi/src/state/types.rs index f850f3eb177..aac10e3dffe 100644 --- a/lib/wasi/src/state/types.rs +++ b/lib/wasi/src/state/types.rs @@ -10,6 +10,7 @@ use std::{ sync::{Arc, Mutex}, time::Duration, }; +use wasmer_vbus::BusError; #[cfg(feature = "host-fs")] pub use wasmer_vfs::host_fs::{Stderr, Stdin, Stdout}; @@ -101,6 +102,56 @@ pub fn net_error_into_wasi_err(net_error: NetworkError) -> __wasi_errno_t { } } +pub fn bus_error_into_wasi_err(bus_error: BusError) -> __bus_errno_t { + use BusError::*; + match bus_error { + Serialization => __BUS_ESER, + Deserialization => __BUS_EDES, + InvalidWapm => __BUS_EWAPM, + FetchFailed => __BUS_EFETCH, + CompileError => __BUS_ECOMPILE, + InvalidABI => __BUS_EABI, + Aborted => __BUS_EABORTED, + BadHandle => __BUS_EBADHANDLE, + InvalidTopic => __BUS_ETOPIC, + BadCallback => __BUS_EBADCB, + Unsupported => __BUS_EUNSUPPORTED, + BadRequest => __BUS_EBADREQUEST, + AccessDenied => __BUS_EDENIED, + InternalError => __BUS_EINTERNAL, + MemoryAllocationFailed => __BUS_EALLOC, + InvokeFailed => __BUS_EINVOKE, + AlreadyConsumed => __BUS_ECONSUMED, + MemoryAccessViolation => __BUS_EMEMVIOLATION, + UnknownError => __BUS_EUNKNOWN, + } +} + +pub fn wasi_error_into_bus_err(bus_error: __bus_errno_t) -> BusError { + use BusError::*; + match bus_error { + __BUS_ESER => Serialization, + __BUS_EDES => Deserialization, + __BUS_EWAPM => InvalidWapm, + __BUS_EFETCH => FetchFailed, + __BUS_ECOMPILE => CompileError, + __BUS_EABI => InvalidABI, + __BUS_EABORTED => Aborted, + __BUS_EBADHANDLE => BadHandle, + __BUS_ETOPIC => InvalidTopic, + __BUS_EBADCB => BadCallback, + __BUS_EUNSUPPORTED => Unsupported, + __BUS_EBADREQUEST => BadRequest, + __BUS_EDENIED => AccessDenied, + __BUS_EINTERNAL => InternalError, + __BUS_EALLOC => MemoryAllocationFailed, + __BUS_EINVOKE => InvokeFailed, + __BUS_ECONSUMED => AlreadyConsumed, + __BUS_EMEMVIOLATION => MemoryAccessViolation, + __BUS_EUNKNOWN | _ => UnknownError, + } +} + #[derive(Debug, Clone)] pub enum PollEvent { /// Data available to read diff --git a/lib/wasi/src/syscalls/legacy/snapshot0.rs b/lib/wasi/src/syscalls/legacy/snapshot0.rs index 374c180a1ad..615e0bee4d2 100644 --- a/lib/wasi/src/syscalls/legacy/snapshot0.rs +++ b/lib/wasi/src/syscalls/legacy/snapshot0.rs @@ -10,11 +10,11 @@ use wasmer::WasmPtr; /// Wasm memory. If the memory clobbered by the current syscall is also used by /// that syscall, then it may break. pub fn fd_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: types::__wasi_fd_t, buf: WasmPtr, ) -> types::__wasi_errno_t { - let memory = thread.memory(); + let memory = env.memory(); // transmute the WasmPtr into a WasmPtr where T2 > T1, this will read extra memory. // The edge case of this cenv.mausing an OOB is not handled, if the new field is OOB, then the entire @@ -26,10 +26,10 @@ pub fn fd_filestat_get( // Set up complete, make the call with the pointer that will write to the // struct and some unrelated memory after the struct. - let result = syscalls::fd_filestat_get::(thread, fd, new_buf); + let result = syscalls::fd_filestat_get::(env, fd, new_buf); // reborrow memory - let memory = thread.memory(); + let memory = env.memory(); // get the values written to memory let new_filestat = wasi_try_mem!(new_buf.deref(memory).read()); @@ -59,7 +59,7 @@ pub fn fd_filestat_get( /// Wrapper around `syscalls::path_filestat_get` with extra logic to handle the size /// difference of `wasi_filestat_t` pub fn path_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: types::__wasi_fd_t, flags: types::__wasi_lookupflags_t, path: WasmPtr, @@ -67,15 +67,14 @@ pub fn path_filestat_get( buf: WasmPtr, ) -> types::__wasi_errno_t { // see `fd_filestat_get` in this file for an explanation of this strange behavior - let memory = thread.memory(); + let memory = env.memory(); let new_buf: WasmPtr = buf.cast(); let new_filestat_setup: types::__wasi_filestat_t = wasi_try_mem!(new_buf.read(memory)); - let result = - syscalls::path_filestat_get::(thread, fd, flags, path, path_len, new_buf); + let result = syscalls::path_filestat_get::(env, fd, flags, path, path_len, new_buf); - let memory = thread.memory(); + let memory = env.memory(); let new_filestat = wasi_try_mem!(new_buf.deref(memory).read()); let old_stat = snapshot0::__wasi_filestat_t { st_dev: new_filestat.st_dev, @@ -97,7 +96,7 @@ pub fn path_filestat_get( /// Wrapper around `syscalls::fd_seek` with extra logic to remap the values /// of `__wasi_whence_t` pub fn fd_seek( - thread: &WasiThread, + env: &WasiEnv, fd: types::__wasi_fd_t, offset: types::__wasi_filedelta_t, whence: snapshot0::__wasi_whence_t, @@ -110,13 +109,13 @@ pub fn fd_seek( // if it's invalid, let the new fd_seek handle it _ => whence, }; - syscalls::fd_seek::(thread, fd, offset, new_whence, newoffset) + syscalls::fd_seek::(env, fd, offset, new_whence, newoffset) } /// Wrapper around `syscalls::poll_oneoff` with extra logic to add the removed /// userdata field back pub fn poll_oneoff( - thread: &WasiThread, + env: &WasiEnv, in_: WasmPtr, out_: WasmPtr, nsubscriptions: u32, @@ -126,7 +125,7 @@ pub fn poll_oneoff( // we just need to readjust and copy it // we start by adjusting `in_` into a format that the new code can understand - let memory = thread.memory(); + let memory = env.memory(); let nsubscriptions_offset: u32 = nsubscriptions.into(); let in_origs = wasi_try_mem_ok!(in_.slice(memory, nsubscriptions_offset)); let in_origs = wasi_try_mem_ok!(in_origs.read_to_vec()); @@ -160,10 +159,10 @@ pub fn poll_oneoff( // make the call let result = - syscalls::poll_oneoff::(thread, in_new_type_ptr, out_, nsubscriptions, nevents); + syscalls::poll_oneoff::(env, in_new_type_ptr, out_, nsubscriptions, nevents); // replace the old values of in, in case the calling code reuses the memory - let memory = thread.memory(); + let memory = env.memory(); for (in_sub, orig) in wasi_try_mem_ok!(in_.slice(memory, nsubscriptions_offset)) .iter() diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index e0d539fcef3..4019c31da43 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -22,8 +22,9 @@ pub mod wasix32; pub mod wasix64; use self::types::*; -use crate::state::InodeHttpSocketType; +use crate::state::{bus_error_into_wasi_err, wasi_error_into_bus_err, InodeHttpSocketType}; use crate::utils::map_io_err; +use crate::WasiBusProcessId; use crate::{ mem_error_to_wasi, state::{ @@ -34,7 +35,7 @@ use crate::{ WasiEnv, WasiError, WasiThread, WasiThreadId, }; use bytes::Bytes; -use std::borrow::Borrow; +use std::borrow::{Borrow, Cow}; use std::convert::{Infallible, TryInto}; use std::io::{self, Read, Seek, Write}; use std::mem::transmute; @@ -44,8 +45,9 @@ use std::sync::atomic::AtomicU64; use std::sync::{atomic::Ordering, Mutex}; use std::sync::{mpsc, Arc}; use std::time::Duration; -use tracing::{debug, error, trace}; +use tracing::{debug, error, trace, warn}; use wasmer::{Memory, Memory32, Memory64, MemorySize, RuntimeError, Value, WasmPtr, WasmSlice}; +use wasmer_vbus::{FileDescriptor, StdioMode}; use wasmer_vfs::{FsError, VirtualFile}; use wasmer_vnet::{SocketHttpRequest, StreamSecurity}; @@ -133,7 +135,7 @@ fn has_rights(rights_set: __wasi_rights_t, rights_check_set: __wasi_rights_t) -> } fn __sock_actor( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, rights: __wasi_rights_t, actor: F, @@ -141,7 +143,7 @@ fn __sock_actor( where F: FnOnce(&crate::state::InodeSocket) -> Result, { - let (_, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (_, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = state.fs.get_fd(sock)?; let ret = { @@ -167,7 +169,7 @@ where } fn __sock_actor_mut( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, rights: __wasi_rights_t, actor: F, @@ -175,7 +177,7 @@ fn __sock_actor_mut( where F: FnOnce(&mut crate::state::InodeSocket) -> Result, { - let (_, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (_, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = state.fs.get_fd(sock)?; let ret = { @@ -201,7 +203,7 @@ where } fn __sock_upgrade( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, rights: __wasi_rights_t, actor: F, @@ -211,7 +213,7 @@ where &mut crate::state::InodeSocket, ) -> Result, __wasi_errno_t>, { - let (_, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (_, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = state.fs.get_fd(sock)?; if rights != 0 { @@ -289,12 +291,12 @@ fn get_current_time_in_nanos() -> Result<__wasi_timestamp_t, __wasi_errno_t> { /// A pointer to a buffer to write the argument string data. /// pub fn args_get( - thread: &WasiThread, + env: &WasiEnv, argv: WasmPtr, M>, argv_buf: WasmPtr, ) -> __wasi_errno_t { debug!("wasi::args_get"); - let (memory, state) = thread.get_memory_and_wasi_state(0); + let (memory, state) = env.get_memory_and_wasi_state(0); let result = write_buffer_array(memory, &*state.args, argv, argv_buf); @@ -320,12 +322,12 @@ pub fn args_get( /// - `size_t *argv_buf_size` /// The size of the argument string data. pub fn args_sizes_get( - thread: &WasiThread, + env: &WasiEnv, argc: WasmPtr, argv_buf_size: WasmPtr, ) -> __wasi_errno_t { debug!("wasi::args_sizes_get"); - let (memory, state) = thread.get_memory_and_wasi_state(0); + let (memory, state) = env.get_memory_and_wasi_state(0); let argc = argc.deref(memory); let argv_buf_size = argv_buf_size.deref(memory); @@ -351,12 +353,12 @@ pub fn args_sizes_get( /// - `__wasi_timestamp_t *resolution` /// The resolution of the clock in nanoseconds pub fn clock_res_get( - thread: &WasiThread, + env: &WasiEnv, clock_id: __wasi_clockid_t, resolution: WasmPtr<__wasi_timestamp_t, M>, ) -> __wasi_errno_t { trace!("wasi::clock_res_get"); - let memory = thread.memory(); + let memory = env.memory(); let out_addr = resolution.deref(memory); let t_out = wasi_try!(platform_clock_res_get(clock_id, out_addr)); @@ -375,7 +377,7 @@ pub fn clock_res_get( /// - `__wasi_timestamp_t *time` /// The value of the clock in nanoseconds pub fn clock_time_get( - thread: &WasiThread, + env: &WasiEnv, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, time: WasmPtr<__wasi_timestamp_t, M>, @@ -384,7 +386,7 @@ pub fn clock_time_get( "wasi::clock_time_get clock_id: {}, precision: {}", clock_id, precision ); - let memory = thread.memory(); + let memory = env.memory(); let t_out = wasi_try!(platform_clock_time_get(clock_id, precision)); wasi_try_mem!(time.write(memory, t_out as __wasi_timestamp_t)); @@ -407,7 +409,7 @@ pub fn clock_time_get( /// - `char *environ_buf` /// A pointer to a buffer to write the environment variable string data. pub fn environ_get( - thread: &WasiThread, + env: &WasiEnv, environ: WasmPtr, M>, environ_buf: WasmPtr, ) -> __wasi_errno_t { @@ -415,7 +417,7 @@ pub fn environ_get( "wasi::environ_get. Environ: {:?}, environ_buf: {:?}", environ, environ_buf ); - let (memory, state) = thread.get_memory_and_wasi_state(0); + let (memory, state) = env.get_memory_and_wasi_state(0); trace!(" -> State envs: {:?}", state.envs); write_buffer_array(memory, &*state.envs, environ, environ_buf) @@ -429,12 +431,12 @@ pub fn environ_get( /// - `size_t *environ_buf_size` /// The size of the environment variable string data. pub fn environ_sizes_get( - thread: &WasiThread, + env: &WasiEnv, environ_count: WasmPtr, environ_buf_size: WasmPtr, ) -> __wasi_errno_t { trace!("wasi::environ_sizes_get"); - let (memory, state) = thread.get_memory_and_wasi_state(0); + let (memory, state) = env.get_memory_and_wasi_state(0); let environ_count = environ_count.deref(memory); let environ_buf_size = environ_buf_size.deref(memory); @@ -467,7 +469,7 @@ pub fn environ_sizes_get( /// - `__wasi_advice_t advice` /// The advice to give pub fn fd_advise( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filesize_t, len: __wasi_filesize_t, @@ -490,13 +492,13 @@ pub fn fd_advise( /// - `__wasi_filesize_t len` /// The length from the offset marking the end of the allocation pub fn fd_allocate( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filesize_t, len: __wasi_filesize_t, ) -> __wasi_errno_t { debug!("wasi::fd_allocate"); - let (_, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (_, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = wasi_try!(state.fs.get_fd(fd)); let inode = fd_entry.inode; @@ -540,9 +542,9 @@ pub fn fd_allocate( /// If `fd` is a directory /// - `__WASI_EBADF` /// If `fd` is invalid or not open -pub fn fd_close(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { +pub fn fd_close(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { debug!("wasi::fd_close: fd={}", fd); - let (_, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (_, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = wasi_try!(state.fs.get_fd(fd)); @@ -556,9 +558,9 @@ pub fn fd_close(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { /// Inputs: /// - `__wasi_fd_t fd` /// The file descriptor to sync -pub fn fd_datasync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { +pub fn fd_datasync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { debug!("wasi::fd_datasync"); - let (_, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (_, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = wasi_try!(state.fs.get_fd(fd)); if !has_rights(fd_entry.rights, __WASI_RIGHT_FD_DATASYNC) { return __WASI_EACCES; @@ -580,7 +582,7 @@ pub fn fd_datasync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { /// - `__wasi_fdstat_t *buf` /// The location where the metadata will be written pub fn fd_fdstat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf_ptr: WasmPtr<__wasi_fdstat_t, M>, ) -> __wasi_errno_t { @@ -589,7 +591,7 @@ pub fn fd_fdstat_get( fd, buf_ptr.offset() ); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let stat = wasi_try!(state.fs.fdstat(inodes.deref(), fd)); wasi_try_mem!(buf_ptr.write(memory, stat)); @@ -605,12 +607,12 @@ pub fn fd_fdstat_get( /// - `__wasi_fdflags_t flags` /// The flags to apply to `fd` pub fn fd_fdstat_set_flags( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_fdflags_t, ) -> __wasi_errno_t { debug!("wasi::fd_fdstat_set_flags"); - let (_, state) = thread.get_memory_and_wasi_state(0); + let (_, state) = env.get_memory_and_wasi_state(0); let mut fd_map = state.fs.fd_map.write().unwrap(); let fd_entry = wasi_try!(fd_map.get_mut(&fd).ok_or(__WASI_EBADF)); @@ -632,13 +634,13 @@ pub fn fd_fdstat_set_flags( /// - `__wasi_rights_t fs_rights_inheriting` /// The inheriting rights to apply to `fd` pub fn fd_fdstat_set_rights( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, fs_rights_base: __wasi_rights_t, fs_rights_inheriting: __wasi_rights_t, ) -> __wasi_errno_t { debug!("wasi::fd_fdstat_set_rights"); - let (_, state) = thread.get_memory_and_wasi_state(0); + let (_, state) = env.get_memory_and_wasi_state(0); let mut fd_map = state.fs.fd_map.write().unwrap(); let fd_entry = wasi_try!(fd_map.get_mut(&fd).ok_or(__WASI_EBADF)); @@ -664,12 +666,12 @@ pub fn fd_fdstat_set_rights( /// - `__wasi_filestat_t *buf` /// Where the metadata from `fd` will be written pub fn fd_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr<__wasi_filestat_t, M>, ) -> __wasi_errno_t { debug!("wasi::fd_filestat_get"); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = wasi_try!(state.fs.get_fd(fd)); if !has_rights(fd_entry.rights, __WASI_RIGHT_FD_FILESTAT_GET) { return __WASI_EACCES; @@ -690,12 +692,12 @@ pub fn fd_filestat_get( /// - `__wasi_filesize_t st_size` /// New size that `fd` will be set to pub fn fd_filestat_set_size( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, st_size: __wasi_filesize_t, ) -> __wasi_errno_t { debug!("wasi::fd_filestat_set_size"); - let (_, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (_, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = wasi_try!(state.fs.get_fd(fd)); let inode = fd_entry.inode; @@ -738,14 +740,14 @@ pub fn fd_filestat_set_size( /// - `__wasi_fstflags_t fst_flags` /// Bit-vector for controlling which times get set pub fn fd_filestat_set_times( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, st_atim: __wasi_timestamp_t, st_mtim: __wasi_timestamp_t, fst_flags: __wasi_fstflags_t, ) -> __wasi_errno_t { debug!("wasi::fd_filestat_set_times"); - let (_, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (_, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = wasi_try!(state.fs.get_fd(fd)); if !has_rights(fd_entry.rights, __WASI_RIGHT_FD_FILESTAT_SET_TIMES) { @@ -799,7 +801,7 @@ pub fn fd_filestat_set_times( /// - `size_t nread` /// The number of bytes read pub fn fd_pread( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_iovec_t, M>, iovs_len: M::Offset, @@ -807,7 +809,7 @@ pub fn fd_pread( nread: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { trace!("wasi::fd_pread: fd={}, offset={}", fd, offset); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let iovs = wasi_try_mem_ok!(iovs.slice(memory, iovs_len)); let nread_ref = nread.deref(memory); @@ -819,10 +821,10 @@ pub fn fd_pread( inodes .stdin_mut(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ); if let Some(ref mut stdin) = guard.deref_mut() { - wasi_try_ok!(read_bytes(stdin, memory, iovs), thread) + wasi_try_ok!(read_bytes(stdin, memory, iovs), env) } else { return Ok(__WASI_EBADF); } @@ -848,27 +850,24 @@ pub fn fd_pread( wasi_try_ok!( h.seek(std::io::SeekFrom::Start(offset as u64)) .map_err(map_io_err), - thread + env ); - wasi_try_ok!(read_bytes(h, memory, iovs), thread) + wasi_try_ok!(read_bytes(h, memory, iovs), env) } else { return Ok(__WASI_EINVAL); } } Kind::Socket { socket } => { - wasi_try_ok!(socket.recv(memory, iovs), thread) + wasi_try_ok!(socket.recv(memory, iovs), env) } Kind::Pipe { pipe } => { - wasi_try_ok!(pipe.recv(memory, iovs), thread) + wasi_try_ok!(pipe.recv(memory, iovs), env) } Kind::EventNotifications { .. } => return Ok(__WASI_EINVAL), Kind::Dir { .. } | Kind::Root { .. } => return Ok(__WASI_EISDIR), Kind::Symlink { .. } => unimplemented!("Symlinks in wasi::fd_pread"), Kind::Buffer { buffer } => { - wasi_try_ok!( - read_bytes(&buffer[(offset as usize)..], memory, iovs), - thread - ) + wasi_try_ok!(read_bytes(&buffer[(offset as usize)..], memory, iovs), env) } } } @@ -889,12 +888,12 @@ pub fn fd_pread( /// - `__wasi_prestat *buf` /// Where the metadata will be written pub fn fd_prestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr<__wasi_prestat_t, M>, ) -> __wasi_errno_t { trace!("wasi::fd_prestat_get: fd={}", fd); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); wasi_try_mem!(buf.write(memory, wasi_try!(state.fs.prestat_fd(inodes.deref(), fd)))); @@ -902,7 +901,7 @@ pub fn fd_prestat_get( } pub fn fd_prestat_dir_name( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: M::Offset, @@ -912,7 +911,7 @@ pub fn fd_prestat_dir_name( fd, path_len ); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let path_chars = wasi_try_mem!(path.slice(memory, path_len)); let real_inode = wasi_try!(state.fs.get_fd_inode(fd)); @@ -963,7 +962,7 @@ pub fn fd_prestat_dir_name( /// - `u32 *nwritten` /// Number of bytes written pub fn fd_pwrite( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_ciovec_t, M>, iovs_len: M::Offset, @@ -972,7 +971,7 @@ pub fn fd_pwrite( ) -> Result<__wasi_errno_t, WasiError> { trace!("wasi::fd_pwrite"); // TODO: refactor, this is just copied from `fd_write`... - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let iovs_arr = wasi_try_mem_ok!(iovs.slice(memory, iovs_len)); let nwritten_ref = nwritten.deref(memory); @@ -984,10 +983,10 @@ pub fn fd_pwrite( inodes .stdout_mut(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ); if let Some(ref mut stdout) = guard.deref_mut() { - wasi_try_ok!(write_bytes(stdout, memory, iovs_arr), thread) + wasi_try_ok!(write_bytes(stdout, memory, iovs_arr), env) } else { return Ok(__WASI_EBADF); } @@ -997,10 +996,10 @@ pub fn fd_pwrite( inodes .stderr_mut(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ); if let Some(ref mut stderr) = guard.deref_mut() { - wasi_try_ok!(write_bytes(stderr, memory, iovs_arr), thread) + wasi_try_ok!(write_bytes(stderr, memory, iovs_arr), env) } else { return Ok(__WASI_EBADF); } @@ -1023,18 +1022,18 @@ pub fn fd_pwrite( handle .seek(std::io::SeekFrom::Start(offset as u64)) .map_err(map_io_err), - thread + env ); - wasi_try_ok!(write_bytes(handle, memory, iovs_arr), thread) + wasi_try_ok!(write_bytes(handle, memory, iovs_arr), env) } else { return Ok(__WASI_EINVAL); } } Kind::Socket { socket } => { - wasi_try_ok!(socket.send(memory, iovs_arr), thread) + wasi_try_ok!(socket.send(memory, iovs_arr), env) } Kind::Pipe { pipe } => { - wasi_try_ok!(pipe.send(memory, iovs_arr), thread) + wasi_try_ok!(pipe.send(memory, iovs_arr), env) } Kind::Dir { .. } | Kind::Root { .. } => { // TODO: verify @@ -1045,7 +1044,7 @@ pub fn fd_pwrite( Kind::Buffer { buffer } => { wasi_try_ok!( write_bytes(&mut buffer[(offset as usize)..], memory, iovs_arr), - thread + env ) } } @@ -1073,14 +1072,14 @@ pub fn fd_pwrite( /// Number of bytes read /// pub fn fd_read( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_iovec_t, M>, iovs_len: M::Offset, nread: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { trace!("wasi::fd_read: fd={}", fd); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let iovs_arr = wasi_try_mem_ok!(iovs.slice(memory, iovs_len)); let nread_ref = nread.deref(memory); @@ -1092,10 +1091,10 @@ pub fn fd_read( inodes .stdin_mut(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ); if let Some(ref mut stdin) = guard.deref_mut() { - wasi_try_ok!(read_bytes(stdin, memory, iovs_arr), thread) + wasi_try_ok!(read_bytes(stdin, memory, iovs_arr), env) } else { return Ok(__WASI_EBADF); } @@ -1121,18 +1120,18 @@ pub fn fd_read( handle .seek(std::io::SeekFrom::Start(offset as u64)) .map_err(map_io_err), - thread + env ); - wasi_try_ok!(read_bytes(handle, memory, iovs_arr), thread) + wasi_try_ok!(read_bytes(handle, memory, iovs_arr), env) } else { return Ok(__WASI_EINVAL); } } Kind::Socket { socket } => { - wasi_try_ok!(socket.recv(memory, iovs_arr), thread) + wasi_try_ok!(socket.recv(memory, iovs_arr), env) } Kind::Pipe { pipe } => { - wasi_try_ok!(pipe.recv(memory, iovs_arr), thread) + wasi_try_ok!(pipe.recv(memory, iovs_arr), env) } Kind::Dir { .. } | Kind::Root { .. } => { // TODO: verify @@ -1172,7 +1171,7 @@ pub fn fd_read( let reader = val.to_ne_bytes(); ret = wasi_try_ok!( read_bytes(&reader[..], memory, iovs_arr), - thread + env ); break; } else { @@ -1186,16 +1185,16 @@ pub fn fd_read( } // Yield for a fixed period of time and then check again - thread.yield_now()?; + env.yield_now()?; if rx.recv_timeout(Duration::from_millis(5)).is_err() { - thread.sleep(Duration::from_millis(5))?; + env.sleep(Duration::from_millis(5))?; } } ret } Kind::Symlink { .. } => unimplemented!("Symlinks in wasi::fd_read"), Kind::Buffer { buffer } => { - wasi_try_ok!(read_bytes(&buffer[offset..], memory, iovs_arr), thread) + wasi_try_ok!(read_bytes(&buffer[offset..], memory, iovs_arr), env) } } }; @@ -1231,7 +1230,7 @@ pub fn fd_read( /// The Number of bytes stored in `buf`; if less than `buf_len` then entire /// directory has been read pub fn fd_readdir( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr, buf_len: M::Offset, @@ -1239,7 +1238,7 @@ pub fn fd_readdir( bufused: WasmPtr, ) -> __wasi_errno_t { trace!("wasi::fd_readdir"); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); // TODO: figure out how this is supposed to work; // is it supposed to pack the buffer full every time until it can't? or do one at a time? @@ -1358,9 +1357,9 @@ pub fn fd_readdir( /// File descriptor to copy /// - `__wasi_fd_t to` /// Location to copy file descriptor to -pub fn fd_renumber(thread: &WasiThread, from: __wasi_fd_t, to: __wasi_fd_t) -> __wasi_errno_t { +pub fn fd_renumber(env: &WasiEnv, from: __wasi_fd_t, to: __wasi_fd_t) -> __wasi_errno_t { debug!("wasi::fd_renumber: from={}, to={}", from, to); - let (_, state) = thread.get_memory_and_wasi_state(0); + let (_, state) = env.get_memory_and_wasi_state(0); let mut fd_map = state.fs.fd_map.write().unwrap(); let fd_entry = wasi_try!(fd_map.get_mut(&from).ok_or(__WASI_EBADF)); @@ -1385,13 +1384,13 @@ pub fn fd_renumber(thread: &WasiThread, from: __wasi_fd_t, to: __wasi_fd_t) -> _ /// - `__wasi_fd_t fd` /// The new file handle that is a duplicate of the original pub fn fd_dup( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, ret_fd: WasmPtr<__wasi_fd_t, M>, ) -> __wasi_errno_t { debug!("wasi::fd_dup"); - let (memory, state) = thread.get_memory_and_wasi_state(0); + let (memory, state) = env.get_memory_and_wasi_state(0); let fd = wasi_try!(state.fs.clone_fd(fd)); wasi_try_mem!(ret_fd.write(memory, fd)); @@ -1402,14 +1401,14 @@ pub fn fd_dup( /// ### `fd_event()` /// Creates a file handle for event notifications pub fn fd_event( - thread: &WasiThread, + env: &WasiEnv, initial_val: u64, flags: __wasi_eventfdflags, ret_fd: WasmPtr<__wasi_fd_t, M>, ) -> __wasi_errno_t { debug!("wasi::fd_event"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let kind = Kind::EventNotifications { counter: Arc::new(AtomicU64::new(initial_val)), @@ -1444,14 +1443,14 @@ pub fn fd_event( /// - `__wasi_filesize_t *fd` /// The new offset relative to the start of the file pub fn fd_seek( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filedelta_t, whence: __wasi_whence_t, newoffset: WasmPtr<__wasi_filesize_t, M>, ) -> Result<__wasi_errno_t, WasiError> { trace!("wasi::fd_seek: fd={}, offset={}", fd, offset); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let new_offset_ref = newoffset.deref(memory); let fd_entry = wasi_try_ok!(state.fs.get_fd(fd)); @@ -1474,7 +1473,7 @@ pub fn fd_seek( Kind::File { ref mut handle, .. } => { if let Some(handle) = handle { let end = - wasi_try_ok!(handle.seek(SeekFrom::End(0)).map_err(map_io_err), thread); + wasi_try_ok!(handle.seek(SeekFrom::End(0)).map_err(map_io_err), env); // TODO: handle case if fd_entry.offset uses 64 bits of a u64 drop(guard); @@ -1526,10 +1525,10 @@ pub fn fd_seek( /// TODO: figure out which errors this should return /// - `__WASI_EPERM` /// - `__WASI_ENOTCAPABLE` -pub fn fd_sync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { +pub fn fd_sync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { debug!("wasi::fd_sync"); debug!("=> fd={}", fd); - let (_, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (_, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let fd_entry = wasi_try!(state.fs.get_fd(fd)); if !has_rights(fd_entry.rights, __WASI_RIGHT_FD_SYNC) { return __WASI_EACCES; @@ -1568,12 +1567,12 @@ pub fn fd_sync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { /// - `__wasi_filesize_t *offset` /// The offset of `fd` relative to the start of the file pub fn fd_tell( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: WasmPtr<__wasi_filesize_t, M>, ) -> __wasi_errno_t { debug!("wasi::fd_tell"); - let (memory, state) = thread.get_memory_and_wasi_state(0); + let (memory, state) = env.get_memory_and_wasi_state(0); let offset_ref = offset.deref(memory); let fd_entry = wasi_try!(state.fs.get_fd(fd)); @@ -1602,14 +1601,14 @@ pub fn fd_tell( /// Errors: /// pub fn fd_write( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_ciovec_t, M>, iovs_len: M::Offset, nwritten: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { trace!("wasi::fd_write: fd={}", fd); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let iovs_arr = wasi_try_mem_ok!(iovs.slice(memory, iovs_len)); let nwritten_ref = nwritten.deref(memory); @@ -1621,10 +1620,10 @@ pub fn fd_write( inodes .stdout_mut(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ); if let Some(ref mut stdout) = guard.deref_mut() { - wasi_try_ok!(write_bytes(stdout, memory, iovs_arr), thread) + wasi_try_ok!(write_bytes(stdout, memory, iovs_arr), env) } else { return Ok(__WASI_EBADF); } @@ -1634,10 +1633,10 @@ pub fn fd_write( inodes .stderr_mut(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ); if let Some(ref mut stderr) = guard.deref_mut() { - wasi_try_ok!(write_bytes(stderr, memory, iovs_arr), thread) + wasi_try_ok!(write_bytes(stderr, memory, iovs_arr), env) } else { return Ok(__WASI_EBADF); } @@ -1660,18 +1659,18 @@ pub fn fd_write( handle .seek(std::io::SeekFrom::Start(offset as u64)) .map_err(map_io_err), - thread + env ); - wasi_try_ok!(write_bytes(handle, memory, iovs_arr), thread) + wasi_try_ok!(write_bytes(handle, memory, iovs_arr), env) } else { return Ok(__WASI_EINVAL); } } Kind::Socket { socket } => { - wasi_try_ok!(socket.send(memory, iovs_arr), thread) + wasi_try_ok!(socket.send(memory, iovs_arr), env) } Kind::Pipe { pipe } => { - wasi_try_ok!(pipe.send(memory, iovs_arr), thread) + wasi_try_ok!(pipe.send(memory, iovs_arr), env) } Kind::Dir { .. } | Kind::Root { .. } => { // TODO: verify @@ -1701,7 +1700,7 @@ pub fn fd_write( } Kind::Symlink { .. } => unimplemented!("Symlinks in wasi::fd_write"), Kind::Buffer { buffer } => { - wasi_try_ok!(write_bytes(&mut buffer[offset..], memory, iovs_arr), thread) + wasi_try_ok!(write_bytes(&mut buffer[offset..], memory, iovs_arr), env) } } }; @@ -1712,7 +1711,7 @@ pub fn fd_write( let fd_entry = wasi_try_ok!(fd_map.get_mut(&fd).ok_or(__WASI_EBADF)); fd_entry.offset += bytes_written as u64; } - wasi_try_ok!(state.fs.filestat_resync_size(inodes.deref(), fd), thread); + wasi_try_ok!(state.fs.filestat_resync_size(inodes.deref(), fd), env); bytes_written } @@ -1733,13 +1732,13 @@ pub fn fd_write( /// - `__wasi_fd_t` /// Second file handle that represents the other end of the pipe pub fn fd_pipe( - thread: &WasiThread, + env: &WasiEnv, ro_fd1: WasmPtr<__wasi_fd_t, M>, ro_fd2: WasmPtr<__wasi_fd_t, M>, ) -> __wasi_errno_t { trace!("wasi::fd_pipe"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let (pipe1, pipe2) = WasiPipe::new(); @@ -1780,13 +1779,13 @@ pub fn fd_pipe( /// - __WASI_RIGHT_PATH_CREATE_DIRECTORY /// This right must be set on the directory that the file is created in (TODO: verify that this is true) pub fn path_create_directory( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: M::Offset, ) -> __wasi_errno_t { debug!("wasi::path_create_directory"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let working_dir = wasi_try!(state.fs.get_fd(fd)); { @@ -1907,15 +1906,15 @@ pub fn path_create_directory( /// - `__wasi_file_stat_t *buf` /// The location where the metadata will be stored pub fn path_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_lookupflags_t, path: WasmPtr, path_len: M::Offset, buf: WasmPtr<__wasi_filestat_t, M>, ) -> __wasi_errno_t { - debug!("wasi::path_filestat_get"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + debug!("wasi::path_filestat_get (fd={})", fd); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let path_string = unsafe { get_input_str!(memory, path, path_len) }; @@ -1999,7 +1998,7 @@ pub fn path_filestat_get_internal( /// - `__wasi_fstflags_t fst_flags` /// A bitmask controlling which attributes are set pub fn path_filestat_set_times( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_lookupflags_t, path: WasmPtr, @@ -2009,7 +2008,7 @@ pub fn path_filestat_set_times( fst_flags: __wasi_fstflags_t, ) -> __wasi_errno_t { debug!("wasi::path_filestat_set_times"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let fd_entry = wasi_try!(state.fs.get_fd(fd)); let fd_inode = fd_entry.inode; if !has_rights(fd_entry.rights, __WASI_RIGHT_PATH_FILESTAT_SET_TIMES) { @@ -2076,7 +2075,7 @@ pub fn path_filestat_set_times( /// - `u32 old_path_len` /// Length of the `new_path` string pub fn path_link( - thread: &WasiThread, + env: &WasiEnv, old_fd: __wasi_fd_t, old_flags: __wasi_lookupflags_t, old_path: WasmPtr, @@ -2089,7 +2088,7 @@ pub fn path_link( if old_flags & __WASI_LOOKUP_SYMLINK_FOLLOW != 0 { debug!(" - will follow symlinks when opening path"); } - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let old_path_str = unsafe { get_input_str!(memory, old_path, old_path_len) }; let new_path_str = unsafe { get_input_str!(memory, new_path, new_path_len) }; let source_fd = wasi_try!(state.fs.get_fd(old_fd)); @@ -2171,7 +2170,7 @@ pub fn path_link( /// Possible Errors: /// - `__WASI_EACCES`, `__WASI_EBADF`, `__WASI_EFAULT`, `__WASI_EFBIG?`, `__WASI_EINVAL`, `__WASI_EIO`, `__WASI_ELOOP`, `__WASI_EMFILE`, `__WASI_ENAMETOOLONG?`, `__WASI_ENFILE`, `__WASI_ENOENT`, `__WASI_ENOTDIR`, `__WASI_EROFS`, and `__WASI_ENOTCAPABLE` pub fn path_open( - thread: &WasiThread, + env: &WasiEnv, dirfd: __wasi_fd_t, dirflags: __wasi_lookupflags_t, path: WasmPtr, @@ -2186,7 +2185,7 @@ pub fn path_open( if dirflags & __WASI_LOOKUP_SYMLINK_FOLLOW != 0 { debug!(" - will follow symlinks when opening path"); } - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); /* TODO: find actual upper bound on name size (also this is a path, not a name :think-fish:) */ let path_len64: u64 = path_len.into(); if path_len64 > 1024u64 * 1024u64 { @@ -2413,7 +2412,7 @@ pub fn path_open( /// - `u32 buf_used` /// The number of bytes written to `buf` pub fn path_readlink( - thread: &WasiThread, + env: &WasiEnv, dir_fd: __wasi_fd_t, path: WasmPtr, path_len: M::Offset, @@ -2422,7 +2421,7 @@ pub fn path_readlink( buf_used: WasmPtr, ) -> __wasi_errno_t { debug!("wasi::path_readlink"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let base_dir = wasi_try!(state.fs.get_fd(dir_fd)); if !has_rights(base_dir.rights, __WASI_RIGHT_PATH_READLINK) { @@ -2462,14 +2461,14 @@ pub fn path_readlink( /// Returns __WASI_ENOTEMTPY if directory is not empty pub fn path_remove_directory( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: M::Offset, ) -> __wasi_errno_t { // TODO check if fd is a dir, ensure it's within sandbox, etc. debug!("wasi::path_remove_directory"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let base_dir = wasi_try!(state.fs.get_fd(fd)); let path_str = unsafe { get_input_str!(memory, path, path_len) }; @@ -2546,7 +2545,7 @@ pub fn path_remove_directory( /// - `u32 new_path_len` /// The number of bytes to read from `new_path` pub fn path_rename( - thread: &WasiThread, + env: &WasiEnv, old_fd: __wasi_fd_t, old_path: WasmPtr, old_path_len: M::Offset, @@ -2558,7 +2557,7 @@ pub fn path_rename( "wasi::path_rename: old_fd = {}, new_fd = {}", old_fd, new_fd ); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let source_str = unsafe { get_input_str!(memory, old_path, old_path_len) }; let source_path = std::path::Path::new(&source_str); let target_str = unsafe { get_input_str!(memory, new_path, new_path_len) }; @@ -2709,7 +2708,7 @@ pub fn path_rename( /// - `u32 new_path_len` /// The number of bytes to read from `new_path` pub fn path_symlink( - thread: &WasiThread, + env: &WasiEnv, old_path: WasmPtr, old_path_len: M::Offset, fd: __wasi_fd_t, @@ -2717,7 +2716,7 @@ pub fn path_symlink( new_path_len: M::Offset, ) -> __wasi_errno_t { debug!("wasi::path_symlink"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let old_path_str = unsafe { get_input_str!(memory, old_path, old_path_len) }; let new_path_str = unsafe { get_input_str!(memory, new_path, new_path_len) }; let base_fd = wasi_try!(state.fs.get_fd(fd)); @@ -2808,13 +2807,13 @@ pub fn path_symlink( /// - `u32 path_len` /// The number of bytes in the `path` array pub fn path_unlink_file( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: M::Offset, ) -> __wasi_errno_t { debug!("wasi::path_unlink_file"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let base_dir = wasi_try!(state.fs.get_fd(fd)); if !has_rights(base_dir.rights, __WASI_RIGHT_PATH_UNLINK_FILE) { @@ -2918,7 +2917,7 @@ pub fn path_unlink_file( /// - `u32 nevents` /// The number of events seen pub fn poll_oneoff( - thread: &WasiThread, + env: &WasiEnv, in_: WasmPtr<__wasi_subscription_t, M>, out_: WasmPtr<__wasi_event_t, M>, nsubscriptions: M::Offset, @@ -2926,7 +2925,7 @@ pub fn poll_oneoff( ) -> Result<__wasi_errno_t, WasiError> { trace!("wasi::poll_oneoff"); trace!(" => nsubscriptions = {}", nsubscriptions); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); let subscription_array = wasi_try_mem_ok!(in_.slice(memory, nsubscriptions)); let event_array = wasi_try_mem_ok!(out_.slice(memory, nsubscriptions)); @@ -2947,7 +2946,7 @@ pub fn poll_oneoff( match fd { __WASI_STDIN_FILENO | __WASI_STDOUT_FILENO | __WASI_STDERR_FILENO => (), _ => { - let fd_entry = wasi_try_ok!(state.fs.get_fd(fd), thread); + let fd_entry = wasi_try_ok!(state.fs.get_fd(fd), env); if !has_rights(fd_entry.rights, __WASI_RIGHT_FD_READ) { return Ok(__WASI_EACCES); } @@ -2960,7 +2959,7 @@ pub fn poll_oneoff( match fd { __WASI_STDIN_FILENO | __WASI_STDOUT_FILENO | __WASI_STDERR_FILENO => (), _ => { - let fd_entry = wasi_try_ok!(state.fs.get_fd(fd), thread); + let fd_entry = wasi_try_ok!(state.fs.get_fd(fd), env); if !has_rights(fd_entry.rights, __WASI_RIGHT_FD_WRITE) { return Ok(__WASI_EACCES); } @@ -2991,7 +2990,7 @@ pub fn poll_oneoff( inodes .stderr(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ) } __WASI_STDIN_FILENO => { @@ -2999,7 +2998,7 @@ pub fn poll_oneoff( inodes .stdin(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ) } __WASI_STDOUT_FILENO => { @@ -3007,11 +3006,11 @@ pub fn poll_oneoff( inodes .stdout(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ) } _ => { - let fd_entry = wasi_try_ok!(state.fs.get_fd(fd), thread); + let fd_entry = wasi_try_ok!(state.fs.get_fd(fd), env); let inode = fd_entry.inode; if !has_rights(fd_entry.rights, __WASI_RIGHT_POLL_FD_READWRITE) { return Ok(__WASI_EACCES); @@ -3071,13 +3070,13 @@ pub fn poll_oneoff( Duration::from_millis(1), ) { Ok(0) => { - thread.yield_now()?; + env.yield_now()?; } Ok(a) => { triggered = a; } Err(FsError::WouldBlock) => { - thread.sleep(Duration::from_millis(1))?; + env.sleep(Duration::from_millis(1))?; } Err(err) => { return Ok(fs_error_into_wasi_err(err)); @@ -3103,7 +3102,7 @@ pub fn poll_oneoff( fds[i] .bytes_available_read() .map_err(fs_error_into_wasi_err), - thread + env ) .unwrap_or(0usize); error = __WASI_ESUCCESS; @@ -3113,7 +3112,7 @@ pub fn poll_oneoff( fds[i] .bytes_available_write() .map_err(fs_error_into_wasi_err), - thread + env ) .unwrap_or(0usize); error = __WASI_ESUCCESS; @@ -3167,7 +3166,7 @@ pub fn poll_oneoff( /// Inputs: /// - `__wasi_exitcode_t` /// Exit code to return to the operating system -pub fn proc_exit(thread: &WasiThread, code: __wasi_exitcode_t) -> Result<(), WasiError> { +pub fn proc_exit(env: &WasiEnv, code: __wasi_exitcode_t) -> Result<(), WasiError> { debug!("wasi::proc_exit, {}", code); Err(WasiError::Exit(code)) } @@ -3178,16 +3177,16 @@ pub fn proc_exit(thread: &WasiThread, code: __wasi_exitcode_t) -> Result<(), Was /// Inputs: /// - `__wasi_signal_t` /// Signal to be raised for this process -pub fn proc_raise(thread: &WasiThread, sig: __wasi_signal_t) -> __wasi_errno_t { +pub fn proc_raise(env: &WasiEnv, sig: __wasi_signal_t) -> __wasi_errno_t { debug!("wasi::proc_raise"); unimplemented!("wasi::proc_raise") } /// ### `sched_yield()` /// Yields execution of the thread -pub fn sched_yield(thread: &WasiThread) -> Result<__wasi_errno_t, WasiError> { +pub fn sched_yield(env: &WasiEnv) -> Result<__wasi_errno_t, WasiError> { trace!("wasi::sched_yield"); - thread.yield_now()?; + env.yield_now()?; Ok(__WASI_ESUCCESS) } @@ -3199,12 +3198,12 @@ pub fn sched_yield(thread: &WasiThread) -> Result<__wasi_errno_t, WasiError> { /// - `size_t buf_len` /// The number of bytes that will be written pub fn random_get( - thread: &WasiThread, + env: &WasiEnv, buf: WasmPtr, buf_len: M::Offset, ) -> __wasi_errno_t { trace!("wasi::random_get buf_len: {}", buf_len); - let memory = thread.memory(); + let memory = env.memory(); let buf_len64: u64 = buf_len.into(); let mut u8_buffer = vec![0; buf_len64 as usize]; let res = getrandom::getrandom(&mut u8_buffer); @@ -3221,12 +3220,12 @@ pub fn random_get( /// ### `tty_get()` /// Retrieves the current state of the TTY pub fn tty_get( - thread: &WasiThread, + env: &WasiEnv, tty_state: WasmPtr<__wasi_tty_t, M>, ) -> __wasi_errno_t { debug!("wasi::tty_stdin"); - let state = thread.env.runtime.tty_get(); + let state = env.runtime.tty_get(); let state = __wasi_tty_t { cols: state.cols, rows: state.rows, @@ -3254,7 +3253,7 @@ pub fn tty_get( }, }; - let memory = thread.memory(); + let memory = env.memory(); wasi_try_mem!(tty_state.write(memory, state)); __WASI_ESUCCESS @@ -3263,12 +3262,12 @@ pub fn tty_get( /// ### `tty_set()` /// Updates the properties of the rect pub fn tty_set( - thread: &WasiThread, + env: &WasiEnv, tty_state: WasmPtr<__wasi_tty_t, M>, ) -> __wasi_errno_t { debug!("wasi::tty_stdout"); - let memory = thread.memory(); + let memory = env.memory(); let state = wasi_try_mem!(tty_state.read(memory)); let state = super::runtime::WasiTtyState { cols: state.cols, @@ -3302,7 +3301,7 @@ pub fn tty_set( }, }; - thread.env.runtime.tty_set(state); + env.runtime.tty_set(state); __WASI_ESUCCESS } @@ -3312,39 +3311,53 @@ pub fn tty_set( /// If the path exceeds the size of the buffer then this function /// will fill the path_len with the needed size and return EOVERFLOW pub fn getcwd( - thread: &WasiThread, + env: &WasiEnv, path: WasmPtr, path_len: WasmPtr, ) -> __wasi_errno_t { debug!("wasi::getpwd"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); - let (_, cur_dir) = wasi_try!(state.fs.get_current_dir(inodes.deref_mut())); - let cur_dir = cur_dir.as_bytes(); + let (_, cur_dir) = wasi_try!(state + .fs + .get_current_dir(inodes.deref_mut(), crate::VIRTUAL_ROOT_FD,)); let max_path_len = wasi_try_mem!(path_len.read(memory)); let path_slice = wasi_try_mem!(path.slice(memory, max_path_len)); + let max_path_len: u64 = max_path_len.into(); + let cur_dir = cur_dir.as_bytes(); wasi_try_mem!(path_len.write(memory, wasi_try!(to_offset::(cur_dir.len())))); - let max_path_len: u64 = max_path_len.into(); - if cur_dir.len() as u64 > max_path_len { + if cur_dir.len() as u64 >= max_path_len { return __WASI_EOVERFLOW; } - wasi_try_mem!(path_slice.write_slice(cur_dir)); + let cur_dir = { + let mut u8_buffer = vec![0; max_path_len as usize]; + let cur_dir_len = cur_dir.len(); + if (cur_dir_len as u64) < max_path_len { + u8_buffer[..cur_dir_len].clone_from_slice(cur_dir); + u8_buffer[cur_dir_len] = 0; + } else { + return __WASI_EOVERFLOW; + } + u8_buffer + }; + + wasi_try_mem!(path_slice.write_slice(&cur_dir[..])); __WASI_ESUCCESS } /// ### `chdir()` /// Sets the current working directory pub fn chdir( - thread: &WasiThread, + env: &WasiEnv, path: WasmPtr, path_len: M::Offset, ) -> __wasi_errno_t { debug!("wasi::chdir"); - let (memory, state) = thread.get_memory_and_wasi_state(0); + let (memory, state) = env.get_memory_and_wasi_state(0); let path = unsafe { get_input_str!(memory, path, path_len) }; state.fs.set_current_dir(path.as_str()); @@ -3370,7 +3383,7 @@ pub fn chdir( /// Returns the thread index of the newly created thread /// (indices always start from zero) pub fn thread_spawn( - thread: &WasiThread, + env: &WasiEnv, method: WasmPtr, method_len: M::Offset, user_data: u64, @@ -3378,21 +3391,67 @@ pub fn thread_spawn( ret_tid: WasmPtr<__wasi_tid_t, M>, ) -> __wasi_errno_t { debug!("wasi::thread_spawn"); - let memory = thread.memory(); + let memory = env.memory(); let method = unsafe { get_input_str!(memory, method, method_len) }; + + // Load the callback function + if method.as_str() != "_thread_start" { + return __WASI_ENOTCAPABLE; + }; + let funct = unsafe { + if env.thread_start_ref().is_none() { + return __WASI_EADDRNOTAVAIL; + } + env.thread_start_ref_unchecked() + }; + let reactor = match reactor { __WASI_BOOL_FALSE => false, __WASI_BOOL_TRUE => true, _ => return __WASI_EINVAL, }; - let child = wasi_try!(thread - .env - .runtime - .thread_spawn(method.as_str(), user_data, reactor) - .map_err(|err| { - let err: __wasi_errno_t = err.into(); - err - })); + + // Create the sub-thread + let mut sub_env = env.clone(); + let mut sub_thread = env.new_thread(); + sub_env.id = sub_thread.id; + + let child = { + let id = sub_thread.id; + wasi_try!(env + .runtime + .thread_spawn(Box::new(move || { + if let Some(funct) = sub_env.thread_start_ref() { + if let Err(err) = funct.call(user_data) { + warn!("thread failed: {}", err); + std::mem::forget(sub_thread); + return; + } + } else { + warn!("failed to start thread: missing callback '__wasix_thread_start'"); + std::mem::forget(sub_thread); + return; + } + + let thread = { + let mut guard = sub_env.state.threading.lock().unwrap(); + let thread = guard.threads.remove(&id); + drop(guard); + thread + }; + + if let Some(thread) = thread { + let mut thread_guard = thread.exit.lock().unwrap(); + thread_guard.take(); + } + drop(sub_thread); + })) + .map_err(|err| { + let err: __wasi_errno_t = err.into(); + err + })); + id + }; let child: __wasi_tid_t = child.into(); wasi_try_mem!(ret_tid.write(memory, child)); @@ -3406,13 +3465,13 @@ pub fn thread_spawn( /// /// * `duration` - Amount of time that the thread should sleep pub fn thread_sleep( - thread: &WasiThread, + env: &WasiEnv, duration: __wasi_timestamp_t, ) -> Result<__wasi_errno_t, WasiError> { debug!("wasi::thread_sleep"); let duration = Duration::from_nanos(duration as u64); - thread.sleep(duration)?; + env.sleep(duration)?; Ok(__WASI_ESUCCESS) } @@ -3420,13 +3479,13 @@ pub fn thread_sleep( /// Returns the index of the current thread /// (threads indices are sequencial from zero) pub fn thread_id( - thread: &WasiThread, + env: &WasiEnv, ret_tid: WasmPtr<__wasi_tid_t, M>, ) -> __wasi_errno_t { debug!("wasi::thread_id"); - let tid: __wasi_tid_t = thread.id.into(); - wasi_try_mem!(ret_tid.write(thread.memory(), tid)); + let tid: __wasi_tid_t = env.id.into(); + wasi_try_mem!(ret_tid.write(env.memory(), tid)); __WASI_ESUCCESS } @@ -3437,47 +3496,53 @@ pub fn thread_id( /// ## Parameters /// /// * `tid` - Handle of the thread to wait on -pub fn thread_join(thread: &WasiThread, tid: __wasi_tid_t) -> __wasi_errno_t { +pub fn thread_join(env: &WasiEnv, tid: __wasi_tid_t) -> Result<__wasi_errno_t, WasiError> { debug!("wasi::thread_join"); let tid: WasiThreadId = tid.into(); - wasi_try!(thread.env.runtime().thread_join(tid).map_err(|err| { - let err: __wasi_errno_t = err.into(); - err - })); - - __WASI_ESUCCESS + let other_thread = { + let guard = env.state.threading.lock().unwrap(); + guard.threads.get(&tid).map(|a| a.clone()) + }; + if let Some(other_thread) = other_thread { + loop { + if other_thread.join(Duration::from_millis(5)) == true { + break; + } + env.yield_now()?; + } + Ok(__WASI_ESUCCESS) + } else { + Ok(__WASI_ESUCCESS) + } } /// ### `thread_parallelism()` /// Returns the available parallelism which is normally the /// number of available cores that can run concurrently pub fn thread_parallelism( - thread: &WasiThread, + env: &WasiEnv, ret_parallelism: WasmPtr, ) -> __wasi_errno_t { debug!("wasi::thread_parallelism"); - let parallelism = wasi_try!(thread.runtime().thread_parallelism().map_err(|err| { + let parallelism = wasi_try!(env.runtime().thread_parallelism().map_err(|err| { let err: __wasi_errno_t = err.into(); err })); let parallelism: M::Offset = wasi_try!(parallelism.try_into().map_err(|_| __WASI_EOVERFLOW)); - wasi_try_mem!(ret_parallelism.write(thread.memory(), parallelism)); + wasi_try_mem!(ret_parallelism.write(env.memory(), parallelism)); __WASI_ESUCCESS } /// ### `getpid()` /// Returns the handle of the current process -pub fn getpid( - thread: &WasiThread, - ret_pid: WasmPtr<__wasi_pid_t, M>, -) -> __wasi_errno_t { +pub fn getpid(env: &WasiEnv, ret_pid: WasmPtr<__wasi_pid_t, M>) -> __wasi_errno_t { debug!("wasi::getpid"); - let pid = thread.runtime().getpid(); + let pid = env.runtime().getpid(); if let Some(pid) = pid { - wasi_try_mem!(ret_pid.write(thread.memory(), pid as __wasi_pid_t)); + wasi_try_mem!(ret_pid.write(env.memory(), pid as __wasi_pid_t)); __WASI_ESUCCESS } else { __WASI_ENOTSUP @@ -3494,16 +3559,14 @@ pub fn getpid( /// /// * `rval` - The exit code returned by the process. pub fn thread_exit( - thread: &WasiThread, + env: &WasiEnv, exitcode: __wasi_exitcode_t, ) -> Result<__wasi_errno_t, WasiError> { debug!("wasi::thread_exit"); Err(WasiError::Exit(exitcode)) } -/// ### `bus_spawn_local()` -/// Spawns a new bus process for a particular web WebAssembly -/// binary that is referenced by its process name. +/// Spawns a new process within the context of this machine /// /// ## Parameters /// @@ -3522,8 +3585,8 @@ pub fn thread_exit( /// ## Return /// /// Returns a bus process id that can be used to invoke calls -pub fn bus_spawn_local( - thread: &WasiThread, +pub fn process_spawn( + env: &WasiEnv, name: WasmPtr, name_len: M::Offset, chroot: __wasi_bool_t, @@ -3537,86 +3600,232 @@ pub fn bus_spawn_local( working_dir: WasmPtr, working_dir_len: M::Offset, ret_handles: WasmPtr<__wasi_bus_handles_t, M>, -) -> __wasi_errno_t { - debug!("wasi::bus_spawn_local"); - unimplemented!("wasi::bus_spawn_local") +) -> __bus_errno_t { + let bus = env.runtime.bus(); + let memory = env.memory(); + let name = unsafe { get_input_str_bus!(memory, name, name_len) }; + let args = unsafe { get_input_str_bus!(memory, args, args_len) }; + let preopen = unsafe { get_input_str_bus!(memory, preopen, preopen_len) }; + let working_dir = unsafe { get_input_str_bus!(memory, working_dir, working_dir_len) }; + let chroot = chroot == __WASI_BOOL_TRUE; + debug!("wasi::process_spawn (name={})", name); + + let args: Vec<_> = args.split(&['\n', '\r']).map(|a| a.to_string()).collect(); + + let preopen: Vec<_> = preopen + .split(&['\n', '\r']) + .map(|a| a.to_string()) + .collect(); + + let conv_stdio_mode = |mode: __wasi_stdiomode_t| match mode { + __WASI_STDIO_MODE_PIPED => StdioMode::Piped, + __WASI_STDIO_MODE_INHERIT => StdioMode::Inherit, + __WASI_STDIO_MODE_LOG => StdioMode::Log, + __WASI_STDIO_MODE_NULL | _ => StdioMode::Null, + }; + + let process = wasi_try_bus!(bus + .new_spawn() + .chroot(chroot) + .args(args) + .preopen(preopen) + .stdin_mode(conv_stdio_mode(stdin)) + .stdout_mode(conv_stdio_mode(stdout)) + .stderr_mode(conv_stdio_mode(stderr)) + .working_dir(working_dir) + .spawn(name.as_str()) + .map_err(bus_error_into_wasi_err)); + + let conv_stdio_fd = |a: Option| match a { + Some(fd) => __wasi_option_fd_t { + tag: __WASI_OPTION_SOME, + fd: fd.into(), + }, + None => __wasi_option_fd_t { + tag: __WASI_OPTION_NONE, + fd: 0, + }, + }; + + // Convert the stdio + let stdin = conv_stdio_fd(process.inst.stdin_fd()); + let stdout = conv_stdio_fd(process.inst.stdout_fd()); + let stderr = conv_stdio_fd(process.inst.stderr_fd()); + + // Add the process to the environment state + let bid = { + let mut guard = env.state.threading.lock().unwrap(); + guard.process_seed += 1; + let bid = guard.process_seed; + guard.processes.insert(bid.into(), process); + bid + }; + + let handles = __wasi_bus_handles_t { + bid: bid, + stdin, + stdout, + stderr, + }; + + wasi_try_mem_bus!(ret_handles.write(memory, handles)); + + __BUS_ESUCCESS } -/// ### `bus_spawn_remote()` /// Spawns a new bus process for a particular web WebAssembly -/// binary that is referenced by its process name on a remote instance +/// binary that is referenced by its process name. /// /// ## Parameters /// /// * `name` - Name of the process to be spawned -/// * `chroot` - Indicates if the process will chroot or not -/// * `args` - List of the arguments to pass the process -/// (entries are separated by line feeds) -/// * `preopen` - List of the preopens for this process -/// (entries are separated by line feeds) -/// * `working_dir` - Working directory where this process should run -/// (passing '.' will use the current directory) -/// * `stdin` - How will stdin be handled -/// * `stdout` - How will stdout be handled -/// * `stderr` - How will stderr be handled +/// * `reuse` - Indicates if the existing processes should be reused +/// if they are already running +/// +/// ## Return +/// +/// Returns a bus process id that can be used to invoke calls +pub fn bus_open_local( + env: &WasiEnv, + name: WasmPtr, + name_len: M::Offset, + reuse: __wasi_bool_t, + ret_bid: WasmPtr<__wasi_bid_t, M>, +) -> __bus_errno_t { + let bus = env.runtime.bus(); + let memory = env.memory(); + let name = unsafe { get_input_str_bus!(memory, name, name_len) }; + let reuse = reuse == __WASI_BOOL_TRUE; + debug!("wasi::bus_open_local (name={}, reuse={})", name, reuse); + + bus_open_local_internal(env, name, reuse, None, None, ret_bid) +} + +/// Spawns a new bus process for a particular web WebAssembly +/// binary that is referenced by its process name on a remote instance. +/// +/// ## Parameters +/// +/// * `name` - Name of the process to be spawned +/// * `reuse` - Indicates if the existing processes should be reused +/// if they are already running /// * `instance` - Instance identifier where this process will be spawned /// * `token` - Acceess token used to authenticate with the instance /// /// ## Return /// /// Returns a bus process id that can be used to invoke calls -pub fn bus_spawn_remote( - thread: &WasiThread, +pub fn bus_open_remote( + env: &WasiEnv, name: WasmPtr, name_len: M::Offset, - chroot: __wasi_bool_t, - args: WasmPtr, - args_len: M::Offset, - preopen: WasmPtr, - preopen_len: M::Offset, - working_dir: WasmPtr, - working_dir_len: M::Offset, - stdin: __wasi_stdiomode_t, - stdout: __wasi_stdiomode_t, - stderr: __wasi_stdiomode_t, + reuse: __wasi_bool_t, instance: WasmPtr, instance_len: M::Offset, token: WasmPtr, token_len: M::Offset, - ret_handles: WasmPtr<__wasi_bus_handles_t, M>, -) -> __wasi_errno_t { - debug!("wasi::bus_spawn_remote"); - unimplemented!("wasi::bus_spawn_remote") + ret_bid: WasmPtr<__wasi_bid_t, M>, +) -> __bus_errno_t { + let bus = env.runtime.bus(); + let memory = env.memory(); + let name = unsafe { get_input_str_bus!(memory, name, name_len) }; + let instance = unsafe { get_input_str_bus!(memory, instance, instance_len) }; + let token = unsafe { get_input_str_bus!(memory, token, token_len) }; + let reuse = reuse == __WASI_BOOL_TRUE; + debug!( + "wasi::bus_open_remote (name={}, reuse={}, instance={})", + name, reuse, instance + ); + + bus_open_local_internal(env, name, reuse, Some(instance), Some(token), ret_bid) +} + +fn bus_open_local_internal( + env: &WasiEnv, + name: String, + reuse: bool, + instance: Option, + token: Option, + ret_bid: WasmPtr<__wasi_bid_t, M>, +) -> __bus_errno_t { + let bus = env.runtime.bus(); + let memory = env.memory(); + let name: Cow<'static, str> = name.into(); + + // Check if it already exists + if reuse { + let guard = env.state.threading.lock().unwrap(); + if let Some(bid) = guard.process_reuse.get(&name) { + if guard.processes.contains_key(bid) { + wasi_try_mem_bus!(ret_bid.write(memory, bid.clone().into())); + return __BUS_ESUCCESS; + } + } + } + + let mut process = bus.new_spawn(); + process + .reuse(reuse) + .stdin_mode(StdioMode::Null) + .stdout_mode(StdioMode::Null) + .stderr_mode(StdioMode::Log); + + if let Some(instance) = instance { + process.remote_instance(instance); + } + + if let Some(token) = token { + process.access_token(token); + } + + let process = wasi_try_bus!(process + .spawn(name.as_ref()) + .map_err(bus_error_into_wasi_err)); + + // Add the process to the environment state + let bid = { + let mut guard = env.state.threading.lock().unwrap(); + guard.process_seed += 1; + let bid: WasiBusProcessId = guard.process_seed.into(); + guard.processes.insert(bid, process); + guard.process_reuse.insert(name, bid); + bid + }; + + wasi_try_mem_bus!(ret_bid.write(memory, bid.into())); + + __BUS_ESUCCESS } -/// ### `bus_close()` /// Closes a bus process and releases all associated resources /// /// ## Parameters /// /// * `bid` - Handle of the bus process handle to be closed -pub fn bus_close(thread: &WasiThread, bid: __wasi_bid_t) -> __wasi_errno_t { - debug!("wasi::bus_close"); - unimplemented!("wasi::bus_close") +pub fn bus_close(env: &WasiEnv, bid: __wasi_bid_t) -> __bus_errno_t { + trace!("wasi::bus_close (bid={})", bid); + let bid: WasiBusProcessId = bid.into(); + + let mut guard = env.state.threading.lock().unwrap(); + guard.processes.remove(&bid); + + __BUS_EUNSUPPORTED } -/// ### `bus_invoke()` /// Invokes a call within a running bus process. /// /// ## Parameters /// /// * `bid` - Handle of the bus process to invoke the call within -/// * `parent` - Optional parent bus call that this is related to /// * `keep_alive` - Causes the call handle to remain open even when A /// reply is received. It is then the callers responsibility /// to invoke 'bus_drop' when they are finished with the call /// * `topic` - Topic that describes the type of call to made /// * `format` - Format of the data pushed onto the bus /// * `buf` - The buffer where data to be transmitted is stored -pub fn bus_invoke( - thread: &WasiThread, +pub fn bus_call( + env: &WasiEnv, bid: __wasi_bid_t, - cid: WasmPtr<__wasi_option_cid_t, M>, keep_alive: __wasi_bool_t, topic: WasmPtr, topic_len: M::Offset, @@ -3624,153 +3833,141 @@ pub fn bus_invoke( buf: WasmPtr, buf_len: M::Offset, ret_cid: WasmPtr<__wasi_cid_t, M>, -) -> __wasi_errno_t { - debug!("wasi::bus_invoke"); - unimplemented!("wasi::bus_invoke") -} - -/// ### `bus_fault()` -/// Causes a fault on a particular call that was made -/// to this process from another process; where 'bid' -/// is the callering process context. -/// -/// ## Parameters -/// -/// * `cid` - Handle of the call to raise a fault on -/// * `fault` - Fault to be raised on the bus -pub fn bus_fault(thread: &WasiThread, cid: __wasi_cid_t, fault: __bus_errno_t) -> __wasi_errno_t { - debug!("wasi::bus_fault"); - unimplemented!("wasi::bus_fault") -} - -/// ### `bus_drop()` -/// Closes a bus call based on its bus call handle -/// -/// ## Parameters -/// -/// * `cid` - Handle of the bus call handle to be dropped -pub fn bus_drop(thread: &WasiThread, cid: __wasi_cid_t) -> __wasi_errno_t { - debug!("wasi::bus_drop"); - unimplemented!("wasi::bus_drop") -} +) -> __bus_errno_t { + let bus = env.runtime.bus(); + let memory = env.memory(); + let topic = unsafe { get_input_str_bus!(memory, topic, topic_len) }; + let keep_alive = keep_alive == __WASI_BOOL_TRUE; + trace!( + "wasi::bus_call (bid={}, topic={}, buf_len={})", + bid, + topic, + buf_len + ); -/// ### `bus_reply()` -/// Replies to a call that was made to this process -/// from another process; where 'cid' is the call context. -/// This will may also drop the handle and release any -/// associated resources (if keepalive is not set) -/// -/// ## Parameters -/// -/// * `cid` - Handle of the call to send a reply on -/// * `format` - Format of the data pushed onto the bus -/// * `buf` - The buffer where data to be transmitted is stored -pub fn bus_reply( - thread: &WasiThread, - cid: __wasi_cid_t, - format: __wasi_busdataformat_t, - buf: WasmPtr, - buf_len: M::Offset, -) -> __wasi_errno_t { - debug!("wasi::bus_reply"); - unimplemented!("wasi::bus_reply") + __BUS_EUNSUPPORTED } -/// ### `bus_callback()` -/// Invokes a callback within the calling process against -/// a particular bus call represented by 'cid'. +/// Invokes a call within the context of another call /// /// ## Parameters /// -/// * `cid` - Handle of the call where a callback will be send -/// * `topic` - Topic that describes the type of callback +/// * `parent` - Parent bus call that this is related to +/// * `keep_alive` - Causes the call handle to remain open even when A +/// reply is received. It is then the callers responsibility +/// to invoke 'bus_drop' when they are finished with the call +/// * `topic` - Topic that describes the type of call to made /// * `format` - Format of the data pushed onto the bus /// * `buf` - The buffer where data to be transmitted is stored -pub fn bus_callback( - thread: &WasiThread, - cid: __wasi_cid_t, +pub fn bus_subcall( + env: &WasiEnv, + parent: __wasi_cid_t, + keep_alive: __wasi_bool_t, topic: WasmPtr, topic_len: M::Offset, format: __wasi_busdataformat_t, buf: WasmPtr, buf_len: M::Offset, -) -> __wasi_errno_t { - debug!("wasi::bus_callback"); - unimplemented!("wasi::bus_callback") -} + ret_cid: WasmPtr<__wasi_cid_t, M>, +) -> __bus_errno_t { + let bus = env.runtime.bus(); + let memory = env.memory(); + let topic = unsafe { get_input_str_bus!(memory, topic, topic_len) }; + let keep_alive = keep_alive == __WASI_BOOL_TRUE; + trace!( + "wasi::bus_subcall (parent={}, topic={}, buf_len={})", + parent, + topic, + buf_len + ); -/// ### `bus_listen()` -/// Tells the operating system that this process is -/// now listening for bus calls on a particular topic -/// -/// ## Parameters -/// -/// * `parent` - Optional parent bus call that this is related to -/// * `topic` - Topic that describes the process will listen forcalls on -pub fn bus_listen( - thread: &WasiThread, - parent: WasmPtr<__wasi_option_cid_t, M>, - topic: WasmPtr, - topic_len: M::Offset, -) -> __wasi_errno_t { - debug!("wasi::bus_listen"); - unimplemented!("wasi::bus_listen") + __BUS_EUNSUPPORTED } -/// ### `bus_poll()` /// Polls for any outstanding events from a particular /// bus process by its handle /// /// ## Parameters /// -/// * `bid` - Handle of the bus process to poll for new events -/// (if no process is supplied then it polls for the current process) /// * `timeout` - Timeout before the poll returns, if one passed 0 /// as the timeout then this call is non blocking. /// * `events` - An events buffer that will hold any received bus events +/// * `malloc` - Name of the function that will be invoked to allocate memory +/// Function signature fn(u64) -> u64 /// /// ## Return /// /// Returns the number of events that have occured pub fn bus_poll( - thread: &WasiThread, - bid: WasmPtr<__wasi_option_bid_t, M>, - timeout: WasmPtr<__wasi_timestamp_t, M>, + env: &WasiEnv, + timeout: __wasi_timestamp_t, events: WasmPtr, nevents: M::Offset, + malloc: WasmPtr, + malloc_len: M::Offset, ret_nevents: WasmPtr, -) -> __wasi_errno_t { - debug!("wasi::bus_poll"); - unimplemented!("wasi::bus_poll") +) -> __bus_errno_t { + let bus = env.runtime.bus(); + let memory = env.memory(); + let malloc = unsafe { get_input_str_bus!(memory, malloc, malloc_len) }; + trace!("wasi::bus_poll (timeout={}, malloc={})", timeout, malloc); + + __BUS_EUNSUPPORTED } -/// ### `bus_poll_data()` -/// Receives the next event data from the bus +/// Replies to a call that was made to this process +/// from another process; where 'cid' is the call context. +/// This will may also drop the handle and release any +/// associated resources (if keepalive is not set) /// /// ## Parameters /// -/// * `bid` - Handle of the bus process to poll for new events -/// (if no process is supplied then it polls for the current process) -/// * `timeout` - Timeout before the poll returns, if one passed 0 -/// as the timeout then this call is non blocking. -/// * `topic` - The topic that describes the event that happened -/// * `buf` - The buffer where event data is stored -/// -/// ## Return -/// -/// Returns the number of events that have occured -pub fn bus_poll_data( - thread: &WasiThread, - bid: WasmPtr<__wasi_option_bid_t, M>, - timeout: WasmPtr<__wasi_timestamp_t, M>, - topic: WasmPtr, - topic_len: M::Offset, +/// * `cid` - Handle of the call to send a reply on +/// * `format` - Format of the data pushed onto the bus +/// * `buf` - The buffer where data to be transmitted is stored +pub fn call_reply( + env: &WasiEnv, + cid: __wasi_cid_t, + format: __wasi_busdataformat_t, buf: WasmPtr, buf_len: M::Offset, - ret_evt: WasmPtr<__wasi_busevent_data_t, M>, -) -> __wasi_errno_t { - debug!("wasi::bus_poll_data"); - unimplemented!("wasi::bus_poll_data") +) -> __bus_errno_t { + let bus = env.runtime.bus(); + trace!( + "wasi::call_reply (cid={}, format={}, data_len={})", + cid, + format, + buf_len + ); + + __BUS_EUNSUPPORTED +} + +/// Causes a fault on a particular call that was made +/// to this process from another process; where 'bid' +/// is the callering process context. +/// +/// ## Parameters +/// +/// * `cid` - Handle of the call to raise a fault on +/// * `fault` - Fault to be raised on the bus +pub fn call_fault(env: &WasiEnv, cid: __wasi_cid_t, fault: __bus_errno_t) -> __bus_errno_t { + let bus = env.runtime.bus(); + debug!("wasi::call_fault (cid={}, fault={})", cid, fault); + + __BUS_EUNSUPPORTED +} + +/// Closes a bus call based on its bus call handle +/// +/// ## Parameters +/// +/// * `cid` - Handle of the bus call handle to be dropped +pub fn call_close(env: &WasiEnv, cid: __wasi_cid_t) -> __bus_errno_t { + let bus = env.runtime.bus(); + trace!("wasi::call_close (cid={})", cid); + + __BUS_EUNSUPPORTED } /// ### `ws_connect()` @@ -3784,21 +3981,21 @@ pub fn bus_poll_data( /// /// Returns a socket handle which is used to send and receive data pub fn ws_connect( - thread: &WasiThread, + env: &WasiEnv, url: WasmPtr, url_len: M::Offset, ret_sock: WasmPtr<__wasi_fd_t, M>, ) -> __wasi_errno_t { debug!("wasi::ws_connect"); - let memory = thread.memory(); + let memory = env.memory(); let url = unsafe { get_input_str!(memory, url, url_len) }; - let socket = wasi_try!(thread + let socket = wasi_try!(env .net() .ws_connect(url.as_str()) .map_err(net_error_into_wasi_err)); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let kind = Kind::Socket { socket: InodeSocket::new(InodeSocketKind::WebSocket(socket)), @@ -3835,7 +4032,7 @@ pub fn ws_connect( /// The body of the response can be streamed from the returned /// file handle pub fn http_request( - thread: &WasiThread, + env: &WasiEnv, url: WasmPtr, url_len: M::Offset, method: WasmPtr, @@ -3846,7 +4043,7 @@ pub fn http_request( ret_handles: WasmPtr<__wasi_http_handles_t, M>, ) -> __wasi_errno_t { debug!("wasi::http_request"); - let memory = thread.memory(); + let memory = env.memory(); let url = unsafe { get_input_str!(memory, url, url_len) }; let method = unsafe { get_input_str!(memory, method, method_len) }; let headers = unsafe { get_input_str!(memory, headers, headers_len) }; @@ -3857,7 +4054,7 @@ pub fn http_request( _ => return __WASI_EINVAL, }; - let socket = wasi_try!(thread + let socket = wasi_try!(env .net() .http_request(url.as_str(), method.as_str(), headers.as_str(), gzip) .map_err(net_error_into_wasi_err)); @@ -3880,7 +4077,7 @@ pub fn http_request( status: socket.status.clone(), }; - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let kind_req = Kind::Socket { socket: InodeSocket::new(InodeSocketKind::HttpRequest( @@ -3941,16 +4138,16 @@ pub fn http_request( /// * `status` - Pointer to a buffer that will be filled with the current /// status of this HTTP request pub fn http_status( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, status: WasmPtr<__wasi_http_status_t, M>, ) -> __wasi_errno_t { debug!("wasi::http_status"); - let memory = thread.memory(); + let memory = env.memory(); let ref_status = status.deref(memory); - let http_status = wasi_try!(__sock_actor(thread, sock, 0, |socket| { + let http_status = wasi_try!(__sock_actor(env, sock, 0, |socket| { socket.http_status() })); @@ -3979,7 +4176,7 @@ pub fn http_status( /// * `token` - Access token used to authenticate with the network /// * `security` - Level of encryption to encapsulate the network connection with pub fn port_bridge( - thread: &WasiThread, + env: &WasiEnv, network: WasmPtr, network_len: M::Offset, token: WasmPtr, @@ -3987,7 +4184,7 @@ pub fn port_bridge( security: __wasi_streamsecurity_t, ) -> __wasi_errno_t { debug!("wasi::port_bridge"); - let memory = thread.memory(); + let memory = env.memory(); let network = unsafe { get_input_str!(memory, network, network_len) }; let token = unsafe { get_input_str!(memory, token, token_len) }; let security = match security { @@ -3998,7 +4195,7 @@ pub fn port_bridge( _ => return __WASI_EINVAL, }; - wasi_try!(thread + wasi_try!(env .net() .bridge(network.as_str(), token.as_str(), security) .map_err(net_error_into_wasi_err)); @@ -4007,17 +4204,17 @@ pub fn port_bridge( /// ### `port_unbridge()` /// Disconnects from a remote network -pub fn port_unbridge(thread: &WasiThread) -> __wasi_errno_t { +pub fn port_unbridge(env: &WasiEnv) -> __wasi_errno_t { debug!("wasi::port_unbridge"); - wasi_try!(thread.net().unbridge().map_err(net_error_into_wasi_err)); + wasi_try!(env.net().unbridge().map_err(net_error_into_wasi_err)); __WASI_ESUCCESS } /// ### `port_dhcp_acquire()` /// Acquires a set of IP addresses using DHCP -pub fn port_dhcp_acquire(thread: &WasiThread) -> __wasi_errno_t { +pub fn port_dhcp_acquire(env: &WasiEnv) -> __wasi_errno_t { debug!("wasi::port_dhcp_acquire"); - wasi_try!(thread.net().dhcp_acquire().map_err(net_error_into_wasi_err)); + wasi_try!(env.net().dhcp_acquire().map_err(net_error_into_wasi_err)); __WASI_ESUCCESS } @@ -4028,13 +4225,13 @@ pub fn port_dhcp_acquire(thread: &WasiThread) -> __wasi_errno_t { /// /// * `addr` - Address to be added pub fn port_addr_add( - thread: &WasiThread, + env: &WasiEnv, ip: WasmPtr<__wasi_cidr_t, M>, ) -> __wasi_errno_t { debug!("wasi::port_addr_add"); - let memory = thread.memory(); + let memory = env.memory(); let cidr = wasi_try!(super::state::read_cidr(memory, ip)); - wasi_try!(thread + wasi_try!(env .net() .ip_add(cidr.ip, cidr.prefix) .map_err(net_error_into_wasi_err)); @@ -4048,33 +4245,33 @@ pub fn port_addr_add( /// /// * `addr` - Address to be removed pub fn port_addr_remove( - thread: &WasiThread, + env: &WasiEnv, ip: WasmPtr<__wasi_addr_t, M>, ) -> __wasi_errno_t { debug!("wasi::port_addr_remove"); - let memory = thread.memory(); + let memory = env.memory(); let ip = wasi_try!(super::state::read_ip(memory, ip)); - wasi_try!(thread.net().ip_remove(ip).map_err(net_error_into_wasi_err)); + wasi_try!(env.net().ip_remove(ip).map_err(net_error_into_wasi_err)); __WASI_ESUCCESS } /// ### `port_addr_clear()` /// Clears all the addresses on the local port -pub fn port_addr_clear(thread: &WasiThread) -> __wasi_errno_t { +pub fn port_addr_clear(env: &WasiEnv) -> __wasi_errno_t { debug!("wasi::port_addr_clear"); - wasi_try!(thread.net().ip_clear().map_err(net_error_into_wasi_err)); + wasi_try!(env.net().ip_clear().map_err(net_error_into_wasi_err)); __WASI_ESUCCESS } /// ### `port_mac()` /// Returns the MAC address of the local port pub fn port_mac( - thread: &WasiThread, + env: &WasiEnv, ret_mac: WasmPtr<__wasi_hardwareaddress_t, M>, ) -> __wasi_errno_t { debug!("wasi::port_mac"); - let memory = thread.memory(); - let mac = wasi_try!(thread.net().mac().map_err(net_error_into_wasi_err)); + let memory = env.memory(); + let mac = wasi_try!(env.net().mac().map_err(net_error_into_wasi_err)); let mac = __wasi_hardwareaddress_t { octs: mac }; wasi_try_mem!(ret_mac.write(memory, mac)); __WASI_ESUCCESS @@ -4094,18 +4291,18 @@ pub fn port_mac( /// /// The number of addresses returned. pub fn port_addr_list( - thread: &WasiThread, + env: &WasiEnv, addrs: WasmPtr<__wasi_cidr_t, M>, naddrs: WasmPtr, ) -> __wasi_errno_t { debug!("wasi::port_addr_list"); - let memory = thread.memory(); + let memory = env.memory(); let max_addrs = wasi_try_mem!(naddrs.read(memory)); let max_addrs: u64 = wasi_try!(max_addrs.try_into().map_err(|_| __WASI_EOVERFLOW)); let ref_addrs = wasi_try_mem!(addrs.slice(memory, wasi_try!(to_offset::(max_addrs as usize)))); - let addrs = wasi_try!(thread.net().ip_list().map_err(net_error_into_wasi_err)); + let addrs = wasi_try!(env.net().ip_list().map_err(net_error_into_wasi_err)); let addrs_len: M::Offset = wasi_try!(addrs.len().try_into().map_err(|_| __WASI_EOVERFLOW)); wasi_try_mem!(naddrs.write(memory, addrs_len)); @@ -4128,31 +4325,28 @@ pub fn port_addr_list( /// /// * `addr` - Address of the default gateway pub fn port_gateway_set( - thread: &WasiThread, + env: &WasiEnv, ip: WasmPtr<__wasi_addr_t, M>, ) -> __wasi_errno_t { debug!("wasi::port_gateway_set"); - let memory = thread.memory(); + let memory = env.memory(); let ip = wasi_try!(super::state::read_ip(memory, ip)); - wasi_try!(thread - .net() - .gateway_set(ip) - .map_err(net_error_into_wasi_err)); + wasi_try!(env.net().gateway_set(ip).map_err(net_error_into_wasi_err)); __WASI_ESUCCESS } /// ### `port_route_add()` /// Adds a new route to the local port pub fn port_route_add( - thread: &WasiThread, + env: &WasiEnv, cidr: WasmPtr<__wasi_cidr_t, M>, via_router: WasmPtr<__wasi_addr_t, M>, preferred_until: WasmPtr<__wasi_option_timestamp_t, M>, expires_at: WasmPtr<__wasi_option_timestamp_t, M>, ) -> __wasi_errno_t { debug!("wasi::port_route_add"); - let memory = thread.memory(); + let memory = env.memory(); let cidr = wasi_try!(super::state::read_cidr(memory, cidr)); let via_router = wasi_try!(super::state::read_ip(memory, via_router)); let preferred_until = wasi_try_mem!(preferred_until.read(memory)); @@ -4168,7 +4362,7 @@ pub fn port_route_add( _ => return __WASI_EINVAL, }; - wasi_try!(thread + wasi_try!(env .net() .route_add(cidr, via_router, preferred_until, expires_at) .map_err(net_error_into_wasi_err)); @@ -4178,24 +4372,21 @@ pub fn port_route_add( /// ### `port_route_remove()` /// Removes an existing route from the local port pub fn port_route_remove( - thread: &WasiThread, + env: &WasiEnv, ip: WasmPtr<__wasi_addr_t, M>, ) -> __wasi_errno_t { debug!("wasi::port_route_remove"); - let memory = thread.memory(); + let memory = env.memory(); let ip = wasi_try!(super::state::read_ip(memory, ip)); - wasi_try!(thread - .net() - .route_remove(ip) - .map_err(net_error_into_wasi_err)); + wasi_try!(env.net().route_remove(ip).map_err(net_error_into_wasi_err)); __WASI_ESUCCESS } /// ### `port_route_clear()` /// Clears all the routes in the local port -pub fn port_route_clear(thread: &WasiThread) -> __wasi_errno_t { +pub fn port_route_clear(env: &WasiEnv) -> __wasi_errno_t { debug!("wasi::port_route_clear"); - wasi_try!(thread.net().route_clear().map_err(net_error_into_wasi_err)); + wasi_try!(env.net().route_clear().map_err(net_error_into_wasi_err)); __WASI_ESUCCESS } @@ -4209,19 +4400,19 @@ pub fn port_route_clear(thread: &WasiThread) -> __wasi_errno_t { /// /// * `routes` - The buffer where routes will be stored pub fn port_route_list( - thread: &WasiThread, + env: &WasiEnv, routes: WasmPtr<__wasi_route_t, M>, nroutes: WasmPtr, ) -> __wasi_errno_t { debug!("wasi::port_route_list"); - let memory = thread.memory(); + let memory = env.memory(); let nroutes = nroutes.deref(memory); let max_routes: usize = wasi_try!(wasi_try_mem!(nroutes.read()) .try_into() .map_err(|_| __WASI_EINVAL)); let ref_routes = wasi_try_mem!(routes.slice(memory, wasi_try!(to_offset::(max_routes)))); - let routes = wasi_try!(thread.net().route_list().map_err(net_error_into_wasi_err)); + let routes = wasi_try!(env.net().route_list().map_err(net_error_into_wasi_err)); let routes_len: M::Offset = wasi_try!(routes.len().try_into().map_err(|_| __WASI_EINVAL)); wasi_try_mem!(nroutes.write(routes_len)); @@ -4244,11 +4435,7 @@ pub fn port_route_list( /// ## Parameters /// /// * `how` - Which channels on the socket to shut down. -pub fn sock_shutdown( - thread: &WasiThread, - sock: __wasi_fd_t, - how: __wasi_sdflags_t, -) -> __wasi_errno_t { +pub fn sock_shutdown(env: &WasiEnv, sock: __wasi_fd_t, how: __wasi_sdflags_t) -> __wasi_errno_t { debug!("wasi::sock_shutdown"); let both = __WASI_SHUT_RD | __WASI_SHUT_WR; @@ -4260,7 +4447,7 @@ pub fn sock_shutdown( }; wasi_try!(__sock_actor_mut( - thread, + env, sock, __WASI_RIGHT_SOCK_SHUTDOWN, |socket| { socket.shutdown(how) } @@ -4272,13 +4459,13 @@ pub fn sock_shutdown( /// ### `sock_status()` /// Returns the current status of a socket pub fn sock_status( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ret_status: WasmPtr<__wasi_sockstatus_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_status"); - let status = wasi_try!(__sock_actor(thread, sock, 0, |socket| { socket.status() })); + let status = wasi_try!(__sock_actor(env, sock, 0, |socket| { socket.status() })); use super::state::WasiSocketStatus; let status = match status { @@ -4288,7 +4475,7 @@ pub fn sock_status( WasiSocketStatus::Failed => __WASI_SOCK_STATUS_FAILED, }; - wasi_try_mem!(ret_status.write(thread.memory(), status)); + wasi_try_mem!(ret_status.write(env.memory(), status)); __WASI_ESUCCESS } @@ -4305,17 +4492,15 @@ pub fn sock_status( /// /// * `fd` - Socket that the address is bound to pub fn sock_addr_local( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ret_addr: WasmPtr<__wasi_addr_port_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_addr_local"); - let addr = wasi_try!(__sock_actor(thread, sock, 0, |socket| { - socket.addr_local() - })); + let addr = wasi_try!(__sock_actor(env, sock, 0, |socket| { socket.addr_local() })); wasi_try!(super::state::write_ip_port( - thread.memory(), + env.memory(), ret_addr, addr.ip(), addr.port() @@ -4335,17 +4520,15 @@ pub fn sock_addr_local( /// /// * `fd` - Socket that the address is bound to pub fn sock_addr_peer( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ro_addr: WasmPtr<__wasi_addr_port_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_addr_peer"); - let addr = wasi_try!(__sock_actor(thread, sock, 0, |socket| { - socket.addr_peer() - })); + let addr = wasi_try!(__sock_actor(env, sock, 0, |socket| { socket.addr_peer() })); wasi_try!(super::state::write_ip_port( - thread.memory(), + env.memory(), ro_addr, addr.ip(), addr.port() @@ -4373,7 +4556,7 @@ pub fn sock_addr_peer( /// /// The file descriptor of the socket that has been opened. pub fn sock_open( - thread: &WasiThread, + env: &WasiEnv, af: __wasi_addressfamily_t, ty: __wasi_socktype_t, pt: __wasi_sockproto_t, @@ -4381,7 +4564,7 @@ pub fn sock_open( ) -> __wasi_errno_t { debug!("wasi::sock_open"); - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let kind = match ty { __WASI_SOCK_TYPE_STREAM | __WASI_SOCK_TYPE_DGRAM => Kind::Socket { @@ -4428,7 +4611,7 @@ pub fn sock_open( /// * `sockopt` - Socket option to be set /// * `flag` - Value to set the option to pub fn sock_set_opt_flag( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, flag: __wasi_bool_t, @@ -4442,7 +4625,7 @@ pub fn sock_set_opt_flag( }; let option: super::state::WasiSocketOption = opt.into(); - wasi_try!(__sock_actor_mut(thread, sock, 0, |socket| { + wasi_try!(__sock_actor_mut(env, sock, 0, |socket| { socket.set_opt_flag(option, flag) })); __WASI_ESUCCESS @@ -4457,16 +4640,16 @@ pub fn sock_set_opt_flag( /// * `fd` - Socket descriptor /// * `sockopt` - Socket option to be retrieved pub fn sock_get_opt_flag( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, ret_flag: WasmPtr<__wasi_bool_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_get_opt_flag(ty={})", opt); - let memory = thread.memory(); + let memory = env.memory(); let option: super::state::WasiSocketOption = opt.into(); - let flag = wasi_try!(__sock_actor(thread, sock, 0, |socket| { + let flag = wasi_try!(__sock_actor(env, sock, 0, |socket| { socket.get_opt_flag(option) })); let flag = match flag { @@ -4488,14 +4671,14 @@ pub fn sock_get_opt_flag( /// * `sockopt` - Socket option to be set /// * `time` - Value to set the time to pub fn sock_set_opt_time( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, time: WasmPtr<__wasi_option_timestamp_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_set_opt_time(ty={})", opt); - let memory = thread.memory(); + let memory = env.memory(); let time = wasi_try_mem!(time.read(memory)); let time = match time.tag { __WASI_OPTION_NONE => None, @@ -4513,7 +4696,7 @@ pub fn sock_set_opt_time( }; let option: super::state::WasiSocketOption = opt.into(); - wasi_try!(__sock_actor_mut(thread, sock, 0, |socket| { + wasi_try!(__sock_actor_mut(env, sock, 0, |socket| { socket.set_opt_time(ty, time) })); __WASI_ESUCCESS @@ -4527,13 +4710,13 @@ pub fn sock_set_opt_time( /// * `fd` - Socket descriptor /// * `sockopt` - Socket option to be retrieved pub fn sock_get_opt_time( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, ret_time: WasmPtr<__wasi_option_timestamp_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_get_opt_time(ty={})", opt); - let memory = thread.memory(); + let memory = env.memory(); let ty = match opt { __WASI_SOCK_OPTION_RECV_TIMEOUT => wasmer_vnet::TimeType::ReadTimeout, @@ -4544,9 +4727,7 @@ pub fn sock_get_opt_time( _ => return __WASI_EINVAL, }; - let time = wasi_try!(__sock_actor(thread, sock, 0, |socket| { - socket.opt_time(ty) - })); + let time = wasi_try!(__sock_actor(env, sock, 0, |socket| { socket.opt_time(ty) })); let time = match time { None => __wasi_option_timestamp_t { tag: __WASI_OPTION_NONE, @@ -4573,7 +4754,7 @@ pub fn sock_get_opt_time( /// * `opt` - Socket option to be set /// * `size` - Buffer size pub fn sock_set_opt_size( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, size: __wasi_filesize_t, @@ -4590,7 +4771,7 @@ pub fn sock_set_opt_size( }; let option: super::state::WasiSocketOption = opt.into(); - wasi_try!(__sock_actor_mut(thread, sock, 0, |socket| { + wasi_try!(__sock_actor_mut(env, sock, 0, |socket| { match opt { __WASI_SOCK_OPTION_RECV_BUF_SIZE => socket.set_recv_buf_size(size as usize), __WASI_SOCK_OPTION_SEND_BUF_SIZE => socket.set_send_buf_size(size as usize), @@ -4611,15 +4792,15 @@ pub fn sock_set_opt_size( /// * `fd` - Socket descriptor /// * `sockopt` - Socket option to be retrieved pub fn sock_get_opt_size( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, ret_size: WasmPtr<__wasi_filesize_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_get_opt_size(ty={})", opt); - let memory = thread.memory(); + let memory = env.memory(); - let size = wasi_try!(__sock_actor(thread, sock, 0, |socket| { + let size = wasi_try!(__sock_actor(env, sock, 0, |socket| { match opt { __WASI_SOCK_OPTION_RECV_BUF_SIZE => { socket.recv_buf_size().map(|a| a as __wasi_filesize_t) @@ -4648,17 +4829,17 @@ pub fn sock_get_opt_size( /// * `multiaddr` - Multicast group to joined /// * `interface` - Interface that will join pub fn sock_join_multicast_v4( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip4_t, M>, iface: WasmPtr<__wasi_addr_ip4_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_join_multicast_v4"); - let memory = thread.memory(); + let memory = env.memory(); let multiaddr = wasi_try!(super::state::read_ip_v4(memory, multiaddr)); let iface = wasi_try!(super::state::read_ip_v4(memory, iface)); - wasi_try!(__sock_actor_mut(thread, sock, 0, |socket| { + wasi_try!(__sock_actor_mut(env, sock, 0, |socket| { socket.join_multicast_v4(multiaddr, iface) })); __WASI_ESUCCESS @@ -4673,17 +4854,17 @@ pub fn sock_join_multicast_v4( /// * `multiaddr` - Multicast group to leave /// * `interface` - Interface that will left pub fn sock_leave_multicast_v4( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip4_t, M>, iface: WasmPtr<__wasi_addr_ip4_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_leave_multicast_v4"); - let memory = thread.memory(); + let memory = env.memory(); let multiaddr = wasi_try!(super::state::read_ip_v4(memory, multiaddr)); let iface = wasi_try!(super::state::read_ip_v4(memory, iface)); - wasi_try!(__sock_actor_mut(thread, sock, 0, |socket| { + wasi_try!(__sock_actor_mut(env, sock, 0, |socket| { socket.leave_multicast_v4(multiaddr, iface) })); __WASI_ESUCCESS @@ -4698,16 +4879,16 @@ pub fn sock_leave_multicast_v4( /// * `multiaddr` - Multicast group to joined /// * `interface` - Interface that will join pub fn sock_join_multicast_v6( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip6_t, M>, iface: u32, ) -> __wasi_errno_t { debug!("wasi::sock_join_multicast_v6"); - let memory = thread.memory(); + let memory = env.memory(); let multiaddr = wasi_try!(super::state::read_ip_v6(memory, multiaddr)); - wasi_try!(__sock_actor_mut(thread, sock, 0, |socket| { + wasi_try!(__sock_actor_mut(env, sock, 0, |socket| { socket.join_multicast_v6(multiaddr, iface) })); __WASI_ESUCCESS @@ -4722,16 +4903,16 @@ pub fn sock_join_multicast_v6( /// * `multiaddr` - Multicast group to leave /// * `interface` - Interface that will left pub fn sock_leave_multicast_v6( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip6_t, M>, iface: u32, ) -> __wasi_errno_t { debug!("wasi::sock_leave_multicast_v6"); - let memory = thread.memory(); + let memory = env.memory(); let multiaddr = wasi_try!(super::state::read_ip_v6(memory, multiaddr)); - wasi_try!(__sock_actor_mut(thread, sock, 0, |socket| { + wasi_try!(__sock_actor_mut(env, sock, 0, |socket| { socket.leave_multicast_v6(multiaddr, iface) })); __WASI_ESUCCESS @@ -4746,19 +4927,19 @@ pub fn sock_leave_multicast_v6( /// * `fd` - File descriptor of the socket to be bind /// * `addr` - Address to bind the socket to pub fn sock_bind( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, addr: WasmPtr<__wasi_addr_port_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_bind"); - let addr = wasi_try!(super::state::read_ip_port(thread.memory(), addr)); + let addr = wasi_try!(super::state::read_ip_port(env.memory(), addr)); let addr = SocketAddr::new(addr.0, addr.1); wasi_try!(__sock_upgrade( - thread, + env, sock, __WASI_RIGHT_SOCK_BIND, - |socket| { socket.bind(thread.net(), addr) } + |socket| { socket.bind(env.net(), addr) } )); __WASI_ESUCCESS } @@ -4776,7 +4957,7 @@ pub fn sock_bind( /// * `fd` - File descriptor of the socket to be bind /// * `backlog` - Maximum size of the queue for pending connections pub fn sock_listen( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, backlog: M::Offset, ) -> __wasi_errno_t { @@ -4784,10 +4965,10 @@ pub fn sock_listen( let backlog: usize = wasi_try!(backlog.try_into().map_err(|_| __WASI_EINVAL)); wasi_try!(__sock_upgrade( - thread, + env, sock, __WASI_RIGHT_SOCK_BIND, - |socket| { socket.listen(thread.net(), backlog) } + |socket| { socket.listen(env.net(), backlog) } )); __WASI_ESUCCESS } @@ -4805,7 +4986,7 @@ pub fn sock_listen( /// /// New socket connection pub fn sock_accept( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, fd_flags: __wasi_fdflags_t, ro_fd: WasmPtr<__wasi_fd_t, M>, @@ -4815,10 +4996,10 @@ pub fn sock_accept( let (child, addr) = { let mut ret; - let (_, state) = thread.get_memory_and_wasi_state(0); + let (_, state) = env.get_memory_and_wasi_state(0); loop { wasi_try_ok!( - match __sock_actor(thread, sock, __WASI_RIGHT_SOCK_ACCEPT, |socket| socket + match __sock_actor(env, sock, __WASI_RIGHT_SOCK_ACCEPT, |socket| socket .accept_timeout(fd_flags, Duration::from_millis(5))) { Ok(a) => { @@ -4826,11 +5007,11 @@ pub fn sock_accept( break; } Err(__WASI_ETIMEDOUT) => { - thread.yield_now()?; + env.yield_now()?; continue; } Err(__WASI_EAGAIN) => { - thread.sleep(Duration::from_millis(5))?; + env.sleep(Duration::from_millis(5))?; continue; } Err(err) => Err(err), @@ -4840,7 +5021,7 @@ pub fn sock_accept( ret }; - let (memory, state, mut inodes) = thread.get_memory_and_wasi_state_and_inodes_mut(0); + let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(0); let kind = Kind::Socket { socket: InodeSocket::new(InodeSocketKind::TcpStream(child)), @@ -4879,19 +5060,19 @@ pub fn sock_accept( /// * `fd` - Socket descriptor /// * `addr` - Address of the socket to connect to pub fn sock_connect( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, addr: WasmPtr<__wasi_addr_port_t, M>, ) -> __wasi_errno_t { debug!("wasi::sock_connect"); - let addr = wasi_try!(super::state::read_ip_port(thread.memory(), addr)); + let addr = wasi_try!(super::state::read_ip_port(env.memory(), addr)); let addr = SocketAddr::new(addr.0, addr.1); wasi_try!(__sock_upgrade( - thread, + env, sock, __WASI_RIGHT_SOCK_CONNECT, - |socket| { socket.connect(thread.net(), addr) } + |socket| { socket.connect(env.net(), addr) } )); __WASI_ESUCCESS } @@ -4910,7 +5091,7 @@ pub fn sock_connect( /// /// Number of bytes stored in ri_data and message flags. pub fn sock_recv( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ri_data: WasmPtr<__wasi_iovec_t, M>, ri_data_len: M::Offset, @@ -4920,11 +5101,11 @@ pub fn sock_recv( ) -> Result<__wasi_errno_t, WasiError> { debug!("wasi::sock_recv"); - let memory = thread.memory(); + let memory = env.memory(); let iovs_arr = wasi_try_mem_ok!(ri_data.slice(memory, ri_data_len)); let bytes_read = wasi_try_ok!(__sock_actor_mut( - thread, + env, sock, __WASI_RIGHT_SOCK_RECV, |socket| { socket.recv(memory, iovs_arr) } @@ -4951,7 +5132,7 @@ pub fn sock_recv( /// /// Number of bytes stored in ri_data and message flags. pub fn sock_recv_from( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ri_data: WasmPtr<__wasi_iovec_t, M>, ri_data_len: M::Offset, @@ -4962,11 +5143,11 @@ pub fn sock_recv_from( ) -> Result<__wasi_errno_t, WasiError> { debug!("wasi::sock_recv_from"); - let memory = thread.memory(); + let memory = env.memory(); let iovs_arr = wasi_try_mem_ok!(ri_data.slice(memory, ri_data_len)); let bytes_read = wasi_try_ok!(__sock_actor_mut( - thread, + env, sock, __WASI_RIGHT_SOCK_RECV_FROM, |socket| { socket.recv_from(memory, iovs_arr, ro_addr) } @@ -4993,7 +5174,7 @@ pub fn sock_recv_from( /// /// Number of bytes transmitted. pub fn sock_send( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, si_data: WasmPtr<__wasi_ciovec_t, M>, si_data_len: M::Offset, @@ -5002,11 +5183,11 @@ pub fn sock_send( ) -> Result<__wasi_errno_t, WasiError> { debug!("wasi::sock_send"); - let memory = thread.memory(); + let memory = env.memory(); let iovs_arr = wasi_try_mem_ok!(si_data.slice(memory, si_data_len)); let bytes_written = wasi_try_ok!(__sock_actor_mut( - thread, + env, sock, __WASI_RIGHT_SOCK_SEND, |socket| { socket.send(memory, iovs_arr) } @@ -5034,7 +5215,7 @@ pub fn sock_send( /// /// Number of bytes transmitted. pub fn sock_send_to( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, si_data: WasmPtr<__wasi_ciovec_t, M>, si_data_len: M::Offset, @@ -5044,11 +5225,11 @@ pub fn sock_send_to( ) -> Result<__wasi_errno_t, WasiError> { debug!("wasi::sock_send_to"); - let memory = thread.memory(); + let memory = env.memory(); let iovs_arr = wasi_try_mem_ok!(si_data.slice(memory, si_data_len)); let bytes_written = wasi_try_ok!(__sock_actor_mut( - thread, + env, sock, __WASI_RIGHT_SOCK_SEND_TO, |socket| { socket.send_to::(memory, iovs_arr, addr) } @@ -5074,7 +5255,7 @@ pub fn sock_send_to( /// /// Number of bytes transmitted. pub unsafe fn sock_send_file( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, in_fd: __wasi_fd_t, offset: __wasi_filesize_t, @@ -5082,7 +5263,7 @@ pub unsafe fn sock_send_file( ret_sent: WasmPtr<__wasi_filesize_t, M>, ) -> Result<__wasi_errno_t, WasiError> { debug!("wasi::send_file"); - let (memory, state, inodes) = thread.get_memory_and_wasi_state_and_inodes(0); + let (memory, state, inodes) = env.get_memory_and_wasi_state_and_inodes(0); // Set the offset of the file { @@ -5105,7 +5286,7 @@ pub unsafe fn sock_send_file( inodes .stdin_mut(&state.fs.fd_map) .map_err(fs_error_into_wasi_err), - thread + env ); if let Some(ref mut stdin) = guard.deref_mut() { wasi_try_ok!(stdin.read(&mut buf).map_err(map_io_err)) @@ -5133,7 +5314,7 @@ pub unsafe fn sock_send_file( handle .seek(std::io::SeekFrom::Start(offset as u64)) .map_err(map_io_err), - thread + env ); wasi_try_ok!(handle.read(&mut buf).map_err(map_io_err)) } else { @@ -5171,7 +5352,7 @@ pub unsafe fn sock_send_file( // Write it down to the socket let bytes_written = wasi_try_ok!(__sock_actor_mut( - thread, + env, sock, __WASI_RIGHT_SOCK_SEND, |socket| { @@ -5206,7 +5387,7 @@ pub unsafe fn sock_send_file( /// /// The number of IP addresses returned during the DNS resolution. pub fn resolve( - thread: &WasiThread, + env: &WasiEnv, host: WasmPtr, host_len: M::Offset, port: u16, @@ -5217,13 +5398,13 @@ pub fn resolve( debug!("wasi::resolve"); let naddrs: usize = wasi_try!(naddrs.try_into().map_err(|_| __WASI_EINVAL)); - let memory = thread.memory(); + let memory = env.memory(); let host_str = unsafe { get_input_str!(memory, host, host_len) }; let addrs = wasi_try_mem!(addrs.slice(memory, wasi_try!(to_offset::(naddrs)))); let port = if port > 0 { Some(port) } else { None }; - let found_ips = wasi_try!(thread + let found_ips = wasi_try!(env .net() .resolve(host_str.as_str(), port, None) .map_err(net_error_into_wasi_err)); diff --git a/lib/wasi/src/syscalls/wasi.rs b/lib/wasi/src/syscalls/wasi.rs index bed579502fe..b7d1c28edc4 100644 --- a/lib/wasi/src/syscalls/wasi.rs +++ b/lib/wasi/src/syscalls/wasi.rs @@ -1,5 +1,5 @@ #![deny(dead_code)] -use crate::{WasiError, WasiState, WasiThread}; +use crate::{WasiEnv, WasiError, WasiState, WasiThread}; use wasmer::{Memory, Memory32, MemorySize, WasmPtr, WasmSlice}; use wasmer_wasi_types::*; @@ -7,254 +7,250 @@ type MemoryType = Memory32; type MemoryOffset = u32; pub(crate) fn args_get( - thread: &WasiThread, + env: &WasiEnv, argv: WasmPtr, MemoryType>, argv_buf: WasmPtr, ) -> __wasi_errno_t { - super::args_get::(thread, argv, argv_buf) + super::args_get::(env, argv, argv_buf) } pub(crate) fn args_sizes_get( - thread: &WasiThread, + env: &WasiEnv, argc: WasmPtr, argv_buf_size: WasmPtr, ) -> __wasi_errno_t { - super::args_sizes_get::(thread, argc, argv_buf_size) + super::args_sizes_get::(env, argc, argv_buf_size) } pub(crate) fn clock_res_get( - thread: &WasiThread, + env: &WasiEnv, clock_id: __wasi_clockid_t, resolution: WasmPtr<__wasi_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::clock_res_get::(thread, clock_id, resolution) + super::clock_res_get::(env, clock_id, resolution) } pub(crate) fn clock_time_get( - thread: &WasiThread, + env: &WasiEnv, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, time: WasmPtr<__wasi_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::clock_time_get::(thread, clock_id, precision, time) + super::clock_time_get::(env, clock_id, precision, time) } pub(crate) fn environ_get( - thread: &WasiThread, + env: &WasiEnv, environ: WasmPtr, MemoryType>, environ_buf: WasmPtr, ) -> __wasi_errno_t { - super::environ_get::(thread, environ, environ_buf) + super::environ_get::(env, environ, environ_buf) } pub(crate) fn environ_sizes_get( - thread: &WasiThread, + env: &WasiEnv, environ_count: WasmPtr, environ_buf_size: WasmPtr, ) -> __wasi_errno_t { - super::environ_sizes_get::(thread, environ_count, environ_buf_size) + super::environ_sizes_get::(env, environ_count, environ_buf_size) } pub(crate) fn fd_advise( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filesize_t, len: __wasi_filesize_t, advice: __wasi_advice_t, ) -> __wasi_errno_t { - super::fd_advise(thread, fd, offset, len, advice) + super::fd_advise(env, fd, offset, len, advice) } pub(crate) fn fd_allocate( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filesize_t, len: __wasi_filesize_t, ) -> __wasi_errno_t { - super::fd_allocate(thread, fd, offset, len) + super::fd_allocate(env, fd, offset, len) } -pub(crate) fn fd_close(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { - super::fd_close(thread, fd) +pub(crate) fn fd_close(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { + super::fd_close(env, fd) } -pub(crate) fn fd_datasync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { - super::fd_datasync(thread, fd) +pub(crate) fn fd_datasync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { + super::fd_datasync(env, fd) } pub(crate) fn fd_fdstat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf_ptr: WasmPtr<__wasi_fdstat_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_fdstat_get::(thread, fd, buf_ptr) + super::fd_fdstat_get::(env, fd, buf_ptr) } pub(crate) fn fd_fdstat_set_flags( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_fdflags_t, ) -> __wasi_errno_t { - super::fd_fdstat_set_flags(thread, fd, flags) + super::fd_fdstat_set_flags(env, fd, flags) } pub(crate) fn fd_fdstat_set_rights( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, fs_rights_base: __wasi_rights_t, fs_rights_inheriting: __wasi_rights_t, ) -> __wasi_errno_t { - super::fd_fdstat_set_rights(thread, fd, fs_rights_base, fs_rights_inheriting) + super::fd_fdstat_set_rights(env, fd, fs_rights_base, fs_rights_inheriting) } pub(crate) fn fd_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr<__wasi_filestat_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_filestat_get::(thread, fd, buf) + super::fd_filestat_get::(env, fd, buf) } pub(crate) fn fd_filestat_set_size( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, st_size: __wasi_filesize_t, ) -> __wasi_errno_t { - super::fd_filestat_set_size(thread, fd, st_size) + super::fd_filestat_set_size(env, fd, st_size) } pub(crate) fn fd_filestat_set_times( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, st_atim: __wasi_timestamp_t, st_mtim: __wasi_timestamp_t, fst_flags: __wasi_fstflags_t, ) -> __wasi_errno_t { - super::fd_filestat_set_times(thread, fd, st_atim, st_mtim, fst_flags) + super::fd_filestat_set_times(env, fd, st_atim, st_mtim, fst_flags) } pub(crate) fn fd_pread( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_iovec_t, MemoryType>, iovs_len: MemoryOffset, offset: __wasi_filesize_t, nread: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_pread::(thread, fd, iovs, iovs_len, offset, nread) + super::fd_pread::(env, fd, iovs, iovs_len, offset, nread) } pub(crate) fn fd_prestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr<__wasi_prestat_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_prestat_get::(thread, fd, buf) + super::fd_prestat_get::(env, fd, buf) } pub(crate) fn fd_prestat_dir_name( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::fd_prestat_dir_name::(thread, fd, path, path_len) + super::fd_prestat_dir_name::(env, fd, path, path_len) } pub(crate) fn fd_pwrite( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_ciovec_t, MemoryType>, iovs_len: MemoryOffset, offset: __wasi_filesize_t, nwritten: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_pwrite::(thread, fd, iovs, iovs_len, offset, nwritten) + super::fd_pwrite::(env, fd, iovs, iovs_len, offset, nwritten) } pub(crate) fn fd_read( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_iovec_t, MemoryType>, iovs_len: MemoryOffset, nread: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_read::(thread, fd, iovs, iovs_len, nread) + super::fd_read::(env, fd, iovs, iovs_len, nread) } pub(crate) fn fd_readdir( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr, buf_len: MemoryOffset, cookie: __wasi_dircookie_t, bufused: WasmPtr, ) -> __wasi_errno_t { - super::fd_readdir::(thread, fd, buf, buf_len, cookie, bufused) + super::fd_readdir::(env, fd, buf, buf_len, cookie, bufused) } -pub(crate) fn fd_renumber( - thread: &WasiThread, - from: __wasi_fd_t, - to: __wasi_fd_t, -) -> __wasi_errno_t { - super::fd_renumber(thread, from, to) +pub(crate) fn fd_renumber(env: &WasiEnv, from: __wasi_fd_t, to: __wasi_fd_t) -> __wasi_errno_t { + super::fd_renumber(env, from, to) } pub(crate) fn fd_seek( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filedelta_t, whence: __wasi_whence_t, newoffset: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_seek::(thread, fd, offset, whence, newoffset) + super::fd_seek::(env, fd, offset, whence, newoffset) } -pub(crate) fn fd_sync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { - super::fd_sync(thread, fd) +pub(crate) fn fd_sync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { + super::fd_sync(env, fd) } pub(crate) fn fd_tell( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_tell::(thread, fd, offset) + super::fd_tell::(env, fd, offset) } pub(crate) fn fd_write( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_ciovec_t, MemoryType>, iovs_len: MemoryOffset, nwritten: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_write::(thread, fd, iovs, iovs_len, nwritten) + super::fd_write::(env, fd, iovs, iovs_len, nwritten) } pub(crate) fn path_create_directory( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_create_directory::(thread, fd, path, path_len) + super::path_create_directory::(env, fd, path, path_len) } pub(crate) fn path_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_lookupflags_t, path: WasmPtr, path_len: MemoryOffset, buf: WasmPtr<__wasi_filestat_t, MemoryType>, ) -> __wasi_errno_t { - super::path_filestat_get::(thread, fd, flags, path, path_len, buf) + super::path_filestat_get::(env, fd, flags, path, path_len, buf) } pub(crate) fn path_filestat_set_times( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_lookupflags_t, path: WasmPtr, @@ -264,12 +260,12 @@ pub(crate) fn path_filestat_set_times( fst_flags: __wasi_fstflags_t, ) -> __wasi_errno_t { super::path_filestat_set_times::( - thread, fd, flags, path, path_len, st_atim, st_mtim, fst_flags, + env, fd, flags, path, path_len, st_atim, st_mtim, fst_flags, ) } pub(crate) fn path_link( - thread: &WasiThread, + env: &WasiEnv, old_fd: __wasi_fd_t, old_flags: __wasi_lookupflags_t, old_path: WasmPtr, @@ -279,7 +275,7 @@ pub(crate) fn path_link( new_path_len: MemoryOffset, ) -> __wasi_errno_t { super::path_link::( - thread, + env, old_fd, old_flags, old_path, @@ -291,7 +287,7 @@ pub(crate) fn path_link( } pub(crate) fn path_open( - thread: &WasiThread, + env: &WasiEnv, dirfd: __wasi_fd_t, dirflags: __wasi_lookupflags_t, path: WasmPtr, @@ -303,7 +299,7 @@ pub(crate) fn path_open( fd: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { super::path_open::( - thread, + env, dirfd, dirflags, path, @@ -317,7 +313,7 @@ pub(crate) fn path_open( } pub(crate) fn path_readlink( - thread: &WasiThread, + env: &WasiEnv, dir_fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, @@ -325,20 +321,20 @@ pub(crate) fn path_readlink( buf_len: MemoryOffset, buf_used: WasmPtr, ) -> __wasi_errno_t { - super::path_readlink::(thread, dir_fd, path, path_len, buf, buf_len, buf_used) + super::path_readlink::(env, dir_fd, path, path_len, buf, buf_len, buf_used) } pub(crate) fn path_remove_directory( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_remove_directory::(thread, fd, path, path_len) + super::path_remove_directory::(env, fd, path, path_len) } pub(crate) fn path_rename( - thread: &WasiThread, + env: &WasiEnv, old_fd: __wasi_fd_t, old_path: WasmPtr, old_path_len: MemoryOffset, @@ -347,7 +343,7 @@ pub(crate) fn path_rename( new_path_len: MemoryOffset, ) -> __wasi_errno_t { super::path_rename::( - thread, + env, old_fd, old_path, old_path_len, @@ -358,57 +354,57 @@ pub(crate) fn path_rename( } pub(crate) fn path_symlink( - thread: &WasiThread, + env: &WasiEnv, old_path: WasmPtr, old_path_len: MemoryOffset, fd: __wasi_fd_t, new_path: WasmPtr, new_path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_symlink::(thread, old_path, old_path_len, fd, new_path, new_path_len) + super::path_symlink::(env, old_path, old_path_len, fd, new_path, new_path_len) } pub(crate) fn path_unlink_file( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_unlink_file::(thread, fd, path, path_len) + super::path_unlink_file::(env, fd, path, path_len) } pub(crate) fn poll_oneoff( - thread: &WasiThread, + env: &WasiEnv, in_: WasmPtr<__wasi_subscription_t, MemoryType>, out_: WasmPtr<__wasi_event_t, MemoryType>, nsubscriptions: MemoryOffset, nevents: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::poll_oneoff::(thread, in_, out_, nsubscriptions, nevents) + super::poll_oneoff::(env, in_, out_, nsubscriptions, nevents) } -pub(crate) fn proc_exit(thread: &WasiThread, code: __wasi_exitcode_t) -> Result<(), WasiError> { - super::proc_exit(thread, code) +pub(crate) fn proc_exit(env: &WasiEnv, code: __wasi_exitcode_t) -> Result<(), WasiError> { + super::proc_exit(env, code) } -pub(crate) fn proc_raise(thread: &WasiThread, sig: __wasi_signal_t) -> __wasi_errno_t { - super::proc_raise(thread, sig) +pub(crate) fn proc_raise(env: &WasiEnv, sig: __wasi_signal_t) -> __wasi_errno_t { + super::proc_raise(env, sig) } pub(crate) fn random_get( - thread: &WasiThread, + env: &WasiEnv, buf: WasmPtr, buf_len: MemoryOffset, ) -> __wasi_errno_t { - super::random_get::(thread, buf, buf_len) + super::random_get::(env, buf, buf_len) } -pub(crate) fn sched_yield(thread: &WasiThread) -> Result<__wasi_errno_t, WasiError> { - super::sched_yield(thread) +pub(crate) fn sched_yield(env: &WasiEnv) -> Result<__wasi_errno_t, WasiError> { + super::sched_yield(env) } pub(crate) fn sock_recv( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ri_data: WasmPtr<__wasi_iovec_t, MemoryType>, ri_data_len: MemoryOffset, @@ -417,7 +413,7 @@ pub(crate) fn sock_recv( ro_flags: WasmPtr<__wasi_roflags_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { super::sock_recv::( - thread, + env, sock, ri_data, ri_data_len, @@ -428,20 +424,20 @@ pub(crate) fn sock_recv( } pub(crate) fn sock_send( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, si_data: WasmPtr<__wasi_ciovec_t, MemoryType>, si_data_len: MemoryOffset, si_flags: __wasi_siflags_t, ret_data_len: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::sock_send::(thread, sock, si_data, si_data_len, si_flags, ret_data_len) + super::sock_send::(env, sock, si_data, si_data_len, si_flags, ret_data_len) } pub(crate) fn sock_shutdown( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, how: __wasi_sdflags_t, ) -> __wasi_errno_t { - super::sock_shutdown(thread, sock, how) + super::sock_shutdown(env, sock, how) } diff --git a/lib/wasi/src/syscalls/wasix32.rs b/lib/wasi/src/syscalls/wasix32.rs index 30e50560df6..557e5c2089a 100644 --- a/lib/wasi/src/syscalls/wasix32.rs +++ b/lib/wasi/src/syscalls/wasix32.rs @@ -1,5 +1,5 @@ #![deny(dead_code)] -use crate::{WasiError, WasiState, WasiThread}; +use crate::{WasiEnv, WasiError, WasiState, WasiThread}; use wasmer::{Memory, Memory32, MemorySize, WasmPtr, WasmSlice}; use wasmer_wasi_types::*; @@ -7,254 +7,250 @@ type MemoryType = Memory32; type MemoryOffset = u32; pub(crate) fn args_get( - thread: &WasiThread, + env: &WasiEnv, argv: WasmPtr, MemoryType>, argv_buf: WasmPtr, ) -> __wasi_errno_t { - super::args_get::(thread, argv, argv_buf) + super::args_get::(env, argv, argv_buf) } pub(crate) fn args_sizes_get( - thread: &WasiThread, + env: &WasiEnv, argc: WasmPtr, argv_buf_size: WasmPtr, ) -> __wasi_errno_t { - super::args_sizes_get::(thread, argc, argv_buf_size) + super::args_sizes_get::(env, argc, argv_buf_size) } pub(crate) fn clock_res_get( - thread: &WasiThread, + env: &WasiEnv, clock_id: __wasi_clockid_t, resolution: WasmPtr<__wasi_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::clock_res_get::(thread, clock_id, resolution) + super::clock_res_get::(env, clock_id, resolution) } pub(crate) fn clock_time_get( - thread: &WasiThread, + env: &WasiEnv, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, time: WasmPtr<__wasi_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::clock_time_get::(thread, clock_id, precision, time) + super::clock_time_get::(env, clock_id, precision, time) } pub(crate) fn environ_get( - thread: &WasiThread, + env: &WasiEnv, environ: WasmPtr, MemoryType>, environ_buf: WasmPtr, ) -> __wasi_errno_t { - super::environ_get::(thread, environ, environ_buf) + super::environ_get::(env, environ, environ_buf) } pub(crate) fn environ_sizes_get( - thread: &WasiThread, + env: &WasiEnv, environ_count: WasmPtr, environ_buf_size: WasmPtr, ) -> __wasi_errno_t { - super::environ_sizes_get::(thread, environ_count, environ_buf_size) + super::environ_sizes_get::(env, environ_count, environ_buf_size) } pub(crate) fn fd_advise( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filesize_t, len: __wasi_filesize_t, advice: __wasi_advice_t, ) -> __wasi_errno_t { - super::fd_advise(thread, fd, offset, len, advice) + super::fd_advise(env, fd, offset, len, advice) } pub(crate) fn fd_allocate( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filesize_t, len: __wasi_filesize_t, ) -> __wasi_errno_t { - super::fd_allocate(thread, fd, offset, len) + super::fd_allocate(env, fd, offset, len) } -pub(crate) fn fd_close(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { - super::fd_close(thread, fd) +pub(crate) fn fd_close(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { + super::fd_close(env, fd) } -pub(crate) fn fd_datasync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { - super::fd_datasync(thread, fd) +pub(crate) fn fd_datasync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { + super::fd_datasync(env, fd) } pub(crate) fn fd_fdstat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf_ptr: WasmPtr<__wasi_fdstat_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_fdstat_get::(thread, fd, buf_ptr) + super::fd_fdstat_get::(env, fd, buf_ptr) } pub(crate) fn fd_fdstat_set_flags( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_fdflags_t, ) -> __wasi_errno_t { - super::fd_fdstat_set_flags(thread, fd, flags) + super::fd_fdstat_set_flags(env, fd, flags) } pub(crate) fn fd_fdstat_set_rights( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, fs_rights_base: __wasi_rights_t, fs_rights_inheriting: __wasi_rights_t, ) -> __wasi_errno_t { - super::fd_fdstat_set_rights(thread, fd, fs_rights_base, fs_rights_inheriting) + super::fd_fdstat_set_rights(env, fd, fs_rights_base, fs_rights_inheriting) } pub(crate) fn fd_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr<__wasi_filestat_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_filestat_get::(thread, fd, buf) + super::fd_filestat_get::(env, fd, buf) } pub(crate) fn fd_filestat_set_size( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, st_size: __wasi_filesize_t, ) -> __wasi_errno_t { - super::fd_filestat_set_size(thread, fd, st_size) + super::fd_filestat_set_size(env, fd, st_size) } pub(crate) fn fd_filestat_set_times( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, st_atim: __wasi_timestamp_t, st_mtim: __wasi_timestamp_t, fst_flags: __wasi_fstflags_t, ) -> __wasi_errno_t { - super::fd_filestat_set_times(thread, fd, st_atim, st_mtim, fst_flags) + super::fd_filestat_set_times(env, fd, st_atim, st_mtim, fst_flags) } pub(crate) fn fd_pread( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_iovec_t, MemoryType>, iovs_len: MemoryOffset, offset: __wasi_filesize_t, nread: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_pread::(thread, fd, iovs, iovs_len, offset, nread) + super::fd_pread::(env, fd, iovs, iovs_len, offset, nread) } pub(crate) fn fd_prestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr<__wasi_prestat_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_prestat_get::(thread, fd, buf) + super::fd_prestat_get::(env, fd, buf) } pub(crate) fn fd_prestat_dir_name( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::fd_prestat_dir_name::(thread, fd, path, path_len) + super::fd_prestat_dir_name::(env, fd, path, path_len) } pub(crate) fn fd_pwrite( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_ciovec_t, MemoryType>, iovs_len: MemoryOffset, offset: __wasi_filesize_t, nwritten: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_pwrite::(thread, fd, iovs, iovs_len, offset, nwritten) + super::fd_pwrite::(env, fd, iovs, iovs_len, offset, nwritten) } pub(crate) fn fd_read( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_iovec_t, MemoryType>, iovs_len: MemoryOffset, nread: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_read::(thread, fd, iovs, iovs_len, nread) + super::fd_read::(env, fd, iovs, iovs_len, nread) } pub(crate) fn fd_readdir( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr, buf_len: MemoryOffset, cookie: __wasi_dircookie_t, bufused: WasmPtr, ) -> __wasi_errno_t { - super::fd_readdir::(thread, fd, buf, buf_len, cookie, bufused) + super::fd_readdir::(env, fd, buf, buf_len, cookie, bufused) } -pub(crate) fn fd_renumber( - thread: &WasiThread, - from: __wasi_fd_t, - to: __wasi_fd_t, -) -> __wasi_errno_t { - super::fd_renumber(thread, from, to) +pub(crate) fn fd_renumber(env: &WasiEnv, from: __wasi_fd_t, to: __wasi_fd_t) -> __wasi_errno_t { + super::fd_renumber(env, from, to) } pub(crate) fn fd_seek( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filedelta_t, whence: __wasi_whence_t, newoffset: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_seek::(thread, fd, offset, whence, newoffset) + super::fd_seek::(env, fd, offset, whence, newoffset) } -pub(crate) fn fd_sync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { - super::fd_sync(thread, fd) +pub(crate) fn fd_sync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { + super::fd_sync(env, fd) } pub(crate) fn fd_tell( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_tell::(thread, fd, offset) + super::fd_tell::(env, fd, offset) } pub(crate) fn fd_write( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_ciovec_t, MemoryType>, iovs_len: MemoryOffset, nwritten: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_write::(thread, fd, iovs, iovs_len, nwritten) + super::fd_write::(env, fd, iovs, iovs_len, nwritten) } pub(crate) fn path_create_directory( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_create_directory::(thread, fd, path, path_len) + super::path_create_directory::(env, fd, path, path_len) } pub(crate) fn path_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_lookupflags_t, path: WasmPtr, path_len: MemoryOffset, buf: WasmPtr<__wasi_filestat_t, MemoryType>, ) -> __wasi_errno_t { - super::path_filestat_get::(thread, fd, flags, path, path_len, buf) + super::path_filestat_get::(env, fd, flags, path, path_len, buf) } pub(crate) fn path_filestat_set_times( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_lookupflags_t, path: WasmPtr, @@ -264,12 +260,12 @@ pub(crate) fn path_filestat_set_times( fst_flags: __wasi_fstflags_t, ) -> __wasi_errno_t { super::path_filestat_set_times::( - thread, fd, flags, path, path_len, st_atim, st_mtim, fst_flags, + env, fd, flags, path, path_len, st_atim, st_mtim, fst_flags, ) } pub(crate) fn path_link( - thread: &WasiThread, + env: &WasiEnv, old_fd: __wasi_fd_t, old_flags: __wasi_lookupflags_t, old_path: WasmPtr, @@ -279,7 +275,7 @@ pub(crate) fn path_link( new_path_len: MemoryOffset, ) -> __wasi_errno_t { super::path_link::( - thread, + env, old_fd, old_flags, old_path, @@ -291,7 +287,7 @@ pub(crate) fn path_link( } pub(crate) fn path_open( - thread: &WasiThread, + env: &WasiEnv, dirfd: __wasi_fd_t, dirflags: __wasi_lookupflags_t, path: WasmPtr, @@ -303,7 +299,7 @@ pub(crate) fn path_open( fd: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { super::path_open::( - thread, + env, dirfd, dirflags, path, @@ -317,7 +313,7 @@ pub(crate) fn path_open( } pub(crate) fn path_readlink( - thread: &WasiThread, + env: &WasiEnv, dir_fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, @@ -325,20 +321,20 @@ pub(crate) fn path_readlink( buf_len: MemoryOffset, buf_used: WasmPtr, ) -> __wasi_errno_t { - super::path_readlink::(thread, dir_fd, path, path_len, buf, buf_len, buf_used) + super::path_readlink::(env, dir_fd, path, path_len, buf, buf_len, buf_used) } pub(crate) fn path_remove_directory( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_remove_directory::(thread, fd, path, path_len) + super::path_remove_directory::(env, fd, path, path_len) } pub(crate) fn path_rename( - thread: &WasiThread, + env: &WasiEnv, old_fd: __wasi_fd_t, old_path: WasmPtr, old_path_len: MemoryOffset, @@ -347,7 +343,7 @@ pub(crate) fn path_rename( new_path_len: MemoryOffset, ) -> __wasi_errno_t { super::path_rename::( - thread, + env, old_fd, old_path, old_path_len, @@ -358,162 +354,159 @@ pub(crate) fn path_rename( } pub(crate) fn path_symlink( - thread: &WasiThread, + env: &WasiEnv, old_path: WasmPtr, old_path_len: MemoryOffset, fd: __wasi_fd_t, new_path: WasmPtr, new_path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_symlink::(thread, old_path, old_path_len, fd, new_path, new_path_len) + super::path_symlink::(env, old_path, old_path_len, fd, new_path, new_path_len) } pub(crate) fn path_unlink_file( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_unlink_file::(thread, fd, path, path_len) + super::path_unlink_file::(env, fd, path, path_len) } pub(crate) fn poll_oneoff( - thread: &WasiThread, + env: &WasiEnv, in_: WasmPtr<__wasi_subscription_t, MemoryType>, out_: WasmPtr<__wasi_event_t, MemoryType>, nsubscriptions: MemoryOffset, nevents: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::poll_oneoff::(thread, in_, out_, nsubscriptions, nevents) + super::poll_oneoff::(env, in_, out_, nsubscriptions, nevents) } -pub(crate) fn proc_exit(thread: &WasiThread, code: __wasi_exitcode_t) -> Result<(), WasiError> { - super::proc_exit(thread, code) +pub(crate) fn proc_exit(env: &WasiEnv, code: __wasi_exitcode_t) -> Result<(), WasiError> { + super::proc_exit(env, code) } -pub(crate) fn proc_raise(thread: &WasiThread, sig: __wasi_signal_t) -> __wasi_errno_t { - super::proc_raise(thread, sig) +pub(crate) fn proc_raise(env: &WasiEnv, sig: __wasi_signal_t) -> __wasi_errno_t { + super::proc_raise(env, sig) } pub(crate) fn random_get( - thread: &WasiThread, + env: &WasiEnv, buf: WasmPtr, buf_len: MemoryOffset, ) -> __wasi_errno_t { - super::random_get::(thread, buf, buf_len) + super::random_get::(env, buf, buf_len) } pub(crate) fn fd_dup( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, ret_fd: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_dup::(thread, fd, ret_fd) + super::fd_dup::(env, fd, ret_fd) } pub(crate) fn fd_event( - thread: &WasiThread, + env: &WasiEnv, initial_val: u64, flags: __wasi_eventfdflags, ret_fd: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_event(thread, initial_val, flags, ret_fd) + super::fd_event(env, initial_val, flags, ret_fd) } pub(crate) fn fd_pipe( - thread: &WasiThread, + env: &WasiEnv, ro_fd1: WasmPtr<__wasi_fd_t, MemoryType>, ro_fd2: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_pipe::(thread, ro_fd1, ro_fd2) + super::fd_pipe::(env, ro_fd1, ro_fd2) } pub(crate) fn tty_get( - thread: &WasiThread, + env: &WasiEnv, tty_state: WasmPtr<__wasi_tty_t, MemoryType>, ) -> __wasi_errno_t { - super::tty_get::(thread, tty_state) + super::tty_get::(env, tty_state) } pub(crate) fn tty_set( - thread: &WasiThread, + env: &WasiEnv, tty_state: WasmPtr<__wasi_tty_t, MemoryType>, ) -> __wasi_errno_t { - super::tty_set::(thread, tty_state) + super::tty_set::(env, tty_state) } pub(crate) fn getcwd( - thread: &WasiThread, + env: &WasiEnv, path: WasmPtr, path_len: WasmPtr, ) -> __wasi_errno_t { - super::getcwd::(thread, path, path_len) + super::getcwd::(env, path, path_len) } pub(crate) fn chdir( - thread: &WasiThread, + env: &WasiEnv, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::chdir::(thread, path, path_len) + super::chdir::(env, path, path_len) } pub(crate) fn thread_spawn( - thread: &WasiThread, + env: &WasiEnv, method: WasmPtr, method_len: MemoryOffset, user_data: u64, reactor: __wasi_bool_t, ret_tid: WasmPtr<__wasi_tid_t, MemoryType>, ) -> __wasi_errno_t { - super::thread_spawn::(thread, method, method_len, user_data, reactor, ret_tid) + super::thread_spawn::(env, method, method_len, user_data, reactor, ret_tid) } pub(crate) fn thread_sleep( - thread: &WasiThread, + env: &WasiEnv, duration: __wasi_timestamp_t, ) -> Result<__wasi_errno_t, WasiError> { - super::thread_sleep(thread, duration) + super::thread_sleep(env, duration) } pub(crate) fn thread_id( - thread: &WasiThread, + env: &WasiEnv, ret_tid: WasmPtr<__wasi_tid_t, MemoryType>, ) -> __wasi_errno_t { - super::thread_id::(thread, ret_tid) + super::thread_id::(env, ret_tid) } -pub(crate) fn thread_join(thread: &WasiThread, tid: __wasi_tid_t) -> __wasi_errno_t { - super::thread_join(thread, tid) +pub(crate) fn thread_join(env: &WasiEnv, tid: __wasi_tid_t) -> Result<__wasi_errno_t, WasiError> { + super::thread_join(env, tid) } pub(crate) fn thread_parallelism( - thread: &WasiThread, + env: &WasiEnv, ret_parallelism: WasmPtr, ) -> __wasi_errno_t { - super::thread_parallelism::(thread, ret_parallelism) + super::thread_parallelism::(env, ret_parallelism) } pub(crate) fn thread_exit( - thread: &WasiThread, + env: &WasiEnv, exitcode: __wasi_exitcode_t, ) -> Result<__wasi_errno_t, WasiError> { - super::thread_exit(thread, exitcode) + super::thread_exit(env, exitcode) } -pub(crate) fn sched_yield(thread: &WasiThread) -> Result<__wasi_errno_t, WasiError> { - super::sched_yield(thread) +pub(crate) fn sched_yield(env: &WasiEnv) -> Result<__wasi_errno_t, WasiError> { + super::sched_yield(env) } -pub(crate) fn getpid( - thread: &WasiThread, - ret_pid: WasmPtr<__wasi_pid_t, MemoryType>, -) -> __wasi_errno_t { - super::getpid::(thread, ret_pid) +pub(crate) fn getpid(env: &WasiEnv, ret_pid: WasmPtr<__wasi_pid_t, MemoryType>) -> __wasi_errno_t { + super::getpid::(env, ret_pid) } -pub(crate) fn bus_spawn_local( - thread: &WasiThread, +pub(crate) fn process_spawn( + env: &WasiEnv, name: WasmPtr, name_len: MemoryOffset, chroot: __wasi_bool_t, @@ -527,9 +520,9 @@ pub(crate) fn bus_spawn_local( working_dir: WasmPtr, working_dir_len: MemoryOffset, ret_handles: WasmPtr<__wasi_bus_handles_t, MemoryType>, -) -> __wasi_errno_t { - super::bus_spawn_local::( - thread, +) -> __bus_errno_t { + super::process_spawn::( + env, name, name_len, chroot, @@ -546,56 +539,47 @@ pub(crate) fn bus_spawn_local( ) } -pub(crate) fn bus_spawn_remote( - thread: &WasiThread, +pub(crate) fn bus_open_local( + env: &WasiEnv, name: WasmPtr, name_len: MemoryOffset, - chroot: __wasi_bool_t, - args: WasmPtr, - args_len: MemoryOffset, - preopen: WasmPtr, - preopen_len: MemoryOffset, - working_dir: WasmPtr, - working_dir_len: MemoryOffset, - stdin: __wasi_stdiomode_t, - stdout: __wasi_stdiomode_t, - stderr: __wasi_stdiomode_t, + reuse: __wasi_bool_t, + ret_bid: WasmPtr<__wasi_bid_t, MemoryType>, +) -> __bus_errno_t { + super::bus_open_local::(env, name, name_len, reuse, ret_bid) +} + +pub(crate) fn bus_open_remote( + env: &WasiEnv, + name: WasmPtr, + name_len: MemoryOffset, + reuse: __wasi_bool_t, instance: WasmPtr, instance_len: MemoryOffset, token: WasmPtr, token_len: MemoryOffset, - ret_handles: WasmPtr<__wasi_bus_handles_t, MemoryType>, -) -> __wasi_errno_t { - super::bus_spawn_remote::( - thread, + ret_bid: WasmPtr<__wasi_bid_t, MemoryType>, +) -> __bus_errno_t { + super::bus_open_remote::( + env, name, name_len, - chroot, - args, - args_len, - preopen, - preopen_len, - working_dir, - working_dir_len, - stdin, - stdout, - stderr, + reuse, instance, instance_len, token, token_len, - ret_handles, + ret_bid, ) } -pub(crate) fn bus_close(thread: &WasiThread, bid: __wasi_bid_t) -> __wasi_errno_t { - super::bus_close(thread, bid) +pub(crate) fn bus_close(env: &WasiEnv, bid: __wasi_bid_t) -> __bus_errno_t { + super::bus_close(env, bid) } -pub(crate) fn bus_invoke( - thread: &WasiThread, +pub(crate) fn bus_call( + env: &WasiEnv, bid: __wasi_bid_t, - cid: WasmPtr<__wasi_option_cid_t, MemoryType>, keep_alive: __wasi_bool_t, topic: WasmPtr, topic_len: MemoryOffset, @@ -603,180 +587,165 @@ pub(crate) fn bus_invoke( buf: WasmPtr, buf_len: MemoryOffset, ret_cid: WasmPtr<__wasi_cid_t, MemoryType>, -) -> __wasi_errno_t { - super::bus_invoke::( - thread, bid, cid, keep_alive, topic, topic_len, format, buf, buf_len, ret_cid, +) -> __bus_errno_t { + super::bus_call::( + env, bid, keep_alive, topic, topic_len, format, buf, buf_len, ret_cid, ) } -pub(crate) fn bus_fault( - thread: &WasiThread, - cid: __wasi_cid_t, - fault: __bus_errno_t, -) -> __wasi_errno_t { - super::bus_fault(thread, cid, fault) -} - -pub(crate) fn bus_drop(thread: &WasiThread, cid: __wasi_cid_t) -> __wasi_errno_t { - super::bus_drop(thread, cid) -} - -pub(crate) fn bus_reply( - thread: &WasiThread, - cid: __wasi_cid_t, - format: __wasi_busdataformat_t, - buf: WasmPtr, - buf_len: MemoryOffset, -) -> __wasi_errno_t { - super::bus_reply::(thread, cid, format, buf, buf_len) -} - -pub(crate) fn bus_callback( - thread: &WasiThread, - cid: __wasi_cid_t, +pub(crate) fn bus_subcall( + env: &WasiEnv, + parent: __wasi_cid_t, + keep_alive: __wasi_bool_t, topic: WasmPtr, topic_len: MemoryOffset, format: __wasi_busdataformat_t, buf: WasmPtr, buf_len: MemoryOffset, -) -> __wasi_errno_t { - super::bus_callback::(thread, cid, topic, topic_len, format, buf, buf_len) -} - -pub(crate) fn bus_listen( - thread: &WasiThread, - parent: WasmPtr<__wasi_option_cid_t, MemoryType>, - topic: WasmPtr, - topic_len: MemoryOffset, -) -> __wasi_errno_t { - super::bus_listen::(thread, parent, topic, topic_len) + ret_cid: WasmPtr<__wasi_cid_t, MemoryType>, +) -> __bus_errno_t { + super::bus_subcall::( + env, parent, keep_alive, topic, topic_len, format, buf, buf_len, ret_cid, + ) } pub(crate) fn bus_poll( - thread: &WasiThread, - bid: WasmPtr<__wasi_option_bid_t, MemoryType>, - timeout: WasmPtr<__wasi_timestamp_t, MemoryType>, + env: &WasiEnv, + timeout: __wasi_timestamp_t, events: WasmPtr, nevents: MemoryOffset, + malloc: WasmPtr, + malloc_len: MemoryOffset, ret_nevents: WasmPtr, -) -> __wasi_errno_t { - super::bus_poll::(thread, bid, timeout, events, nevents, ret_nevents) +) -> __bus_errno_t { + super::bus_poll::( + env, + timeout, + events, + nevents, + malloc, + malloc_len, + ret_nevents, + ) } -pub(crate) fn bus_poll_data( - thread: &WasiThread, - bid: WasmPtr<__wasi_option_bid_t, MemoryType>, - timeout: WasmPtr<__wasi_timestamp_t, MemoryType>, - topic: WasmPtr, - topic_len: MemoryOffset, +pub(crate) fn call_reply( + env: &WasiEnv, + cid: __wasi_cid_t, + format: __wasi_busdataformat_t, buf: WasmPtr, buf_len: MemoryOffset, - ret_evt: WasmPtr<__wasi_busevent_data_t, MemoryType>, -) -> __wasi_errno_t { - super::bus_poll_data::( - thread, bid, timeout, topic, topic_len, buf, buf_len, ret_evt, - ) +) -> __bus_errno_t { + super::call_reply::(env, cid, format, buf, buf_len) +} + +pub(crate) fn call_fault(env: &WasiEnv, cid: __wasi_cid_t, fault: __bus_errno_t) -> __bus_errno_t { + super::call_fault(env, cid, fault) +} + +pub(crate) fn call_close(env: &WasiEnv, cid: __wasi_cid_t) -> __bus_errno_t { + super::call_close(env, cid) } pub(crate) fn port_bridge( - thread: &WasiThread, + env: &WasiEnv, network: WasmPtr, network_len: MemoryOffset, token: WasmPtr, token_len: MemoryOffset, security: __wasi_streamsecurity_t, ) -> __wasi_errno_t { - super::port_bridge::(thread, network, network_len, token, token_len, security) + super::port_bridge::(env, network, network_len, token, token_len, security) } -pub(crate) fn port_unbridge(thread: &WasiThread) -> __wasi_errno_t { - super::port_unbridge(thread) +pub(crate) fn port_unbridge(env: &WasiEnv) -> __wasi_errno_t { + super::port_unbridge(env) } -pub(crate) fn port_dhcp_acquire(thread: &WasiThread) -> __wasi_errno_t { - super::port_dhcp_acquire(thread) +pub(crate) fn port_dhcp_acquire(env: &WasiEnv) -> __wasi_errno_t { + super::port_dhcp_acquire(env) } pub(crate) fn port_addr_add( - thread: &WasiThread, + env: &WasiEnv, addr: WasmPtr<__wasi_cidr_t, MemoryType>, ) -> __wasi_errno_t { - super::port_addr_add::(thread, addr) + super::port_addr_add::(env, addr) } pub(crate) fn port_addr_remove( - thread: &WasiThread, + env: &WasiEnv, addr: WasmPtr<__wasi_addr_t, MemoryType>, ) -> __wasi_errno_t { - super::port_addr_remove::(thread, addr) + super::port_addr_remove::(env, addr) } -pub(crate) fn port_addr_clear(thread: &WasiThread) -> __wasi_errno_t { - super::port_addr_clear(thread) +pub(crate) fn port_addr_clear(env: &WasiEnv) -> __wasi_errno_t { + super::port_addr_clear(env) } pub(crate) fn port_addr_list( - thread: &WasiThread, + env: &WasiEnv, addrs: WasmPtr<__wasi_cidr_t, MemoryType>, naddrs: WasmPtr, ) -> __wasi_errno_t { - super::port_addr_list::(thread, addrs, naddrs) + super::port_addr_list::(env, addrs, naddrs) } pub(crate) fn port_mac( - thread: &WasiThread, + env: &WasiEnv, ret_mac: WasmPtr<__wasi_hardwareaddress_t, MemoryType>, ) -> __wasi_errno_t { - super::port_mac::(thread, ret_mac) + super::port_mac::(env, ret_mac) } pub(crate) fn port_gateway_set( - thread: &WasiThread, + env: &WasiEnv, ip: WasmPtr<__wasi_addr_t, MemoryType>, ) -> __wasi_errno_t { - super::port_gateway_set::(thread, ip) + super::port_gateway_set::(env, ip) } pub(crate) fn port_route_add( - thread: &WasiThread, + env: &WasiEnv, cidr: WasmPtr<__wasi_cidr_t, MemoryType>, via_router: WasmPtr<__wasi_addr_t, MemoryType>, preferred_until: WasmPtr<__wasi_option_timestamp_t, MemoryType>, expires_at: WasmPtr<__wasi_option_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::port_route_add::(thread, cidr, via_router, preferred_until, expires_at) + super::port_route_add::(env, cidr, via_router, preferred_until, expires_at) } pub(crate) fn port_route_remove( - thread: &WasiThread, + env: &WasiEnv, ip: WasmPtr<__wasi_addr_t, MemoryType>, ) -> __wasi_errno_t { - super::port_route_remove::(thread, ip) + super::port_route_remove::(env, ip) } -pub(crate) fn port_route_clear(thread: &WasiThread) -> __wasi_errno_t { - super::port_route_clear(thread) +pub(crate) fn port_route_clear(env: &WasiEnv) -> __wasi_errno_t { + super::port_route_clear(env) } pub(crate) fn port_route_list( - thread: &WasiThread, + env: &WasiEnv, routes: WasmPtr<__wasi_route_t, MemoryType>, nroutes: WasmPtr, ) -> __wasi_errno_t { - super::port_route_list::(thread, routes, nroutes) + super::port_route_list::(env, routes, nroutes) } pub(crate) fn ws_connect( - thread: &WasiThread, + env: &WasiEnv, url: WasmPtr, url_len: MemoryOffset, ret_sock: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::ws_connect::(thread, url, url_len, ret_sock) + super::ws_connect::(env, url, url_len, ret_sock) } pub(crate) fn http_request( - thread: &WasiThread, + env: &WasiEnv, url: WasmPtr, url_len: MemoryOffset, method: WasmPtr, @@ -787,7 +756,7 @@ pub(crate) fn http_request( ret_handles: WasmPtr<__wasi_http_handles_t, MemoryType>, ) -> __wasi_errno_t { super::http_request::( - thread, + env, url, url_len, method, @@ -800,7 +769,7 @@ pub(crate) fn http_request( } pub(crate) fn http_status( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, status: WasmPtr<__wasi_http_status_t, MemoryType>, status_text: WasmPtr, @@ -808,169 +777,169 @@ pub(crate) fn http_status( headers: WasmPtr, headers_len: WasmPtr, ) -> __wasi_errno_t { - super::http_status::(thread, sock, status) + super::http_status::(env, sock, status) } pub(crate) fn sock_status( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ret_status: WasmPtr<__wasi_sockstatus_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_status::(thread, sock, ret_status) + super::sock_status::(env, sock, ret_status) } pub(crate) fn sock_addr_local( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ret_addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_addr_local::(thread, sock, ret_addr) + super::sock_addr_local::(env, sock, ret_addr) } pub(crate) fn sock_addr_peer( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ro_addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_addr_peer::(thread, sock, ro_addr) + super::sock_addr_peer::(env, sock, ro_addr) } pub(crate) fn sock_open( - thread: &WasiThread, + env: &WasiEnv, af: __wasi_addressfamily_t, ty: __wasi_socktype_t, pt: __wasi_sockproto_t, ro_sock: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_open::(thread, af, ty, pt, ro_sock) + super::sock_open::(env, af, ty, pt, ro_sock) } pub(crate) fn sock_set_opt_flag( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, flag: __wasi_bool_t, ) -> __wasi_errno_t { - super::sock_set_opt_flag(thread, sock, opt, flag) + super::sock_set_opt_flag(env, sock, opt, flag) } pub(crate) fn sock_get_opt_flag( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, ret_flag: WasmPtr<__wasi_bool_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_get_opt_flag::(thread, sock, opt, ret_flag) + super::sock_get_opt_flag::(env, sock, opt, ret_flag) } pub fn sock_set_opt_time( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, time: WasmPtr<__wasi_option_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_set_opt_time(thread, sock, opt, time) + super::sock_set_opt_time(env, sock, opt, time) } pub fn sock_get_opt_time( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, ret_time: WasmPtr<__wasi_option_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_get_opt_time(thread, sock, opt, ret_time) + super::sock_get_opt_time(env, sock, opt, ret_time) } pub fn sock_set_opt_size( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, size: __wasi_filesize_t, ) -> __wasi_errno_t { - super::sock_set_opt_size(thread, sock, opt, size) + super::sock_set_opt_size(env, sock, opt, size) } pub fn sock_get_opt_size( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, ret_size: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_get_opt_size(thread, sock, opt, ret_size) + super::sock_get_opt_size(env, sock, opt, ret_size) } pub(crate) fn sock_join_multicast_v4( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip4_t, MemoryType>, iface: WasmPtr<__wasi_addr_ip4_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_join_multicast_v4::(thread, sock, multiaddr, iface) + super::sock_join_multicast_v4::(env, sock, multiaddr, iface) } pub(crate) fn sock_leave_multicast_v4( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip4_t, MemoryType>, iface: WasmPtr<__wasi_addr_ip4_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_leave_multicast_v4::(thread, sock, multiaddr, iface) + super::sock_leave_multicast_v4::(env, sock, multiaddr, iface) } pub(crate) fn sock_join_multicast_v6( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip6_t, MemoryType>, iface: u32, ) -> __wasi_errno_t { - super::sock_join_multicast_v6::(thread, sock, multiaddr, iface) + super::sock_join_multicast_v6::(env, sock, multiaddr, iface) } pub(crate) fn sock_leave_multicast_v6( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip6_t, MemoryType>, iface: u32, ) -> __wasi_errno_t { - super::sock_leave_multicast_v6::(thread, sock, multiaddr, iface) + super::sock_leave_multicast_v6::(env, sock, multiaddr, iface) } pub(crate) fn sock_bind( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_bind::(thread, sock, addr) + super::sock_bind::(env, sock, addr) } pub(crate) fn sock_listen( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, backlog: MemoryOffset, ) -> __wasi_errno_t { - super::sock_listen::(thread, sock, backlog) + super::sock_listen::(env, sock, backlog) } pub(crate) fn sock_accept( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, fd_flags: __wasi_fdflags_t, ro_fd: WasmPtr<__wasi_fd_t, MemoryType>, ro_addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { - super::sock_accept::(thread, sock, fd_flags, ro_fd, ro_addr) + super::sock_accept::(env, sock, fd_flags, ro_fd, ro_addr) } pub(crate) fn sock_connect( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_connect::(thread, sock, addr) + super::sock_connect::(env, sock, addr) } pub(crate) fn sock_recv( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ri_data: WasmPtr<__wasi_iovec_t, MemoryType>, ri_data_len: MemoryOffset, @@ -979,7 +948,7 @@ pub(crate) fn sock_recv( ro_flags: WasmPtr<__wasi_roflags_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { super::sock_recv::( - thread, + env, sock, ri_data, ri_data_len, @@ -990,7 +959,7 @@ pub(crate) fn sock_recv( } pub(crate) fn sock_recv_from( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ri_data: WasmPtr<__wasi_iovec_t, MemoryType>, ri_data_len: MemoryOffset, @@ -1000,7 +969,7 @@ pub(crate) fn sock_recv_from( ro_addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { super::sock_recv_from::( - thread, + env, sock, ri_data, ri_data_len, @@ -1012,18 +981,18 @@ pub(crate) fn sock_recv_from( } pub(crate) fn sock_send( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, si_data: WasmPtr<__wasi_ciovec_t, MemoryType>, si_data_len: MemoryOffset, si_flags: __wasi_siflags_t, ret_data_len: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::sock_send::(thread, sock, si_data, si_data_len, si_flags, ret_data_len) + super::sock_send::(env, sock, si_data, si_data_len, si_flags, ret_data_len) } pub(crate) fn sock_send_to( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, si_data: WasmPtr<__wasi_ciovec_t, MemoryType>, si_data_len: MemoryOffset, @@ -1032,7 +1001,7 @@ pub(crate) fn sock_send_to( ret_data_len: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { super::sock_send_to::( - thread, + env, sock, si_data, si_data_len, @@ -1043,26 +1012,26 @@ pub(crate) fn sock_send_to( } pub(crate) fn sock_send_file( - thread: &WasiThread, + env: &WasiEnv, out_fd: __wasi_fd_t, in_fd: __wasi_fd_t, offset: __wasi_filesize_t, count: __wasi_filesize_t, ret_sent: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { - unsafe { super::sock_send_file::(thread, out_fd, in_fd, offset, count, ret_sent) } + unsafe { super::sock_send_file::(env, out_fd, in_fd, offset, count, ret_sent) } } pub(crate) fn sock_shutdown( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, how: __wasi_sdflags_t, ) -> __wasi_errno_t { - super::sock_shutdown(thread, sock, how) + super::sock_shutdown(env, sock, how) } pub(crate) fn resolve( - thread: &WasiThread, + env: &WasiEnv, host: WasmPtr, host_len: MemoryOffset, port: u16, @@ -1070,5 +1039,5 @@ pub(crate) fn resolve( nips: MemoryOffset, ret_nips: WasmPtr, ) -> __wasi_errno_t { - super::resolve::(thread, host, host_len, port, ips, nips, ret_nips) + super::resolve::(env, host, host_len, port, ips, nips, ret_nips) } diff --git a/lib/wasi/src/syscalls/wasix64.rs b/lib/wasi/src/syscalls/wasix64.rs index 6bbd9abb4c7..e3ece1fb80d 100644 --- a/lib/wasi/src/syscalls/wasix64.rs +++ b/lib/wasi/src/syscalls/wasix64.rs @@ -1,5 +1,5 @@ #![deny(dead_code)] -use crate::{WasiError, WasiState, WasiThread}; +use crate::{WasiEnv, WasiError, WasiState, WasiThread}; use wasmer::{Memory, Memory64, MemorySize, WasmPtr, WasmSlice}; use wasmer_wasi_types::*; @@ -7,254 +7,250 @@ type MemoryType = Memory64; type MemoryOffset = u64; pub(crate) fn args_get( - thread: &WasiThread, + env: &WasiEnv, argv: WasmPtr, MemoryType>, argv_buf: WasmPtr, ) -> __wasi_errno_t { - super::args_get::(thread, argv, argv_buf) + super::args_get::(env, argv, argv_buf) } pub(crate) fn args_sizes_get( - thread: &WasiThread, + env: &WasiEnv, argc: WasmPtr, argv_buf_size: WasmPtr, ) -> __wasi_errno_t { - super::args_sizes_get::(thread, argc, argv_buf_size) + super::args_sizes_get::(env, argc, argv_buf_size) } pub(crate) fn clock_res_get( - thread: &WasiThread, + env: &WasiEnv, clock_id: __wasi_clockid_t, resolution: WasmPtr<__wasi_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::clock_res_get::(thread, clock_id, resolution) + super::clock_res_get::(env, clock_id, resolution) } pub(crate) fn clock_time_get( - thread: &WasiThread, + env: &WasiEnv, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, time: WasmPtr<__wasi_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::clock_time_get::(thread, clock_id, precision, time) + super::clock_time_get::(env, clock_id, precision, time) } pub(crate) fn environ_get( - thread: &WasiThread, + env: &WasiEnv, environ: WasmPtr, MemoryType>, environ_buf: WasmPtr, ) -> __wasi_errno_t { - super::environ_get::(thread, environ, environ_buf) + super::environ_get::(env, environ, environ_buf) } pub(crate) fn environ_sizes_get( - thread: &WasiThread, + env: &WasiEnv, environ_count: WasmPtr, environ_buf_size: WasmPtr, ) -> __wasi_errno_t { - super::environ_sizes_get::(thread, environ_count, environ_buf_size) + super::environ_sizes_get::(env, environ_count, environ_buf_size) } pub(crate) fn fd_advise( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filesize_t, len: __wasi_filesize_t, advice: __wasi_advice_t, ) -> __wasi_errno_t { - super::fd_advise(thread, fd, offset, len, advice) + super::fd_advise(env, fd, offset, len, advice) } pub(crate) fn fd_allocate( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filesize_t, len: __wasi_filesize_t, ) -> __wasi_errno_t { - super::fd_allocate(thread, fd, offset, len) + super::fd_allocate(env, fd, offset, len) } -pub(crate) fn fd_close(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { - super::fd_close(thread, fd) +pub(crate) fn fd_close(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { + super::fd_close(env, fd) } -pub(crate) fn fd_datasync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { - super::fd_datasync(thread, fd) +pub(crate) fn fd_datasync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { + super::fd_datasync(env, fd) } pub(crate) fn fd_fdstat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf_ptr: WasmPtr<__wasi_fdstat_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_fdstat_get::(thread, fd, buf_ptr) + super::fd_fdstat_get::(env, fd, buf_ptr) } pub(crate) fn fd_fdstat_set_flags( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_fdflags_t, ) -> __wasi_errno_t { - super::fd_fdstat_set_flags(thread, fd, flags) + super::fd_fdstat_set_flags(env, fd, flags) } pub(crate) fn fd_fdstat_set_rights( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, fs_rights_base: __wasi_rights_t, fs_rights_inheriting: __wasi_rights_t, ) -> __wasi_errno_t { - super::fd_fdstat_set_rights(thread, fd, fs_rights_base, fs_rights_inheriting) + super::fd_fdstat_set_rights(env, fd, fs_rights_base, fs_rights_inheriting) } pub(crate) fn fd_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr<__wasi_filestat_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_filestat_get::(thread, fd, buf) + super::fd_filestat_get::(env, fd, buf) } pub(crate) fn fd_filestat_set_size( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, st_size: __wasi_filesize_t, ) -> __wasi_errno_t { - super::fd_filestat_set_size(thread, fd, st_size) + super::fd_filestat_set_size(env, fd, st_size) } pub(crate) fn fd_filestat_set_times( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, st_atim: __wasi_timestamp_t, st_mtim: __wasi_timestamp_t, fst_flags: __wasi_fstflags_t, ) -> __wasi_errno_t { - super::fd_filestat_set_times(thread, fd, st_atim, st_mtim, fst_flags) + super::fd_filestat_set_times(env, fd, st_atim, st_mtim, fst_flags) } pub(crate) fn fd_pread( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_iovec_t, MemoryType>, iovs_len: MemoryOffset, offset: __wasi_filesize_t, nread: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_pread::(thread, fd, iovs, iovs_len, offset, nread) + super::fd_pread::(env, fd, iovs, iovs_len, offset, nread) } pub(crate) fn fd_prestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr<__wasi_prestat_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_prestat_get::(thread, fd, buf) + super::fd_prestat_get::(env, fd, buf) } pub(crate) fn fd_prestat_dir_name( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::fd_prestat_dir_name::(thread, fd, path, path_len) + super::fd_prestat_dir_name::(env, fd, path, path_len) } pub(crate) fn fd_pwrite( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_ciovec_t, MemoryType>, iovs_len: MemoryOffset, offset: __wasi_filesize_t, nwritten: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_pwrite::(thread, fd, iovs, iovs_len, offset, nwritten) + super::fd_pwrite::(env, fd, iovs, iovs_len, offset, nwritten) } pub(crate) fn fd_read( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_iovec_t, MemoryType>, iovs_len: MemoryOffset, nread: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_read::(thread, fd, iovs, iovs_len, nread) + super::fd_read::(env, fd, iovs, iovs_len, nread) } pub(crate) fn fd_readdir( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, buf: WasmPtr, buf_len: MemoryOffset, cookie: __wasi_dircookie_t, bufused: WasmPtr, ) -> __wasi_errno_t { - super::fd_readdir::(thread, fd, buf, buf_len, cookie, bufused) + super::fd_readdir::(env, fd, buf, buf_len, cookie, bufused) } -pub(crate) fn fd_renumber( - thread: &WasiThread, - from: __wasi_fd_t, - to: __wasi_fd_t, -) -> __wasi_errno_t { - super::fd_renumber(thread, from, to) +pub(crate) fn fd_renumber(env: &WasiEnv, from: __wasi_fd_t, to: __wasi_fd_t) -> __wasi_errno_t { + super::fd_renumber(env, from, to) } pub(crate) fn fd_seek( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: __wasi_filedelta_t, whence: __wasi_whence_t, newoffset: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_seek::(thread, fd, offset, whence, newoffset) + super::fd_seek::(env, fd, offset, whence, newoffset) } -pub(crate) fn fd_sync(thread: &WasiThread, fd: __wasi_fd_t) -> __wasi_errno_t { - super::fd_sync(thread, fd) +pub(crate) fn fd_sync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t { + super::fd_sync(env, fd) } pub(crate) fn fd_tell( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, offset: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_tell::(thread, fd, offset) + super::fd_tell::(env, fd, offset) } pub(crate) fn fd_write( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, iovs: WasmPtr<__wasi_ciovec_t, MemoryType>, iovs_len: MemoryOffset, nwritten: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::fd_write::(thread, fd, iovs, iovs_len, nwritten) + super::fd_write::(env, fd, iovs, iovs_len, nwritten) } pub(crate) fn path_create_directory( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_create_directory::(thread, fd, path, path_len) + super::path_create_directory::(env, fd, path, path_len) } pub(crate) fn path_filestat_get( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_lookupflags_t, path: WasmPtr, path_len: MemoryOffset, buf: WasmPtr<__wasi_filestat_t, MemoryType>, ) -> __wasi_errno_t { - super::path_filestat_get::(thread, fd, flags, path, path_len, buf) + super::path_filestat_get::(env, fd, flags, path, path_len, buf) } pub(crate) fn path_filestat_set_times( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, flags: __wasi_lookupflags_t, path: WasmPtr, @@ -264,12 +260,12 @@ pub(crate) fn path_filestat_set_times( fst_flags: __wasi_fstflags_t, ) -> __wasi_errno_t { super::path_filestat_set_times::( - thread, fd, flags, path, path_len, st_atim, st_mtim, fst_flags, + env, fd, flags, path, path_len, st_atim, st_mtim, fst_flags, ) } pub(crate) fn path_link( - thread: &WasiThread, + env: &WasiEnv, old_fd: __wasi_fd_t, old_flags: __wasi_lookupflags_t, old_path: WasmPtr, @@ -279,7 +275,7 @@ pub(crate) fn path_link( new_path_len: MemoryOffset, ) -> __wasi_errno_t { super::path_link::( - thread, + env, old_fd, old_flags, old_path, @@ -291,7 +287,7 @@ pub(crate) fn path_link( } pub(crate) fn path_open( - thread: &WasiThread, + env: &WasiEnv, dirfd: __wasi_fd_t, dirflags: __wasi_lookupflags_t, path: WasmPtr, @@ -303,7 +299,7 @@ pub(crate) fn path_open( fd: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { super::path_open::( - thread, + env, dirfd, dirflags, path, @@ -317,7 +313,7 @@ pub(crate) fn path_open( } pub(crate) fn path_readlink( - thread: &WasiThread, + env: &WasiEnv, dir_fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, @@ -325,20 +321,20 @@ pub(crate) fn path_readlink( buf_len: MemoryOffset, buf_used: WasmPtr, ) -> __wasi_errno_t { - super::path_readlink::(thread, dir_fd, path, path_len, buf, buf_len, buf_used) + super::path_readlink::(env, dir_fd, path, path_len, buf, buf_len, buf_used) } pub(crate) fn path_remove_directory( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_remove_directory::(thread, fd, path, path_len) + super::path_remove_directory::(env, fd, path, path_len) } pub(crate) fn path_rename( - thread: &WasiThread, + env: &WasiEnv, old_fd: __wasi_fd_t, old_path: WasmPtr, old_path_len: MemoryOffset, @@ -347,7 +343,7 @@ pub(crate) fn path_rename( new_path_len: MemoryOffset, ) -> __wasi_errno_t { super::path_rename::( - thread, + env, old_fd, old_path, old_path_len, @@ -358,162 +354,159 @@ pub(crate) fn path_rename( } pub(crate) fn path_symlink( - thread: &WasiThread, + env: &WasiEnv, old_path: WasmPtr, old_path_len: MemoryOffset, fd: __wasi_fd_t, new_path: WasmPtr, new_path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_symlink::(thread, old_path, old_path_len, fd, new_path, new_path_len) + super::path_symlink::(env, old_path, old_path_len, fd, new_path, new_path_len) } pub(crate) fn path_unlink_file( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::path_unlink_file::(thread, fd, path, path_len) + super::path_unlink_file::(env, fd, path, path_len) } pub(crate) fn poll_oneoff( - thread: &WasiThread, + env: &WasiEnv, in_: WasmPtr<__wasi_subscription_t, MemoryType>, out_: WasmPtr<__wasi_event_t, MemoryType>, nsubscriptions: MemoryOffset, nevents: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::poll_oneoff::(thread, in_, out_, nsubscriptions, nevents) + super::poll_oneoff::(env, in_, out_, nsubscriptions, nevents) } -pub(crate) fn proc_exit(thread: &WasiThread, code: __wasi_exitcode_t) -> Result<(), WasiError> { - super::proc_exit(thread, code) +pub(crate) fn proc_exit(env: &WasiEnv, code: __wasi_exitcode_t) -> Result<(), WasiError> { + super::proc_exit(env, code) } -pub(crate) fn proc_raise(thread: &WasiThread, sig: __wasi_signal_t) -> __wasi_errno_t { - super::proc_raise(thread, sig) +pub(crate) fn proc_raise(env: &WasiEnv, sig: __wasi_signal_t) -> __wasi_errno_t { + super::proc_raise(env, sig) } pub(crate) fn random_get( - thread: &WasiThread, + env: &WasiEnv, buf: WasmPtr, buf_len: MemoryOffset, ) -> __wasi_errno_t { - super::random_get::(thread, buf, buf_len) + super::random_get::(env, buf, buf_len) } pub(crate) fn fd_dup( - thread: &WasiThread, + env: &WasiEnv, fd: __wasi_fd_t, ret_fd: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_dup::(thread, fd, ret_fd) + super::fd_dup::(env, fd, ret_fd) } pub(crate) fn fd_event( - thread: &WasiThread, + env: &WasiEnv, initial_val: u64, flags: __wasi_eventfdflags, ret_fd: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_event(thread, initial_val, flags, ret_fd) + super::fd_event(env, initial_val, flags, ret_fd) } pub(crate) fn fd_pipe( - thread: &WasiThread, + env: &WasiEnv, ro_fd1: WasmPtr<__wasi_fd_t, MemoryType>, ro_fd2: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::fd_pipe::(thread, ro_fd1, ro_fd2) + super::fd_pipe::(env, ro_fd1, ro_fd2) } pub(crate) fn tty_get( - thread: &WasiThread, + env: &WasiEnv, tty_state: WasmPtr<__wasi_tty_t, MemoryType>, ) -> __wasi_errno_t { - super::tty_get::(thread, tty_state) + super::tty_get::(env, tty_state) } pub(crate) fn tty_set( - thread: &WasiThread, + env: &WasiEnv, tty_state: WasmPtr<__wasi_tty_t, MemoryType>, ) -> __wasi_errno_t { - super::tty_set::(thread, tty_state) + super::tty_set::(env, tty_state) } pub(crate) fn getcwd( - thread: &WasiThread, + env: &WasiEnv, path: WasmPtr, path_len: WasmPtr, ) -> __wasi_errno_t { - super::getcwd::(thread, path, path_len) + super::getcwd::(env, path, path_len) } pub(crate) fn chdir( - thread: &WasiThread, + env: &WasiEnv, path: WasmPtr, path_len: MemoryOffset, ) -> __wasi_errno_t { - super::chdir::(thread, path, path_len) + super::chdir::(env, path, path_len) } pub(crate) fn thread_spawn( - thread: &WasiThread, + env: &WasiEnv, method: WasmPtr, method_len: MemoryOffset, user_data: u64, reactor: __wasi_bool_t, ret_tid: WasmPtr<__wasi_tid_t, MemoryType>, ) -> __wasi_errno_t { - super::thread_spawn::(thread, method, method_len, user_data, reactor, ret_tid) + super::thread_spawn::(env, method, method_len, user_data, reactor, ret_tid) } pub(crate) fn thread_sleep( - thread: &WasiThread, + env: &WasiEnv, duration: __wasi_timestamp_t, ) -> Result<__wasi_errno_t, WasiError> { - super::thread_sleep(thread, duration) + super::thread_sleep(env, duration) } pub(crate) fn thread_id( - thread: &WasiThread, + env: &WasiEnv, ret_tid: WasmPtr<__wasi_tid_t, MemoryType>, ) -> __wasi_errno_t { - super::thread_id::(thread, ret_tid) + super::thread_id::(env, ret_tid) } -pub(crate) fn thread_join(thread: &WasiThread, tid: __wasi_tid_t) -> __wasi_errno_t { - super::thread_join(thread, tid) +pub(crate) fn thread_join(env: &WasiEnv, tid: __wasi_tid_t) -> Result<__wasi_errno_t, WasiError> { + super::thread_join(env, tid) } pub(crate) fn thread_parallelism( - thread: &WasiThread, + env: &WasiEnv, ret_parallelism: WasmPtr, ) -> __wasi_errno_t { - super::thread_parallelism::(thread, ret_parallelism) + super::thread_parallelism::(env, ret_parallelism) } pub(crate) fn thread_exit( - thread: &WasiThread, + env: &WasiEnv, exitcode: __wasi_exitcode_t, ) -> Result<__wasi_errno_t, WasiError> { - super::thread_exit(thread, exitcode) + super::thread_exit(env, exitcode) } -pub(crate) fn sched_yield(thread: &WasiThread) -> Result<__wasi_errno_t, WasiError> { - super::sched_yield(thread) +pub(crate) fn sched_yield(env: &WasiEnv) -> Result<__wasi_errno_t, WasiError> { + super::sched_yield(env) } -pub(crate) fn getpid( - thread: &WasiThread, - ret_pid: WasmPtr<__wasi_pid_t, MemoryType>, -) -> __wasi_errno_t { - super::getpid::(thread, ret_pid) +pub(crate) fn getpid(env: &WasiEnv, ret_pid: WasmPtr<__wasi_pid_t, MemoryType>) -> __wasi_errno_t { + super::getpid::(env, ret_pid) } -pub(crate) fn bus_spawn_local( - thread: &WasiThread, +pub(crate) fn process_spawn( + env: &WasiEnv, name: WasmPtr, name_len: MemoryOffset, chroot: __wasi_bool_t, @@ -527,9 +520,9 @@ pub(crate) fn bus_spawn_local( working_dir: WasmPtr, working_dir_len: MemoryOffset, ret_handles: WasmPtr<__wasi_bus_handles_t, MemoryType>, -) -> __wasi_errno_t { - super::bus_spawn_local::( - thread, +) -> __bus_errno_t { + super::process_spawn::( + env, name, name_len, chroot, @@ -546,56 +539,47 @@ pub(crate) fn bus_spawn_local( ) } -pub(crate) fn bus_spawn_remote( - thread: &WasiThread, +pub(crate) fn bus_open_local( + env: &WasiEnv, name: WasmPtr, name_len: MemoryOffset, - chroot: __wasi_bool_t, - args: WasmPtr, - args_len: MemoryOffset, - preopen: WasmPtr, - preopen_len: MemoryOffset, - working_dir: WasmPtr, - working_dir_len: MemoryOffset, - stdin: __wasi_stdiomode_t, - stdout: __wasi_stdiomode_t, - stderr: __wasi_stdiomode_t, + reuse: __wasi_bool_t, + ret_bid: WasmPtr<__wasi_bid_t, MemoryType>, +) -> __bus_errno_t { + super::bus_open_local::(env, name, name_len, reuse, ret_bid) +} + +pub(crate) fn bus_open_remote( + env: &WasiEnv, + name: WasmPtr, + name_len: MemoryOffset, + reuse: __wasi_bool_t, instance: WasmPtr, instance_len: MemoryOffset, token: WasmPtr, token_len: MemoryOffset, - ret_handles: WasmPtr<__wasi_bus_handles_t, MemoryType>, -) -> __wasi_errno_t { - super::bus_spawn_remote::( - thread, + ret_bid: WasmPtr<__wasi_bid_t, MemoryType>, +) -> __bus_errno_t { + super::bus_open_remote::( + env, name, name_len, - chroot, - args, - args_len, - preopen, - preopen_len, - working_dir, - working_dir_len, - stdin, - stdout, - stderr, + reuse, instance, instance_len, token, token_len, - ret_handles, + ret_bid, ) } -pub(crate) fn bus_close(thread: &WasiThread, bid: __wasi_bid_t) -> __wasi_errno_t { - super::bus_close(thread, bid) +pub(crate) fn bus_close(env: &WasiEnv, bid: __wasi_bid_t) -> __bus_errno_t { + super::bus_close(env, bid) } -pub(crate) fn bus_invoke( - thread: &WasiThread, +pub(crate) fn bus_call( + env: &WasiEnv, bid: __wasi_bid_t, - cid: WasmPtr<__wasi_option_cid_t, MemoryType>, keep_alive: __wasi_bool_t, topic: WasmPtr, topic_len: MemoryOffset, @@ -603,180 +587,165 @@ pub(crate) fn bus_invoke( buf: WasmPtr, buf_len: MemoryOffset, ret_cid: WasmPtr<__wasi_cid_t, MemoryType>, -) -> __wasi_errno_t { - super::bus_invoke::( - thread, bid, cid, keep_alive, topic, topic_len, format, buf, buf_len, ret_cid, +) -> __bus_errno_t { + super::bus_call::( + env, bid, keep_alive, topic, topic_len, format, buf, buf_len, ret_cid, ) } -pub(crate) fn bus_fault( - thread: &WasiThread, - cid: __wasi_cid_t, - fault: __bus_errno_t, -) -> __wasi_errno_t { - super::bus_fault(thread, cid, fault) -} - -pub(crate) fn bus_drop(thread: &WasiThread, cid: __wasi_cid_t) -> __wasi_errno_t { - super::bus_drop(thread, cid) -} - -pub(crate) fn bus_reply( - thread: &WasiThread, - cid: __wasi_cid_t, - format: __wasi_busdataformat_t, - buf: WasmPtr, - buf_len: MemoryOffset, -) -> __wasi_errno_t { - super::bus_reply::(thread, cid, format, buf, buf_len) -} - -pub(crate) fn bus_callback( - thread: &WasiThread, - cid: __wasi_cid_t, +pub(crate) fn bus_subcall( + env: &WasiEnv, + parent: __wasi_cid_t, + keep_alive: __wasi_bool_t, topic: WasmPtr, topic_len: MemoryOffset, format: __wasi_busdataformat_t, buf: WasmPtr, buf_len: MemoryOffset, -) -> __wasi_errno_t { - super::bus_callback::(thread, cid, topic, topic_len, format, buf, buf_len) -} - -pub(crate) fn bus_listen( - thread: &WasiThread, - parent: WasmPtr<__wasi_option_cid_t, MemoryType>, - topic: WasmPtr, - topic_len: MemoryOffset, -) -> __wasi_errno_t { - super::bus_listen::(thread, parent, topic, topic_len) + ret_cid: WasmPtr<__wasi_cid_t, MemoryType>, +) -> __bus_errno_t { + super::bus_subcall::( + env, parent, keep_alive, topic, topic_len, format, buf, buf_len, ret_cid, + ) } pub(crate) fn bus_poll( - thread: &WasiThread, - bid: WasmPtr<__wasi_option_bid_t, MemoryType>, - timeout: WasmPtr<__wasi_timestamp_t, MemoryType>, + env: &WasiEnv, + timeout: __wasi_timestamp_t, events: WasmPtr, nevents: MemoryOffset, + malloc: WasmPtr, + malloc_len: MemoryOffset, ret_nevents: WasmPtr, -) -> __wasi_errno_t { - super::bus_poll::(thread, bid, timeout, events, nevents, ret_nevents) +) -> __bus_errno_t { + super::bus_poll::( + env, + timeout, + events, + nevents, + malloc, + malloc_len, + ret_nevents, + ) } -pub(crate) fn bus_poll_data( - thread: &WasiThread, - bid: WasmPtr<__wasi_option_bid_t, MemoryType>, - timeout: WasmPtr<__wasi_timestamp_t, MemoryType>, - topic: WasmPtr, - topic_len: MemoryOffset, +pub(crate) fn call_reply( + env: &WasiEnv, + cid: __wasi_cid_t, + format: __wasi_busdataformat_t, buf: WasmPtr, buf_len: MemoryOffset, - ret_evt: WasmPtr<__wasi_busevent_data_t, MemoryType>, -) -> __wasi_errno_t { - super::bus_poll_data::( - thread, bid, timeout, topic, topic_len, buf, buf_len, ret_evt, - ) +) -> __bus_errno_t { + super::call_reply::(env, cid, format, buf, buf_len) +} + +pub(crate) fn call_fault(env: &WasiEnv, cid: __wasi_cid_t, fault: __bus_errno_t) -> __bus_errno_t { + super::call_fault(env, cid, fault) +} + +pub(crate) fn call_close(env: &WasiEnv, cid: __wasi_cid_t) -> __bus_errno_t { + super::call_close(env, cid) } pub(crate) fn port_bridge( - thread: &WasiThread, + env: &WasiEnv, network: WasmPtr, network_len: MemoryOffset, token: WasmPtr, token_len: MemoryOffset, security: __wasi_streamsecurity_t, ) -> __wasi_errno_t { - super::port_bridge::(thread, network, network_len, token, token_len, security) + super::port_bridge::(env, network, network_len, token, token_len, security) } -pub(crate) fn port_unbridge(thread: &WasiThread) -> __wasi_errno_t { - super::port_unbridge(thread) +pub(crate) fn port_unbridge(env: &WasiEnv) -> __wasi_errno_t { + super::port_unbridge(env) } -pub(crate) fn port_dhcp_acquire(thread: &WasiThread) -> __wasi_errno_t { - super::port_dhcp_acquire(thread) +pub(crate) fn port_dhcp_acquire(env: &WasiEnv) -> __wasi_errno_t { + super::port_dhcp_acquire(env) } pub(crate) fn port_addr_add( - thread: &WasiThread, + env: &WasiEnv, addr: WasmPtr<__wasi_cidr_t, MemoryType>, ) -> __wasi_errno_t { - super::port_addr_add::(thread, addr) + super::port_addr_add::(env, addr) } pub(crate) fn port_addr_remove( - thread: &WasiThread, + env: &WasiEnv, addr: WasmPtr<__wasi_addr_t, MemoryType>, ) -> __wasi_errno_t { - super::port_addr_remove::(thread, addr) + super::port_addr_remove::(env, addr) } -pub(crate) fn port_addr_clear(thread: &WasiThread) -> __wasi_errno_t { - super::port_addr_clear(thread) +pub(crate) fn port_addr_clear(env: &WasiEnv) -> __wasi_errno_t { + super::port_addr_clear(env) } pub(crate) fn port_addr_list( - thread: &WasiThread, + env: &WasiEnv, addrs: WasmPtr<__wasi_cidr_t, MemoryType>, naddrs: WasmPtr, ) -> __wasi_errno_t { - super::port_addr_list::(thread, addrs, naddrs) + super::port_addr_list::(env, addrs, naddrs) } pub(crate) fn port_mac( - thread: &WasiThread, + env: &WasiEnv, ret_mac: WasmPtr<__wasi_hardwareaddress_t, MemoryType>, ) -> __wasi_errno_t { - super::port_mac::(thread, ret_mac) + super::port_mac::(env, ret_mac) } pub(crate) fn port_gateway_set( - thread: &WasiThread, + env: &WasiEnv, ip: WasmPtr<__wasi_addr_t, MemoryType>, ) -> __wasi_errno_t { - super::port_gateway_set::(thread, ip) + super::port_gateway_set::(env, ip) } pub(crate) fn port_route_add( - thread: &WasiThread, + env: &WasiEnv, cidr: WasmPtr<__wasi_cidr_t, MemoryType>, via_router: WasmPtr<__wasi_addr_t, MemoryType>, preferred_until: WasmPtr<__wasi_option_timestamp_t, MemoryType>, expires_at: WasmPtr<__wasi_option_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::port_route_add::(thread, cidr, via_router, preferred_until, expires_at) + super::port_route_add::(env, cidr, via_router, preferred_until, expires_at) } pub(crate) fn port_route_remove( - thread: &WasiThread, + env: &WasiEnv, ip: WasmPtr<__wasi_addr_t, MemoryType>, ) -> __wasi_errno_t { - super::port_route_remove::(thread, ip) + super::port_route_remove::(env, ip) } -pub(crate) fn port_route_clear(thread: &WasiThread) -> __wasi_errno_t { - super::port_route_clear(thread) +pub(crate) fn port_route_clear(env: &WasiEnv) -> __wasi_errno_t { + super::port_route_clear(env) } pub(crate) fn port_route_list( - thread: &WasiThread, + env: &WasiEnv, routes: WasmPtr<__wasi_route_t, MemoryType>, nroutes: WasmPtr, ) -> __wasi_errno_t { - super::port_route_list::(thread, routes, nroutes) + super::port_route_list::(env, routes, nroutes) } pub(crate) fn ws_connect( - thread: &WasiThread, + env: &WasiEnv, url: WasmPtr, url_len: MemoryOffset, ret_sock: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::ws_connect::(thread, url, url_len, ret_sock) + super::ws_connect::(env, url, url_len, ret_sock) } pub(crate) fn http_request( - thread: &WasiThread, + env: &WasiEnv, url: WasmPtr, url_len: MemoryOffset, method: WasmPtr, @@ -787,7 +756,7 @@ pub(crate) fn http_request( ret_handles: WasmPtr<__wasi_http_handles_t, MemoryType>, ) -> __wasi_errno_t { super::http_request::( - thread, + env, url, url_len, method, @@ -800,7 +769,7 @@ pub(crate) fn http_request( } pub(crate) fn http_status( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, status: WasmPtr<__wasi_http_status_t, MemoryType>, status_text: WasmPtr, @@ -808,169 +777,169 @@ pub(crate) fn http_status( headers: WasmPtr, headers_len: WasmPtr, ) -> __wasi_errno_t { - super::http_status::(thread, sock, status) + super::http_status::(env, sock, status) } pub(crate) fn sock_status( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ret_status: WasmPtr<__wasi_sockstatus_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_status::(thread, sock, ret_status) + super::sock_status::(env, sock, ret_status) } pub(crate) fn sock_addr_local( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ret_addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_addr_local::(thread, sock, ret_addr) + super::sock_addr_local::(env, sock, ret_addr) } pub(crate) fn sock_addr_peer( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ro_addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_addr_peer::(thread, sock, ro_addr) + super::sock_addr_peer::(env, sock, ro_addr) } pub(crate) fn sock_open( - thread: &WasiThread, + env: &WasiEnv, af: __wasi_addressfamily_t, ty: __wasi_socktype_t, pt: __wasi_sockproto_t, ro_sock: WasmPtr<__wasi_fd_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_open::(thread, af, ty, pt, ro_sock) + super::sock_open::(env, af, ty, pt, ro_sock) } pub(crate) fn sock_set_opt_flag( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, flag: __wasi_bool_t, ) -> __wasi_errno_t { - super::sock_set_opt_flag(thread, sock, opt, flag) + super::sock_set_opt_flag(env, sock, opt, flag) } pub(crate) fn sock_get_opt_flag( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, ret_flag: WasmPtr<__wasi_bool_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_get_opt_flag::(thread, sock, opt, ret_flag) + super::sock_get_opt_flag::(env, sock, opt, ret_flag) } pub fn sock_set_opt_time( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, time: WasmPtr<__wasi_option_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_set_opt_time(thread, sock, opt, time) + super::sock_set_opt_time(env, sock, opt, time) } pub fn sock_get_opt_time( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, ret_time: WasmPtr<__wasi_option_timestamp_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_get_opt_time(thread, sock, opt, ret_time) + super::sock_get_opt_time(env, sock, opt, ret_time) } pub fn sock_set_opt_size( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, size: __wasi_filesize_t, ) -> __wasi_errno_t { - super::sock_set_opt_size(thread, sock, opt, size) + super::sock_set_opt_size(env, sock, opt, size) } pub fn sock_get_opt_size( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, opt: __wasi_sockoption_t, ret_size: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_get_opt_size(thread, sock, opt, ret_size) + super::sock_get_opt_size(env, sock, opt, ret_size) } pub(crate) fn sock_join_multicast_v4( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip4_t, MemoryType>, iface: WasmPtr<__wasi_addr_ip4_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_join_multicast_v4::(thread, sock, multiaddr, iface) + super::sock_join_multicast_v4::(env, sock, multiaddr, iface) } pub(crate) fn sock_leave_multicast_v4( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip4_t, MemoryType>, iface: WasmPtr<__wasi_addr_ip4_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_leave_multicast_v4::(thread, sock, multiaddr, iface) + super::sock_leave_multicast_v4::(env, sock, multiaddr, iface) } pub(crate) fn sock_join_multicast_v6( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip6_t, MemoryType>, iface: u32, ) -> __wasi_errno_t { - super::sock_join_multicast_v6::(thread, sock, multiaddr, iface) + super::sock_join_multicast_v6::(env, sock, multiaddr, iface) } pub(crate) fn sock_leave_multicast_v6( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, multiaddr: WasmPtr<__wasi_addr_ip6_t, MemoryType>, iface: u32, ) -> __wasi_errno_t { - super::sock_leave_multicast_v6::(thread, sock, multiaddr, iface) + super::sock_leave_multicast_v6::(env, sock, multiaddr, iface) } pub(crate) fn sock_bind( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_bind::(thread, sock, addr) + super::sock_bind::(env, sock, addr) } pub(crate) fn sock_listen( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, backlog: MemoryOffset, ) -> __wasi_errno_t { - super::sock_listen::(thread, sock, backlog) + super::sock_listen::(env, sock, backlog) } pub(crate) fn sock_accept( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, fd_flags: __wasi_fdflags_t, ro_fd: WasmPtr<__wasi_fd_t, MemoryType>, ro_addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { - super::sock_accept::(thread, sock, fd_flags, ro_fd, ro_addr) + super::sock_accept::(env, sock, fd_flags, ro_fd, ro_addr) } pub(crate) fn sock_connect( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> __wasi_errno_t { - super::sock_connect::(thread, sock, addr) + super::sock_connect::(env, sock, addr) } pub(crate) fn sock_recv( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ri_data: WasmPtr<__wasi_iovec_t, MemoryType>, ri_data_len: MemoryOffset, @@ -979,7 +948,7 @@ pub(crate) fn sock_recv( ro_flags: WasmPtr<__wasi_roflags_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { super::sock_recv::( - thread, + env, sock, ri_data, ri_data_len, @@ -990,7 +959,7 @@ pub(crate) fn sock_recv( } pub(crate) fn sock_recv_from( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, ri_data: WasmPtr<__wasi_iovec_t, MemoryType>, ri_data_len: MemoryOffset, @@ -1000,7 +969,7 @@ pub(crate) fn sock_recv_from( ro_addr: WasmPtr<__wasi_addr_port_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { super::sock_recv_from::( - thread, + env, sock, ri_data, ri_data_len, @@ -1012,18 +981,18 @@ pub(crate) fn sock_recv_from( } pub(crate) fn sock_send( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, si_data: WasmPtr<__wasi_ciovec_t, MemoryType>, si_data_len: MemoryOffset, si_flags: __wasi_siflags_t, ret_data_len: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { - super::sock_send::(thread, sock, si_data, si_data_len, si_flags, ret_data_len) + super::sock_send::(env, sock, si_data, si_data_len, si_flags, ret_data_len) } pub(crate) fn sock_send_to( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, si_data: WasmPtr<__wasi_ciovec_t, MemoryType>, si_data_len: MemoryOffset, @@ -1032,7 +1001,7 @@ pub(crate) fn sock_send_to( ret_data_len: WasmPtr, ) -> Result<__wasi_errno_t, WasiError> { super::sock_send_to::( - thread, + env, sock, si_data, si_data_len, @@ -1043,26 +1012,26 @@ pub(crate) fn sock_send_to( } pub(crate) fn sock_send_file( - thread: &WasiThread, + env: &WasiEnv, out_fd: __wasi_fd_t, in_fd: __wasi_fd_t, offset: __wasi_filesize_t, count: __wasi_filesize_t, ret_sent: WasmPtr<__wasi_filesize_t, MemoryType>, ) -> Result<__wasi_errno_t, WasiError> { - unsafe { super::sock_send_file::(thread, out_fd, in_fd, offset, count, ret_sent) } + unsafe { super::sock_send_file::(env, out_fd, in_fd, offset, count, ret_sent) } } pub(crate) fn sock_shutdown( - thread: &WasiThread, + env: &WasiEnv, sock: __wasi_fd_t, how: __wasi_sdflags_t, ) -> __wasi_errno_t { - super::sock_shutdown(thread, sock, how) + super::sock_shutdown(env, sock, how) } pub(crate) fn resolve( - thread: &WasiThread, + env: &WasiEnv, host: WasmPtr, host_len: MemoryOffset, port: u16, @@ -1070,5 +1039,5 @@ pub(crate) fn resolve( nips: MemoryOffset, ret_nips: WasmPtr, ) -> __wasi_errno_t { - super::resolve::(thread, host, host_len, port, ips, nips, ret_nips) + super::resolve::(env, host, host_len, port, ips, nips, ret_nips) } diff --git a/lib/wasi/src/utils.rs b/lib/wasi/src/utils.rs index a0ce908b09d..77222889179 100644 --- a/lib/wasi/src/utils.rs +++ b/lib/wasi/src/utils.rs @@ -9,6 +9,18 @@ pub fn is_wasi_module(module: &Module) -> bool { get_wasi_version(module, false).is_some() } +#[allow(dead_code)] +/// Returns if the module is WASIX or not +pub fn is_wasix_module(module: &Module) -> bool { + match get_wasi_versions(module, false).ok_or(false) { + Ok(wasi_versions) => { + wasi_versions.contains(&WasiVersion::Wasix32v1) + || wasi_versions.contains(&WasiVersion::Wasix64v1) + } + Err(_) => false, + } +} + pub fn map_io_err(err: std::io::Error) -> __wasi_errno_t { use std::io::ErrorKind; match err.kind() { diff --git a/lib/wasi/tests/stdio.rs b/lib/wasi/tests/stdio.rs index fcbbf99e733..bb7c0e62cc7 100644 --- a/lib/wasi/tests/stdio.rs +++ b/lib/wasi/tests/stdio.rs @@ -1,7 +1,5 @@ use std::io::{Read, Write}; -#[cfg(feature = "js")] -use wasm_bindgen_test::*; use wasmer::{Instance, Module, Store}; use wasmer_wasi::{Pipe, WasiState}; @@ -24,6 +22,8 @@ mod sys { #[cfg(feature = "js")] mod js { + use wasm_bindgen_test::*; + #[wasm_bindgen_test] fn test_stdout() { super::test_stdout() @@ -74,15 +74,14 @@ fn test_stdout() { // Create the `WasiEnv`. let mut stdout = Pipe::default(); - let wasi_env = WasiState::new("command-name") + let mut wasi_env = WasiState::new("command-name") .args(&["Gordon"]) .stdout(Box::new(stdout.clone())) .finalize() .unwrap(); // Generate an `ImportObject`. - let mut wasi_thread = wasi_env.new_thread(); - let import_object = wasi_thread.import_object(&module).unwrap(); + let import_object = wasi_env.import_object(&module).unwrap(); // Let's instantiate the module with the imports. let instance = Instance::new(&module, &import_object).unwrap(); @@ -117,14 +116,13 @@ fn test_env() { .env("TEST", "VALUE") .env("TEST2", "VALUE2"); // panic!("envs: {:?}", wasi_state_builder.envs); - let wasi_env = wasi_state_builder + let mut wasi_env = wasi_state_builder .stdout(Box::new(stdout.clone())) .finalize() .unwrap(); // Generate an `ImportObject`. - let mut wasi_thread = wasi_env.new_thread(); - let import_object = wasi_thread.import_object(&module).unwrap(); + let import_object = wasi_env.import_object(&module).unwrap(); // Let's instantiate the module with the imports. let instance = Instance::new(&module, &import_object).unwrap(); @@ -145,7 +143,7 @@ fn test_stdin() { // Create the `WasiEnv`. let mut stdin = Pipe::new(); - let wasi_env = WasiState::new("command-name") + let mut wasi_env = WasiState::new("command-name") .stdin(Box::new(stdin.clone())) .finalize() .unwrap(); @@ -155,8 +153,7 @@ fn test_stdin() { stdin.write(&buf[..]).unwrap(); // Generate an `ImportObject`. - let mut wasi_thread = wasi_env.new_thread(); - let import_object = wasi_thread.import_object(&module).unwrap(); + let import_object = wasi_env.import_object(&module).unwrap(); // Let's instantiate the module with the imports. let instance = Instance::new(&module, &import_object).unwrap(); diff --git a/tests/integration/cli/tests/staticlib_engine_test_c_source.c b/tests/integration/cli/tests/staticlib_engine_test_c_source.c index 68a79f1a49d..0a3bc08878c 100644 --- a/tests/integration/cli/tests/staticlib_engine_test_c_source.c +++ b/tests/integration/cli/tests/staticlib_engine_test_c_source.c @@ -8,11 +8,13 @@ static void print_wasmer_error() { int error_len = wasmer_last_error_length(); - printf("Error len: `%d`\n", error_len); - char *error_str = (char *)malloc(error_len); - wasmer_last_error_message(error_str, error_len); - printf("Error str: `%s`\n", error_str); - free(error_str); + if (error_len > 0) { + printf("Error len: `%d`\n", error_len); + char *error_str = (char *)malloc(error_len); + wasmer_last_error_message(error_str, error_len); + printf("Error str: `%s`\n", error_str); + free(error_str); + } } int main() { diff --git a/tests/lib/wast/src/wasi_wast.rs b/tests/lib/wast/src/wasi_wast.rs index 42cb04e36d1..d8e2d6d7a9e 100644 --- a/tests/lib/wast/src/wasi_wast.rs +++ b/tests/lib/wast/src/wasi_wast.rs @@ -7,7 +7,7 @@ use wasmer::{Imports, Instance, Module, Store}; use wasmer_vfs::{host_fs, mem_fs, FileSystem}; use wasmer_wasi::types::{__wasi_filesize_t, __wasi_timestamp_t}; use wasmer_wasi::{ - generate_import_object_from_thread, get_wasi_version, FsError, Pipe, VirtualFile, WasiEnv, + generate_import_object_from_env, get_wasi_version, FsError, Pipe, VirtualFile, WasiEnv, WasiState, WasiVersion, }; use wast::parser::{self, Parse, ParseBuffer, Parser}; @@ -90,7 +90,7 @@ impl<'a> WasiTest<'a> { if let Some(stdin) = &self.stdin { let state = env.state(); - let mut wasi_stdin = state.stdin().unwrap(); + let mut wasi_stdin = state.stdin().unwrap().unwrap(); // Then we can write to it! write!(wasi_stdin, "{}", stdin.stream)?; } @@ -227,9 +227,8 @@ impl<'a> WasiTest<'a> { /// Get the correct WASI import object for the given module and set it up with the /// [`WasiEnv`]. fn get_imports(&self, store: &Store, module: &Module, env: WasiEnv) -> anyhow::Result { - let thread = env.new_thread(); let version = self.get_version(module)?; - Ok(generate_import_object_from_thread(store, thread, version)) + Ok(generate_import_object_from_env(store, env, version)) } }