From 1222ae92fd21b958e01262d9f123c7523de86b5c Mon Sep 17 00:00:00 2001 From: nerodesu017 Date: Tue, 17 Dec 2024 11:35:12 +0200 Subject: [PATCH] fix: wrong Span length --- src/core/reader/mod.rs | 5 ++ src/validation/read_constant_expression.rs | 2 +- tests/specification/mod.rs | 3 +- tests/specification/run.rs | 58 +++++++++++----------- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/core/reader/mod.rs b/src/core/reader/mod.rs index f263efbd..ae388058 100644 --- a/src/core/reader/mod.rs +++ b/src/core/reader/mod.rs @@ -272,6 +272,11 @@ mod test { assert_eq!(wasm_reader.move_start_to(span), Err(Error::Eof)); } + // #[test] + // fn should_not_die() { + // let my_bytes = vec![0x1] + // } + #[test] fn remaining_bytes_1() { let my_bytes = vec![0x11, 0x12, 0x13, 0x14, 0x15]; diff --git a/src/validation/read_constant_expression.rs b/src/validation/read_constant_expression.rs index 2bf7bcd8..cf7eb79c 100644 --- a/src/validation/read_constant_expression.rs +++ b/src/validation/read_constant_expression.rs @@ -104,7 +104,7 @@ pub fn read_constant_expression( if this_global_valtype.is_some() { stack.assert_val_types(&[this_global_valtype.unwrap()])?; } - return Ok(Span::new(start_pc, wasm.pc - start_pc + 1)); + return Ok(Span::new(start_pc, wasm.pc - start_pc)); } I32_CONST => { let _num = wasm.read_var_i32()?; diff --git a/tests/specification/mod.rs b/tests/specification/mod.rs index dc87b1e2..08cab839 100644 --- a/tests/specification/mod.rs +++ b/tests/specification/mod.rs @@ -8,6 +8,7 @@ mod run; mod test_errors; enum Filter { + #[allow(dead_code)] Include(FnF), Exclude(FnF), } @@ -30,8 +31,8 @@ impl Default for FnF { #[test_log::test] pub fn spec_tests() { let filters = Filter::Exclude(FnF { - files: Some(vec!["binary-leb128.wast".to_string()]), folders: Some(vec!["proposals".to_string()]), + ..Default::default() }); // let only_these_tests: Vec = vec![]; diff --git a/tests/specification/run.rs b/tests/specification/run.rs index 23ee7b43..55c9f98d 100644 --- a/tests/specification/run.rs +++ b/tests/specification/run.rs @@ -193,7 +193,7 @@ pub fn run_spec_test(filepath: &str) -> WastTestReport { let interpeter = interpeter.as_mut().unwrap(); - let err_or_panic: Result<(), Box> = + let err_or_panic: Result<(), Box> = match catch_unwind(AssertUnwindSafe(|| { execute_assert_trap(interpeter, exec, message) })) { @@ -205,24 +205,24 @@ pub fn run_spec_test(filepath: &str) -> WastTestReport { Err(Box::new(PanicError::new("Unknown panic"))) } } - Ok(original_result) => original_result + Ok(original_result) => original_result, }; - match err_or_panic { - Ok(_) => { - asserts.push_success(WastSuccess::new( - span.linecol_in(&contents).0 as u32 + 1, - get_command(&contents, span), - )); - } - Err(inner) => { - asserts.push_error(WastError::new( - inner, - span.linecol_in(&contents).0 as u32 + 1, - get_command(&contents, span), - )); - } + match err_or_panic { + Ok(_) => { + asserts.push_success(WastSuccess::new( + span.linecol_in(&contents).0 as u32 + 1, + get_command(&contents, span), + )); } + Err(inner) => { + asserts.push_error(WastError::new( + inner, + span.linecol_in(&contents).0 as u32 + 1, + get_command(&contents, span), + )); + } + } // match exec { // wast::WastExecute::Invoke(invoke_info) => { @@ -308,11 +308,6 @@ pub fn run_spec_test(filepath: &str) -> WastTestReport { name: _, module: _, } - // | wast::WastDirective::AssertTrap { - // span, - // exec: _, - // message: _, - // } | wast::WastDirective::AssertExhaustion { span, call: _, @@ -403,7 +398,7 @@ fn execute_assert_return( fn execute_assert_trap( interpeter: &mut RuntimeInstance, exec: wast::WastExecute, - message: &str + message: &str, ) -> Result<(), Box> { match exec { wast::WastExecute::Invoke(invoke_info) => { @@ -416,9 +411,7 @@ fn execute_assert_trap( // TODO: more modules ¯\_(ツ)_/¯ let func_res = interpeter .get_function_by_name(DEFAULT_MODULE, invoke_info.name) - .map_err(|err| { - Box::new(WasmInterpreterError(wasm::Error::RuntimeError(err))) - }); + .map_err(|err| Box::new(WasmInterpreterError(wasm::Error::RuntimeError(err)))); let func: FunctionRef; match func_res { @@ -437,11 +430,16 @@ fn execute_assert_trap( let expected = message; if actual.contains(expected) - || (expected.contains("uninitialized element 2") && actual.contains("uninitialized element")) { - Ok(()) - } else { - Err(Box::new(GenericError::new(format!("'assert_trap': Expected '{expected}' - Actual: '{actual}'").as_str()))) - } + || (expected.contains("uninitialized element 2") + && actual.contains("uninitialized element")) + { + Ok(()) + } else { + Err(Box::new(GenericError::new( + format!("'assert_trap': Expected '{expected}' - Actual: '{actual}'") + .as_str(), + ))) + } } } }