From 4765aecf7806bfa0020845a476662d64a3cd1d08 Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Mon, 29 Jan 2018 17:23:44 +0300 Subject: [PATCH] call_indirect traps. --- src/lib.rs | 3 +++ src/runner.rs | 12 ++++-------- src/table.rs | 7 ++----- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b14a184..20ae2f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,6 +110,9 @@ use std::collections::HashMap; pub enum Trap { Unreachable, MemoryAccessOutOfBounds, + TableAccessOutOfBounds, + ElemUninitialized, + ElemSignatureMismatch, } /// Internal interpreter error. diff --git a/src/runner.rs b/src/runner.rs index 21ced8a..fc88e1a 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -438,7 +438,9 @@ impl<'a, E: Externals> Interpreter<'a, E> { .module() .table_by_index(DEFAULT_TABLE_INDEX) .expect("Due to validation table should exists"); - let func_ref = table.get(table_func_idx)?; + let func_ref = table.get(table_func_idx) + .map_err(|_| Error::Trap(Trap::TableAccessOutOfBounds))? + .ok_or_else(|| Error::Trap(Trap::ElemUninitialized))?; { let actual_function_type = func_ref.signature(); @@ -448,13 +450,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { .expect("Due to validation type should exists"); if &*required_function_type != actual_function_type { - return Err(Error::Function(format!( - "expected function with signature ({:?}) -> {:?} when got with ({:?}) -> {:?}", - required_function_type.params(), - required_function_type.return_type(), - actual_function_type.params(), - actual_function_type.return_type() - ))); + return Err(Error::Trap(Trap::ElemSignatureMismatch)); } } diff --git a/src/table.rs b/src/table.rs index eac4b0b..4c47da3 100644 --- a/src/table.rs +++ b/src/table.rs @@ -107,7 +107,7 @@ impl TableInstance { } /// Get the specific value in the table - pub fn get(&self, offset: u32) -> Result { + pub fn get(&self, offset: u32) -> Result, Error> { let buffer = self.buffer.borrow(); let buffer_len = buffer.len(); let table_elem = buffer.get(offset as usize).cloned().ok_or_else(|| @@ -117,10 +117,7 @@ impl TableInstance { buffer_len )), )?; - Ok(table_elem.ok_or_else(|| Error::Table(format!( - "trying to read uninitialized element on index {}", - offset - )))?) + Ok(table_elem) } /// Set the table element to the specified function.