mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-06-14 15:41:02 +00:00
Cleanup (#162)
* Remove unnecessary references * Shorten expressions * Remove unnecessary reference * Simplify expression * Fix formatting
This commit is contained in:
+11
-11
@@ -109,7 +109,7 @@ impl Counter {
|
|||||||
self.finalize_metered_block(cursor)?;
|
self.finalize_metered_block(cursor)?;
|
||||||
|
|
||||||
// Pop the control block stack.
|
// Pop the control block stack.
|
||||||
let closing_control_block = self.stack.pop().ok_or_else(|| ())?;
|
let closing_control_block = self.stack.pop().ok_or(())?;
|
||||||
let closing_control_index = self.stack.len();
|
let closing_control_index = self.stack.len();
|
||||||
|
|
||||||
if self.stack.is_empty() {
|
if self.stack.is_empty() {
|
||||||
@@ -118,7 +118,7 @@ impl Counter {
|
|||||||
|
|
||||||
// Update the lowest_forward_br_target for the control block now on top of the stack.
|
// Update the lowest_forward_br_target for the control block now on top of the stack.
|
||||||
{
|
{
|
||||||
let control_block = self.stack.last_mut().ok_or_else(|| ())?;
|
let control_block = self.stack.last_mut().ok_or(())?;
|
||||||
control_block.lowest_forward_br_target = min(
|
control_block.lowest_forward_br_target = min(
|
||||||
control_block.lowest_forward_br_target,
|
control_block.lowest_forward_br_target,
|
||||||
closing_control_block.lowest_forward_br_target,
|
closing_control_block.lowest_forward_br_target,
|
||||||
@@ -140,7 +140,7 @@ impl Counter {
|
|||||||
/// Finalized blocks have final cost which will not change later.
|
/// Finalized blocks have final cost which will not change later.
|
||||||
fn finalize_metered_block(&mut self, cursor: usize) -> Result<(), ()> {
|
fn finalize_metered_block(&mut self, cursor: usize) -> Result<(), ()> {
|
||||||
let closing_metered_block = {
|
let closing_metered_block = {
|
||||||
let control_block = self.stack.last_mut().ok_or_else(|| ())?;
|
let control_block = self.stack.last_mut().ok_or(())?;
|
||||||
mem::replace(
|
mem::replace(
|
||||||
&mut control_block.active_metered_block,
|
&mut control_block.active_metered_block,
|
||||||
MeteredBlock { start_pos: cursor + 1, cost: 0 },
|
MeteredBlock { start_pos: cursor + 1, cost: 0 },
|
||||||
@@ -181,14 +181,14 @@ impl Counter {
|
|||||||
// Update the lowest_forward_br_target of the current control block.
|
// Update the lowest_forward_br_target of the current control block.
|
||||||
for &index in indices {
|
for &index in indices {
|
||||||
let target_is_loop = {
|
let target_is_loop = {
|
||||||
let target_block = self.stack.get(index).ok_or_else(|| ())?;
|
let target_block = self.stack.get(index).ok_or(())?;
|
||||||
target_block.is_loop
|
target_block.is_loop
|
||||||
};
|
};
|
||||||
if target_is_loop {
|
if target_is_loop {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
let control_block = self.stack.last_mut().ok_or_else(|| ())?;
|
let control_block = self.stack.last_mut().ok_or(())?;
|
||||||
control_block.lowest_forward_br_target =
|
control_block.lowest_forward_br_target =
|
||||||
min(control_block.lowest_forward_br_target, index);
|
min(control_block.lowest_forward_br_target, index);
|
||||||
}
|
}
|
||||||
@@ -203,14 +203,14 @@ impl Counter {
|
|||||||
|
|
||||||
/// Get a reference to the currently active metered block.
|
/// Get a reference to the currently active metered block.
|
||||||
fn active_metered_block(&mut self) -> Result<&mut MeteredBlock, ()> {
|
fn active_metered_block(&mut self) -> Result<&mut MeteredBlock, ()> {
|
||||||
let top_block = self.stack.last_mut().ok_or_else(|| ())?;
|
let top_block = self.stack.last_mut().ok_or(())?;
|
||||||
Ok(&mut top_block.active_metered_block)
|
Ok(&mut top_block.active_metered_block)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Increment the cost of the current block by the specified value.
|
/// Increment the cost of the current block by the specified value.
|
||||||
fn increment(&mut self, val: u32) -> Result<(), ()> {
|
fn increment(&mut self, val: u32) -> Result<(), ()> {
|
||||||
let top_block = self.active_metered_block()?;
|
let top_block = self.active_metered_block()?;
|
||||||
top_block.cost = top_block.cost.checked_add(val).ok_or_else(|| ())?;
|
top_block.cost = top_block.cost.checked_add(val).ok_or(())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -308,20 +308,20 @@ pub(crate) fn determine_metered_blocks<R: Rules>(
|
|||||||
counter.increment(instruction_cost)?;
|
counter.increment(instruction_cost)?;
|
||||||
|
|
||||||
// Label is a relative index into the control stack.
|
// Label is a relative index into the control stack.
|
||||||
let active_index = counter.active_control_block_index().ok_or_else(|| ())?;
|
let active_index = counter.active_control_block_index().ok_or(())?;
|
||||||
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(())?;
|
||||||
counter.branch(cursor, &[target_index])?;
|
counter.branch(cursor, &[target_index])?;
|
||||||
},
|
},
|
||||||
BrTable(br_table_data) => {
|
BrTable(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(())?;
|
||||||
let target_indices = [br_table_data.default]
|
let target_indices = [br_table_data.default]
|
||||||
.iter()
|
.iter()
|
||||||
.chain(br_table_data.table.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(())?;
|
||||||
counter.branch(cursor, &target_indices)?;
|
counter.branch(cursor, &target_indices)?;
|
||||||
},
|
},
|
||||||
Return => {
|
Return => {
|
||||||
|
|||||||
+1
-1
@@ -723,7 +723,7 @@ fn custom_round(
|
|||||||
idx: &mut usize,
|
idx: &mut usize,
|
||||||
sections: &mut Vec<elements::Section>,
|
sections: &mut Vec<elements::Section>,
|
||||||
) {
|
) {
|
||||||
while let Some(other_section) = map.get(&idx) {
|
while let Some(other_section) = map.get(idx) {
|
||||||
sections.push(other_section.clone());
|
sections.push(other_section.clone());
|
||||||
*idx += 1;
|
*idx += 1;
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-11
@@ -43,14 +43,14 @@ pub fn optimize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If there is start function in module, it should stary
|
// If there is start function in module, it should stary
|
||||||
module.start_section().map(|ss| stay.insert(resolve_function(&module, ss)));
|
module.start_section().map(|ss| stay.insert(resolve_function(module, ss)));
|
||||||
|
|
||||||
// All symbols used in data/element segments are also should be preserved
|
// All symbols used in data/element segments are also should be preserved
|
||||||
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(
|
push_code_symbols(
|
||||||
&module,
|
module,
|
||||||
segment
|
segment
|
||||||
.offset()
|
.offset()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@@ -63,7 +63,7 @@ pub fn optimize(
|
|||||||
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(
|
push_code_symbols(
|
||||||
&module,
|
module,
|
||||||
segment
|
segment
|
||||||
.offset()
|
.offset()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@@ -72,7 +72,7 @@ pub fn optimize(
|
|||||||
&mut init_symbols,
|
&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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -403,13 +403,9 @@ pub fn optimize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Also drop all custom sections
|
// Also drop all custom sections
|
||||||
module.sections_mut().retain(|section| {
|
module
|
||||||
if let elements::Section::Custom(_) = section {
|
.sections_mut()
|
||||||
false
|
.retain(|section| !matches!(section, elements::Section::Custom(_)));
|
||||||
} else {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-4
@@ -120,7 +120,7 @@ pub fn pack_instance(
|
|||||||
mbuilder.push_import(
|
mbuilder.push_import(
|
||||||
builder::import()
|
builder::import()
|
||||||
.module("env")
|
.module("env")
|
||||||
.field(&target.symbols().ret)
|
.field(target.symbols().ret)
|
||||||
.external()
|
.external()
|
||||||
.func(import_sig)
|
.func(import_sig)
|
||||||
.build(),
|
.build(),
|
||||||
@@ -172,11 +172,10 @@ pub fn pack_instance(
|
|||||||
let last_function_index = ctor_module.functions_space();
|
let last_function_index = ctor_module.functions_space();
|
||||||
|
|
||||||
// We ensure here that module has the DataSection
|
// We ensure here that module has the DataSection
|
||||||
if ctor_module
|
if !ctor_module
|
||||||
.sections()
|
.sections()
|
||||||
.iter()
|
.iter()
|
||||||
.find(|section| matches!(**section, Section::Data(_)))
|
.any(|section| matches!(*section, Section::Data(_)))
|
||||||
.is_none()
|
|
||||||
{
|
{
|
||||||
// DataSection has to be the last non-custom section according the to the spec
|
// DataSection has to be the last non-custom section according the to the spec
|
||||||
ctor_module
|
ctor_module
|
||||||
|
|||||||
+1
-5
@@ -340,10 +340,6 @@ impl Rules for Set {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn memory_grow_cost(&self) -> Option<MemoryGrowCost> {
|
fn memory_grow_cost(&self) -> Option<MemoryGrowCost> {
|
||||||
if let Some(val) = NonZeroU32::new(self.grow) {
|
NonZeroU32::new(self.grow).map(MemoryGrowCost::Linear)
|
||||||
Some(MemoryGrowCost::Linear(val))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,10 +80,7 @@ impl Stack {
|
|||||||
/// Returns `Err` if the control stack is empty.
|
/// Returns `Err` if the control stack is empty.
|
||||||
fn pop_frame(&mut self) -> Result<Frame, Error> {
|
fn pop_frame(&mut self) -> Result<Frame, Error> {
|
||||||
trace!(target: "max_height", "pop_frame: {:?}", self.control_stack.last());
|
trace!(target: "max_height", "pop_frame: {:?}", self.control_stack.last());
|
||||||
Ok(self
|
self.control_stack.pop().ok_or_else(|| Error("stack must be non-empty".into()))
|
||||||
.control_stack
|
|
||||||
.pop()
|
|
||||||
.ok_or_else(|| Error("stack must be non-empty".into()))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Truncate the height of value stack to the specified height.
|
/// Truncate the height of value stack to the specified height.
|
||||||
|
|||||||
@@ -176,7 +176,7 @@ fn compute_stack_costs(module: &elements::Module) -> Result<Vec<u32>, Error> {
|
|||||||
// We can't calculate stack_cost of the import functions.
|
// We can't calculate stack_cost of the import functions.
|
||||||
Ok(0)
|
Ok(0)
|
||||||
} else {
|
} else {
|
||||||
compute_stack_cost(func_idx as u32, &module)
|
compute_stack_cost(func_idx as u32, module)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
|||||||
Reference in New Issue
Block a user