From 43f7bb7ddcd4cef5e98c6baa508722144e3d743b Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Fri, 23 Jun 2023 00:33:10 +0200 Subject: [PATCH] implement the first half of zesterer's proposals and update wasmer --- Cargo.lock | 28 +++++++++++------------ common/state/Cargo.toml | 4 ++-- common/state/src/plugin/exports.rs | 22 +++++++++++------- common/state/src/plugin/memory_manager.rs | 10 ++++---- common/state/src/plugin/module.rs | 10 ++++---- 5 files changed, 39 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cfcf297ab5..7c9133c4cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7805,9 +7805,9 @@ dependencies = [ [[package]] name = "wasmer" -version = "4.0.0-beta.3" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d924829e1d14ca3b0ab2562ef7c958fd61ec3fdc6401fd09f20becf59c1f1d17" +checksum = "ea790bcdfb4e6e9d1e5ddf75b4699aac62b078fcc9f27f44e1748165ceea67bf" dependencies = [ "bytes", "cfg-if 1.0.0", @@ -7833,9 +7833,9 @@ dependencies = [ [[package]] name = "wasmer-compiler" -version = "4.0.0-beta.3" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "945e8cd172c68cf10fefcd40f9c97bd301ecc9bf0136972bf807474f1b5951de" +checksum = "f093937725e242e5529fed27e08ff836c011a9ecc22e6819fb818c2ac6ff5f88" dependencies = [ "backtrace", "cfg-if 1.0.0", @@ -7856,9 +7856,9 @@ dependencies = [ [[package]] name = "wasmer-compiler-cranelift" -version = "4.0.0-beta.3" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdce36e48ec8c70e34d150073d6214e52757474bb375c013f2ae0bac85800cce" +checksum = "3b27b1670d27158789ebe14e4da3902c72132174884a1c6a3533ce4fd9dd83db" dependencies = [ "cranelift-codegen", "cranelift-entity", @@ -7875,9 +7875,9 @@ dependencies = [ [[package]] name = "wasmer-derive" -version = "4.0.0-beta.3" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90277df86705920fefed23c2724ed652fa974af788f0983ca0493b51691a76bd" +checksum = "13ae8286cba2acb10065a4dac129c7c7f7bcd24acd6538555d96616eea16bc27" dependencies = [ "proc-macro-error", "proc-macro2 1.0.56", @@ -7887,9 +7887,9 @@ dependencies = [ [[package]] name = "wasmer-types" -version = "4.0.0-beta.3" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "918344b93266cf4efe56e6cd23f35c7325275524f534678145f7ead4b2b8c762" +checksum = "918d2f0bb5eaa95a80c06be33f21dee92f40f12cd0982da34490d121a99d244b" dependencies = [ "bytecheck", "enum-iterator 0.7.0", @@ -7903,9 +7903,9 @@ dependencies = [ [[package]] name = "wasmer-vm" -version = "4.0.0-beta.3" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e36cba570e0b2662ff52d40fe9a48fd368f6eb35183a84ce43f79bc51b9fd15" +checksum = "a1e000c2cbd4f9805427af5f3b3446574caf89ab3a1e66c2f3579fbde22b072b" dependencies = [ "backtrace", "cc", @@ -7930,9 +7930,9 @@ dependencies = [ [[package]] name = "wasmer-wasix-types" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13597c9c4febf4a6b0f36a8c76d17f90a6be7f65bc4667472a50a93121a6f2c5" +checksum = "7a4a519e8f0b878bb4cd2b1bc733235aa6c331b7b4857dd6e0ac3c9a36d942ae" dependencies = [ "anyhow", "bitflags 1.3.2", diff --git a/common/state/Cargo.toml b/common/state/Cargo.toml index ab09f5a468..1990611369 100644 --- a/common/state/Cargo.toml +++ b/common/state/Cargo.toml @@ -32,11 +32,11 @@ scopeguard = "1.1.0" serde = { workspace = true, optional = true } toml = { version = "0.7", optional = true } tar = { version = "0.4.37", optional = true } -wasmer = { version = "4.0.0-beta.3", optional = true, default-features = false, features = ["sys", "wat", "cranelift"] } +wasmer = { version = "4.0.0", optional = true, default-features = false, features = ["sys", "wat", "cranelift"] } bincode = { workspace = true, optional = true } plugin-api = { package = "veloren-plugin-api", path = "../../plugin/api", optional = true } timer-queue = "0.1.0" -wasmer-wasix-types = { version = "0.8.0", default-features = false } +wasmer-wasix-types = { version = "0.9.0", default-features = false } # Tweak running code #inline_tweak = { version = "1.0.8", features = ["release_tweak"] } diff --git a/common/state/src/plugin/exports.rs b/common/state/src/plugin/exports.rs index bd55ea46f9..e27faa14c4 100644 --- a/common/state/src/plugin/exports.rs +++ b/common/state/src/plugin/exports.rs @@ -35,7 +35,6 @@ fn print_impl( }) .and_then(|bytes| { std::str::from_utf8(bytes.as_slice()) - .map(|msg| tracing::info!("[{}]: {}", env.name, msg)) .map_err(|error| { tracing::error!( "Logging message from plugin {} failed with {}!", @@ -44,10 +43,12 @@ fn print_impl( ); wasmer_wasix_types::wasi::Errno::Inval }) + .map(|msg| tracing::info!("[{}]: {}", env.name, msg)) }) } #[derive(Clone, Copy)] +#[repr(C)] pub(crate) struct CioVec { buf: WasmPtr, buf_len: ::Offset, @@ -55,7 +56,13 @@ pub(crate) struct CioVec { // CioVec has no padding bytes, thus no action is necessary unsafe impl wasmer::ValueType for CioVec { - fn zero_padding_bytes(&self, _bytes: &mut [std::mem::MaybeUninit]) {} + fn zero_padding_bytes(&self, _bytes: &mut [std::mem::MaybeUninit]) { + const _: () = assert!( + core::mem::size_of::() + == core::mem::size_of::>() + + core::mem::size_of::<::Offset>() + ); + } } // fd_write(fd: fd, iovs: ciovec_array) -> Result @@ -67,7 +74,7 @@ pub(crate) fn wasi_fd_write( out_result: WasmPtr<::Offset, MemoryModel>, ) -> i32 { use wasmer_wasix_types::wasi::Errno; - if fd != 0 { + if fd != 1 && fd != 2 { Errno::Badf as i32 } else { let memory = env.data().memory().clone(); @@ -111,12 +118,11 @@ pub(crate) fn wasi_env_sizes_get( let memory = env.data().memory().clone(); let store = env.as_store_mut(); let mem = memory.view(&store); - if numptr.write(&mem, 0).is_err() { - return Errno::Memviolation as i32; - } - bytesptr + numptr .write(&mem, 0) - .map_or(Errno::Memviolation as i32, |()| Errno::Success as i32) + .and_then(|()| bytesptr.write(&mem, 0)) + .map(|()| Errno::Success) + .unwrap_or(Errno::Memviolation) as i32 } // proc_exit(rval: exitcode) diff --git a/common/state/src/plugin/memory_manager.rs b/common/state/src/plugin/memory_manager.rs index 6a7da95f86..2aafdccf9a 100644 --- a/common/state/src/plugin/memory_manager.rs +++ b/common/state/src/plugin/memory_manager.rs @@ -172,6 +172,8 @@ pub fn write_data_as_pointer( /// This function writes an raw bytes to WASM memory returning a pointer and /// a length. Will realloc the buffer is not wide enough +/// As this is often called with a length and an object it accepts to slices and +/// concatenates them pub fn write_bytes( store: &mut StoreMut, memory: &Memory, @@ -194,12 +196,8 @@ pub fn write_bytes( len as ::Offset, ) .and_then(|s| { - if !bytes.1.is_empty() { - s.subslice(0..bytes.0.len() as u64).write_slice(bytes.0)?; - s.subslice(bytes.0.len() as u64..len).write_slice(bytes.1) - } else { - s.write_slice(bytes.0) - } + s.subslice(0..bytes.0.len() as u64).write_slice(bytes.0)?; + s.subslice(bytes.0.len() as u64..len).write_slice(bytes.1) }) .map_err(|_| PluginModuleError::InvalidPointer)?; Ok((ptr, len)) diff --git a/common/state/src/plugin/module.rs b/common/state/src/plugin/module.rs index 1477c80c46..eb0fd53530 100644 --- a/common/state/src/plugin/module.rs +++ b/common/state/src/plugin/module.rs @@ -239,17 +239,17 @@ fn execute_raw( .call(&mut module.store.as_store_mut(), ptr, len) .map_err(PluginModuleError::RunFunction)?; - // The first 4 bytes correspond to the length of the result - let result_len: Vec = result_ptr + // The first 4/8 bytes correspond to the length of the result + let mut result_len = [0u8; std::mem::size_of::<::Offset>()]; + result_ptr .slice( &module.memory.view(&module.store.as_store_ref()), std::mem::size_of::<::Offset>() as ::Offset, ) - .and_then(|s| s.read_to_vec()) + .and_then(|s| s.read_slice(&mut result_len)) .map_err(|_| PluginModuleError::InvalidPointer)?; - let result_len = - ::Offset::from_le_bytes(result_len.try_into().unwrap()); + let result_len = ::Offset::from_le_bytes(result_len); // Read the result of the function with the pointer and the length let bytes = memory_manager::read_bytes(