diff --git a/Cargo.toml b/Cargo.toml index 0cd0575..ef43a42 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.30", default-features = false } +parity-wasm = { version = "0.31", default-features = false } log = { version = "0.4", default-features = false } byteorder = { version = "1", default-features = false } diff --git a/src/ext.rs b/src/ext.rs index 8a232f8..23a61bd 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -8,10 +8,10 @@ use byteorder::{LittleEndian, ByteOrder}; type Insertion = (usize, u32, u32, String); -pub fn update_call_index(opcodes: &mut elements::Opcodes, original_imports: usize, inserts: &[Insertion]) { - use parity_wasm::elements::Opcode::*; - for opcode in opcodes.elements_mut().iter_mut() { - if let &mut Call(ref mut call_index) = opcode { +pub fn update_call_index(instructions: &mut elements::Instructions, original_imports: usize, inserts: &[Insertion]) { + use parity_wasm::elements::Instruction::*; + for instruction in instructions.elements_mut().iter_mut() { + if let &mut Call(ref mut call_index) = instruction { if let Some(pos) = inserts.iter().position(|x| x.1 == *call_index) { *call_index = (original_imports + pos) as u32; } else if *call_index as usize > original_imports { @@ -98,7 +98,7 @@ 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::Opcode::I32Const(4), elements::Opcode::End] { + if data_segment.offset().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.rs b/src/gas.rs index 18d1197..c189fe1 100644 --- a/src/gas.rs +++ b/src/gas.rs @@ -3,10 +3,10 @@ use std::vec::Vec; use parity_wasm::{elements, builder}; use rules; -pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) { - use parity_wasm::elements::Opcode::*; - for opcode in opcodes.elements_mut().iter_mut() { - if let &mut Call(ref mut call_index) = opcode { +pub fn update_call_index(instructions: &mut elements::Instructions, inserted_index: u32) { + use parity_wasm::elements::Instruction::*; + for instruction in instructions.elements_mut().iter_mut() { + if let &mut Call(ref mut call_index) = instruction { if *call_index >= inserted_index { *call_index += 1} } } @@ -83,12 +83,12 @@ impl Counter { } } -fn inject_grow_counter(opcodes: &mut elements::Opcodes, grow_counter_func: u32) -> usize { - use parity_wasm::elements::Opcode::*; +fn inject_grow_counter(instructions: &mut elements::Instructions, grow_counter_func: u32) -> usize { + use parity_wasm::elements::Instruction::*; let mut counter = 0; - for opcode in opcodes.elements_mut() { - if let GrowMemory(_) = *opcode { - *opcode = Call(grow_counter_func); + for instruction in instructions.elements_mut() { + if let GrowMemory(_) = *instruction { + *instruction = Call(grow_counter_func); counter += 1; } } @@ -96,14 +96,14 @@ fn inject_grow_counter(opcodes: &mut elements::Opcodes, grow_counter_func: u32) } fn add_grow_counter(module: elements::Module, rules: &rules::Set, gas_func: u32) -> elements::Module { - use parity_wasm::elements::Opcode::*; + use parity_wasm::elements::Instruction::*; let mut b = builder::from_module(module); b.push_function( builder::function() .signature().params().i32().build().with_return_type(Some(elements::ValueType::I32)).build() .body() - .with_opcodes(elements::Opcodes::new(vec![ + .with_instructions(elements::Instructions::new(vec![ GetLocal(0), GetLocal(0), I32Const(rules.grow_cost() as i32), @@ -121,24 +121,24 @@ fn add_grow_counter(module: elements::Module, rules: &rules::Set, gas_func: u32) } pub fn inject_counter( - opcodes: &mut elements::Opcodes, + instructions: &mut elements::Instructions, rules: &rules::Set, gas_func: u32, ) -> Result<(), ()> { - use parity_wasm::elements::Opcode::*; + use parity_wasm::elements::Instruction::*; let mut counter = Counter::new(); // Begin an implicit function (i.e. `func...end`) block. counter.begin(0); - for cursor in 0..opcodes.elements().len() { - let opcode = &opcodes.elements()[cursor]; - match *opcode { + for cursor in 0..instructions.elements().len() { + let instruction = &instructions.elements()[cursor]; + match *instruction { Block(_) | If(_) | Loop(_) => { // Increment previous block with the cost of the current opcode. - let opcode_cost = rules.process(opcode)?; - counter.increment(opcode_cost)?; + let instruction_cost = rules.process(instruction)?; + counter.increment(instruction_cost)?; // Begin new block. The cost of the following opcodes until `End` or `Else` will // be included into this block. @@ -164,8 +164,8 @@ pub fn inject_counter( } _ => { // An ordinal non control flow instruction. Just increment the cost of the current block. - let opcode_cost = rules.process(opcode)?; - counter.increment(opcode_cost)?; + let instruction_cost = rules.process(instruction)?; + counter.increment(instruction_cost)?; } } } @@ -175,8 +175,8 @@ pub fn inject_counter( for block in counter.blocks { let effective_pos = block.start_pos + cumulative_offset; - opcodes.elements_mut().insert(effective_pos, I32Const(block.cost as i32)); - opcodes.elements_mut().insert(effective_pos+1, Call(gas_func)); + instructions.elements_mut().insert(effective_pos, I32Const(block.cost as i32)); + instructions.elements_mut().insert(effective_pos+1, Call(gas_func)); // Take into account these two inserted instructions. cumulative_offset += 2; diff --git a/src/optimizer.rs b/src/optimizer.rs index 2d86ddc..62a37e5 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -270,10 +270,10 @@ pub fn optimize( } -pub fn update_call_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) { - use parity_wasm::elements::Opcode::*; - for opcode in opcodes.elements_mut().iter_mut() { - if let &mut Call(ref mut call_index) = opcode { +pub fn update_call_index(instructions: &mut elements::Instructions, eliminated_indices: &[usize]) { + use parity_wasm::elements::Instruction::*; + for instruction in instructions.elements_mut().iter_mut() { + if let &mut Call(ref mut call_index) = instruction { let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *call_index).count(); trace!("rewired call {} -> call {}", *call_index, *call_index - totalle as u32); *call_index -= totalle as u32; @@ -282,10 +282,10 @@ pub fn update_call_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[ } /// Updates global references considering the _ordered_ list of eliminated indices -pub fn update_global_index(opcodes: &mut Vec, eliminated_indices: &[usize]) { - use parity_wasm::elements::Opcode::*; - for opcode in opcodes.iter_mut() { - match opcode { +pub fn update_global_index(instructions: &mut Vec, eliminated_indices: &[usize]) { + use parity_wasm::elements::Instruction::*; + for instruction in instructions.iter_mut() { + match instruction { &mut GetGlobal(ref mut index) | &mut SetGlobal(ref mut index) => { let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *index).count(); trace!("rewired global {} -> global {}", *index, *index - totalle as u32); @@ -297,10 +297,10 @@ pub fn update_global_index(opcodes: &mut Vec, eliminated_indic } /// Updates global references considering the _ordered_ list of eliminated indices -pub fn update_type_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) { - use parity_wasm::elements::Opcode::*; - for opcode in opcodes.elements_mut().iter_mut() { - if let &mut CallIndirect(ref mut call_index, _) = opcode { +pub fn update_type_index(instructions: &mut elements::Instructions, eliminated_indices: &[usize]) { + use parity_wasm::elements::Instruction::*; + for instruction in instructions.elements_mut().iter_mut() { + if let &mut CallIndirect(ref mut call_index, _) = instruction { let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *call_index).count(); trace!("rewired call_indrect {} -> call_indirect {}", *call_index, *call_index - totalle as u32); *call_index -= totalle as u32; @@ -435,10 +435,10 @@ mod tests { .function() .signature().param().i32().build() .body() - .with_opcodes(elements::Opcodes::new( + .with_opcodes(elements::Instructions::new( vec![ - elements::Opcode::GetGlobal(0), - elements::Opcode::End + elements::Instruction::GetGlobal(0), + elements::Instruction::End ] )) .build() @@ -477,10 +477,10 @@ mod tests { .function() .signature().param().i32().build() .body() - .with_opcodes(elements::Opcodes::new( + .with_opcodes(elements::Instructions::new( vec![ - elements::Opcode::GetGlobal(1), - elements::Opcode::End + elements::Instruction::GetGlobal(1), + elements::Instruction::End ] )) .build() @@ -510,10 +510,10 @@ mod tests { .function() .signature().param().i32().build() .body() - .with_opcodes(elements::Opcodes::new( + .with_opcodes(elements::Instructions::new( vec![ - elements::Opcode::Call(1), - elements::Opcode::End + elements::Instruction::Call(1), + elements::Instruction::End ] )) .build() @@ -563,10 +563,10 @@ mod tests { .function() .signature().param().i32().build() .body() - .with_opcodes(elements::Opcodes::new( + .with_opcodes(elements::Instructions::new( vec![ - elements::Opcode::CallIndirect(1, 0), - elements::Opcode::End + elements::Instruction::CallIndirect(1, 0), + elements::Instruction::End ] )) .build() @@ -586,7 +586,7 @@ mod tests { let indirect_opcode = &module.code_section().expect("code section to be generated").bodies()[0].code().elements()[0]; match *indirect_opcode { - elements::Opcode::CallIndirect(0, 0) => {}, + elements::Instruction::CallIndirect(0, 0) => {}, _ => { panic!( "Expected call_indirect to use index 0 after optimization, since previois 0th was eliminated, but got {:?}", diff --git a/src/pack.rs b/src/pack.rs index 206d879..ee38d34 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -3,7 +3,7 @@ use std::vec::Vec; use std::borrow::ToOwned; use parity_wasm::elements::{ - self, Section, DataSection, Opcode, DataSegment, InitExpr, Internal, External, + self, Section, DataSection, Instruction, DataSegment, InitExpr, Internal, External, ImportCountType, }; use parity_wasm::builder; @@ -162,7 +162,7 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> 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 Opcode::I32Const(offst) = entry.offset().code()[0] { + if let Instruction::I32Const(offst) = entry.offset().code()[0] { let len = entry.value().len() as i32; let offst = offst as i32; (entry.index(), offst + (len + 4) - len % 4) @@ -174,7 +174,7 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> }; let code_data = DataSegment::new( index, - InitExpr::new(vec![Opcode::I32Const(offset), Opcode::End]), + InitExpr::new(vec![Instruction::I32Const(offset), Instruction::End]), raw_module.clone() ); data_section.entries_mut().push(code_data); @@ -185,13 +185,13 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> let mut new_module = builder::from_module(ctor_module) .function() .signature().build() - .body().with_opcodes(elements::Opcodes::new( + .body().with_instructions(elements::Instructions::new( vec![ - Opcode::Call((create_func_id + ctor_import_functions) as u32), - Opcode::I32Const(code_data_address), - Opcode::I32Const(raw_module.len() as i32), - Opcode::Call(ret_function_id as u32), - Opcode::End, + Instruction::Call((create_func_id + ctor_import_functions) as u32), + Instruction::I32Const(code_data_address), + Instruction::I32Const(raw_module.len() as i32), + Instruction::Call(ret_function_id as u32), + Instruction::End, ])).build() .build() .build(); @@ -249,9 +249,9 @@ mod test { .function() .signature().build() .body() - .with_opcodes(elements::Opcodes::new( + .with_opcodes(elements::Instructions::new( vec![ - elements::Opcode::End + elements::Instruction::End ] )) .build() @@ -259,9 +259,9 @@ mod test { .function() .signature().build() .body() - .with_opcodes(elements::Opcodes::new( + .with_opcodes(elements::Instructions::new( vec![ - elements::Opcode::End + elements::Instruction::End ] )) .build() @@ -287,7 +287,7 @@ mod test { .external().memory(1 as u32, Some(1 as u32)) .build() .data() - .offset(elements::Opcode::I32Const(16)).value(vec![0u8]) + .offset(elements::Instruction::I32Const(16)).value(vec![0u8]) .build() .function() .signature() @@ -298,9 +298,9 @@ mod test { .function() .signature().build() .body() - .with_opcodes(elements::Opcodes::new( + .with_opcodes(elements::Instructions::new( vec![ - elements::Opcode::End + elements::Instruction::End ] )) .build() @@ -308,9 +308,9 @@ mod test { .function() .signature().build() .body() - .with_opcodes(elements::Opcodes::new( + .with_opcodes(elements::Instructions::new( vec![ - elements::Opcode::End + elements::Instruction::End ] )) .build() diff --git a/src/rules.rs b/src/rules.rs index 0c94f18..673812d 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -70,10 +70,10 @@ impl ::std::str::FromStr for InstructionType { } impl InstructionType { - pub fn op(opcode: &elements::Opcode) -> Self { - use parity_wasm::elements::Opcode::*; + pub fn op(instruction: &elements::Instruction) -> Self { + use parity_wasm::elements::Instruction::*; - match *opcode { + match *instruction { Unreachable => InstructionType::Unreachable, Nop => InstructionType::Nop, Block(_) => InstructionType::ControlFlow, @@ -288,8 +288,8 @@ impl Set { Set { regular: regular, entries: entries, grow: 0 } } - pub fn process(&self, opcode: &elements::Opcode) -> Result { - match self.entries.get(&InstructionType::op(opcode)).map(|x| *x) { + pub fn process(&self, instruction: &elements::Instruction) -> Result { + match self.entries.get(&InstructionType::op(instruction)).map(|x| *x) { None | Some(Metering::Regular) => Ok(self.regular), Some(Metering::Forbidden) => Err(()), Some(Metering::Fixed(val)) => Ok(val), diff --git a/src/runtime_type.rs b/src/runtime_type.rs index 24443ef..3786fdc 100644 --- a/src/runtime_type.rs +++ b/src/runtime_type.rs @@ -1,5 +1,5 @@ use parity_wasm::{elements, builder}; -use self::elements::{ Module, GlobalEntry, External, ExportEntry, GlobalType, ValueType, InitExpr, Opcode, Internal }; +use self::elements::{ Module, GlobalEntry, External, ExportEntry, GlobalType, ValueType, InitExpr, Instruction, Internal }; use byteorder::{ LittleEndian, ByteOrder }; pub fn inject_runtime_type(module: Module, runtime_type: &[u8], runtime_version: u32) -> Module { @@ -18,9 +18,9 @@ pub fn inject_runtime_type(module: Module, runtime_type: &[u8], runtime_version: let total_globals_count: u32 = globals_count + imported_globals_count; builder::from_module(module) - .with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, false), InitExpr::new(vec![Opcode::I32Const(runtime_type as i32), Opcode::End]))) + .with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, false), InitExpr::new(vec![Instruction::I32Const(runtime_type as i32), Instruction::End]))) .with_export(ExportEntry::new("RUNTIME_TYPE".into(), Internal::Global(total_globals_count))) - .with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, false), InitExpr::new(vec![Opcode::I32Const(runtime_version as i32), Opcode::End]))) + .with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, false), InitExpr::new(vec![Instruction::I32Const(runtime_version as i32), Instruction::End]))) .with_export(ExportEntry::new("RUNTIME_VERSION".into(), Internal::Global(total_globals_count + 1))) .build() } @@ -31,7 +31,7 @@ mod tests { #[test] fn it_injects() { let mut module = builder::module() - .with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, false), InitExpr::new(vec![Opcode::I32Const(42 as i32)]))) + .with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, false), InitExpr::new(vec![Instruction::I32Const(42 as i32)]))) .build(); module = inject_runtime_type(module, b"emcc", 1); let global_section = module.global_section().expect("Global section expected"); diff --git a/src/stack_height/max_height.rs b/src/stack_height/max_height.rs index 3d55e69..eae4ad8 100644 --- a/src/stack_height/max_height.rs +++ b/src/stack_height/max_height.rs @@ -136,7 +136,7 @@ impl Stack { /// This function expects the function to be validated. pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result { - use parity_wasm::elements::Opcode::*; + use parity_wasm::elements::Instruction::*; let func_section = module .function_section() @@ -165,7 +165,7 @@ pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result Result= opcodes.elements().len() { + if pc >= instructions.elements().len() { break; } @@ -197,7 +197,7 @@ pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result {{ - use $crate::parity_wasm::elements::Opcode::*; + use $crate::parity_wasm::elements::Instruction::*; [ // stack_height += stack_cost(F) GetGlobal($stack_height_global_idx), @@ -160,7 +160,7 @@ fn generate_stack_height_global(ctx: &mut Context, module: &mut elements::Module .value_type() .i32() .mutable() - .init_expr(elements::Opcode::I32Const(0)) + .init_expr(elements::Instruction::I32Const(0)) .build(); // Try to find an existing global section. @@ -268,13 +268,13 @@ fn instrument_functions(ctx: &mut Context, module: &mut elements::Module) -> Res /// ``` fn instrument_function( ctx: &mut Context, - opcodes: &mut elements::Opcodes, + instructions: &mut elements::Instructions, ) -> Result<(), Error> { - use parity_wasm::elements::Opcode::*; + use parity_wasm::elements::Instruction::*; let mut cursor = 0; loop { - if cursor >= opcodes.elements().len() { + if cursor >= instructions.elements().len() { break; } @@ -287,8 +287,8 @@ fn instrument_function( } let action: Action = { - let opcode = &opcodes.elements()[cursor]; - match *opcode { + let instruction = &instructions.elements()[cursor]; + match *instruction { Call(ref callee_idx) => { let callee_stack_cost = ctx .stack_cost(*callee_idx) @@ -330,7 +330,7 @@ fn instrument_function( // // To splice actually take a place, we need to consume iterator // splice returns. So we just `count()` it. - let _ = opcodes + let _ = instructions .elements_mut() .splice(cursor..(cursor + 1), new_seq.iter().cloned()) .count(); diff --git a/src/stack_height/thunk.rs b/src/stack_height/thunk.rs index 666084d..747ef7d 100644 --- a/src/stack_height/thunk.rs +++ b/src/stack_height/thunk.rs @@ -93,17 +93,17 @@ pub(crate) fn generate_thunks( // - argument pushing // - instrumented call // - end - let mut thunk_body: Vec = Vec::with_capacity( + let mut thunk_body: Vec = Vec::with_capacity( thunk.signature.params().len() + instrumented_call.len() + 1 ); for (arg_idx, _) in thunk.signature.params().iter().enumerate() { - thunk_body.push(elements::Opcode::GetLocal(arg_idx as u32)); + thunk_body.push(elements::Instruction::GetLocal(arg_idx as u32)); } thunk_body.extend(instrumented_call.iter().cloned()); - thunk_body.push(elements::Opcode::End); + thunk_body.push(elements::Instruction::End); // TODO: Don't generate a signature, but find an existing one. @@ -114,7 +114,7 @@ pub(crate) fn generate_thunks( .with_return_type(thunk.signature.return_type().clone()) .build() .body() - .with_opcodes(elements::Opcodes::new( + .with_instructions(elements::Instructions::new( thunk_body )) .build() diff --git a/src/symbols.rs b/src/symbols.rs index ce2803a..a15fcfa 100644 --- a/src/symbols.rs +++ b/src/symbols.rs @@ -47,11 +47,11 @@ pub fn resolve_global(module: &elements::Module, index: u32) -> Symbol { Symbol::Global(index as usize - globals as usize) } -pub fn push_code_symbols(module: &elements::Module, opcodes: &[elements::Opcode], dest: &mut Vec) { - use parity_wasm::elements::Opcode::*; +pub fn push_code_symbols(module: &elements::Module, instructions: &[elements::Instruction], dest: &mut Vec) { + use parity_wasm::elements::Instruction::*; - for opcode in opcodes { - match opcode { + for instruction in instructions { + match instruction { &Call(idx) => { dest.push(resolve_function(module, idx)); },