diff --git a/lib/llvm-backend/src/code.rs b/lib/llvm-backend/src/code.rs index 021c3665a..bf98b8c06 100644 --- a/lib/llvm-backend/src/code.rs +++ b/lib/llvm-backend/src/code.rs @@ -37,7 +37,7 @@ use wasmer_runtime_core::{ codegen::*, memory::MemoryType, module::{ModuleInfo, ModuleInner}, - parse::wp_type_to_type, + parse::{wp_type_to_type, LoadError}, structures::{Map, TypedIndex}, types::{ FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, SigIndex, TableIndex, Type, @@ -670,7 +670,7 @@ fn resolve_memory_ptr<'ctx>( memarg: &MemoryImmediate, ptr_ty: PointerType<'ctx>, value_size: usize, -) -> Result, BinaryReaderError> { +) -> Result, CodegenError> { // Look up the memory base (as pointer) and bounds (as unsigned integer). let memory_cache = ctx.memory(MemoryIndex::new(0), intrinsics, module.clone()); let (mem_base, mem_bound, minimum, _maximum) = match memory_cache { @@ -1200,9 +1200,8 @@ impl<'ctx> FunctionCodeGenerator for LLVMFunctionCodeGenerator<'ct * https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#control-flow-instructions ***************************/ Operator::Block { ty } => { - let current_block = builder.get_insert_block().ok_or(BinaryReaderError { - message: "not currently in a block", - offset: -1isize as usize, + let current_block = builder.get_insert_block().ok_or(CodegenError { + message: "not currently in a block".to_string(), })?; let end_block = context.append_basic_block(function, "end"); @@ -1276,9 +1275,8 @@ impl<'ctx> FunctionCodeGenerator for LLVMFunctionCodeGenerator<'ct Operator::Br { relative_depth } => { let frame = state.frame_at_depth(relative_depth)?; - let current_block = builder.get_insert_block().ok_or(BinaryReaderError { - message: "not currently in a block", - offset: -1isize as usize, + let current_block = builder.get_insert_block().ok_or(CodegenError { + message: "not currently in a block".to_string(), })?; let value_len = if frame.is_loop() { @@ -1308,9 +1306,8 @@ impl<'ctx> FunctionCodeGenerator for LLVMFunctionCodeGenerator<'ct let cond = state.pop1()?; let frame = state.frame_at_depth(relative_depth)?; - let current_block = builder.get_insert_block().ok_or(BinaryReaderError { - message: "not currently in a block", - offset: -1isize as usize, + let current_block = builder.get_insert_block().ok_or(CodegenError { + message: "not currently in a block".to_string(), })?; let value_len = if frame.is_loop() { @@ -1340,9 +1337,8 @@ impl<'ctx> FunctionCodeGenerator for LLVMFunctionCodeGenerator<'ct builder.position_at_end(&else_block); } Operator::BrTable { ref table } => { - let current_block = builder.get_insert_block().ok_or(BinaryReaderError { - message: "not currently in a block", - offset: -1isize as usize, + let current_block = builder.get_insert_block().ok_or(CodegenError { + message: "not currently in a block".to_string(), })?; let (label_depths, default_depth) = table.read_table()?; @@ -1366,7 +1362,7 @@ impl<'ctx> FunctionCodeGenerator for LLVMFunctionCodeGenerator<'ct .iter() .enumerate() .map(|(case_index, &depth)| { - let frame_result: Result<&ControlFrame, BinaryReaderError> = + let frame_result: Result<&ControlFrame, CodegenError> = state.frame_at_depth(depth); let frame = match frame_result { Ok(v) => v, @@ -1390,9 +1386,8 @@ impl<'ctx> FunctionCodeGenerator for LLVMFunctionCodeGenerator<'ct state.reachable = false; } Operator::If { ty } => { - let current_block = builder.get_insert_block().ok_or(BinaryReaderError { - message: "not currently in a block", - offset: -1isize as usize, + let current_block = builder.get_insert_block().ok_or(CodegenError { + message: "not currently in a block".to_string(), })?; let if_then_block = context.append_basic_block(function, "if_then"); let if_else_block = context.append_basic_block(function, "if_else"); @@ -1431,9 +1426,8 @@ impl<'ctx> FunctionCodeGenerator for LLVMFunctionCodeGenerator<'ct Operator::Else => { if state.reachable { let frame = state.frame_at_depth(0)?; - let current_block = builder.get_insert_block().ok_or(BinaryReaderError { - message: "not currently in a block", - offset: -1isize as usize, + let current_block = builder.get_insert_block().ok_or(CodegenError { + message: "not currently in a block".to_string(), })?; for phi in frame.phis().to_vec().iter().rev() { @@ -1465,9 +1459,8 @@ impl<'ctx> FunctionCodeGenerator for LLVMFunctionCodeGenerator<'ct Operator::End => { let frame = state.pop_frame()?; - let current_block = builder.get_insert_block().ok_or(BinaryReaderError { - message: "not currently in a block", - offset: -1isize as usize, + let current_block = builder.get_insert_block().ok_or(CodegenError { + message: "not currently in a block".to_string(), })?; if state.reachable { @@ -1524,9 +1517,8 @@ impl<'ctx> FunctionCodeGenerator for LLVMFunctionCodeGenerator<'ct } } Operator::Return => { - let current_block = builder.get_insert_block().ok_or(BinaryReaderError { - message: "not currently in a block", - offset: -1isize as usize, + let current_block = builder.get_insert_block().ok_or(CodegenError { + message: "not currently in a block".to_string(), })?; let frame = state.outermost_frame()?; @@ -8611,6 +8603,14 @@ impl From for CodegenError { } } +impl From for CodegenError { + fn from(other: LoadError) -> CodegenError { + CodegenError { + message: format!("{:?}", other), + } + } +} + impl Drop for LLVMModuleCodeGenerator<'_> { fn drop(&mut self) { // Ensure that all members of the context are dropped before we drop the context. diff --git a/lib/llvm-backend/src/read_info.rs b/lib/llvm-backend/src/read_info.rs index 6f7fbca5e..958d3a49e 100644 --- a/lib/llvm-backend/src/read_info.rs +++ b/lib/llvm-backend/src/read_info.rs @@ -1,15 +1,16 @@ +use crate::code::CodegenError; use wasmer_runtime_core::parse::wp_type_to_type; use wasmer_runtime_core::types::Type; -use wasmparser::{BinaryReaderError, TypeOrFuncType as WpTypeOrFuncType}; +use wasmparser::TypeOrFuncType as WpTypeOrFuncType; -pub fn blocktype_to_type(ty: WpTypeOrFuncType) -> Result { +pub fn blocktype_to_type(ty: WpTypeOrFuncType) -> Result { match ty { - WpTypeOrFuncType::Type(inner_ty) => wp_type_to_type(inner_ty), + WpTypeOrFuncType::Type(inner_ty) => Ok(wp_type_to_type(inner_ty)?), _ => { - return Err(BinaryReaderError { + return Err(CodegenError { message: - "the wasmer llvm backend does not yet support the multi-value return extension", - offset: -1isize as usize, + "the wasmer llvm backend does not yet support the multi-value return extension" + .to_string(), }); } } diff --git a/lib/llvm-backend/src/state.rs b/lib/llvm-backend/src/state.rs index 8db358fe2..ed3f8bb3a 100644 --- a/lib/llvm-backend/src/state.rs +++ b/lib/llvm-backend/src/state.rs @@ -1,3 +1,4 @@ +use crate::code::CodegenError; use inkwell::{ basic_block::BasicBlock, values::{BasicValue, BasicValueEnum, PhiValue}, @@ -5,7 +6,6 @@ use inkwell::{ use smallvec::SmallVec; use std::cell::Cell; use std::ops::{BitAnd, BitOr, BitOrAssign}; -use wasmparser::BinaryReaderError; #[derive(Debug)] pub enum ControlFrame<'ctx> { @@ -229,21 +229,19 @@ impl<'ctx> State<'ctx> { self.stack.truncate(stack_size_snapshot); } - pub fn outermost_frame(&self) -> Result<&ControlFrame<'ctx>, BinaryReaderError> { - self.control_stack.get(0).ok_or(BinaryReaderError { - message: "outermost_frame: invalid control stack depth", - offset: -1isize as usize, + pub fn outermost_frame(&self) -> Result<&ControlFrame<'ctx>, CodegenError> { + self.control_stack.get(0).ok_or(CodegenError { + message: "outermost_frame: invalid control stack depth".to_string(), }) } - pub fn frame_at_depth(&self, depth: u32) -> Result<&ControlFrame<'ctx>, BinaryReaderError> { + pub fn frame_at_depth(&self, depth: u32) -> Result<&ControlFrame<'ctx>, CodegenError> { let index = self .control_stack .len() .checked_sub(1 + (depth as usize)) - .ok_or(BinaryReaderError { - message: "frame_at_depth: invalid control stack depth", - offset: -1isize as usize, + .ok_or(CodegenError { + message: "frame_at_depth: invalid control stack depth".to_string(), })?; Ok(&self.control_stack[index]) } @@ -251,22 +249,20 @@ impl<'ctx> State<'ctx> { pub fn frame_at_depth_mut( &mut self, depth: u32, - ) -> Result<&mut ControlFrame<'ctx>, BinaryReaderError> { + ) -> Result<&mut ControlFrame<'ctx>, CodegenError> { let index = self .control_stack .len() .checked_sub(1 + (depth as usize)) - .ok_or(BinaryReaderError { - message: "frame_at_depth_mut: invalid control stack depth", - offset: -1isize as usize, + .ok_or(CodegenError { + message: "frame_at_depth_mut: invalid control stack depth".to_string(), })?; Ok(&mut self.control_stack[index]) } - pub fn pop_frame(&mut self) -> Result, BinaryReaderError> { - self.control_stack.pop().ok_or(BinaryReaderError { - message: "pop_frame: cannot pop from control stack", - offset: -1isize as usize, + pub fn pop_frame(&mut self) -> Result, CodegenError> { + self.control_stack.pop().ok_or(CodegenError { + message: "pop_frame: cannot pop from control stack".to_string(), }) } @@ -285,20 +281,17 @@ impl<'ctx> State<'ctx> { self.stack.push((value.as_basic_value_enum(), info)); } - pub fn pop1(&mut self) -> Result, BinaryReaderError> { + pub fn pop1(&mut self) -> Result, CodegenError> { Ok(self.pop1_extra()?.0) } - pub fn pop1_extra(&mut self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), BinaryReaderError> { - self.stack.pop().ok_or(BinaryReaderError { - message: "pop1_extra: invalid value stack", - offset: -1isize as usize, + pub fn pop1_extra(&mut self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), CodegenError> { + self.stack.pop().ok_or(CodegenError { + message: "pop1_extra: invalid value stack".to_string(), }) } - pub fn pop2( - &mut self, - ) -> Result<(BasicValueEnum<'ctx>, BasicValueEnum<'ctx>), BinaryReaderError> { + pub fn pop2(&mut self) -> Result<(BasicValueEnum<'ctx>, BasicValueEnum<'ctx>), CodegenError> { let v2 = self.pop1()?; let v1 = self.pop1()?; Ok((v1, v2)) @@ -311,7 +304,7 @@ impl<'ctx> State<'ctx> { (BasicValueEnum<'ctx>, ExtraInfo), (BasicValueEnum<'ctx>, ExtraInfo), ), - BinaryReaderError, + CodegenError, > { let v2 = self.pop1_extra()?; let v1 = self.pop1_extra()?; @@ -326,7 +319,7 @@ impl<'ctx> State<'ctx> { (BasicValueEnum<'ctx>, ExtraInfo), (BasicValueEnum<'ctx>, ExtraInfo), ), - BinaryReaderError, + CodegenError, > { let v3 = self.pop1_extra()?; let v2 = self.pop1_extra()?; @@ -334,25 +327,23 @@ impl<'ctx> State<'ctx> { Ok((v1, v2, v3)) } - pub fn peek1_extra(&self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), BinaryReaderError> { - let index = self.stack.len().checked_sub(1).ok_or(BinaryReaderError { - message: "peek1_extra: invalid value stack", - offset: -1isize as usize, + pub fn peek1_extra(&self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), CodegenError> { + let index = self.stack.len().checked_sub(1).ok_or(CodegenError { + message: "peek1_extra: invalid value stack".to_string(), })?; Ok(self.stack[index]) } - pub fn peekn(&self, n: usize) -> Result>, BinaryReaderError> { + pub fn peekn(&self, n: usize) -> Result>, CodegenError> { Ok(self.peekn_extra(n)?.iter().map(|x| x.0).collect()) } pub fn peekn_extra( &self, n: usize, - ) -> Result<&[(BasicValueEnum<'ctx>, ExtraInfo)], BinaryReaderError> { - let index = self.stack.len().checked_sub(n).ok_or(BinaryReaderError { - message: "peekn_extra: invalid value stack", - offset: -1isize as usize, + ) -> Result<&[(BasicValueEnum<'ctx>, ExtraInfo)], CodegenError> { + let index = self.stack.len().checked_sub(n).ok_or(CodegenError { + message: "peekn_extra: invalid value stack".to_string(), })?; Ok(&self.stack[index..]) @@ -361,16 +352,15 @@ impl<'ctx> State<'ctx> { pub fn popn_save_extra( &mut self, n: usize, - ) -> Result, ExtraInfo)>, BinaryReaderError> { + ) -> Result, ExtraInfo)>, CodegenError> { let v = self.peekn_extra(n)?.to_vec(); self.popn(n)?; Ok(v) } - pub fn popn(&mut self, n: usize) -> Result<(), BinaryReaderError> { - let index = self.stack.len().checked_sub(n).ok_or(BinaryReaderError { - message: "popn: invalid value stack", - offset: -1isize as usize, + pub fn popn(&mut self, n: usize) -> Result<(), CodegenError> { + let index = self.stack.len().checked_sub(n).ok_or(CodegenError { + message: "popn: invalid value stack".to_string(), })?; self.stack.truncate(index);