Update testsuite (#39)

* ok_or_else in stack + top_mut.

* Model polymorphic stack explicitly

* Upgrade wasm testsuite.

* Update fixtures.

* Validate duplicate exports.

* Fix and clean spec runner.

* with_capcity for HashSet
This commit is contained in:
Sergey Pepyakin 2018-02-08 17:52:16 +03:00 committed by GitHub
parent 528f468ef2
commit c0ed715b37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 154 additions and 186 deletions

View File

@ -1,10 +1,4 @@
macro_rules! run_test {
($label: expr, $test_name: ident, fail) => (
#[test]
fn $test_name() {
::run::failing_spec($label)
}
);
($label: expr, $test_name: ident) => (
#[test]
fn $test_name() {
@ -13,39 +7,31 @@ macro_rules! run_test {
);
}
run_test!("address-offset-range.fail", wasm_address_offset_range_fail, fail);
run_test!("address", wasm_address);
run_test!("align", wasm_align);
run_test!("binary", wasm_binary);
run_test!("block-end-label-mismatch.fail", wasm_block_end_label_mismatch_fail, fail);
run_test!("block-end-label-superfluous.fail", wasm_block_end_label_superfluous_fail, fail);
run_test!("block", wasm_block);
run_test!("br", wasm_br);
run_test!("br_if", wasm_br_if);
run_test!("br_table", wasm_br_table);
run_test!("br", wasm_br);
run_test!("break-drop", wasm_break_drop);
run_test!("call_indirect", wasm_call_indirect);
run_test!("call", wasm_call);
run_test!("call_indirect", wasm_call_indirect);
run_test!("comments", wasm_comments);
run_test!("const", wasm_const);
// TODO: commented out until sNaN issue is resolved:
// https://github.com/NikVolf/parity-wasm/blob/b5aaf103cf28f1e36df832f4883f55043e67894b/src/interpreter/value.rs#L510
// run_test!("conversions", wasm_conversions);
run_test!("custom_section", wasm_custom_section);
run_test!("elem", wasm_elem);
run_test!("endianness", wasm_endianness);
run_test!("f32_exports", wasm_exports);
run_test!("exports", wasm_exports);
run_test!("f32", wasm_f32);
run_test!("f32_bitwise", wasm_f32_bitwise);
run_test!("f32_cmp", wasm_f32_cmp);
run_test!("f32.load32.fail", wasm_f32_load32_fail, fail);
run_test!("f32.load64.fail", wasm_f32_load64_fail, fail);
run_test!("f32.store32.fail", wasm_f32_store32_fail, fail);
run_test!("f32.store64.fail", wasm_f32_store64_fail, fail);
run_test!("f32", wasm_f32);
run_test!("f64", wasm_f64);
run_test!("f64_bitwise", wasm_f64_bitwise);
run_test!("f64_cmp", wasm_f64_cmp);
run_test!("f64.load32.fail", wasm_f64_load32_fail, fail);
run_test!("f64.load64.fail", wasm_f64_load64_fail, fail);
run_test!("f64.store32.fail", wasm_f64_store32_fail, fail);
run_test!("f64.store64.fail", wasm_f64_store64_fail, fail);
run_test!("f64", wasm_f64);
run_test!("fac", wasm_fac);
// TODO: commented out until sNaN issue is resolved:
// https://github.com/NikVolf/parity-wasm/blob/b5aaf103cf28f1e36df832f4883f55043e67894b/src/interpreter/value.rs#L510
@ -54,61 +40,26 @@ run_test!("fac", wasm_fac);
// run_test!("float_memory", wasm_float_memory);
run_test!("float_misc", wasm_float_misc);
run_test!("forward", wasm_forward);
run_test!("func_ptrs", wasm_func_ptrs);
run_test!("func-local-after-body.fail", wasm_func_local_after_body_fail, fail);
run_test!("func-local-before-param.fail", wasm_func_local_before_param_fail, fail);
run_test!("func-local-before-result.fail", wasm_func_local_before_result_fail, fail);
run_test!("func-param-after-body.fail", wasm_func_param_after_body_fail, fail);
run_test!("func-result-after-body.fail", wasm_func_result_after_body_fail, fail);
run_test!("func-result-before-param.fail", wasm_func_result_before_param_fail, fail);
run_test!("func", wasm_func);
run_test!("func_ptrs", wasm_func_ptrs);
run_test!("get_local", wasm_get_local);
run_test!("globals", wasm_globals);
run_test!("i32.load32_s.fail", wasm_i32_load32s_fail, fail);
run_test!("i32.load32_u.fail", wasm_i32_load32u_fail, fail);
run_test!("i32.load64_s.fail", wasm_i32_load64s_fail, fail);
run_test!("i32.load64_u.fail", wasm_i32_load64u_fail, fail);
run_test!("i32.store32.fail", wasm_i32_store32_fail, fail);
run_test!("i32.store64.fail", wasm_i32_store64_fail, fail);
run_test!("i32", wasm_i32);
run_test!("i64.load64_s.fail", wasm_i64_load64s_fail, fail);
run_test!("i64.load64_u.fail", wasm_i64_load64u_fail, fail);
run_test!("i64.store64.fail", wasm_i64_store64_fail, fail);
run_test!("i64", wasm_i64);
run_test!("if-else-end-label-mismatch.fail", wasm_if_else_end_label_mismatch_fail, fail);
run_test!("if-else-end-label-superfluous.fail", wasm_if_else_end_label_superfluous_fail, fail);
run_test!("if-else-label-mismatch.fail", wasm_if_else_label_mismatch_fail, fail);
run_test!("if-else-label-superfluous.fail", wasm_if_else_label_superfluous_fail, fail);
run_test!("if-end-label-mismatch.fail", wasm_if_end_label_mismatch_fail, fail);
run_test!("if-end-label-superfluous.fail", wasm_if_end_label_superfluous_fail, fail);
run_test!("if", wasm_if);
run_test!("import-after-func.fail", wasm_import_after_func_fail, fail);
run_test!("import-after-global.fail", wasm_import_after_global_fail, fail);
run_test!("import-after-memory.fail", wasm_import_after_memory_fail, fail);
run_test!("import-after-table.fail", wasm_import_after_table_fail, fail);
run_test!("imports", wasm_imports);
run_test!("inline-module", inline_module);
run_test!("int_exprs", wasm_int_exprs);
run_test!("int_literals", wasm_int_literals);
run_test!("labels", wasm_labels);
run_test!("left-to-right", wasm_left_to_right);
run_test!("linking", wasm_linking);
run_test!("load-align-0.fail", wasm_load_align_0_fail, fail);
run_test!("load-align-big.fail", wasm_load_align_big_fail, fail);
run_test!("load-align-odd.fail", wasm_load_align_odd_fail, fail);
run_test!("loop-end-label-mismatch.fail", wasm_end_label_mismatch_fail, fail);
run_test!("loop-end-label-superfluous.fail", wasm_end_label_superfluous_fail, fail);
run_test!("loop", wasm_loop);
run_test!("memory", wasm_memory);
run_test!("memory_redundancy", wasm_memory_redundancy);
run_test!("memory_trap", wasm_memory_trap);
run_test!("memory", wasm_memory);
run_test!("names", wasm_names);
run_test!("nop", wasm_nop);
run_test!("of_string-overflow-hex-u32.fail", wasm_of_string_overflow_hex_u32_fail, fail);
run_test!("of_string-overflow-hex-u64.fail", wasm_of_string_overflow_hex_u64_fail, fail);
run_test!("of_string-overflow-s32.fail", wasm_of_string_overflow_s32_fail, fail);
run_test!("of_string-overflow-s64.fail", wasm_of_string_overflow_s64_fail, fail);
run_test!("of_string-overflow-u32.fail", wasm_of_string_overflow_u32_fail, fail);
run_test!("of_string-overflow-u64.fail", wasm_of_string_overflow_u64_fail, fail);
run_test!("resizing", wasm_resizing);
run_test!("return", wasm_return);
run_test!("select", wasm_select);
@ -117,16 +68,16 @@ run_test!("skip-stack-guard-page", wasm_skip_stack_guard_page);
run_test!("stack", wasm_stack);
run_test!("start", wasm_start);
run_test!("store_retval", wasm_store_retval);
run_test!("store-align-0.fail", wasm_store_align_0_fail, fail);
run_test!("store-align-big.fail", wasm_store_align_big_fail, fail);
run_test!("store-align-odd.fail", wasm_store_align_odd_fail, fail);
run_test!("switch", wasm_switch);
run_test!("tee_local", wasm_tee_local);
run_test!("token", wasm_token);
run_test!("traps", wasm_traps);
run_test!("type", wasm_type);
run_test!("typecheck", wasm_typecheck);
run_test!("unreachable", wasm_unreachable);
run_test!("unreached-invalid", wasm_unreached_invalid);
run_test!("unwind", wasm_unwind);
run_test!("utf8-custom-selection-id", wasm_utf8_custom_selection_id);
run_test!("utf8-custom-section-id", wasm_utf8_custom_section_id);
run_test!("utf8-import-field", wasm_utf8_import_field);
run_test!("utf8-import-module", wasm_utf8_import_module);
run_test!("utf8-invalid-encoding", wasm_utf8_invalid_encoding);

View File

@ -25,9 +25,9 @@ enum Error {
}
impl From<InterpreterError> for Error {
fn from(e: InterpreterError) -> Error {
Error::Interpreter(e)
}
fn from(e: InterpreterError) -> Error {
Error::Interpreter(e)
}
}
struct SpecModule {
@ -143,6 +143,7 @@ impl ModuleImportResolver for SpecModule {
struct SpecDriver {
spec_module: SpecModule,
instances: HashMap<String, ModuleRef>,
last_module: Option<ModuleRef>,
}
impl SpecDriver {
@ -150,6 +151,7 @@ impl SpecDriver {
SpecDriver {
spec_module: SpecModule::new(),
instances: HashMap::new(),
last_module: None,
}
}
@ -157,8 +159,11 @@ impl SpecDriver {
&mut self.spec_module
}
fn add_module(&mut self, name: String, module: ModuleRef) {
self.instances.insert(name, module);
fn add_module(&mut self, name: Option<String>, module: ModuleRef) {
self.last_module = Some(module.clone());
if let Some(name) = name {
self.instances.insert(name, module);
}
}
fn module(&self, name: &str) -> Result<ModuleRef, InterpreterError> {
@ -166,6 +171,15 @@ impl SpecDriver {
InterpreterError::Instantiation(format!("Module not registered {}", name))
})
}
fn module_or_last(&self, name: Option<&str>) -> Result<ModuleRef, InterpreterError> {
match name {
Some(name) => self.module(name),
None => self.last_module.clone().ok_or_else(|| {
InterpreterError::Instantiation("No modules registered".into())
}),
}
}
}
impl ImportResolver for SpecDriver {
@ -231,10 +245,10 @@ fn try_load_module(base_dir: &Path, module_path: &str) -> Result<Module, Error>
let mut wasm_path = PathBuf::from(base_dir.clone());
wasm_path.push(module_path);
let mut file = File::open(wasm_path).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
Module::from_buffer(buf).map_err(|e| Error::Load(e.to_string()))
let mut file = File::open(wasm_path).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
Module::from_buffer(buf).map_err(|e| Error::Load(e.to_string()))
}
fn try_load(
@ -245,8 +259,8 @@ fn try_load(
let module = try_load_module(base_dir, module_path)?;
let instance = ModuleInstance::new(&module, &ImportsBuilder::default())?;
instance
.run_start(spec_driver.spec_module())
.map_err(|trap| Error::Start(trap))?;
.run_start(spec_driver.spec_module())
.map_err(|trap| Error::Start(trap))?;
Ok(())
}
@ -263,11 +277,8 @@ fn load_module(
.run_start(spec_driver.spec_module())
.expect("Run start failed");
let module_name = name.as_ref()
.map(|s| s.as_ref())
.unwrap_or("wasm_test")
.trim_left_matches('$');
spec_driver.add_module(module_name.to_owned(), instance.clone());
let module_name = name.clone();
spec_driver.add_module(module_name, instance.clone());
instance
}
@ -311,10 +322,8 @@ fn run_action(
ref field,
ref args,
} => {
let module = module.clone().unwrap_or("wasm_test".into());
let module = module.trim_left_matches('$');
let module = program.module(&module).expect(&format!(
"Expected program to have loaded module {}",
let module = program.module_or_last(module.as_ref().map(|x| x.as_ref())).expect(&format!(
"Expected program to have loaded module {:?}",
module
));
module.invoke_export(
@ -328,10 +337,8 @@ fn run_action(
ref field,
..
} => {
let module = module.clone().unwrap_or("wasm_test".into());
let module = module.trim_left_matches('$');
let module = program.module(&module).expect(&format!(
"Expected program to have loaded module {}",
let module = program.module_or_last(module.as_ref().map(|x| x.as_ref())).expect(&format!(
"Expected program to have loaded module {:?}",
module
));
let field = jstring_to_rstring(&field);
@ -361,13 +368,12 @@ pub fn run_wast2wasm(name: &str) -> FixtureParams {
let mut wast2wasm_path = PathBuf::from(outdir.clone());
wast2wasm_path.push("bin");
wast2wasm_path.push("wast2wasm");
wast2wasm_path.push("wast2json");
let mut json_spec_path = PathBuf::from(outdir.clone());
json_spec_path.push(&format!("{}.json", name));
let wast2wasm_output = Command::new(wast2wasm_path)
.arg("--spec")
.arg("-o")
.arg(&json_spec_path)
.arg(&format!("./wabt/third_party/testsuite/{}.wast", name))
@ -378,13 +384,13 @@ pub fn run_wast2wasm(name: &str) -> FixtureParams {
json: json_spec_path.to_str().unwrap().to_owned(),
failing: {
if !wast2wasm_output.status.success() {
println!("wasm2wast error code: {}", wast2wasm_output.status);
println!("wast2json error code: {}", wast2wasm_output.status);
println!(
"wasm2wast stdout: {}",
"wast2json stdout: {}",
String::from_utf8_lossy(&wast2wasm_output.stdout)
);
println!(
"wasm2wast stderr: {}",
"wast2json stderr: {}",
String::from_utf8_lossy(&wast2wasm_output.stderr)
);
true
@ -395,19 +401,22 @@ pub fn run_wast2wasm(name: &str) -> FixtureParams {
}
}
pub fn failing_spec(name: &str) {
let fixture = run_wast2wasm(name);
if !fixture.failing {
panic!("wasm2wast expected to fail, but terminated normally");
}
}
pub fn spec(name: &str) {
let tmpdir = env::var("OUT_DIR").unwrap();
let fixture = run_wast2wasm(name);
let wast2wasm_fail_expected = name.ends_with(".fail");
if wast2wasm_fail_expected {
if !fixture.failing {
panic!("wast2json expected to fail, but terminated normally");
}
// Failing fixture, bail out.
return;
}
if fixture.failing {
panic!("wasm2wast terminated abnormally, expected to success");
panic!("wast2json terminated abnormally, expected to success");
}
let mut f =
@ -416,7 +425,6 @@ pub fn spec(name: &str) {
serde_json::from_reader(&mut f).expect("Failed to deserialize JSON file");
let mut spec_driver = SpecDriver::new();
let mut last_module = None;
for command in &spec.commands {
println!("command {:?}", command);
match command {
@ -425,7 +433,7 @@ pub fn spec(name: &str) {
ref filename,
..
} => {
last_module = Some(load_module(tmpdir.as_ref(), &filename, &name, &mut spec_driver));
load_module(tmpdir.as_ref(), &filename, &name, &mut spec_driver);
}
&test::Command::AssertReturn {
line,
@ -533,19 +541,16 @@ pub fn spec(name: &str) {
Err(e) => println!("assert_uninstantiable - success ({:?})", e),
},
&test::Command::Register {
line,
ref name,
ref as_name,
..
} => {
match name {
&Some(ref name) => assert_eq!(name.trim_left_matches('$'), as_name), // we have already registered this module without $ prefix
&None => spec_driver.add_module(
as_name.clone(),
last_module
.take()
.expect("Last module must be set for this command"),
),
}
let module = match spec_driver.module_or_last(name.as_ref().map(|x| x.as_ref())) {
Ok(module) => module,
Err(e) => panic!("No such module, at line {} - ({:?})", e, line),
};
spec_driver.add_module(Some(as_name.clone()), module);
}
&test::Command::Action { line, ref action } => {
match run_action(&mut spec_driver, action) {

@ -1 +1 @@
Subproject commit 1e5b546c65e8fef609431e77b1b2434dbf162e6c
Subproject commit a74072b2163ca20645e4a313636507cb3984f5fb

View File

@ -26,6 +26,10 @@ pub struct BlockFrame {
pub end_position: usize,
/// A limit integer value, which is an index into the value stack indicating where to reset it to on a branch to that label.
pub value_stack_len: usize,
/// Boolean which signals whether value stack became polymorphic. Value stack starts in non-polymorphic state and
/// becomes polymorphic only after an instruction that never passes control further is executed,
/// i.e. `unreachable`, `br` (but not `br_if`!), etc.
pub polymorphic_stack: bool,
}
/// Type of block frame.

View File

@ -50,7 +50,13 @@ impl<T> StackWithLimit<T> where T: Clone {
pub fn top(&self) -> Result<&T, Error> {
self.values
.back()
.ok_or(Error("non-empty stack expected".into()))
.ok_or_else(|| Error("non-empty stack expected".into()))
}
pub fn top_mut(&mut self) -> Result<&mut T, Error> {
self.values
.back_mut()
.ok_or_else(|| Error("non-empty stack expected".into()))
}
pub fn get(&self, index: usize) -> Result<&T, Error> {
@ -73,7 +79,7 @@ impl<T> StackWithLimit<T> where T: Clone {
pub fn pop(&mut self) -> Result<T, Error> {
self.values
.pop_back()
.ok_or(Error("non-empty stack expected".into()))
.ok_or_else(|| Error("non-empty stack expected".into()))
}
pub fn resize(&mut self, new_size: usize, dummy: T) {

View File

@ -1153,6 +1153,7 @@ impl FunctionContext {
branch_position: branch_position,
end_position: end_position,
value_stack_len: self.value_stack.len(),
polymorphic_stack: false,
}).map_err(|_| TrapKind::StackOverflow)?;
Ok(())

View File

@ -41,8 +41,6 @@ struct FunctionValidationContext<'a> {
enum StackValueType {
/// Any value type.
Any,
/// Any number of any values of any type.
AnyUnlimited,
/// Concrete value type.
Specific(ValueType),
}
@ -347,13 +345,13 @@ impl Validator {
}
fn validate_drop(context: &mut FunctionValidationContext) -> Result<InstructionOutcome, Error> {
context.pop_any_value().map(|_| ())?;
context.pop_value(StackValueType::Any).map(|_| ())?;
Ok(InstructionOutcome::ValidateNextInstruction)
}
fn validate_select(context: &mut FunctionValidationContext) -> Result<InstructionOutcome, Error> {
context.pop_value(ValueType::I32.into())?;
let select_type = context.pop_any_value()?;
let select_type = context.pop_value(StackValueType::Any)?;
context.pop_value(select_type)?;
context.push_value(select_type)?;
Ok(InstructionOutcome::ValidateNextInstruction)
@ -367,7 +365,7 @@ impl Validator {
fn validate_set_local(context: &mut FunctionValidationContext, index: u32) -> Result<InstructionOutcome, Error> {
let local_type = context.require_local(index)?;
let value_type = context.pop_any_value()?;
let value_type = context.pop_value(StackValueType::Any)?;
if local_type != value_type {
return Err(Error(format!("Trying to update local {} of type {:?} with value of type {:?}", index, local_type, value_type)));
}
@ -376,7 +374,7 @@ impl Validator {
fn validate_tee_local(context: &mut FunctionValidationContext, index: u32) -> Result<InstructionOutcome, Error> {
let local_type = context.require_local(index)?;
let value_type = context.tee_any_value()?;
let value_type = context.tee_value(StackValueType::Any)?;
if local_type != value_type {
return Err(Error(format!("Trying to update local {} of type {:?} with value of type {:?}", index, local_type, value_type)));
}
@ -397,7 +395,7 @@ impl Validator {
let global = context.module.require_global(index, Some(true))?;
global.content_type().into()
};
let value_type = context.pop_any_value()?;
let value_type = context.pop_value(StackValueType::Any)?;
if global_type != value_type {
return Err(Error(format!("Trying to update global {} of type {:?} with value of type {:?}", index, global_type, value_type)));
}
@ -610,47 +608,50 @@ impl<'a> FunctionValidationContext<'a> {
Ok(self.value_stack.push(value_type.into())?)
}
fn pop_value(&mut self, value_type: StackValueType) -> Result<(), Error> {
self.check_stack_access()?;
match self.value_stack.pop()? {
StackValueType::Specific(stack_value_type) if stack_value_type == value_type => Ok(()),
StackValueType::Any => Ok(()),
StackValueType::AnyUnlimited => {
self.value_stack.push(StackValueType::AnyUnlimited)?;
Ok(())
},
stack_value_type @ _ => Err(Error(format!("Expected value of type {:?} on top of stack. Got {:?}", value_type, stack_value_type))),
fn pop_value(&mut self, value_type: StackValueType) -> Result<StackValueType, Error> {
let (is_stack_polymorphic, label_value_stack_len) = {
let frame = self.top_label()?;
(frame.polymorphic_stack, frame.value_stack_len)
};
let stack_is_empty = self.value_stack.len() == label_value_stack_len;
let actual_value = if stack_is_empty && is_stack_polymorphic {
StackValueType::Any
} else {
self.check_stack_access()?;
self.value_stack.pop()?
};
match actual_value {
StackValueType::Specific(stack_value_type) if stack_value_type == value_type => {
Ok(actual_value)
}
StackValueType::Any => Ok(actual_value),
stack_value_type @ _ => Err(Error(format!(
"Expected value of type {:?} on top of stack. Got {:?}",
value_type, stack_value_type
))),
}
}
fn tee_value(&mut self, value_type: StackValueType) -> Result<(), Error> {
self.check_stack_access()?;
match *self.value_stack.top()? {
StackValueType::Specific(stack_value_type) if stack_value_type == value_type => Ok(()),
StackValueType::Any | StackValueType::AnyUnlimited => Ok(()),
stack_value_type @ _ => Err(Error(format!("Expected value of type {:?} on top of stack. Got {:?}", value_type, stack_value_type))),
fn check_stack_access(&self) -> Result<(), Error> {
let value_stack_min = self.frame_stack.top().expect("at least 1 topmost block").value_stack_len;
if self.value_stack.len() > value_stack_min {
Ok(())
} else {
Err(Error("Trying to access parent frame stack values.".into()))
}
}
fn pop_any_value(&mut self) -> Result<StackValueType, Error> {
self.check_stack_access()?;
match self.value_stack.pop()? {
StackValueType::Specific(stack_value_type) => Ok(StackValueType::Specific(stack_value_type)),
StackValueType::Any => Ok(StackValueType::Any),
StackValueType::AnyUnlimited => {
self.value_stack.push(StackValueType::AnyUnlimited)?;
Ok(StackValueType::Any)
},
}
}
fn tee_any_value(&mut self) -> Result<StackValueType, Error> {
self.check_stack_access()?;
Ok(self.value_stack.top().map(Clone::clone)?)
fn tee_value(&mut self, value_type: StackValueType) -> Result<StackValueType, Error> {
let value = self.pop_value(value_type)?;
self.push_value(value)?;
Ok(value)
}
fn unreachable(&mut self) -> Result<(), Error> {
Ok(self.value_stack.push(StackValueType::AnyUnlimited)?)
let frame = self.frame_stack.top_mut()?;
self.value_stack.resize(frame.value_stack_len, StackValueType::Any);
frame.polymorphic_stack = true;
Ok(())
}
fn top_label(&self) -> Result<&BlockFrame, Error> {
@ -665,23 +666,31 @@ impl<'a> FunctionValidationContext<'a> {
branch_position: self.position,
end_position: self.position,
value_stack_len: self.value_stack.len(),
polymorphic_stack: false,
})?)
}
fn pop_label(&mut self) -> Result<InstructionOutcome, Error> {
let frame = self.frame_stack.pop()?;
let actual_value_type = if self.value_stack.len() > frame.value_stack_len {
Some(self.value_stack.pop()?)
} else {
None
};
self.value_stack.resize(frame.value_stack_len, StackValueType::Any);
match frame.block_type {
BlockType::NoResult if actual_value_type.map(|vt| vt.is_any_unlimited()).unwrap_or(true) => (),
BlockType::Value(required_value_type) if actual_value_type.map(|vt| vt == required_value_type).unwrap_or(false) => (),
_ => return Err(Error(format!("Expected block to return {:?} while it has returned {:?}", frame.block_type, actual_value_type))),
// Don't pop frame yet. This is essential since we still might pop values from the value stack
// and this in turn requires current frame to check whether or not we've reached
// unreachable.
let block_type = self.frame_stack.top()?.block_type;
match block_type {
BlockType::NoResult => (),
BlockType::Value(required_value_type) => {
self.pop_value(StackValueType::Specific(required_value_type))?;
}
}
let frame = self.frame_stack.pop()?;
if self.value_stack.len() != frame.value_stack_len {
return Err(Error(format!(
"Unexpected stack height {}, expected {}",
self.value_stack.len(),
frame.value_stack_len
)));
}
if !self.frame_stack.is_empty() {
self.labels.insert(frame.begin_position, self.position);
}
@ -707,15 +716,6 @@ impl<'a> FunctionValidationContext<'a> {
.ok_or(Error(format!("Trying to access local with index {} when there are only {} locals", idx, self.locals.len())))
}
fn check_stack_access(&self) -> Result<(), Error> {
let value_stack_min = self.frame_stack.top().expect("at least 1 topmost block").value_stack_len;
if self.value_stack.len() > value_stack_min {
Ok(())
} else {
Err(Error("Trying to access parent frame stack values.".into()))
}
}
fn into_labels(self) -> HashMap<usize, usize> {
self.labels
}
@ -729,16 +729,9 @@ impl StackValueType {
}
}
fn is_any_unlimited(&self) -> bool {
match self {
&StackValueType::AnyUnlimited => true,
_ => false,
}
}
fn value_type(&self) -> ValueType {
match self {
&StackValueType::Any | &StackValueType::AnyUnlimited => unreachable!("must be checked by caller"),
&StackValueType::Any => unreachable!("must be checked by caller"),
&StackValueType::Specific(value_type) => value_type,
}
}
@ -752,7 +745,7 @@ impl From<ValueType> for StackValueType {
impl PartialEq<StackValueType> for StackValueType {
fn eq(&self, other: &StackValueType) -> bool {
if self.is_any() || other.is_any() || self.is_any_unlimited() || other.is_any_unlimited() {
if self.is_any() || other.is_any() {
true
} else {
self.value_type() == other.value_type()
@ -762,7 +755,7 @@ impl PartialEq<StackValueType> for StackValueType {
impl PartialEq<ValueType> for StackValueType {
fn eq(&self, other: &ValueType) -> bool {
if self.is_any() || self.is_any_unlimited() {
if self.is_any() {
true
} else {
self.value_type() == *other

View File

@ -1,6 +1,6 @@
use std::error;
use std::fmt;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use parity_wasm::elements::{
BlockType, External, GlobalEntry, GlobalType, Internal, MemoryType, Module, Opcode,
ResizableLimits, TableType, ValueType, InitExpr, Type
@ -163,7 +163,15 @@ pub fn validate_module(module: Module) -> Result<ValidatedModule, Error> {
// validate export section
if let Some(export_section) = module.export_section() {
let mut export_names = HashSet::with_capacity(export_section.entries().len());
for export in export_section.entries() {
// HashSet::insert returns false if item already in set.
let duplicate = export_names.insert(export.field()) == false;
if duplicate {
return Err(Error(
format!("duplicate export {}", export.field()),
));
}
match *export.internal() {
Internal::Function(function_index) => {
context.require_function(function_index)?;