From 2a8f8925c9a224bd3338eb084e078c2eb78a9876 Mon Sep 17 00:00:00 2001 From: Valery Antopol Date: Fri, 18 Feb 2022 01:39:21 +0300 Subject: [PATCH] Fix fatal crash made in previous PR (#15) --- Cargo.lock | 2 +- wasmer-it/Cargo.toml | 2 +- .../src/interpreter/instructions/arrays.rs | 32 +++++++++++++++---- .../interpreter/instructions/byte_arrays.rs | 7 ++-- .../src/interpreter/instructions/records.rs | 32 +++++++++++++++---- .../src/interpreter/instructions/strings.rs | 8 ++--- wasmer-it/src/interpreter/mod.rs | 25 +++++++++++---- wasmer-it/src/interpreter/wasm/structures.rs | 4 ++- 8 files changed, 79 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e7b112e..a2459f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -280,7 +280,7 @@ checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" [[package]] name = "wasmer-interface-types-fl" -version = "0.21.0" +version = "0.21.1" dependencies = [ "fluence-it-types", "it-lilo", diff --git a/wasmer-it/Cargo.toml b/wasmer-it/Cargo.toml index 01272d7..6d6b7aa 100644 --- a/wasmer-it/Cargo.toml +++ b/wasmer-it/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasmer-interface-types-fl" -version = "0.21.0" +version = "0.21.1" description = "WebAssembly Interface Types library for Wasmer" license = "MIT" authors = ["The Wasmer Engineering Team "] diff --git a/wasmer-it/src/interpreter/instructions/arrays.rs b/wasmer-it/src/interpreter/instructions/arrays.rs index b27d29b..f6b1026 100644 --- a/wasmer-it/src/interpreter/instructions/arrays.rs +++ b/wasmer-it/src/interpreter/instructions/arrays.rs @@ -17,14 +17,24 @@ use std::convert::TryInto; pub(crate) fn array_lift_memory( instruction: Instruction, value_type: IType, -) -> crate::interpreter::ExecutableInstruction +) -> crate::interpreter::ExecutableInstruction< + Instance, + Export, + LocalImport, + Memory, + SequentialMemoryView, +> where Export: crate::interpreter::wasm::structures::Export, LocalImport: crate::interpreter::wasm::structures::LocalImport, Memory: crate::interpreter::wasm::structures::Memory, SequentialMemoryView: for<'a> crate::interpreter::wasm::structures::SequentialMemoryView<'a>, - Instance: - crate::interpreter::wasm::structures::Instance, + Instance: crate::interpreter::wasm::structures::Instance< + Export, + LocalImport, + Memory, + SequentialMemoryView, + >, { #[allow(unused_imports)] use crate::interpreter::stack::Stackable; @@ -84,14 +94,24 @@ where pub(crate) fn array_lower_memory( instruction: Instruction, value_type: IType, -) -> crate::interpreter::ExecutableInstruction +) -> crate::interpreter::ExecutableInstruction< + Instance, + Export, + LocalImport, + Memory, + SequentialMemoryView, +> where Export: crate::interpreter::wasm::structures::Export, LocalImport: crate::interpreter::wasm::structures::LocalImport, Memory: crate::interpreter::wasm::structures::Memory, SequentialMemoryView: for<'a> crate::interpreter::wasm::structures::SequentialMemoryView<'a>, - Instance: - crate::interpreter::wasm::structures::Instance, + Instance: crate::interpreter::wasm::structures::Instance< + Export, + LocalImport, + Memory, + SequentialMemoryView, + >, { #[allow(unused_imports)] use crate::interpreter::stack::Stackable; diff --git a/wasmer-it/src/interpreter/instructions/byte_arrays.rs b/wasmer-it/src/interpreter/instructions/byte_arrays.rs index 7169398..60d7f4a 100644 --- a/wasmer-it/src/interpreter/instructions/byte_arrays.rs +++ b/wasmer-it/src/interpreter/instructions/byte_arrays.rs @@ -7,8 +7,8 @@ use crate::{ interpreter::Instruction, }; -use it_memory_traits::{SequentialReader, SequentialWriter}; use it_lilo::traits::DEFAULT_MEMORY_INDEX; +use it_memory_traits::{SequentialReader, SequentialWriter}; use std::convert::TryInto; @@ -54,10 +54,7 @@ executable_instruction!( .sequential_reader(pointer, length) .map_err(|e| InstructionError::from_memory_access(instruction.clone(), e))?; - let mut data = Vec::::with_capacity(length); - for index in 0..length { - data[index] = reader.read_u8(); - } + let data = (0..length).map(|_| reader.read_u8()).collect::>(); log::debug!("byte_array.lift_memory: pushing {:?} on the stack", data); runtime.stack.push(IValue::ByteArray(data)); diff --git a/wasmer-it/src/interpreter/instructions/records.rs b/wasmer-it/src/interpreter/instructions/records.rs index e133ac5..5b45101 100644 --- a/wasmer-it/src/interpreter/instructions/records.rs +++ b/wasmer-it/src/interpreter/instructions/records.rs @@ -14,14 +14,24 @@ use std::convert::TryInto; pub(crate) fn record_lift_memory( record_type_id: u64, instruction: Instruction, -) -> crate::interpreter::ExecutableInstruction +) -> crate::interpreter::ExecutableInstruction< + Instance, + Export, + LocalImport, + Memory, + SequentialMemoryView, +> where Export: crate::interpreter::wasm::structures::Export, LocalImport: crate::interpreter::wasm::structures::LocalImport, Memory: crate::interpreter::wasm::structures::Memory, SequentialMemoryView: for<'a> crate::interpreter::wasm::structures::SequentialMemoryView<'a>, - Instance: - crate::interpreter::wasm::structures::Instance, + Instance: crate::interpreter::wasm::structures::Instance< + Export, + LocalImport, + Memory, + SequentialMemoryView, + >, { #[allow(unused_imports)] use crate::interpreter::stack::Stackable; @@ -81,14 +91,24 @@ where pub(crate) fn record_lower_memory( record_type_id: u64, instruction: Instruction, -) -> crate::interpreter::ExecutableInstruction +) -> crate::interpreter::ExecutableInstruction< + Instance, + Export, + LocalImport, + Memory, + SequentialMemoryView, +> where Export: crate::interpreter::wasm::structures::Export, LocalImport: crate::interpreter::wasm::structures::LocalImport, Memory: crate::interpreter::wasm::structures::Memory, SequentialMemoryView: for<'a> crate::interpreter::wasm::structures::SequentialMemoryView<'a>, - Instance: - crate::interpreter::wasm::structures::Instance, + Instance: crate::interpreter::wasm::structures::Instance< + Export, + LocalImport, + Memory, + SequentialMemoryView, + >, { #[allow(unused_imports)] use crate::interpreter::stack::Stackable; diff --git a/wasmer-it/src/interpreter/instructions/strings.rs b/wasmer-it/src/interpreter/instructions/strings.rs index 8e0808b..6490fe5 100644 --- a/wasmer-it/src/interpreter/instructions/strings.rs +++ b/wasmer-it/src/interpreter/instructions/strings.rs @@ -7,8 +7,8 @@ use crate::{ interpreter::Instruction, }; -use it_memory_traits::{SequentialReader, SequentialWriter}; use it_lilo::traits::DEFAULT_MEMORY_INDEX; +use it_memory_traits::{SequentialReader, SequentialWriter}; use std::convert::TryInto; @@ -53,11 +53,7 @@ executable_instruction!( .sequential_reader(pointer, length) .map_err(|e| InstructionError::from_memory_access(instruction.clone(), e))?; - let mut data = Vec::::with_capacity(length); - for index in 0..length { - data[index] = reader.read_u8(); -} - + let data = (0..length).map(|_| reader.read_u8()).collect::>(); let string = String::from_utf8(data) .map_err(|error| InstructionError::from_error_kind(instruction.clone(), InstructionErrorKind::String(error)))?; diff --git a/wasmer-it/src/interpreter/mod.rs b/wasmer-it/src/interpreter/mod.rs index 5f1f0ec..7a307c9 100644 --- a/wasmer-it/src/interpreter/mod.rs +++ b/wasmer-it/src/interpreter/mod.rs @@ -13,13 +13,21 @@ use std::{convert::TryFrom, marker::PhantomData}; /// Represents the `Runtime`, which is used by an adapter to execute /// its instructions. -pub(crate) struct Runtime<'invocation, 'instance, Instance, Export, LocalImport, Memory, SequentialMemoryView> -where +pub(crate) struct Runtime< + 'invocation, + 'instance, + Instance, + Export, + LocalImport, + Memory, + SequentialMemoryView, +> where Export: wasm::structures::Export + 'instance, LocalImport: wasm::structures::LocalImport + 'instance, Memory: wasm::structures::Memory + 'instance, SequentialMemoryView: (for<'a> wasm::structures::SequentialMemoryView<'a>), - Instance: wasm::structures::Instance + 'instance, + Instance: + wasm::structures::Instance + 'instance, { /// The invocation inputs are all the arguments received by an /// adapter. @@ -38,10 +46,13 @@ where /// Type alias for an executable instruction. It's an implementation /// details, but an instruction is a boxed closure instance. -pub(crate) type ExecutableInstruction = Box< - dyn Fn(&mut Runtime) -> InstructionResult<()> - + Send, ->; +pub(crate) type ExecutableInstruction = + Box< + dyn Fn( + &mut Runtime, + ) -> InstructionResult<()> + + Send, + >; /// An interpreter is the central piece of this crate. It is a set of /// executable instructions. Each instruction takes the runtime as diff --git a/wasmer-it/src/interpreter/wasm/structures.rs b/wasmer-it/src/interpreter/wasm/structures.rs index 62f2cd9..5d3784e 100644 --- a/wasmer-it/src/interpreter/wasm/structures.rs +++ b/wasmer-it/src/interpreter/wasm/structures.rs @@ -6,7 +6,9 @@ use crate::IType; use crate::IValue; use std::rc::Rc; -pub use it_memory_traits::{Memory, SequentialMemoryView, SequentialReader, SequentialWriter, MemoryAccessError}; +pub use it_memory_traits::{ + Memory, MemoryAccessError, SequentialMemoryView, SequentialReader, SequentialWriter, +}; pub trait TypedIndex: Copy + Clone { fn new(index: usize) -> Self;