diff --git a/wasmer-it/src/errors.rs b/wasmer-it/src/errors.rs index aa5c267..91b9f4b 100644 --- a/wasmer-it/src/errors.rs +++ b/wasmer-it/src/errors.rs @@ -41,6 +41,7 @@ impl InstructionError { impl Error for InstructionError {} +/// Allows you to shorten the expression creates a new InstructionError. #[macro_export] macro_rules! instr_error { ($instruction:expr, $error_kind:expr) => { @@ -178,6 +179,9 @@ pub enum InstructionErrorKind { /// Errors related to Serialization/deserialization of record. SerdeError(String), + + /// Errors related to lifting incorrect UTF8 string from a Wasm module. + CorruptedUTF8String(std::string::FromUtf8Error), } impl Error for InstructionErrorKind {} @@ -288,6 +292,11 @@ impl Display for InstructionErrorKind { formatter, "serde error: {}", err, ), + + Self::CorruptedUTF8String(err) => write!( + formatter, + "corrupted utf8 string: {}", err + ) } } } diff --git a/wasmer-it/src/interpreter/instructions/arrays/lift_array.rs b/wasmer-it/src/interpreter/instructions/arrays/lift_array.rs index 4520d0f..05134cc 100644 --- a/wasmer-it/src/interpreter/instructions/arrays/lift_array.rs +++ b/wasmer-it/src/interpreter/instructions/arrays/lift_array.rs @@ -22,36 +22,34 @@ where } match value_type { - IType::Boolean => read_bool_array(instance, instruction.clone(), offset, elements_count), - IType::S8 => read_s8_array(instance, instruction.clone(), offset, elements_count), - IType::S16 => read_s16_array(instance, instruction.clone(), offset, elements_count), - IType::S32 => read_s32_array(instance, instruction.clone(), offset, elements_count), - IType::S64 => read_s64_array(instance, instruction.clone(), offset, elements_count), - IType::I32 => read_i32_array(instance, instruction.clone(), offset, elements_count), - IType::I64 => read_i64_array(instance, instruction.clone(), offset, elements_count), - IType::U8 => read_u8_array(instance, instruction.clone(), offset, elements_count), - IType::U16 => read_u16_array(instance, instruction.clone(), offset, elements_count), - IType::U32 => read_u32_array(instance, instruction.clone(), offset, elements_count), - IType::U64 => read_u64_array(instance, instruction.clone(), offset, elements_count), - IType::F32 => read_f32_array(instance, instruction.clone(), offset, elements_count), - IType::F64 => read_f64_array(instance, instruction.clone(), offset, elements_count), - IType::String => read_string_array(instance, instruction.clone(), offset, elements_count), + IType::Boolean => read_bool_array(instance, instruction, offset, elements_count), + IType::S8 => read_s8_array(instance, instruction, offset, elements_count), + IType::S16 => read_s16_array(instance, instruction, offset, elements_count), + IType::S32 => read_s32_array(instance, instruction, offset, elements_count), + IType::S64 => read_s64_array(instance, instruction, offset, elements_count), + IType::I32 => read_i32_array(instance, instruction, offset, elements_count), + IType::I64 => read_i64_array(instance, instruction, offset, elements_count), + IType::U8 => read_u8_array(instance, instruction, offset, elements_count), + IType::U16 => read_u16_array(instance, instruction, offset, elements_count), + IType::U32 => read_u32_array(instance, instruction, offset, elements_count), + IType::U64 => read_u64_array(instance, instruction, offset, elements_count), + IType::F32 => read_f32_array(instance, instruction, offset, elements_count), + IType::F64 => read_f64_array(instance, instruction, offset, elements_count), + IType::String => read_string_array(instance, instruction, offset, elements_count), IType::Record(record_type_id) => read_record_array( instance, - instruction.clone(), + instruction, *record_type_id, offset, elements_count, ), IType::ByteArray => read_array_array( instance, - instruction.clone(), + instruction, &IType::ByteArray, offset, elements_count, ), - IType::Array(ty) => { - read_array_array(instance, instruction.clone(), &ty, offset, elements_count) - } + IType::Array(ty) => read_array_array(instance, instruction, &ty, offset, elements_count), } } diff --git a/wasmer-it/src/interpreter/instructions/arrays/read_arrays.rs b/wasmer-it/src/interpreter/instructions/arrays/read_arrays.rs index d4df139..37d69b5 100644 --- a/wasmer-it/src/interpreter/instructions/arrays/read_arrays.rs +++ b/wasmer-it/src/interpreter/instructions/arrays/read_arrays.rs @@ -354,8 +354,12 @@ where *string_size as _, )?; - // TODO: check - let string = String::from_utf8(string_mem).unwrap(); + let string = String::from_utf8(string_mem).map_err(|e| { + InstructionError::new( + instruction.clone(), + InstructionErrorKind::CorruptedUTF8String(e), + ) + })?; result.push(IValue::String(string)); } diff --git a/wasmer-it/src/interpreter/instructions/records/lift_record.rs b/wasmer-it/src/interpreter/instructions/records/lift_record.rs index 1989067..0837c98 100644 --- a/wasmer-it/src/interpreter/instructions/records/lift_record.rs +++ b/wasmer-it/src/interpreter/instructions/records/lift_record.rs @@ -104,8 +104,9 @@ where string_size as _, )?; - // TODO: check - let string = String::from_utf8(string_mem).unwrap(); + let string = String::from_utf8(string_mem).map_err(|e| { + InstructionError::new(instruction, InstructionErrorKind::CorruptedUTF8String(e)) + })?; Ok(string) } @@ -126,12 +127,7 @@ where let offset = reader.read_u32(); let elements_count = reader.read_u32(); - let array = read_from_instance_mem( - instance, - instruction.clone(), - offset as _, - elements_count as _, - )?; + let array = read_from_instance_mem(instance, instruction, offset as _, elements_count as _)?; let byte_array = IValue::ByteArray(array); Ok(byte_array) @@ -159,7 +155,7 @@ where ty, array_offset as _, elements_count as _, - instruction.clone(), + instruction, ) } @@ -182,11 +178,9 @@ where let record_type = instance.wit_record_by_id(record_type_id).ok_or_else(|| { InstructionError::new( instruction.clone(), - InstructionErrorKind::RecordTypeByNameIsMissing { - record_type_id, - }, + InstructionErrorKind::RecordTypeByNameIsMissing { record_type_id }, ) })?; - record_lift_memory_impl(instance, record_type, offset as _, instruction.clone()) + record_lift_memory_impl(instance, record_type, offset as _, instruction) } diff --git a/wasmer-it/src/lib.rs b/wasmer-it/src/lib.rs index 800c58a..013fa6b 100644 --- a/wasmer-it/src/lib.rs +++ b/wasmer-it/src/lib.rs @@ -40,7 +40,7 @@ #![deny( dead_code, - broken_intra_doc_links, + rustdoc::broken_intra_doc_links, missing_docs, nonstandard_style, unreachable_patterns, @@ -52,7 +52,6 @@ // #![forbid(unsafe_code)] #![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")] #![doc(html_logo_url = "https://github.com/wasmerio.png")] -#![recursion_limit = "512"] pub mod ast; #[macro_use] diff --git a/wasmer-it/src/serde/de.rs b/wasmer-it/src/serde/de.rs index adc21c1..b31279d 100644 --- a/wasmer-it/src/serde/de.rs +++ b/wasmer-it/src/serde/de.rs @@ -206,6 +206,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { V: de::Visitor<'de>, { match self.iterator.peek() { + Some(IValue::Boolean(_)) => self.deserialize_bool(visitor), Some(IValue::S8(_)) => self.deserialize_i8(visitor), Some(IValue::S16(_)) => self.deserialize_i16(visitor), Some(IValue::S32(_)) => self.deserialize_i32(visitor), @@ -217,6 +218,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { Some(IValue::F32(_)) => self.deserialize_f32(visitor), Some(IValue::F64(_)) => self.deserialize_f64(visitor), Some(IValue::String(_)) => self.deserialize_string(visitor), + Some(IValue::ByteArray(_)) => self.deserialize_bytes(visitor), Some(IValue::Array(_)) => self.deserialize_bytes(visitor), Some(IValue::I32(_)) => self.deserialize_i32(visitor), Some(IValue::I64(_)) => self.deserialize_i64(visitor), @@ -225,11 +227,11 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { } } - fn deserialize_bool(self, _visitor: V) -> Result + fn deserialize_bool(self, visitor: V) -> Result where V: de::Visitor<'de>, { - unimplemented!("`bool` is not supported by WIT for the moment.") + visitor.visit_bool(self.next_u8()? == 1) } fn deserialize_i8(self, visitor: V) -> Result