7 Commits

Author SHA1 Message Date
Sergei Shulepov 310db5f92c Improve performance of stack height injection
Specifically,

- avoid using `import_count` and `function_space` since those are slow.
- Avoid using the `builder` module since it requires recreation of the module essentially and is not efficient.
- That also allows us to avoid creation of redundant signatures for thunks.
2022-10-19 17:56:29 +02:00
dependabot[bot] 4c1d47a618 Update wasmparser requirement from 0.90 to 0.92 (#33)
Updates the requirements on [wasmparser](https://github.com/bytecodealliance/wasm-tools) to permit the latest version.
- [Release notes](https://github.com/bytecodealliance/wasm-tools/releases)
- [Commits](https://github.com/bytecodealliance/wasm-tools/compare/wasmparser-0.90.0...wasmparser-0.92.0)

---
updated-dependencies:
- dependency-name: wasmparser
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-10-10 11:22:57 +02:00
dependabot[bot] 840af19d4b Update criterion requirement from 0.3 to 0.4 (#31)
Updates the requirements on [criterion](https://github.com/bheisler/criterion.rs) to permit the latest version.
- [Release notes](https://github.com/bheisler/criterion.rs/releases)
- [Changelog](https://github.com/bheisler/criterion.rs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/bheisler/criterion.rs/compare/0.3.0...0.4.0)

---
updated-dependencies:
- dependency-name: criterion
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-09-14 12:00:09 +02:00
Alexander Theißen 0229f865b6 Use u64 for gas counter (#30)
* Use `u64` for gas counter

* Update doc
2022-09-11 14:36:06 +02:00
dependabot[bot] c2db4b8365 Update wasmparser requirement from 0.89 to 0.90 (#28)
Updates the requirements on [wasmparser](https://github.com/bytecodealliance/wasm-tools) to permit the latest version.
- [Release notes](https://github.com/bytecodealliance/wasm-tools/releases)
- [Commits](https://github.com/bytecodealliance/wasm-tools/compare/wasmparser-0.89.0...wasmparser-0.90.0)

---
updated-dependencies:
- dependency-name: wasmparser
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-08-24 08:59:46 +02:00
dependabot[bot] 1ade161da4 Update wasmparser requirement from 0.88 to 0.89 (#27)
Updates the requirements on [wasmparser](https://github.com/bytecodealliance/wasm-tools) to permit the latest version.
- [Release notes](https://github.com/bytecodealliance/wasm-tools/releases)
- [Commits](https://github.com/bytecodealliance/wasm-tools/compare/wasmparser-0.88.0...wasmparser-0.89.0)

---
updated-dependencies:
- dependency-name: wasmparser
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-08-17 14:08:30 +02:00
dependabot[bot] 25ff883bbd Update wasmparser requirement from 0.87 to 0.88 (#26)
Updates the requirements on [wasmparser](https://github.com/bytecodealliance/wasm-tools) to permit the latest version.
- [Release notes](https://github.com/bytecodealliance/wasm-tools/releases)
- [Commits](https://github.com/bytecodealliance/wasm-tools/compare/wasmparser-0.87.0...wasmparser-0.88.0)

---
updated-dependencies:
- dependency-name: wasmparser
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-07-30 17:39:07 +02:00
12 changed files with 319 additions and 249 deletions
+10
View File
@@ -16,7 +16,17 @@ The interface provided to smart contracts will adhere to semver with one excepti
major version bumps will be backwards compatible with regard to already deployed contracts. major version bumps will be backwards compatible with regard to already deployed contracts.
In other words: Upgrading this pallet will not break pre-existing contracts. In other words: Upgrading this pallet will not break pre-existing contracts.
## [v0.3.0]
### Changed
- Use 64bit arithmetic for per-block gas counter
[#30](https://github.com/paritytech/wasm-instrument/pull/30)
## [v0.2.0] 2022-06-06 ## [v0.2.0] 2022-06-06
### Changed
- Adjust debug information (if already parsed) when injecting gas metering - Adjust debug information (if already parsed) when injecting gas metering
[#16](https://github.com/paritytech/wasm-instrument/pull/16) [#16](https://github.com/paritytech/wasm-instrument/pull/16)
+4 -3
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "wasm-instrument" name = "wasm-instrument"
version = "0.2.0" version = "0.3.0"
edition = "2021" edition = "2021"
rust-version = "1.56.1" rust-version = "1.56.1"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
@@ -25,11 +25,12 @@ parity-wasm = { version = "0.45", default-features = false }
[dev-dependencies] [dev-dependencies]
binaryen = "0.12" binaryen = "0.12"
criterion = "0.3" criterion = "0.4"
diff = "0.1" diff = "0.1"
pretty_assertions = "1"
rand = "0.8" rand = "0.8"
wat = "1" wat = "1"
wasmparser = "0.87" wasmparser = "0.92"
wasmprinter = "0.2" wasmprinter = "0.2"
[features] [features]
+49 -38
View File
@@ -105,7 +105,7 @@ impl Rules for ConstantCostRules {
/// imported gas metering function. /// imported gas metering function.
/// ///
/// The output module imports a function "gas" from the specified module with type signature /// The output module imports a function "gas" from the specified module with type signature
/// [i32] -> []. The argument is the amount of gas required to continue execution. The external /// [i64] -> []. The argument is the amount of gas required to continue execution. The external
/// function is meant to keep track of the total amount of gas used and trap or otherwise halt /// function is meant to keep track of the total amount of gas used and trap or otherwise halt
/// execution of the runtime if the gas usage exceeds some allowed limit. /// execution of the runtime if the gas usage exceeds some allowed limit.
/// ///
@@ -144,7 +144,7 @@ pub fn inject<R: Rules>(
// Injecting gas counting external // Injecting gas counting external
let mut mbuilder = builder::from_module(module); let mut mbuilder = builder::from_module(module);
let import_sig = let import_sig =
mbuilder.push_signature(builder::signature().with_param(ValueType::I32).build_sig()); mbuilder.push_signature(builder::signature().with_param(ValueType::I64).build_sig());
mbuilder.push_import( mbuilder.push_import(
builder::import() builder::import()
@@ -284,7 +284,7 @@ struct MeteredBlock {
/// Index of the first instruction (aka `Opcode`) in the block. /// Index of the first instruction (aka `Opcode`) in the block.
start_pos: usize, start_pos: usize,
/// Sum of costs of all instructions until end of the block. /// Sum of costs of all instructions until end of the block.
cost: u32, cost: u64,
} }
/// Counter is used to manage state during the gas metering algorithm implemented by /// Counter is used to manage state during the gas metering algorithm implemented by
@@ -375,7 +375,8 @@ impl Counter {
.expect("last_index is greater than 0; last_index is stack size - 1; qed"); .expect("last_index is greater than 0; last_index is stack size - 1; qed");
let prev_metered_block = &mut prev_control_block.active_metered_block; let prev_metered_block = &mut prev_control_block.active_metered_block;
if closing_metered_block.start_pos == prev_metered_block.start_pos { if closing_metered_block.start_pos == prev_metered_block.start_pos {
prev_metered_block.cost += closing_metered_block.cost; prev_metered_block.cost =
prev_metered_block.cost.checked_add(closing_metered_block.cost).ok_or(())?;
return Ok(()) return Ok(())
} }
} }
@@ -425,7 +426,7 @@ impl Counter {
/// 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(())?; top_block.cost = top_block.cost.checked_add(val.into()).ok_or(())?;
Ok(()) Ok(())
} }
} }
@@ -465,8 +466,9 @@ fn add_grow_counter<R: Rules>(
.with_instructions(elements::Instructions::new(vec![ .with_instructions(elements::Instructions::new(vec![
GetLocal(0), GetLocal(0),
GetLocal(0), GetLocal(0),
I32Const(cost as i32), I64ExtendUI32,
I32Mul, I64Const(i64::from(cost)),
I64Mul,
// todo: there should be strong guarantee that it does not return anything on // todo: there should be strong guarantee that it does not return anything on
// stack? // stack?
Call(gas_func), Call(gas_func),
@@ -583,7 +585,7 @@ fn insert_metering_calls(
// If there the next block starts at this position, inject metering instructions. // If there the next block starts at this position, inject metering instructions.
let used_block = if let Some(block) = block_iter.peek() { let used_block = if let Some(block) = block_iter.peek() {
if block.start_pos == original_pos { if block.start_pos == original_pos {
new_instrs.push(I32Const(block.cost as i32)); new_instrs.push(I64Const(block.cost as i64));
new_instrs.push(Call(gas_func)); new_instrs.push(Call(gas_func));
true true
} else { } else {
@@ -612,6 +614,7 @@ fn insert_metering_calls(
mod tests { mod tests {
use super::*; use super::*;
use parity_wasm::{builder, elements, elements::Instruction::*, serialize}; use parity_wasm::{builder, elements, elements::Instruction::*, serialize};
use pretty_assertions::assert_eq;
fn get_function_body( fn get_function_body(
module: &elements::Module, module: &elements::Module,
@@ -639,12 +642,20 @@ mod tests {
assert_eq!( assert_eq!(
get_function_body(&injected_module, 0).unwrap(), get_function_body(&injected_module, 0).unwrap(),
&vec![I32Const(2), Call(0), GetGlobal(0), Call(2), End][..] &vec![I64Const(2), Call(0), GetGlobal(0), Call(2), End][..]
); );
assert_eq!( assert_eq!(
get_function_body(&injected_module, 1).unwrap(), get_function_body(&injected_module, 1).unwrap(),
&vec![GetLocal(0), GetLocal(0), I32Const(10000), I32Mul, Call(0), GrowMemory(0), End,] &vec![
[..] GetLocal(0),
GetLocal(0),
I64ExtendUI32,
I64Const(10000),
I64Mul,
Call(0),
GrowMemory(0),
End,
][..]
); );
let binary = serialize(injected_module).expect("serialization failed"); let binary = serialize(injected_module).expect("serialization failed");
@@ -667,7 +678,7 @@ mod tests {
assert_eq!( assert_eq!(
get_function_body(&injected_module, 0).unwrap(), get_function_body(&injected_module, 0).unwrap(),
&vec![I32Const(2), Call(0), GetGlobal(0), GrowMemory(0), End][..] &vec![I64Const(2), Call(0), GetGlobal(0), GrowMemory(0), End][..]
); );
assert_eq!(injected_module.functions_space(), 2); assert_eq!(injected_module.functions_space(), 2);
@@ -719,17 +730,17 @@ mod tests {
assert_eq!( assert_eq!(
get_function_body(&injected_module, 1).unwrap(), get_function_body(&injected_module, 1).unwrap(),
&vec![ &vec![
I32Const(3), I64Const(3),
Call(0), Call(0),
Call(1), Call(1),
If(elements::BlockType::NoResult), If(elements::BlockType::NoResult),
I32Const(3), I64Const(3),
Call(0), Call(0),
Call(1), Call(1),
Call(1), Call(1),
Call(1), Call(1),
Else, Else,
I32Const(2), I64Const(2),
Call(0), Call(0),
Call(1), Call(1),
Call(1), Call(1),
@@ -775,7 +786,7 @@ mod tests {
expected = r#" expected = r#"
(module (module
(func (result i32) (func (result i32)
(call 0 (i32.const 1)) (call 0 (i64.const 1))
(get_global 0))) (get_global 0)))
"# "#
} }
@@ -795,7 +806,7 @@ mod tests {
expected = r#" expected = r#"
(module (module
(func (result i32) (func (result i32)
(call 0 (i32.const 6)) (call 0 (i64.const 6))
(get_global 0) (get_global 0)
(block (block
(get_global 0) (get_global 0)
@@ -824,16 +835,16 @@ mod tests {
expected = r#" expected = r#"
(module (module
(func (result i32) (func (result i32)
(call 0 (i32.const 3)) (call 0 (i64.const 3))
(get_global 0) (get_global 0)
(if (if
(then (then
(call 0 (i32.const 3)) (call 0 (i64.const 3))
(get_global 0) (get_global 0)
(get_global 0) (get_global 0)
(get_global 0)) (get_global 0))
(else (else
(call 0 (i32.const 2)) (call 0 (i64.const 2))
(get_global 0) (get_global 0)
(get_global 0))) (get_global 0)))
(get_global 0))) (get_global 0)))
@@ -857,13 +868,13 @@ mod tests {
expected = r#" expected = r#"
(module (module
(func (result i32) (func (result i32)
(call 0 (i32.const 6)) (call 0 (i64.const 6))
(get_global 0) (get_global 0)
(block (block
(get_global 0) (get_global 0)
(drop) (drop)
(br 0) (br 0)
(call 0 (i32.const 2)) (call 0 (i64.const 2))
(get_global 0) (get_global 0)
(drop)) (drop))
(get_global 0))) (get_global 0)))
@@ -891,18 +902,18 @@ mod tests {
expected = r#" expected = r#"
(module (module
(func (result i32) (func (result i32)
(call 0 (i32.const 5)) (call 0 (i64.const 5))
(get_global 0) (get_global 0)
(block (block
(get_global 0) (get_global 0)
(if (if
(then (then
(call 0 (i32.const 4)) (call 0 (i64.const 4))
(get_global 0) (get_global 0)
(get_global 0) (get_global 0)
(drop) (drop)
(br_if 1))) (br_if 1)))
(call 0 (i32.const 2)) (call 0 (i64.const 2))
(get_global 0) (get_global 0)
(drop)) (drop))
(get_global 0))) (get_global 0)))
@@ -933,18 +944,18 @@ mod tests {
expected = r#" expected = r#"
(module (module
(func (result i32) (func (result i32)
(call 0 (i32.const 3)) (call 0 (i64.const 3))
(get_global 0) (get_global 0)
(loop (loop
(call 0 (i32.const 4)) (call 0 (i64.const 4))
(get_global 0) (get_global 0)
(if (if
(then (then
(call 0 (i32.const 2)) (call 0 (i64.const 2))
(get_global 0) (get_global 0)
(br_if 0)) (br_if 0))
(else (else
(call 0 (i32.const 4)) (call 0 (i64.const 4))
(get_global 0) (get_global 0)
(get_global 0) (get_global 0)
(drop) (drop)
@@ -969,13 +980,13 @@ mod tests {
expected = r#" expected = r#"
(module (module
(func (result i32) (func (result i32)
(call 0 (i32.const 2)) (call 0 (i64.const 2))
(get_global 0) (get_global 0)
(if (if
(then (then
(call 0 (i32.const 1)) (call 0 (i64.const 1))
(return))) (return)))
(call 0 (i32.const 1)) (call 0 (i64.const 1))
(get_global 0))) (get_global 0)))
"# "#
} }
@@ -998,18 +1009,18 @@ mod tests {
expected = r#" expected = r#"
(module (module
(func (result i32) (func (result i32)
(call 0 (i32.const 5)) (call 0 (i64.const 5))
(get_global 0) (get_global 0)
(block (block
(get_global 0) (get_global 0)
(if (if
(then (then
(call 0 (i32.const 1)) (call 0 (i64.const 1))
(br 1)) (br 1))
(else (else
(call 0 (i32.const 1)) (call 0 (i64.const 1))
(br 0))) (br 0)))
(call 0 (i32.const 2)) (call 0 (i64.const 2))
(get_global 0) (get_global 0)
(drop)) (drop))
(get_global 0))) (get_global 0)))
@@ -1031,9 +1042,9 @@ mod tests {
expected = r#" expected = r#"
(module (module
(func (func
(call 0 (i32.const 2)) (call 0 (i64.const 2))
(loop (loop
(call 0 (i32.const 1)) (call 0 (i64.const 1))
(br 0) (br 0)
) )
unreachable unreachable
+7 -7
View File
@@ -23,10 +23,10 @@ struct ControlFlowNode {
first_instr_pos: Option<usize>, first_instr_pos: Option<usize>,
/// The actual gas cost of executing all instructions in the basic block. /// The actual gas cost of executing all instructions in the basic block.
actual_cost: u32, actual_cost: u64,
/// The amount of gas charged by the injected metering instructions within this basic block. /// The amount of gas charged by the injected metering instructions within this basic block.
charged_cost: u32, charged_cost: u64,
/// Whether there are any other nodes in the graph that loop back to this one. Every cycle in /// Whether there are any other nodes in the graph that loop back to this one. Every cycle in
/// the control flow graph contains at least one node with this flag set. /// the control flow graph contains at least one node with this flag set.
@@ -68,10 +68,10 @@ impl ControlFlowGraph {
} }
fn increment_actual_cost(&mut self, node_id: NodeId, cost: u32) { fn increment_actual_cost(&mut self, node_id: NodeId, cost: u32) {
self.get_node_mut(node_id).actual_cost += cost; self.get_node_mut(node_id).actual_cost += u64::from(cost);
} }
fn increment_charged_cost(&mut self, node_id: NodeId, cost: u32) { fn increment_charged_cost(&mut self, node_id: NodeId, cost: u64) {
self.get_node_mut(node_id).charged_cost += cost; self.get_node_mut(node_id).charged_cost += cost;
} }
@@ -267,9 +267,9 @@ fn validate_graph_gas_costs(graph: &ControlFlowGraph) -> bool {
fn visit( fn visit(
graph: &ControlFlowGraph, graph: &ControlFlowGraph,
node_id: NodeId, node_id: NodeId,
mut total_actual: u32, mut total_actual: u64,
mut total_charged: u32, mut total_charged: u64,
loop_costs: &mut Map<NodeId, (u32, u32)>, loop_costs: &mut Map<NodeId, (u64, u64)>,
) -> bool { ) -> bool {
let node = graph.get_node(node_id); let node = graph.get_node(node_id);
+40 -40
View File
@@ -1,10 +1,11 @@
use super::resolve_func_type;
use alloc::vec::Vec; use alloc::vec::Vec;
use parity_wasm::elements::{self, BlockType, Type}; use parity_wasm::elements::{self, BlockType, Type};
#[cfg(feature = "sign_ext")] #[cfg(feature = "sign_ext")]
use parity_wasm::elements::SignExtInstruction; use parity_wasm::elements::SignExtInstruction;
use super::Context;
// The cost in stack items that should be charged per call of a function. This is // The cost in stack items that should be charged per call of a function. This is
// is a static cost that is added to each function call. This makes sense because even // is a static cost that is added to each function call. This makes sense because even
// if a function does not use any parameters or locals some stack space on the host // if a function does not use any parameters or locals some stack space on the host
@@ -122,7 +123,11 @@ impl Stack {
} }
/// This function expects the function to be validated. /// This function expects the function to be validated.
pub fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, &'static str> { pub fn compute(
func_idx: u32,
ctx: &Context,
module: &elements::Module,
) -> Result<u32, &'static str> {
use parity_wasm::elements::Instruction::*; use parity_wasm::elements::Instruction::*;
let func_section = module.function_section().ok_or("No function section")?; let func_section = module.function_section().ok_or("No function section")?;
@@ -246,8 +251,10 @@ pub fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, &'static
stack.pop_values(func_arity)?; stack.pop_values(func_arity)?;
stack.mark_unreachable()?; stack.mark_unreachable()?;
}, },
Call(idx) => { Call(fn_idx) => {
let ty = resolve_func_type(*idx, module)?; let ty_idx = ctx.func_type(*fn_idx).ok_or("function idx is not found in the func types list")?;
let Type::Function(ty) =
type_section.types().get(ty_idx as usize).ok_or("Type not found")?;
// Pop values for arguments of the function. // Pop values for arguments of the function.
stack.pop_values(ty.params().len() as u32)?; stack.pop_values(ty.params().len() as u32)?;
@@ -411,7 +418,8 @@ pub fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, &'static
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::ACTIVATION_FRAME_COST;
use crate::stack_limiter::prepare_context;
use parity_wasm::elements; use parity_wasm::elements;
fn parse_wat(source: &str) -> elements::Module { fn parse_wat(source: &str) -> elements::Module {
@@ -419,10 +427,15 @@ mod tests {
.expect("Failed to deserialize the module") .expect("Failed to deserialize the module")
} }
fn test_compute(func_idx: u32, source: &str) -> u32 {
let module = parse_wat(source);
let ctx = prepare_context(&module, 0).unwrap();
ctx.stack_cost(func_idx).unwrap()
}
#[test] #[test]
fn simple_test() { fn simple_test() {
let module = parse_wat( let module = r#"
r#"
(module (module
(func (func
i32.const 1 i32.const 1
@@ -433,34 +446,30 @@ mod tests {
drop drop
) )
) )
"#, "#;
);
let height = compute(0, &module).unwrap(); let height = test_compute(0, module);
assert_eq!(height, 3 + ACTIVATION_FRAME_COST); assert_eq!(height, 3 + ACTIVATION_FRAME_COST);
} }
#[test] #[test]
fn implicit_and_explicit_return() { fn implicit_and_explicit_return() {
let module = parse_wat( let module = r#"
r#"
(module (module
(func (result i32) (func (result i32)
i32.const 0 i32.const 0
return return
) )
) )
"#, "#;
);
let height = compute(0, &module).unwrap(); let height = test_compute(0, module);
assert_eq!(height, 1 + ACTIVATION_FRAME_COST); assert_eq!(height, 1 + ACTIVATION_FRAME_COST);
} }
#[test] #[test]
fn dont_count_in_unreachable() { fn dont_count_in_unreachable() {
let module = parse_wat( let module = r#"
r#"
(module (module
(memory 0) (memory 0)
(func (result i32) (func (result i32)
@@ -468,17 +477,15 @@ mod tests {
grow_memory grow_memory
) )
) )
"#, "#;
);
let height = compute(0, &module).unwrap(); let height = test_compute(0, module);
assert_eq!(height, ACTIVATION_FRAME_COST); assert_eq!(height, ACTIVATION_FRAME_COST);
} }
#[test] #[test]
fn yet_another_test() { fn yet_another_test() {
let module = parse_wat( let module = r#"
r#"
(module (module
(memory 0) (memory 0)
(func (func
@@ -497,17 +504,15 @@ mod tests {
i32.const 2 i32.const 2
) )
) )
"#, "#;
);
let height = compute(0, &module).unwrap(); let height = test_compute(0, module);
assert_eq!(height, 2 + ACTIVATION_FRAME_COST); assert_eq!(height, 2 + ACTIVATION_FRAME_COST);
} }
#[test] #[test]
fn call_indirect() { fn call_indirect() {
let module = parse_wat( let module = r#"
r#"
(module (module
(table $ptr 1 1 funcref) (table $ptr 1 1 funcref)
(elem $ptr (i32.const 0) func 1) (elem $ptr (i32.const 0) func 1)
@@ -521,17 +526,15 @@ mod tests {
drop drop
) )
) )
"#, "#;
);
let height = compute(0, &module).unwrap(); let height = test_compute(0, module);
assert_eq!(height, 1 + ACTIVATION_FRAME_COST); assert_eq!(height, 1 + ACTIVATION_FRAME_COST);
} }
#[test] #[test]
fn breaks() { fn breaks() {
let module = parse_wat( let module = r#"
r#"
(module (module
(func $main (func $main
block (result i32) block (result i32)
@@ -543,17 +546,15 @@ mod tests {
drop drop
) )
) )
"#, "#;
);
let height = compute(0, &module).unwrap(); let height = test_compute(0, module);
assert_eq!(height, 1 + ACTIVATION_FRAME_COST); assert_eq!(height, 1 + ACTIVATION_FRAME_COST);
} }
#[test] #[test]
fn if_else_works() { fn if_else_works() {
let module = parse_wat( let module = r#"
r#"
(module (module
(func $main (func $main
i32.const 7 i32.const 7
@@ -569,10 +570,9 @@ mod tests {
drop drop
) )
) )
"#, "#;
);
let height = compute(0, &module).unwrap(); let height = test_compute(0, module);
assert_eq!(height, 3 + ACTIVATION_FRAME_COST); assert_eq!(height, 3 + ACTIVATION_FRAME_COST);
} }
} }
+134 -101
View File
@@ -39,7 +39,15 @@ mod max_height;
mod thunk; mod thunk;
pub struct Context { pub struct Context {
/// Number of functions that the module imports. Required to convert defined functions indicies
/// into the global function index space.
func_imports: u32,
/// For each function in the function space this vector stores the respective type index.
func_types: Vec<u32>,
/// The index of the global variable that contains the current stack height.
stack_height_global_idx: u32, stack_height_global_idx: u32,
/// Logical stack costs for each function in the function space. Imported functions have cost
/// of 0.
func_stack_costs: Vec<u32>, func_stack_costs: Vec<u32>,
stack_limit: u32, stack_limit: u32,
} }
@@ -55,6 +63,11 @@ impl Context {
self.func_stack_costs.get(func_idx as usize).cloned() self.func_stack_costs.get(func_idx as usize).cloned()
} }
/// Returns a reference to the function type index given by the index into the function space.
fn func_type(&self, func_idx: u32) -> Option<u32> {
self.func_types.get(func_idx as usize).copied()
}
/// Returns stack limit specified by the rules. /// Returns stack limit specified by the rules.
fn stack_limit(&self) -> u32 { fn stack_limit(&self) -> u32 {
self.stack_limit self.stack_limit
@@ -115,20 +128,106 @@ pub fn inject(
mut module: elements::Module, mut module: elements::Module,
stack_limit: u32, stack_limit: u32,
) -> Result<elements::Module, &'static str> { ) -> Result<elements::Module, &'static str> {
let mut ctx = Context { let mut ctx = prepare_context(&module, stack_limit)?;
stack_height_global_idx: generate_stack_height_global(&mut module),
func_stack_costs: compute_stack_costs(&module)?,
stack_limit,
};
instrument_functions(&mut ctx, &mut module)?; generate_stack_height_global(&mut ctx.stack_height_global_idx, &mut module)?;
instrument_functions(&ctx, &mut module)?;
let module = thunk::generate_thunks(&mut ctx, module)?; let module = thunk::generate_thunks(&mut ctx, module)?;
Ok(module) Ok(module)
} }
fn prepare_context(module: &elements::Module, stack_limit: u32) -> Result<Context, &'static str> {
let mut ctx = Context {
func_imports: module.import_count(elements::ImportCountType::Function) as u32,
func_types: Vec::new(),
stack_height_global_idx: 0,
func_stack_costs: Vec::new(),
stack_limit,
};
collect_func_types(&mut ctx, &module)?;
compute_stack_costs(&mut ctx, &module)?;
Ok(ctx)
}
fn collect_func_types(ctx: &mut Context, module: &elements::Module) -> Result<(), &'static str> {
let types = module.type_section().map(|ts| ts.types()).unwrap_or(&[]);
let functions = module.function_section().map(|fs| fs.entries()).unwrap_or(&[]);
let imports = module.import_section().map(|is| is.entries()).unwrap_or(&[]);
let ensure_ty = |sig_idx: u32| -> Result<(), &'static str> {
let Type::Function(_) = types
.get(sig_idx as usize)
.ok_or("The signature as specified by a function isn't defined")?;
Ok(())
};
for import in imports {
if let elements::External::Function(sig_idx) = import.external() {
ensure_ty(*sig_idx)?;
ctx.func_types.push(*sig_idx);
}
}
for def_func_idx in functions {
ensure_ty(def_func_idx.type_ref())?;
ctx.func_types.push(def_func_idx.type_ref());
}
Ok(())
}
/// Calculate stack costs for all functions in the function space.
///
/// The function space consists of the imported functions followed by defined functions.
/// All imported functions assumed to have the cost of 0.
fn compute_stack_costs(ctx: &mut Context, module: &elements::Module) -> Result<(), &'static str> {
for _ in 0..ctx.func_imports {
ctx.func_stack_costs.push(0);
}
let def_func_n = module.function_section().map(|fs| fs.entries().len()).unwrap_or(0) as u32;
for def_func_idx in 0..def_func_n {
let cost = compute_stack_cost(def_func_idx, ctx, module)?;
ctx.func_stack_costs.push(cost);
}
Ok(())
}
/// Computes the stack cost of a given function. The function is specified by its index in the
/// declared function space.
///
/// Stack cost of a given function is the sum of it's locals count (that is,
/// number of arguments plus number of local variables) and the maximal stack
/// height.
fn compute_stack_cost(
def_func_idx: u32,
ctx: &Context,
module: &elements::Module,
) -> Result<u32, &'static str> {
let code_section =
module.code_section().ok_or("Due to validation code section should exists")?;
let body = &code_section
.bodies()
.get(def_func_idx as usize)
.ok_or("Function body is out of bounds")?;
let mut locals_count: u32 = 0;
for local_group in body.locals() {
locals_count =
locals_count.checked_add(local_group.count()).ok_or("Overflow in local count")?;
}
let max_stack_height = max_height::compute(def_func_idx, ctx, module)?;
locals_count
.checked_add(max_stack_height)
.ok_or("Overflow in adding locals_count and max_stack_height")
}
/// Generate a new global that will be used for tracking current stack height. /// Generate a new global that will be used for tracking current stack height.
fn generate_stack_height_global(module: &mut elements::Module) -> u32 { fn generate_stack_height_global(
stack_height_global_idx: &mut u32,
module: &mut elements::Module,
) -> Result<(), &'static str> {
let global_entry = builder::global() let global_entry = builder::global()
.value_type() .value_type()
.i32() .i32()
@@ -140,71 +239,41 @@ fn generate_stack_height_global(module: &mut elements::Module) -> u32 {
for section in module.sections_mut() { for section in module.sections_mut() {
if let elements::Section::Global(gs) = section { if let elements::Section::Global(gs) = section {
gs.entries_mut().push(global_entry); gs.entries_mut().push(global_entry);
return (gs.entries().len() as u32) - 1 *stack_height_global_idx = (gs.entries().len() as u32) - 1;
return Ok(());
} }
} }
// Existing section not found, create one! // Existing section not found, create one!
module //
.sections_mut() // It's a bit tricky since the sections have a strict prescribed order.
.push(elements::Section::Global(elements::GlobalSection::with_entries(vec![global_entry]))); let global_section = elements::GlobalSection::with_entries(vec![global_entry]);
0 let prec_index = module
} .sections()
.iter()
/// Calculate stack costs for all functions. .rposition(|section| {
/// use elements::Section::*;
/// Returns a vector with a stack cost for each function, including imports. match section {
fn compute_stack_costs(module: &elements::Module) -> Result<Vec<u32>, &'static str> { Type(_) | Import(_) | Function(_) | Table(_) | Memory(_) => true,
let func_imports = module.import_count(elements::ImportCountType::Function); _ => false,
// TODO: optimize!
(0..module.functions_space())
.map(|func_idx| {
if func_idx < func_imports {
// We can't calculate stack_cost of the import functions.
Ok(0)
} else {
compute_stack_cost(func_idx as u32, module)
} }
}) })
.collect() .ok_or("generate stack height global hasn't found any preceding sections")?;
// now `prec_index` points to the last section preceding the `global_section`. It's guaranteed that at least
// one of those functions is present. Therefore, the candidate position for the global section is the following
// one. However, technically, custom sections could occupy any place between the well-known sections.
//
// Now, regarding `+1` here. `insert` panics iff `index > len`. `prec_index + 1` can only be equal to `len`.
module
.sections_mut()
.insert(prec_index + 1, elements::Section::Global(global_section));
// First entry in the brand new globals section.
*stack_height_global_idx = 0;
Ok(())
} }
/// Stack cost of the given *defined* function is the sum of it's locals count (that is, fn instrument_functions(ctx: &Context, module: &mut elements::Module) -> Result<(), &'static str> {
/// number of arguments plus number of local variables) and the maximal stack
/// height.
fn compute_stack_cost(func_idx: u32, module: &elements::Module) -> Result<u32, &'static str> {
// To calculate the cost of a function we need to convert index from
// function index space to defined function spaces.
let func_imports = module.import_count(elements::ImportCountType::Function) as u32;
let defined_func_idx = func_idx
.checked_sub(func_imports)
.ok_or("This should be a index of a defined function")?;
let code_section =
module.code_section().ok_or("Due to validation code section should exists")?;
let body = &code_section
.bodies()
.get(defined_func_idx as usize)
.ok_or("Function body is out of bounds")?;
let mut locals_count: u32 = 0;
for local_group in body.locals() {
locals_count =
locals_count.checked_add(local_group.count()).ok_or("Overflow in local count")?;
}
let max_stack_height = max_height::compute(defined_func_idx, module)?;
locals_count
.checked_add(max_stack_height)
.ok_or("Overflow in adding locals_count and max_stack_height")
}
fn instrument_functions(
ctx: &mut Context,
module: &mut elements::Module,
) -> Result<(), &'static str> {
for section in module.sections_mut() { for section in module.sections_mut() {
if let elements::Section::Code(code_section) = section { if let elements::Section::Code(code_section) = section {
for func_body in code_section.bodies_mut() { for func_body in code_section.bodies_mut() {
@@ -242,7 +311,7 @@ fn instrument_functions(
/// ///
/// drop /// drop
/// ``` /// ```
fn instrument_function(ctx: &mut Context, func: &mut Instructions) -> Result<(), &'static str> { fn instrument_function(ctx: &Context, func: &mut Instructions) -> Result<(), &'static str> {
use Instruction::*; use Instruction::*;
struct InstrumentCall { struct InstrumentCall {
@@ -309,42 +378,6 @@ fn instrument_function(ctx: &mut Context, func: &mut Instructions) -> Result<(),
Ok(()) Ok(())
} }
fn resolve_func_type(
func_idx: u32,
module: &elements::Module,
) -> Result<&elements::FunctionType, &'static str> {
let types = module.type_section().map(|ts| ts.types()).unwrap_or(&[]);
let functions = module.function_section().map(|fs| fs.entries()).unwrap_or(&[]);
let func_imports = module.import_count(elements::ImportCountType::Function);
let sig_idx = if func_idx < func_imports as u32 {
module
.import_section()
.expect("function import count is not zero; import section must exists; qed")
.entries()
.iter()
.filter_map(|entry| match entry.external() {
elements::External::Function(idx) => Some(*idx),
_ => None,
})
.nth(func_idx as usize)
.expect(
"func_idx is less than function imports count;
nth function import must be `Some`;
qed",
)
} else {
functions
.get(func_idx as usize - func_imports)
.ok_or("Function at the specified index is not defined")?
.type_ref()
};
let Type::Function(ty) = types
.get(sig_idx as usize)
.ok_or("The signature as specified by a function isn't defined")?;
Ok(ty)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
+58 -43
View File
@@ -1,17 +1,17 @@
#[cfg(not(features = "std"))] #[cfg(not(features = "std"))]
use alloc::collections::BTreeMap as Map; use alloc::collections::BTreeMap as Map;
use alloc::vec::Vec; use alloc::vec::Vec;
use parity_wasm::{ use parity_wasm::elements::{self, Internal};
builder,
elements::{self, FunctionType, Internal},
};
#[cfg(features = "std")] #[cfg(features = "std")]
use std::collections::HashMap as Map; use std::collections::HashMap as Map;
use super::{resolve_func_type, Context}; use super::Context;
struct Thunk { struct Thunk {
signature: FunctionType, /// The index of the signature in the type section.
type_idx: u32,
/// The number of parameters the function has.
param_num: u32,
// Index in function space of this thunk. // Index in function space of this thunk.
idx: Option<u32>, idx: Option<u32>,
callee_stack_cost: u32, callee_stack_cost: u32,
@@ -19,10 +19,11 @@ struct Thunk {
pub fn generate_thunks( pub fn generate_thunks(
ctx: &mut Context, ctx: &mut Context,
module: elements::Module, mut module: elements::Module,
) -> Result<elements::Module, &'static str> { ) -> Result<elements::Module, &'static str> {
// First, we need to collect all function indices that should be replaced by thunks // First, we need to collect all function indices that should be replaced by thunks
let mut replacement_map: Map<u32, Thunk> = { let mut replacement_map: Map<u32, Thunk> = {
let types = module.type_section().map(|ts| ts.types()).unwrap_or(&[]);
let exports = module.export_section().map(|es| es.entries()).unwrap_or(&[]); let exports = module.export_section().map(|es| es.entries()).unwrap_or(&[]);
let elem_segments = module.elements_section().map(|es| es.entries()).unwrap_or(&[]); let elem_segments = module.elements_section().map(|es| es.entries()).unwrap_or(&[]);
let start_func_idx = module.start_section(); let start_func_idx = module.start_section();
@@ -45,14 +46,12 @@ pub fn generate_thunks(
// Don't generate a thunk if stack_cost of a callee is zero. // Don't generate a thunk if stack_cost of a callee is zero.
if callee_stack_cost != 0 { if callee_stack_cost != 0 {
replacement_map.insert( let type_idx = ctx.func_type(func_idx).ok_or("type idx for thunk not found")?;
func_idx, let elements::Type::Function(func_ty) =
Thunk { types.get(type_idx as usize).ok_or("sig for thunk is not found")?;
signature: resolve_func_type(func_idx, &module)?.clone(), let param_num = func_ty.params().len() as u32;
idx: None, replacement_map
callee_stack_cost, .insert(func_idx, Thunk { type_idx, param_num, idx: None, callee_stack_cost });
},
);
} }
} }
@@ -61,10 +60,6 @@ pub fn generate_thunks(
// Then, we generate a thunk for each original function. // Then, we generate a thunk for each original function.
// Save current func_idx
let mut next_func_idx = module.functions_space() as u32;
let mut mbuilder = builder::from_module(module);
for (func_idx, thunk) in replacement_map.iter_mut() { for (func_idx, thunk) in replacement_map.iter_mut() {
let instrumented_call = instrument_call!( let instrumented_call = instrument_call!(
*func_idx, *func_idx,
@@ -77,32 +72,23 @@ pub fn generate_thunks(
// - instrumented call // - instrumented call
// - end // - end
let mut thunk_body: Vec<elements::Instruction> = let mut thunk_body: Vec<elements::Instruction> =
Vec::with_capacity(thunk.signature.params().len() + instrumented_call.len() + 1); Vec::with_capacity(thunk.param_num as usize + instrumented_call.len() + 1);
for (arg_idx, _) in thunk.signature.params().iter().enumerate() { for arg_idx in 0..thunk.param_num {
thunk_body.push(elements::Instruction::GetLocal(arg_idx as u32)); thunk_body.push(elements::Instruction::GetLocal(arg_idx));
} }
thunk_body.extend_from_slice(&instrumented_call); thunk_body.extend_from_slice(&instrumented_call);
thunk_body.push(elements::Instruction::End); thunk_body.push(elements::Instruction::End);
// TODO: Don't generate a signature, but find an existing one. let func_idx = insert_function(
ctx,
mbuilder = mbuilder &mut module,
.function() thunk.type_idx,
// Signature of the thunk should match the original function signature. Vec::new(), // No declared local variables.
.signature() elements::Instructions::new(thunk_body),
.with_params(thunk.signature.params().to_vec()) )?;
.with_results(thunk.signature.results().to_vec()) thunk.idx = Some(func_idx);
.build()
.body()
.with_instructions(elements::Instructions::new(thunk_body))
.build()
.build();
thunk.idx = Some(next_func_idx);
next_func_idx += 1;
} }
let mut module = mbuilder.build();
// And finally, fixup thunks in export and table sections. // And finally, fixup thunks in export and table sections.
@@ -118,18 +104,20 @@ pub fn generate_thunks(
for section in module.sections_mut() { for section in module.sections_mut() {
match section { match section {
elements::Section::Export(export_section) => elements::Section::Export(export_section) => {
for entry in export_section.entries_mut() { for entry in export_section.entries_mut() {
if let Internal::Function(function_idx) = entry.internal_mut() { if let Internal::Function(function_idx) = entry.internal_mut() {
fixup(function_idx) fixup(function_idx)
} }
}, }
elements::Section::Element(elem_section) => },
elements::Section::Element(elem_section) => {
for segment in elem_section.entries_mut() { for segment in elem_section.entries_mut() {
for function_idx in segment.members_mut() { for function_idx in segment.members_mut() {
fixup(function_idx) fixup(function_idx)
} }
}, }
},
elements::Section::Start(start_idx) => fixup(start_idx), elements::Section::Start(start_idx) => fixup(start_idx),
_ => {}, _ => {},
} }
@@ -137,3 +125,30 @@ pub fn generate_thunks(
Ok(module) Ok(module)
} }
/// Inserts a new function into the module and returns it's index in the function space.
///
/// Specifically, inserts entires into the function section and the code section.
fn insert_function(
ctx: &Context,
module: &mut elements::Module,
type_idx: u32,
locals: Vec<elements::Local>,
insns: elements::Instructions,
) -> Result<u32, &'static str> {
let funcs = module
.function_section_mut()
.ok_or("insert function no function section")?
.entries_mut();
let new_func_idx = ctx
.func_imports
.checked_add(funcs.len() as u32)
.ok_or("insert function func idx overflow")?;
funcs.push(elements::Func::new(type_idx));
let func_bodies =
module.code_section_mut().ok_or("insert function no code section")?.bodies_mut();
func_bodies.push(elements::FuncBody::new(locals, insns));
Ok(new_func_idx)
}
+3 -3
View File
@@ -1,10 +1,10 @@
(module (module
(type (;0;) (func (result i32))) (type (;0;) (func (result i32)))
(type (;1;) (func (param i32))) (type (;1;) (func (param i64)))
(import "env" "gas" (func (;0;) (type 1))) (import "env" "gas" (func (;0;) (type 1)))
(func $fibonacci_with_break (;1;) (type 0) (result i32) (func $fibonacci_with_break (;1;) (type 0) (result i32)
(local i32 i32) (local i32 i32)
i32.const 13 i64.const 13
call 0 call 0
block ;; label = @1 block ;; label = @1
i32.const 0 i32.const 0
@@ -18,7 +18,7 @@
local.set 1 local.set 1
i32.const 1 i32.const 1
br_if 0 (;@1;) br_if 0 (;@1;)
i32.const 5 i64.const 5
call 0 call 0
local.get 0 local.get 0
local.get 1 local.get 1
+3 -3
View File
@@ -1,10 +1,10 @@
(module (module
(type (;0;) (func (param i32 i32) (result i32))) (type (;0;) (func (param i32 i32) (result i32)))
(type (;1;) (func (param i32))) (type (;1;) (func (param i64)))
(import "env" "gas" (func (;0;) (type 1))) (import "env" "gas" (func (;0;) (type 1)))
(func $add_locals (;1;) (type 0) (param $x i32) (param $y i32) (result i32) (func $add_locals (;1;) (type 0) (param $x i32) (param $y i32) (result i32)
(local i32) (local i32)
i32.const 5 i64.const 5
call 0 call 0
local.get $x local.get $x
local.get $y local.get $y
@@ -13,7 +13,7 @@
local.get 2 local.get 2
) )
(func $add (;2;) (type 0) (param i32 i32) (result i32) (func $add (;2;) (type 0) (param i32 i32) (result i32)
i32.const 3 i64.const 3
call 0 call 0
local.get 0 local.get 0
local.get 1 local.get 1
+4 -4
View File
@@ -1,19 +1,19 @@
(module (module
(type (;0;) (func (param i32) (result i32))) (type (;0;) (func (param i32) (result i32)))
(type (;1;) (func (param i32))) (type (;1;) (func (param i64)))
(import "env" "gas" (func (;0;) (type 1))) (import "env" "gas" (func (;0;) (type 1)))
(func (;1;) (type 0) (param i32) (result i32) (func (;1;) (type 0) (param i32) (result i32)
i32.const 2 i64.const 2
call 0 call 0
i32.const 1 i32.const 1
if (result i32) ;; label = @1 if (result i32) ;; label = @1
i32.const 3 i64.const 3
call 0 call 0
local.get 0 local.get 0
i32.const 1 i32.const 1
i32.add i32.add
else else
i32.const 2 i64.const 2
call 0 call 0
local.get 0 local.get 0
i32.popcnt i32.popcnt
+5 -5
View File
@@ -1,16 +1,16 @@
(module (module
(type (;0;) (func)) (type (;0;) (func))
(type (;1;) (func (param i32))) (type (;1;) (func (param i64)))
(import "env" "gas" (func (;0;) (type 1))) (import "env" "gas" (func (;0;) (type 1)))
(func (;1;) (type 0) (func (;1;) (type 0)
i32.const 2 i64.const 2
call 0 call 0
i32.const 1 i32.const 1
if ;; label = @1 if ;; label = @1
i32.const 1 i64.const 1
call 0 call 0
loop ;; label = @2 loop ;; label = @2
i32.const 2 i64.const 2
call 0 call 0
i32.const 123 i32.const 123
drop drop
@@ -18,7 +18,7 @@
end end
) )
(func (;2;) (type 0) (func (;2;) (type 0)
i32.const 1 i64.const 1
call 0 call 0
block ;; label = @1 block ;; label = @1
end end
+2 -2
View File
@@ -1,12 +1,12 @@
(module (module
(type (;0;) (func (param i32 i32))) (type (;0;) (func (param i32 i32)))
(type (;1;) (func)) (type (;1;) (func))
(type (;2;) (func (param i32))) (type (;2;) (func (param i64)))
(import "env" "ext_return" (func $ext_return (;0;) (type 0))) (import "env" "ext_return" (func $ext_return (;0;) (type 0)))
(import "env" "memory" (memory (;0;) 1 1)) (import "env" "memory" (memory (;0;) 1 1))
(import "env" "gas" (func (;1;) (type 2))) (import "env" "gas" (func (;1;) (type 2)))
(func $start (;2;) (type 1) (func $start (;2;) (type 1)
i32.const 4 i64.const 4
call 1 call 1
i32.const 8 i32.const 8
i32.const 4 i32.const 4