diff --git a/.appveyor.yml b/.appveyor.yml index 8aaf485d6..47eb1bd22 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -31,8 +31,8 @@ install: # Install Rust # uncomment these lines if the cache is cleared, or if we must re-install rust for some reason - # - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - # - rustup-init.exe -yv --default-host %target% + - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe + - rustup-init.exe -yv --default-host %target% - set PATH=%PATH%;%USERPROFILE%\.cargo\bin - rustup default stable-%target% - rustup update diff --git a/examples/plugin-for-example.wasm b/examples/plugin-for-example.wasm index 4719a0b63..c87ae2081 100755 Binary files a/examples/plugin-for-example.wasm and b/examples/plugin-for-example.wasm differ diff --git a/examples/plugin-for-example/README.md b/examples/plugin-for-example/README.md index a405e250c..42bc3ac26 100644 --- a/examples/plugin-for-example/README.md +++ b/examples/plugin-for-example/README.md @@ -40,6 +40,4 @@ In this example, we instantiate a system with an extended (WASI)[wasi] ABI, allo Because the Rust WASI doesn't support the crate type of `cdylib`, we have to include a main function which we don't use. This is being discussed [here](https://github.com/WebAssembly/WASI/issues/24). -We call the main function to initialize WASI's libpreopen internal datastructures and have the module call back into the host to set swap out the modules implementation of stdout. The host then provides a wrapper around stdout, allowing the guest's writes to stdout to be formatted in a host-appropriate manner. - [wasi]: https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ diff --git a/examples/plugin-for-example/src/main.rs b/examples/plugin-for-example/src/main.rs index 7479e9bcf..9ee284aab 100644 --- a/examples/plugin-for-example/src/main.rs +++ b/examples/plugin-for-example/src/main.rs @@ -1,6 +1,5 @@ extern "C" { fn it_works() -> i32; - fn initialize(); } #[no_mangle] @@ -10,6 +9,4 @@ pub fn plugin_entrypoint(n: i32) -> i32 { result + n } -pub fn main() { - unsafe { initialize() }; -} +pub fn main() {} diff --git a/examples/plugin.rs b/examples/plugin.rs index 6d576914e..c4f1d2de3 100644 --- a/examples/plugin.rs +++ b/examples/plugin.rs @@ -103,7 +103,7 @@ impl WasiFile for LoggingWrapper { /// Called by the program when it wants to set itself up fn initialize(ctx: &mut Ctx) { - let state = state::get_wasi_state(ctx); + let state = unsafe { state::get_wasi_state(ctx) }; let wasi_file_inner = LoggingWrapper { wasm_module_name: "example module name".to_string(), }; @@ -127,17 +127,16 @@ fn main() { let custom_imports = imports! { "env" => { "it_works" => func!(it_works), - "initialize" => func!(initialize), }, }; // The WASI imports object contains all required import functions for a WASI module to run. // Extend this imports with our custom imports containing "it_works" function so that our custom wasm code may run. base_imports.extend(custom_imports); - let instance = + let mut instance = instantiate(&wasm_bytes[..], &base_imports).expect("failed to instantiate wasm module"); + // set up logging by replacing stdout + initialize(instance.context_mut()); - let main = instance.func::<(), ()>("_start").unwrap(); - main.call().expect("Could not initialize"); // get a reference to the function "plugin_entrypoint" which takes an i32 and returns an i32 let entry_point = instance.func::<(i32), i32>("plugin_entrypoint").unwrap(); // call the "entry_point" function in WebAssembly with the number "2" as the i32 argument diff --git a/lib/wasi/src/state.rs b/lib/wasi/src/state.rs index 051a46850..a7f36d232 100644 --- a/lib/wasi/src/state.rs +++ b/lib/wasi/src/state.rs @@ -25,8 +25,9 @@ pub const VIRTUAL_ROOT_FD: __wasi_fd_t = 4; pub const ALL_RIGHTS: __wasi_rights_t = 0x1FFFFFFF; /// Get WasiState from a Ctx -pub fn get_wasi_state(ctx: &mut Ctx) -> &mut WasiState { - unsafe { &mut *(ctx.data as *mut WasiState) } +/// This function is unsafe because it must be called on a WASI Ctx +pub unsafe fn get_wasi_state(ctx: &mut Ctx) -> &mut WasiState { + &mut *(ctx.data as *mut WasiState) } /// A completely aribtrary "big enough" number used as the upper limit for