mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-04-17 10:52:22 +00:00
Update parity-wasm dependency to 0.31
This commit is contained in:
parent
f4b75bd840
commit
d6f82000ee
@ -8,7 +8,7 @@ description = "Collection of command-line utilities and corresponding Rust api f
|
|||||||
keywords = ["wasm", "webassembly", "pwasm"]
|
keywords = ["wasm", "webassembly", "pwasm"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
parity-wasm = { version = "0.30", default-features = false }
|
parity-wasm = { version = "0.31", default-features = false }
|
||||||
log = { version = "0.4", default-features = false }
|
log = { version = "0.4", default-features = false }
|
||||||
byteorder = { version = "1", default-features = false }
|
byteorder = { version = "1", default-features = false }
|
||||||
|
|
||||||
|
10
src/ext.rs
10
src/ext.rs
@ -8,10 +8,10 @@ use byteorder::{LittleEndian, ByteOrder};
|
|||||||
|
|
||||||
type Insertion = (usize, u32, u32, String);
|
type Insertion = (usize, u32, u32, String);
|
||||||
|
|
||||||
pub fn update_call_index(opcodes: &mut elements::Opcodes, original_imports: usize, inserts: &[Insertion]) {
|
pub fn update_call_index(instructions: &mut elements::Instructions, original_imports: usize, inserts: &[Insertion]) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
for opcode in opcodes.elements_mut().iter_mut() {
|
for instruction in instructions.elements_mut().iter_mut() {
|
||||||
if let &mut Call(ref mut call_index) = opcode {
|
if let &mut Call(ref mut call_index) = instruction {
|
||||||
if let Some(pos) = inserts.iter().position(|x| x.1 == *call_index) {
|
if let Some(pos) = inserts.iter().position(|x| x.1 == *call_index) {
|
||||||
*call_index = (original_imports + pos) as u32;
|
*call_index = (original_imports + pos) as u32;
|
||||||
} else if *call_index as usize > original_imports {
|
} else if *call_index as usize > original_imports {
|
||||||
@ -98,7 +98,7 @@ pub fn shrink_unknown_stack(
|
|||||||
match section {
|
match section {
|
||||||
&mut elements::Section::Data(ref mut data_section) => {
|
&mut elements::Section::Data(ref mut data_section) => {
|
||||||
for ref mut data_segment in data_section.entries_mut() {
|
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);
|
assert_eq!(data_segment.value().len(), 4);
|
||||||
let current_val = LittleEndian::read_u32(data_segment.value());
|
let current_val = LittleEndian::read_u32(data_segment.value());
|
||||||
let new_val = current_val - shrink_amount;
|
let new_val = current_val - shrink_amount;
|
||||||
|
44
src/gas.rs
44
src/gas.rs
@ -3,10 +3,10 @@ use std::vec::Vec;
|
|||||||
use parity_wasm::{elements, builder};
|
use parity_wasm::{elements, builder};
|
||||||
use rules;
|
use rules;
|
||||||
|
|
||||||
pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) {
|
pub fn update_call_index(instructions: &mut elements::Instructions, inserted_index: u32) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
for opcode in opcodes.elements_mut().iter_mut() {
|
for instruction in instructions.elements_mut().iter_mut() {
|
||||||
if let &mut Call(ref mut call_index) = opcode {
|
if let &mut Call(ref mut call_index) = instruction {
|
||||||
if *call_index >= inserted_index { *call_index += 1}
|
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 {
|
fn inject_grow_counter(instructions: &mut elements::Instructions, grow_counter_func: u32) -> usize {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
for opcode in opcodes.elements_mut() {
|
for instruction in instructions.elements_mut() {
|
||||||
if let GrowMemory(_) = *opcode {
|
if let GrowMemory(_) = *instruction {
|
||||||
*opcode = Call(grow_counter_func);
|
*instruction = Call(grow_counter_func);
|
||||||
counter += 1;
|
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 {
|
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);
|
let mut b = builder::from_module(module);
|
||||||
b.push_function(
|
b.push_function(
|
||||||
builder::function()
|
builder::function()
|
||||||
.signature().params().i32().build().with_return_type(Some(elements::ValueType::I32)).build()
|
.signature().params().i32().build().with_return_type(Some(elements::ValueType::I32)).build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(vec![
|
.with_instructions(elements::Instructions::new(vec![
|
||||||
GetLocal(0),
|
GetLocal(0),
|
||||||
GetLocal(0),
|
GetLocal(0),
|
||||||
I32Const(rules.grow_cost() as i32),
|
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(
|
pub fn inject_counter(
|
||||||
opcodes: &mut elements::Opcodes,
|
instructions: &mut elements::Instructions,
|
||||||
rules: &rules::Set,
|
rules: &rules::Set,
|
||||||
gas_func: u32,
|
gas_func: u32,
|
||||||
) -> Result<(), ()> {
|
) -> Result<(), ()> {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
|
|
||||||
let mut counter = Counter::new();
|
let mut counter = Counter::new();
|
||||||
|
|
||||||
// Begin an implicit function (i.e. `func...end`) block.
|
// Begin an implicit function (i.e. `func...end`) block.
|
||||||
counter.begin(0);
|
counter.begin(0);
|
||||||
|
|
||||||
for cursor in 0..opcodes.elements().len() {
|
for cursor in 0..instructions.elements().len() {
|
||||||
let opcode = &opcodes.elements()[cursor];
|
let instruction = &instructions.elements()[cursor];
|
||||||
match *opcode {
|
match *instruction {
|
||||||
Block(_) | If(_) | Loop(_) => {
|
Block(_) | If(_) | Loop(_) => {
|
||||||
// Increment previous block with the cost of the current opcode.
|
// Increment previous block with the cost of the current opcode.
|
||||||
let opcode_cost = rules.process(opcode)?;
|
let instruction_cost = rules.process(instruction)?;
|
||||||
counter.increment(opcode_cost)?;
|
counter.increment(instruction_cost)?;
|
||||||
|
|
||||||
// Begin new block. The cost of the following opcodes until `End` or `Else` will
|
// Begin new block. The cost of the following opcodes until `End` or `Else` will
|
||||||
// be included into this block.
|
// 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.
|
// An ordinal non control flow instruction. Just increment the cost of the current block.
|
||||||
let opcode_cost = rules.process(opcode)?;
|
let instruction_cost = rules.process(instruction)?;
|
||||||
counter.increment(opcode_cost)?;
|
counter.increment(instruction_cost)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -175,8 +175,8 @@ pub fn inject_counter(
|
|||||||
for block in counter.blocks {
|
for block in counter.blocks {
|
||||||
let effective_pos = block.start_pos + cumulative_offset;
|
let effective_pos = block.start_pos + cumulative_offset;
|
||||||
|
|
||||||
opcodes.elements_mut().insert(effective_pos, I32Const(block.cost as i32));
|
instructions.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+1, Call(gas_func));
|
||||||
|
|
||||||
// Take into account these two inserted instructions.
|
// Take into account these two inserted instructions.
|
||||||
cumulative_offset += 2;
|
cumulative_offset += 2;
|
||||||
|
@ -270,10 +270,10 @@ pub fn optimize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn update_call_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) {
|
pub fn update_call_index(instructions: &mut elements::Instructions, eliminated_indices: &[usize]) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
for opcode in opcodes.elements_mut().iter_mut() {
|
for instruction in instructions.elements_mut().iter_mut() {
|
||||||
if let &mut Call(ref mut call_index) = opcode {
|
if let &mut Call(ref mut call_index) = instruction {
|
||||||
let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *call_index).count();
|
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);
|
trace!("rewired call {} -> call {}", *call_index, *call_index - totalle as u32);
|
||||||
*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
|
/// Updates global references considering the _ordered_ list of eliminated indices
|
||||||
pub fn update_global_index(opcodes: &mut Vec<elements::Opcode>, eliminated_indices: &[usize]) {
|
pub fn update_global_index(instructions: &mut Vec<elements::Instruction>, eliminated_indices: &[usize]) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
for opcode in opcodes.iter_mut() {
|
for instruction in instructions.iter_mut() {
|
||||||
match opcode {
|
match instruction {
|
||||||
&mut GetGlobal(ref mut index) | &mut SetGlobal(ref mut index) => {
|
&mut GetGlobal(ref mut index) | &mut SetGlobal(ref mut index) => {
|
||||||
let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *index).count();
|
let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *index).count();
|
||||||
trace!("rewired global {} -> global {}", *index, *index - totalle as u32);
|
trace!("rewired global {} -> global {}", *index, *index - totalle as u32);
|
||||||
@ -297,10 +297,10 @@ pub fn update_global_index(opcodes: &mut Vec<elements::Opcode>, eliminated_indic
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Updates global references considering the _ordered_ list of eliminated indices
|
/// Updates global references considering the _ordered_ list of eliminated indices
|
||||||
pub fn update_type_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) {
|
pub fn update_type_index(instructions: &mut elements::Instructions, eliminated_indices: &[usize]) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
for opcode in opcodes.elements_mut().iter_mut() {
|
for instruction in instructions.elements_mut().iter_mut() {
|
||||||
if let &mut CallIndirect(ref mut call_index, _) = opcode {
|
if let &mut CallIndirect(ref mut call_index, _) = instruction {
|
||||||
let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *call_index).count();
|
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);
|
trace!("rewired call_indrect {} -> call_indirect {}", *call_index, *call_index - totalle as u32);
|
||||||
*call_index -= totalle as u32;
|
*call_index -= totalle as u32;
|
||||||
@ -435,10 +435,10 @@ mod tests {
|
|||||||
.function()
|
.function()
|
||||||
.signature().param().i32().build()
|
.signature().param().i32().build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_opcodes(elements::Instructions::new(
|
||||||
vec![
|
vec![
|
||||||
elements::Opcode::GetGlobal(0),
|
elements::Instruction::GetGlobal(0),
|
||||||
elements::Opcode::End
|
elements::Instruction::End
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
.build()
|
.build()
|
||||||
@ -477,10 +477,10 @@ mod tests {
|
|||||||
.function()
|
.function()
|
||||||
.signature().param().i32().build()
|
.signature().param().i32().build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_opcodes(elements::Instructions::new(
|
||||||
vec![
|
vec![
|
||||||
elements::Opcode::GetGlobal(1),
|
elements::Instruction::GetGlobal(1),
|
||||||
elements::Opcode::End
|
elements::Instruction::End
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
.build()
|
.build()
|
||||||
@ -510,10 +510,10 @@ mod tests {
|
|||||||
.function()
|
.function()
|
||||||
.signature().param().i32().build()
|
.signature().param().i32().build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_opcodes(elements::Instructions::new(
|
||||||
vec![
|
vec![
|
||||||
elements::Opcode::Call(1),
|
elements::Instruction::Call(1),
|
||||||
elements::Opcode::End
|
elements::Instruction::End
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
.build()
|
.build()
|
||||||
@ -563,10 +563,10 @@ mod tests {
|
|||||||
.function()
|
.function()
|
||||||
.signature().param().i32().build()
|
.signature().param().i32().build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_opcodes(elements::Instructions::new(
|
||||||
vec![
|
vec![
|
||||||
elements::Opcode::CallIndirect(1, 0),
|
elements::Instruction::CallIndirect(1, 0),
|
||||||
elements::Opcode::End
|
elements::Instruction::End
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
.build()
|
.build()
|
||||||
@ -586,7 +586,7 @@ mod tests {
|
|||||||
|
|
||||||
let indirect_opcode = &module.code_section().expect("code section to be generated").bodies()[0].code().elements()[0];
|
let indirect_opcode = &module.code_section().expect("code section to be generated").bodies()[0].code().elements()[0];
|
||||||
match *indirect_opcode {
|
match *indirect_opcode {
|
||||||
elements::Opcode::CallIndirect(0, 0) => {},
|
elements::Instruction::CallIndirect(0, 0) => {},
|
||||||
_ => {
|
_ => {
|
||||||
panic!(
|
panic!(
|
||||||
"Expected call_indirect to use index 0 after optimization, since previois 0th was eliminated, but got {:?}",
|
"Expected call_indirect to use index 0 after optimization, since previois 0th was eliminated, but got {:?}",
|
||||||
|
36
src/pack.rs
36
src/pack.rs
@ -3,7 +3,7 @@ use std::vec::Vec;
|
|||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
|
|
||||||
use parity_wasm::elements::{
|
use parity_wasm::elements::{
|
||||||
self, Section, DataSection, Opcode, DataSegment, InitExpr, Internal, External,
|
self, Section, DataSection, Instruction, DataSegment, InitExpr, Internal, External,
|
||||||
ImportCountType,
|
ImportCountType,
|
||||||
};
|
};
|
||||||
use parity_wasm::builder;
|
use parity_wasm::builder;
|
||||||
@ -162,7 +162,7 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module) ->
|
|||||||
for section in ctor_module.sections_mut() {
|
for section in ctor_module.sections_mut() {
|
||||||
if let &mut Section::Data(ref mut data_section) = section {
|
if let &mut Section::Data(ref mut data_section) = section {
|
||||||
let (index, offset) = if let Some(ref entry) = data_section.entries().iter().last() {
|
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 len = entry.value().len() as i32;
|
||||||
let offst = offst as i32;
|
let offst = offst as i32;
|
||||||
(entry.index(), offst + (len + 4) - len % 4)
|
(entry.index(), offst + (len + 4) - len % 4)
|
||||||
@ -174,7 +174,7 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module) ->
|
|||||||
};
|
};
|
||||||
let code_data = DataSegment::new(
|
let code_data = DataSegment::new(
|
||||||
index,
|
index,
|
||||||
InitExpr::new(vec![Opcode::I32Const(offset), Opcode::End]),
|
InitExpr::new(vec![Instruction::I32Const(offset), Instruction::End]),
|
||||||
raw_module.clone()
|
raw_module.clone()
|
||||||
);
|
);
|
||||||
data_section.entries_mut().push(code_data);
|
data_section.entries_mut().push(code_data);
|
||||||
@ -185,13 +185,13 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module) ->
|
|||||||
let mut new_module = builder::from_module(ctor_module)
|
let mut new_module = builder::from_module(ctor_module)
|
||||||
.function()
|
.function()
|
||||||
.signature().build()
|
.signature().build()
|
||||||
.body().with_opcodes(elements::Opcodes::new(
|
.body().with_instructions(elements::Instructions::new(
|
||||||
vec![
|
vec![
|
||||||
Opcode::Call((create_func_id + ctor_import_functions) as u32),
|
Instruction::Call((create_func_id + ctor_import_functions) as u32),
|
||||||
Opcode::I32Const(code_data_address),
|
Instruction::I32Const(code_data_address),
|
||||||
Opcode::I32Const(raw_module.len() as i32),
|
Instruction::I32Const(raw_module.len() as i32),
|
||||||
Opcode::Call(ret_function_id as u32),
|
Instruction::Call(ret_function_id as u32),
|
||||||
Opcode::End,
|
Instruction::End,
|
||||||
])).build()
|
])).build()
|
||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
@ -249,9 +249,9 @@ mod test {
|
|||||||
.function()
|
.function()
|
||||||
.signature().build()
|
.signature().build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_opcodes(elements::Instructions::new(
|
||||||
vec![
|
vec![
|
||||||
elements::Opcode::End
|
elements::Instruction::End
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
.build()
|
.build()
|
||||||
@ -259,9 +259,9 @@ mod test {
|
|||||||
.function()
|
.function()
|
||||||
.signature().build()
|
.signature().build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_opcodes(elements::Instructions::new(
|
||||||
vec![
|
vec![
|
||||||
elements::Opcode::End
|
elements::Instruction::End
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
.build()
|
.build()
|
||||||
@ -287,7 +287,7 @@ mod test {
|
|||||||
.external().memory(1 as u32, Some(1 as u32))
|
.external().memory(1 as u32, Some(1 as u32))
|
||||||
.build()
|
.build()
|
||||||
.data()
|
.data()
|
||||||
.offset(elements::Opcode::I32Const(16)).value(vec![0u8])
|
.offset(elements::Instruction::I32Const(16)).value(vec![0u8])
|
||||||
.build()
|
.build()
|
||||||
.function()
|
.function()
|
||||||
.signature()
|
.signature()
|
||||||
@ -298,9 +298,9 @@ mod test {
|
|||||||
.function()
|
.function()
|
||||||
.signature().build()
|
.signature().build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_opcodes(elements::Instructions::new(
|
||||||
vec![
|
vec![
|
||||||
elements::Opcode::End
|
elements::Instruction::End
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
.build()
|
.build()
|
||||||
@ -308,9 +308,9 @@ mod test {
|
|||||||
.function()
|
.function()
|
||||||
.signature().build()
|
.signature().build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_opcodes(elements::Instructions::new(
|
||||||
vec![
|
vec![
|
||||||
elements::Opcode::End
|
elements::Instruction::End
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
.build()
|
.build()
|
||||||
|
10
src/rules.rs
10
src/rules.rs
@ -70,10 +70,10 @@ impl ::std::str::FromStr for InstructionType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl InstructionType {
|
impl InstructionType {
|
||||||
pub fn op(opcode: &elements::Opcode) -> Self {
|
pub fn op(instruction: &elements::Instruction) -> Self {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
|
|
||||||
match *opcode {
|
match *instruction {
|
||||||
Unreachable => InstructionType::Unreachable,
|
Unreachable => InstructionType::Unreachable,
|
||||||
Nop => InstructionType::Nop,
|
Nop => InstructionType::Nop,
|
||||||
Block(_) => InstructionType::ControlFlow,
|
Block(_) => InstructionType::ControlFlow,
|
||||||
@ -288,8 +288,8 @@ impl Set {
|
|||||||
Set { regular: regular, entries: entries, grow: 0 }
|
Set { regular: regular, entries: entries, grow: 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process(&self, opcode: &elements::Opcode) -> Result<u32, ()> {
|
pub fn process(&self, instruction: &elements::Instruction) -> Result<u32, ()> {
|
||||||
match self.entries.get(&InstructionType::op(opcode)).map(|x| *x) {
|
match self.entries.get(&InstructionType::op(instruction)).map(|x| *x) {
|
||||||
None | Some(Metering::Regular) => Ok(self.regular),
|
None | Some(Metering::Regular) => Ok(self.regular),
|
||||||
Some(Metering::Forbidden) => Err(()),
|
Some(Metering::Forbidden) => Err(()),
|
||||||
Some(Metering::Fixed(val)) => Ok(val),
|
Some(Metering::Fixed(val)) => Ok(val),
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use parity_wasm::{elements, builder};
|
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 };
|
use byteorder::{ LittleEndian, ByteOrder };
|
||||||
|
|
||||||
pub fn inject_runtime_type(module: Module, runtime_type: &[u8], runtime_version: u32) -> Module {
|
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;
|
let total_globals_count: u32 = globals_count + imported_globals_count;
|
||||||
|
|
||||||
builder::from_module(module)
|
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_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)))
|
.with_export(ExportEntry::new("RUNTIME_VERSION".into(), Internal::Global(total_globals_count + 1)))
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn it_injects() {
|
fn it_injects() {
|
||||||
let mut module = builder::module()
|
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();
|
.build();
|
||||||
module = inject_runtime_type(module, b"emcc", 1);
|
module = inject_runtime_type(module, b"emcc", 1);
|
||||||
let global_section = module.global_section().expect("Global section expected");
|
let global_section = module.global_section().expect("Global section expected");
|
||||||
|
@ -136,7 +136,7 @@ impl Stack {
|
|||||||
|
|
||||||
/// This function expects the function to be validated.
|
/// This function expects the function to be validated.
|
||||||
pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, Error> {
|
pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, Error> {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
|
|
||||||
let func_section = module
|
let func_section = module
|
||||||
.function_section()
|
.function_section()
|
||||||
@ -165,7 +165,7 @@ pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, E
|
|||||||
.bodies()
|
.bodies()
|
||||||
.get(func_idx as usize)
|
.get(func_idx as usize)
|
||||||
.ok_or_else(|| Error("Function body for the index isn't found".into()))?;
|
.ok_or_else(|| Error("Function body for the index isn't found".into()))?;
|
||||||
let opcodes = body.code();
|
let instructions = body.code();
|
||||||
|
|
||||||
let mut stack = Stack::new();
|
let mut stack = Stack::new();
|
||||||
let mut max_height: u32 = 0;
|
let mut max_height: u32 = 0;
|
||||||
@ -186,7 +186,7 @@ pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, E
|
|||||||
});
|
});
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if pc >= opcodes.elements().len() {
|
if pc >= instructions.elements().len() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +197,7 @@ pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, E
|
|||||||
max_height = stack.height();
|
max_height = stack.height();
|
||||||
}
|
}
|
||||||
|
|
||||||
let opcode = &opcodes.elements()[pc];
|
let opcode = &instructions.elements()[pc];
|
||||||
trace!(target: "max_height", "{:?}", opcode);
|
trace!(target: "max_height", "{:?}", opcode);
|
||||||
|
|
||||||
match *opcode {
|
match *opcode {
|
||||||
|
@ -57,7 +57,7 @@ use parity_wasm::builder;
|
|||||||
/// Macro to generate preamble and postamble.
|
/// Macro to generate preamble and postamble.
|
||||||
macro_rules! instrument_call {
|
macro_rules! instrument_call {
|
||||||
($callee_idx: expr, $callee_stack_cost: expr, $stack_height_global_idx: expr, $stack_limit: expr) => {{
|
($callee_idx: expr, $callee_stack_cost: expr, $stack_height_global_idx: expr, $stack_limit: expr) => {{
|
||||||
use $crate::parity_wasm::elements::Opcode::*;
|
use $crate::parity_wasm::elements::Instruction::*;
|
||||||
[
|
[
|
||||||
// stack_height += stack_cost(F)
|
// stack_height += stack_cost(F)
|
||||||
GetGlobal($stack_height_global_idx),
|
GetGlobal($stack_height_global_idx),
|
||||||
@ -160,7 +160,7 @@ fn generate_stack_height_global(ctx: &mut Context, module: &mut elements::Module
|
|||||||
.value_type()
|
.value_type()
|
||||||
.i32()
|
.i32()
|
||||||
.mutable()
|
.mutable()
|
||||||
.init_expr(elements::Opcode::I32Const(0))
|
.init_expr(elements::Instruction::I32Const(0))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Try to find an existing global section.
|
// 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(
|
fn instrument_function(
|
||||||
ctx: &mut Context,
|
ctx: &mut Context,
|
||||||
opcodes: &mut elements::Opcodes,
|
instructions: &mut elements::Instructions,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
|
|
||||||
let mut cursor = 0;
|
let mut cursor = 0;
|
||||||
loop {
|
loop {
|
||||||
if cursor >= opcodes.elements().len() {
|
if cursor >= instructions.elements().len() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,8 +287,8 @@ fn instrument_function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let action: Action = {
|
let action: Action = {
|
||||||
let opcode = &opcodes.elements()[cursor];
|
let instruction = &instructions.elements()[cursor];
|
||||||
match *opcode {
|
match *instruction {
|
||||||
Call(ref callee_idx) => {
|
Call(ref callee_idx) => {
|
||||||
let callee_stack_cost = ctx
|
let callee_stack_cost = ctx
|
||||||
.stack_cost(*callee_idx)
|
.stack_cost(*callee_idx)
|
||||||
@ -330,7 +330,7 @@ fn instrument_function(
|
|||||||
//
|
//
|
||||||
// To splice actually take a place, we need to consume iterator
|
// To splice actually take a place, we need to consume iterator
|
||||||
// splice returns. So we just `count()` it.
|
// splice returns. So we just `count()` it.
|
||||||
let _ = opcodes
|
let _ = instructions
|
||||||
.elements_mut()
|
.elements_mut()
|
||||||
.splice(cursor..(cursor + 1), new_seq.iter().cloned())
|
.splice(cursor..(cursor + 1), new_seq.iter().cloned())
|
||||||
.count();
|
.count();
|
||||||
|
@ -93,17 +93,17 @@ pub(crate) fn generate_thunks(
|
|||||||
// - argument pushing
|
// - argument pushing
|
||||||
// - instrumented call
|
// - instrumented call
|
||||||
// - end
|
// - end
|
||||||
let mut thunk_body: Vec<elements::Opcode> = Vec::with_capacity(
|
let mut thunk_body: Vec<elements::Instruction> = Vec::with_capacity(
|
||||||
thunk.signature.params().len() +
|
thunk.signature.params().len() +
|
||||||
instrumented_call.len() +
|
instrumented_call.len() +
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
|
|
||||||
for (arg_idx, _) in thunk.signature.params().iter().enumerate() {
|
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.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.
|
// 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())
|
.with_return_type(thunk.signature.return_type().clone())
|
||||||
.build()
|
.build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_instructions(elements::Instructions::new(
|
||||||
thunk_body
|
thunk_body
|
||||||
))
|
))
|
||||||
.build()
|
.build()
|
||||||
|
@ -47,11 +47,11 @@ pub fn resolve_global(module: &elements::Module, index: u32) -> Symbol {
|
|||||||
Symbol::Global(index as usize - globals as usize)
|
Symbol::Global(index as usize - globals as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_code_symbols(module: &elements::Module, opcodes: &[elements::Opcode], dest: &mut Vec<Symbol>) {
|
pub fn push_code_symbols(module: &elements::Module, instructions: &[elements::Instruction], dest: &mut Vec<Symbol>) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
|
|
||||||
for opcode in opcodes {
|
for instruction in instructions {
|
||||||
match opcode {
|
match instruction {
|
||||||
&Call(idx) => {
|
&Call(idx) => {
|
||||||
dest.push(resolve_function(module, idx));
|
dest.push(resolve_function(module, idx));
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user