implement the first half of zesterer's proposals and update wasmer

This commit is contained in:
Christof Petig 2023-06-23 00:33:10 +02:00
parent 5802f94daf
commit 43f7bb7ddc
5 changed files with 39 additions and 35 deletions

28
Cargo.lock generated
View File

@ -7805,9 +7805,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmer" name = "wasmer"
version = "4.0.0-beta.3" version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d924829e1d14ca3b0ab2562ef7c958fd61ec3fdc6401fd09f20becf59c1f1d17" checksum = "ea790bcdfb4e6e9d1e5ddf75b4699aac62b078fcc9f27f44e1748165ceea67bf"
dependencies = [ dependencies = [
"bytes", "bytes",
"cfg-if 1.0.0", "cfg-if 1.0.0",
@ -7833,9 +7833,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmer-compiler" name = "wasmer-compiler"
version = "4.0.0-beta.3" version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "945e8cd172c68cf10fefcd40f9c97bd301ecc9bf0136972bf807474f1b5951de" checksum = "f093937725e242e5529fed27e08ff836c011a9ecc22e6819fb818c2ac6ff5f88"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"cfg-if 1.0.0", "cfg-if 1.0.0",
@ -7856,9 +7856,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmer-compiler-cranelift" name = "wasmer-compiler-cranelift"
version = "4.0.0-beta.3" version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdce36e48ec8c70e34d150073d6214e52757474bb375c013f2ae0bac85800cce" checksum = "3b27b1670d27158789ebe14e4da3902c72132174884a1c6a3533ce4fd9dd83db"
dependencies = [ dependencies = [
"cranelift-codegen", "cranelift-codegen",
"cranelift-entity", "cranelift-entity",
@ -7875,9 +7875,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmer-derive" name = "wasmer-derive"
version = "4.0.0-beta.3" version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90277df86705920fefed23c2724ed652fa974af788f0983ca0493b51691a76bd" checksum = "13ae8286cba2acb10065a4dac129c7c7f7bcd24acd6538555d96616eea16bc27"
dependencies = [ dependencies = [
"proc-macro-error", "proc-macro-error",
"proc-macro2 1.0.56", "proc-macro2 1.0.56",
@ -7887,9 +7887,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmer-types" name = "wasmer-types"
version = "4.0.0-beta.3" version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "918344b93266cf4efe56e6cd23f35c7325275524f534678145f7ead4b2b8c762" checksum = "918d2f0bb5eaa95a80c06be33f21dee92f40f12cd0982da34490d121a99d244b"
dependencies = [ dependencies = [
"bytecheck", "bytecheck",
"enum-iterator 0.7.0", "enum-iterator 0.7.0",
@ -7903,9 +7903,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmer-vm" name = "wasmer-vm"
version = "4.0.0-beta.3" version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e36cba570e0b2662ff52d40fe9a48fd368f6eb35183a84ce43f79bc51b9fd15" checksum = "a1e000c2cbd4f9805427af5f3b3446574caf89ab3a1e66c2f3579fbde22b072b"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"cc", "cc",
@ -7930,9 +7930,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmer-wasix-types" name = "wasmer-wasix-types"
version = "0.8.0" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13597c9c4febf4a6b0f36a8c76d17f90a6be7f65bc4667472a50a93121a6f2c5" checksum = "7a4a519e8f0b878bb4cd2b1bc733235aa6c331b7b4857dd6e0ac3c9a36d942ae"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bitflags 1.3.2", "bitflags 1.3.2",

View File

@ -32,11 +32,11 @@ scopeguard = "1.1.0"
serde = { workspace = true, optional = true } serde = { workspace = true, optional = true }
toml = { version = "0.7", optional = true } toml = { version = "0.7", optional = true }
tar = { version = "0.4.37", 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 } bincode = { workspace = true, optional = true }
plugin-api = { package = "veloren-plugin-api", path = "../../plugin/api", optional = true } plugin-api = { package = "veloren-plugin-api", path = "../../plugin/api", optional = true }
timer-queue = "0.1.0" 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 # Tweak running code
#inline_tweak = { version = "1.0.8", features = ["release_tweak"] } #inline_tweak = { version = "1.0.8", features = ["release_tweak"] }

View File

@ -35,7 +35,6 @@ fn print_impl(
}) })
.and_then(|bytes| { .and_then(|bytes| {
std::str::from_utf8(bytes.as_slice()) std::str::from_utf8(bytes.as_slice())
.map(|msg| tracing::info!("[{}]: {}", env.name, msg))
.map_err(|error| { .map_err(|error| {
tracing::error!( tracing::error!(
"Logging message from plugin {} failed with {}!", "Logging message from plugin {} failed with {}!",
@ -44,10 +43,12 @@ fn print_impl(
); );
wasmer_wasix_types::wasi::Errno::Inval wasmer_wasix_types::wasi::Errno::Inval
}) })
.map(|msg| tracing::info!("[{}]: {}", env.name, msg))
}) })
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
#[repr(C)]
pub(crate) struct CioVec { pub(crate) struct CioVec {
buf: WasmPtr<u8, MemoryModel>, buf: WasmPtr<u8, MemoryModel>,
buf_len: <MemoryModel as wasmer::MemorySize>::Offset, buf_len: <MemoryModel as wasmer::MemorySize>::Offset,
@ -55,7 +56,13 @@ pub(crate) struct CioVec {
// CioVec has no padding bytes, thus no action is necessary // CioVec has no padding bytes, thus no action is necessary
unsafe impl wasmer::ValueType for CioVec { unsafe impl wasmer::ValueType for CioVec {
fn zero_padding_bytes(&self, _bytes: &mut [std::mem::MaybeUninit<u8>]) {} fn zero_padding_bytes(&self, _bytes: &mut [std::mem::MaybeUninit<u8>]) {
const _: () = assert!(
core::mem::size_of::<CioVec>()
== core::mem::size_of::<WasmPtr<u8, MemoryModel>>()
+ core::mem::size_of::<<MemoryModel as wasmer::MemorySize>::Offset>()
);
}
} }
// fd_write(fd: fd, iovs: ciovec_array) -> Result<size, errno> // fd_write(fd: fd, iovs: ciovec_array) -> Result<size, errno>
@ -67,7 +74,7 @@ pub(crate) fn wasi_fd_write(
out_result: WasmPtr<<MemoryModel as wasmer::MemorySize>::Offset, MemoryModel>, out_result: WasmPtr<<MemoryModel as wasmer::MemorySize>::Offset, MemoryModel>,
) -> i32 { ) -> i32 {
use wasmer_wasix_types::wasi::Errno; use wasmer_wasix_types::wasi::Errno;
if fd != 0 { if fd != 1 && fd != 2 {
Errno::Badf as i32 Errno::Badf as i32
} else { } else {
let memory = env.data().memory().clone(); let memory = env.data().memory().clone();
@ -111,12 +118,11 @@ pub(crate) fn wasi_env_sizes_get(
let memory = env.data().memory().clone(); let memory = env.data().memory().clone();
let store = env.as_store_mut(); let store = env.as_store_mut();
let mem = memory.view(&store); let mem = memory.view(&store);
if numptr.write(&mem, 0).is_err() { numptr
return Errno::Memviolation as i32;
}
bytesptr
.write(&mem, 0) .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) // proc_exit(rval: exitcode)

View File

@ -172,6 +172,8 @@ pub fn write_data_as_pointer<T: Serialize>(
/// This function writes an raw bytes to WASM memory returning a pointer and /// This function writes an raw bytes to WASM memory returning a pointer and
/// a length. Will realloc the buffer is not wide enough /// 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( pub fn write_bytes(
store: &mut StoreMut, store: &mut StoreMut,
memory: &Memory, memory: &Memory,
@ -194,12 +196,8 @@ pub fn write_bytes(
len as <MemoryModel as wasmer::MemorySize>::Offset, len as <MemoryModel as wasmer::MemorySize>::Offset,
) )
.and_then(|s| { .and_then(|s| {
if !bytes.1.is_empty() { s.subslice(0..bytes.0.len() as u64).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)
s.subslice(bytes.0.len() as u64..len).write_slice(bytes.1)
} else {
s.write_slice(bytes.0)
}
}) })
.map_err(|_| PluginModuleError::InvalidPointer)?; .map_err(|_| PluginModuleError::InvalidPointer)?;
Ok((ptr, len)) Ok((ptr, len))

View File

@ -239,17 +239,17 @@ fn execute_raw(
.call(&mut module.store.as_store_mut(), ptr, len) .call(&mut module.store.as_store_mut(), ptr, len)
.map_err(PluginModuleError::RunFunction)?; .map_err(PluginModuleError::RunFunction)?;
// The first 4 bytes correspond to the length of the result // The first 4/8 bytes correspond to the length of the result
let result_len: Vec<u8> = result_ptr let mut result_len = [0u8; std::mem::size_of::<<MemoryModel as wasmer::MemorySize>::Offset>()];
result_ptr
.slice( .slice(
&module.memory.view(&module.store.as_store_ref()), &module.memory.view(&module.store.as_store_ref()),
std::mem::size_of::<<MemoryModel as wasmer::MemorySize>::Offset>() std::mem::size_of::<<MemoryModel as wasmer::MemorySize>::Offset>()
as <MemoryModel as wasmer::MemorySize>::Offset, as <MemoryModel as wasmer::MemorySize>::Offset,
) )
.and_then(|s| s.read_to_vec()) .and_then(|s| s.read_slice(&mut result_len))
.map_err(|_| PluginModuleError::InvalidPointer)?; .map_err(|_| PluginModuleError::InvalidPointer)?;
let result_len = let result_len = <MemoryModel as wasmer::MemorySize>::Offset::from_le_bytes(result_len);
<MemoryModel as wasmer::MemorySize>::Offset::from_le_bytes(result_len.try_into().unwrap());
// Read the result of the function with the pointer and the length // Read the result of the function with the pointer and the length
let bytes = memory_manager::read_bytes( let bytes = memory_manager::read_bytes(