From a774a2cb29816fd0152a90229536ce8a8bfdf3dc Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Tue, 30 Jul 2019 16:44:05 +0200 Subject: [PATCH] Update parity-wasm to 0.39 --- Cargo.toml | 2 +- cli/Cargo.toml | 2 +- src/ext.rs | 7 ++- src/gas/mod.rs | 6 ++- src/gas/validation.rs | 4 +- src/graph.rs | 22 ++++++---- src/optimizer.rs | 80 +++++++++++++++++++++++----------- src/pack.rs | 9 +++- src/rules.rs | 2 +- src/stack_height/max_height.rs | 6 +-- 10 files changed, 93 insertions(+), 47 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e221c41..136619f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ description = "Collection of command-line utilities and corresponding Rust api f keywords = ["wasm", "webassembly", "pwasm"] [dependencies] -parity-wasm = { version = "0.31", default-features = false } +parity-wasm = { version = "0.39", default-features = false } log = { version = "0.4", default-features = false } byteorder = { version = "1", default-features = false } diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 5a4cb9e..728435a 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -38,7 +38,7 @@ name = "wasm-check" path = "check/main.rs" [dependencies] -parity-wasm = "0.31" +parity-wasm = "0.39" pwasm-utils = { path = "..", version = "0.8" } glob = "0.2" clap = "2.24" diff --git a/src/ext.rs b/src/ext.rs index 23a61bd..b13029a 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -98,7 +98,12 @@ pub fn shrink_unknown_stack( match section { &mut elements::Section::Data(ref mut data_section) => { for ref mut data_segment in data_section.entries_mut() { - if data_segment.offset().code() == &[elements::Instruction::I32Const(4), elements::Instruction::End] { + if data_segment + .offset() + .as_ref() + .expect("parity-wasm is compiled without bulk-memory operations") + .code() == &[elements::Instruction::I32Const(4), elements::Instruction::End] + { assert_eq!(data_segment.value().len(), 4); let current_val = LittleEndian::read_u32(data_segment.value()); let new_val = current_val - shrink_amount; diff --git a/src/gas/mod.rs b/src/gas/mod.rs index 59d728a..5869a0d 100644 --- a/src/gas/mod.rs +++ b/src/gas/mod.rs @@ -306,11 +306,13 @@ pub(crate) fn determine_metered_blocks( let target_index = active_index.checked_sub(label as usize).ok_or_else(|| ())?; counter.branch(cursor, &[target_index])?; } - BrTable(ref label_vec, label_default) => { + BrTable(ref br_table_data) => { counter.increment(instruction_cost)?; let active_index = counter.active_control_block_index().ok_or_else(|| ())?; - let target_indices = [label_default].iter().chain(label_vec.iter()) + let target_indices = [br_table_data.default] + .iter() + .chain(br_table_data.table.iter()) .map(|label| active_index.checked_sub(*label as usize)) .collect::>>() .ok_or_else(|| ())?; diff --git a/src/gas/validation.rs b/src/gas/validation.rs index 7fb6c6e..18dc101 100644 --- a/src/gas/validation.rs +++ b/src/gas/validation.rs @@ -241,11 +241,11 @@ fn build_control_flow_graph( graph.new_forward_edge(active_node_id, new_node_id); graph.set_first_instr_pos(new_node_id, cursor + 1); } - Instruction::BrTable(ref label_vec, label_default) => { + Instruction::BrTable(ref br_table_data) => { graph.increment_actual_cost(active_node_id, instruction_cost); let active_frame_idx = stack.len() - 1; - for &label in [label_default].iter().chain(label_vec.iter()) { + for &label in [br_table_data.default].iter().chain(br_table_data.table.iter()) { let target_frame_idx = active_frame_idx - (label as usize); graph.new_edge(active_node_id, &stack[target_frame_idx]); } diff --git a/src/graph.rs b/src/graph.rs index bfb0838..ad10598 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -354,9 +354,12 @@ impl Module { // }; // TODO: update parity-wasm and uncomment the above instead - let location = SegmentLocation::Default( - res.map_instructions(element_segment.offset().code()) - ); + let init_expr = element_segment + .offset() + .as_ref() + .expect("parity-wasm is compiled without bulk-memory operations") + .code(); + let location = SegmentLocation::Default(res.map_instructions(init_expr)); let funcs_map = element_segment .members().iter() @@ -390,9 +393,12 @@ impl Module { for data_segment in data_section.entries() { // TODO: update parity-wasm and use the same logic as in // commented element segment branch - let location = SegmentLocation::Default( - res.map_instructions(data_segment.offset().code()) - ); + let init_expr = data_segment + .offset() + .as_ref() + .expect("parity-wasm is compiled without bulk-memory operations") + .code(); + let location = SegmentLocation::Default(res.map_instructions(init_expr)); res.data.push(DataSegment { value: data_segment.value().to_vec(), @@ -675,7 +681,7 @@ impl Module { element_segments.push( elements::ElementSegment::new( 0, - elements::InitExpr::new(self.generate_instructions(&offset_expr[..])), + Some(elements::InitExpr::new(self.generate_instructions(&offset_expr[..]))), elements_map, ) ); @@ -728,7 +734,7 @@ impl Module { data_segments.push( elements::DataSegment::new( 0, - elements::InitExpr::new(self.generate_instructions(&offset_expr[..])), + Some(elements::InitExpr::new(self.generate_instructions(&offset_expr[..]))), data_entry.value.clone(), ) ); diff --git a/src/optimizer.rs b/src/optimizer.rs index ca2a3c5..8e2388f 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -46,12 +46,28 @@ pub fn optimize( let mut init_symbols = Vec::new(); if let Some(data_section) = module.data_section() { for segment in data_section.entries() { - push_code_symbols(&module, segment.offset().code(), &mut init_symbols); + push_code_symbols( + &module, + segment + .offset() + .as_ref() + .expect("parity-wasm is compiled without bulk-memory operations") + .code(), + &mut init_symbols, + ); } } if let Some(elements_section) = module.elements_section() { for segment in elements_section.entries() { - push_code_symbols(&module, segment.offset().code(), &mut init_symbols); + push_code_symbols( + &module, + segment + .offset() + .as_ref() + .expect("parity-wasm is compiled without bulk-memory operations") + .code(), + &mut init_symbols + ); for func_index in segment.members() { stay.insert(resolve_function(&module, *func_index)); } @@ -256,12 +272,26 @@ pub fn optimize( }, &mut elements::Section::Data(ref mut data_section) => { for ref mut segment in data_section.entries_mut() { - update_global_index(segment.offset_mut().code_mut(), &eliminated_globals) + update_global_index( + segment + .offset_mut() + .as_mut() + .expect("parity-wasm is compiled without bulk-memory operations") + .code_mut(), + &eliminated_globals, + ) } }, &mut elements::Section::Element(ref mut elements_section) => { for ref mut segment in elements_section.entries_mut() { - update_global_index(segment.offset_mut().code_mut(), &eliminated_globals); + update_global_index( + segment + .offset_mut() + .as_mut() + .expect("parity-wasm is compiled without bulk-memory operations") + .code_mut(), + &eliminated_globals + ); // update all indirect call addresses initial values for func_index in segment.members_mut() { let totalle = eliminated_funcs.iter().take_while(|i| (**i as u32) < *func_index).count(); @@ -270,31 +300,29 @@ pub fn optimize( } }, &mut elements::Section::Name(ref mut name_section) => { - match name_section { - &mut elements::NameSection::Function(ref mut func_name) => { - let mut func_name_map = mem::replace(func_name.names_mut(), elements::IndexMap::default()); - for index in &eliminated_funcs { - func_name_map.remove(*index as u32); - } - let updated_map = func_name_map.into_iter().map(|(index, value)| { - let totalle = eliminated_funcs.iter().take_while(|i| (**i as u32) < index).count() as u32; - (index - totalle, value) - }).collect(); - mem::replace(func_name.names_mut(), updated_map); + if let Some(ref mut func_name) = name_section.functions_mut() { + let mut func_name_map = mem::replace(func_name.names_mut(), elements::IndexMap::default()); + for index in &eliminated_funcs { + func_name_map.remove(*index as u32); } - &mut elements::NameSection::Local(ref mut local_name) => { - let mut local_names_map = mem::replace(local_name.local_names_mut(), elements::IndexMap::default()); - for index in &eliminated_funcs { - local_names_map.remove(*index as u32); - } - let updated_map = local_names_map.into_iter().map(|(index, value)| { - let totalle = eliminated_funcs.iter().take_while(|i| (**i as u32) < index).count() as u32; - (index - totalle, value) - }).collect(); + let updated_map = func_name_map.into_iter().map(|(index, value)| { + let totalle = eliminated_funcs.iter().take_while(|i| (**i as u32) < index).count() as u32; + (index - totalle, value) + }).collect(); + mem::replace(func_name.names_mut(), updated_map); + } - mem::replace(local_name.local_names_mut(), updated_map); + if let Some(ref mut local_name) = name_section.locals_mut() { + let mut local_names_map = mem::replace(local_name.local_names_mut(), elements::IndexMap::default()); + for index in &eliminated_funcs { + local_names_map.remove(*index as u32); } - _ => {} + let updated_map = local_names_map.into_iter().map(|(index, value)| { + let totalle = eliminated_funcs.iter().take_while(|i| (**i as u32) < index).count() as u32; + (index - totalle, value) + }).collect(); + + mem::replace(local_name.local_names_mut(), updated_map); } } _ => { } diff --git a/src/pack.rs b/src/pack.rs index b850ed9..946074f 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -162,7 +162,12 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module, tar for section in ctor_module.sections_mut() { if let &mut Section::Data(ref mut data_section) = section { let (index, offset) = if let Some(ref entry) = data_section.entries().iter().last() { - if let Instruction::I32Const(offst) = entry.offset().code()[0] { + let init_expr = entry + .offset() + .as_ref() + .expect("parity-wasm is compiled without bulk-memory operations") + .code(); + if let Instruction::I32Const(offst) = init_expr[0] { let len = entry.value().len() as i32; let offst = offst as i32; (entry.index(), offst + (len + 4) - len % 4) @@ -174,7 +179,7 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module, tar }; let code_data = DataSegment::new( index, - InitExpr::new(vec![Instruction::I32Const(offset), Instruction::End]), + Some(InitExpr::new(vec![Instruction::I32Const(offset), Instruction::End])), raw_module.clone() ); data_section.entries_mut().push(code_data); diff --git a/src/rules.rs b/src/rules.rs index 78fe153..ae0dd17 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -83,7 +83,7 @@ impl InstructionType { End => InstructionType::ControlFlow, Br(_) => InstructionType::ControlFlow, BrIf(_) => InstructionType::ControlFlow, - BrTable(_, _) => InstructionType::ControlFlow, + BrTable(_) => InstructionType::ControlFlow, Return => InstructionType::ControlFlow, Call(_) => InstructionType::ControlFlow, CallIndirect(_, _) => InstructionType::ControlFlow, diff --git a/src/stack_height/max_height.rs b/src/stack_height/max_height.rs index 834f445..2f7cde1 100644 --- a/src/stack_height/max_height.rs +++ b/src/stack_height/max_height.rs @@ -245,11 +245,11 @@ pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result { - let arity_of_default = stack.frame(default_target)?.branch_arity; + BrTable(ref br_table_data) => { + let arity_of_default = stack.frame(br_table_data.default)?.branch_arity; // Check that all jump targets have an equal arities. - for target in targets.iter() { + for target in &*br_table_data.table { let arity = stack.frame(*target)?.branch_arity; if arity != arity_of_default { return Err(Error(