mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-03-15 03:00:49 +00:00
Update parity-wasm to 0.39
This commit is contained in:
parent
6fd636a41d
commit
a774a2cb29
@ -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.31", default-features = false }
|
parity-wasm = { version = "0.39", 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 }
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ name = "wasm-check"
|
|||||||
path = "check/main.rs"
|
path = "check/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
parity-wasm = "0.31"
|
parity-wasm = "0.39"
|
||||||
pwasm-utils = { path = "..", version = "0.8" }
|
pwasm-utils = { path = "..", version = "0.8" }
|
||||||
glob = "0.2"
|
glob = "0.2"
|
||||||
clap = "2.24"
|
clap = "2.24"
|
||||||
|
@ -98,7 +98,12 @@ 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::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);
|
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;
|
||||||
|
@ -306,11 +306,13 @@ pub(crate) fn determine_metered_blocks(
|
|||||||
let target_index = active_index.checked_sub(label as usize).ok_or_else(|| ())?;
|
let target_index = active_index.checked_sub(label as usize).ok_or_else(|| ())?;
|
||||||
counter.branch(cursor, &[target_index])?;
|
counter.branch(cursor, &[target_index])?;
|
||||||
}
|
}
|
||||||
BrTable(ref label_vec, label_default) => {
|
BrTable(ref br_table_data) => {
|
||||||
counter.increment(instruction_cost)?;
|
counter.increment(instruction_cost)?;
|
||||||
|
|
||||||
let active_index = counter.active_control_block_index().ok_or_else(|| ())?;
|
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))
|
.map(|label| active_index.checked_sub(*label as usize))
|
||||||
.collect::<Option<Vec<_>>>()
|
.collect::<Option<Vec<_>>>()
|
||||||
.ok_or_else(|| ())?;
|
.ok_or_else(|| ())?;
|
||||||
|
@ -241,11 +241,11 @@ fn build_control_flow_graph(
|
|||||||
graph.new_forward_edge(active_node_id, new_node_id);
|
graph.new_forward_edge(active_node_id, new_node_id);
|
||||||
graph.set_first_instr_pos(new_node_id, cursor + 1);
|
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);
|
graph.increment_actual_cost(active_node_id, instruction_cost);
|
||||||
|
|
||||||
let active_frame_idx = stack.len() - 1;
|
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);
|
let target_frame_idx = active_frame_idx - (label as usize);
|
||||||
graph.new_edge(active_node_id, &stack[target_frame_idx]);
|
graph.new_edge(active_node_id, &stack[target_frame_idx]);
|
||||||
}
|
}
|
||||||
|
22
src/graph.rs
22
src/graph.rs
@ -354,9 +354,12 @@ impl Module {
|
|||||||
// };
|
// };
|
||||||
|
|
||||||
// TODO: update parity-wasm and uncomment the above instead
|
// TODO: update parity-wasm and uncomment the above instead
|
||||||
let location = SegmentLocation::Default(
|
let init_expr = element_segment
|
||||||
res.map_instructions(element_segment.offset().code())
|
.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
|
let funcs_map = element_segment
|
||||||
.members().iter()
|
.members().iter()
|
||||||
@ -390,9 +393,12 @@ impl Module {
|
|||||||
for data_segment in data_section.entries() {
|
for data_segment in data_section.entries() {
|
||||||
// TODO: update parity-wasm and use the same logic as in
|
// TODO: update parity-wasm and use the same logic as in
|
||||||
// commented element segment branch
|
// commented element segment branch
|
||||||
let location = SegmentLocation::Default(
|
let init_expr = data_segment
|
||||||
res.map_instructions(data_segment.offset().code())
|
.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 {
|
res.data.push(DataSegment {
|
||||||
value: data_segment.value().to_vec(),
|
value: data_segment.value().to_vec(),
|
||||||
@ -675,7 +681,7 @@ impl Module {
|
|||||||
element_segments.push(
|
element_segments.push(
|
||||||
elements::ElementSegment::new(
|
elements::ElementSegment::new(
|
||||||
0,
|
0,
|
||||||
elements::InitExpr::new(self.generate_instructions(&offset_expr[..])),
|
Some(elements::InitExpr::new(self.generate_instructions(&offset_expr[..]))),
|
||||||
elements_map,
|
elements_map,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -728,7 +734,7 @@ impl Module {
|
|||||||
data_segments.push(
|
data_segments.push(
|
||||||
elements::DataSegment::new(
|
elements::DataSegment::new(
|
||||||
0,
|
0,
|
||||||
elements::InitExpr::new(self.generate_instructions(&offset_expr[..])),
|
Some(elements::InitExpr::new(self.generate_instructions(&offset_expr[..]))),
|
||||||
data_entry.value.clone(),
|
data_entry.value.clone(),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -46,12 +46,28 @@ pub fn optimize(
|
|||||||
let mut init_symbols = Vec::new();
|
let mut init_symbols = Vec::new();
|
||||||
if let Some(data_section) = module.data_section() {
|
if let Some(data_section) = module.data_section() {
|
||||||
for segment in data_section.entries() {
|
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() {
|
if let Some(elements_section) = module.elements_section() {
|
||||||
for segment in elements_section.entries() {
|
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() {
|
for func_index in segment.members() {
|
||||||
stay.insert(resolve_function(&module, *func_index));
|
stay.insert(resolve_function(&module, *func_index));
|
||||||
}
|
}
|
||||||
@ -256,12 +272,26 @@ pub fn optimize(
|
|||||||
},
|
},
|
||||||
&mut elements::Section::Data(ref mut data_section) => {
|
&mut elements::Section::Data(ref mut data_section) => {
|
||||||
for ref mut segment in data_section.entries_mut() {
|
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) => {
|
&mut elements::Section::Element(ref mut elements_section) => {
|
||||||
for ref mut segment in elements_section.entries_mut() {
|
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
|
// update all indirect call addresses initial values
|
||||||
for func_index in segment.members_mut() {
|
for func_index in segment.members_mut() {
|
||||||
let totalle = eliminated_funcs.iter().take_while(|i| (**i as u32) < *func_index).count();
|
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) => {
|
&mut elements::Section::Name(ref mut name_section) => {
|
||||||
match name_section {
|
if let Some(ref mut func_name) = name_section.functions_mut() {
|
||||||
&mut elements::NameSection::Function(ref mut func_name) => {
|
let mut func_name_map = mem::replace(func_name.names_mut(), elements::IndexMap::default());
|
||||||
let mut func_name_map = mem::replace(func_name.names_mut(), elements::IndexMap::default());
|
for index in &eliminated_funcs {
|
||||||
for index in &eliminated_funcs {
|
func_name_map.remove(*index as u32);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
&mut elements::NameSection::Local(ref mut local_name) => {
|
let updated_map = func_name_map.into_iter().map(|(index, value)| {
|
||||||
let mut local_names_map = mem::replace(local_name.local_names_mut(), elements::IndexMap::default());
|
let totalle = eliminated_funcs.iter().take_while(|i| (**i as u32) < index).count() as u32;
|
||||||
for index in &eliminated_funcs {
|
(index - totalle, value)
|
||||||
local_names_map.remove(*index as u32);
|
}).collect();
|
||||||
}
|
mem::replace(func_name.names_mut(), updated_map);
|
||||||
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);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => { }
|
_ => { }
|
||||||
|
@ -162,7 +162,12 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module, tar
|
|||||||
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 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 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 +179,7 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module, tar
|
|||||||
};
|
};
|
||||||
let code_data = DataSegment::new(
|
let code_data = DataSegment::new(
|
||||||
index,
|
index,
|
||||||
InitExpr::new(vec![Instruction::I32Const(offset), Instruction::End]),
|
Some(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);
|
||||||
|
@ -83,7 +83,7 @@ impl InstructionType {
|
|||||||
End => InstructionType::ControlFlow,
|
End => InstructionType::ControlFlow,
|
||||||
Br(_) => InstructionType::ControlFlow,
|
Br(_) => InstructionType::ControlFlow,
|
||||||
BrIf(_) => InstructionType::ControlFlow,
|
BrIf(_) => InstructionType::ControlFlow,
|
||||||
BrTable(_, _) => InstructionType::ControlFlow,
|
BrTable(_) => InstructionType::ControlFlow,
|
||||||
Return => InstructionType::ControlFlow,
|
Return => InstructionType::ControlFlow,
|
||||||
Call(_) => InstructionType::ControlFlow,
|
Call(_) => InstructionType::ControlFlow,
|
||||||
CallIndirect(_, _) => InstructionType::ControlFlow,
|
CallIndirect(_, _) => InstructionType::ControlFlow,
|
||||||
|
@ -245,11 +245,11 @@ pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, E
|
|||||||
// Push values back.
|
// Push values back.
|
||||||
stack.push_values(target_arity)?;
|
stack.push_values(target_arity)?;
|
||||||
}
|
}
|
||||||
BrTable(ref targets, default_target) => {
|
BrTable(ref br_table_data) => {
|
||||||
let arity_of_default = stack.frame(default_target)?.branch_arity;
|
let arity_of_default = stack.frame(br_table_data.default)?.branch_arity;
|
||||||
|
|
||||||
// Check that all jump targets have an equal arities.
|
// 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;
|
let arity = stack.frame(*target)?.branch_arity;
|
||||||
if arity != arity_of_default {
|
if arity != arity_of_default {
|
||||||
return Err(Error(
|
return Err(Error(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user