wasm-utils/src/gas.rs

609 lines
14 KiB
Rust
Raw Normal View History

2018-05-15 08:22:29 +08:00
use std::vec::Vec;
2017-05-04 17:38:09 +03:00
use parity_wasm::{elements, builder};
2017-10-04 13:53:03 +03:00
use rules;
2017-05-04 17:38:09 +03:00
2018-06-29 19:01:06 +08:00
pub fn update_call_index(instructions: &mut elements::Instructions, inserted_index: u32) {
use parity_wasm::elements::Instruction::*;
for instruction in instructions.elements_mut().iter_mut() {
if let &mut Call(ref mut call_index) = instruction {
if *call_index >= inserted_index { *call_index += 1}
2017-05-19 16:54:53 +03:00
}
}
2017-05-04 17:38:09 +03:00
}
2018-04-24 14:51:54 +03:00
/// A block of code represented by it's start position and cost.
///
/// The block typically starts with instructions such as `loop`, `block`, `if`, etc.
///
/// An example of block:
///
/// ```ignore
/// loop
/// i32.const 1
/// get_local 0
/// i32.sub
/// tee_local 0
/// br_if 0
/// end
/// ```
///
/// The start of the block is `i32.const 1`.
///
#[derive(Debug)]
struct BlockEntry {
/// Index of the first instruction (aka `Opcode`) in the block.
start_pos: usize,
/// Sum of costs of all instructions until end of the block.
cost: u32,
}
struct Counter {
/// All blocks in the order of theirs start position.
blocks: Vec<BlockEntry>,
// Stack of blocks. Each element is an index to a `self.blocks` vector.
stack: Vec<usize>,
}
impl Counter {
fn new() -> Counter {
Counter {
stack: Vec::new(),
blocks: Vec::new(),
}
}
/// Begin a new block.
fn begin(&mut self, cursor: usize) {
let block_idx = self.blocks.len();
self.blocks.push(BlockEntry {
start_pos: cursor,
cost: 1,
});
self.stack.push(block_idx);
}
/// Finalize the current block.
///
/// Finalized blocks have final cost which will not change later.
fn finalize(&mut self) -> Result<(), ()> {
self.stack.pop().ok_or_else(|| ())?;
Ok(())
}
/// Increment the cost of the current block by the specified value.
fn increment(&mut self, val: u32) -> Result<(), ()> {
let stack_top = self.stack.last_mut().ok_or_else(|| ())?;
let top_block = self.blocks.get_mut(*stack_top).ok_or_else(|| ())?;
top_block.cost = top_block.cost.checked_add(val).ok_or_else(|| ())?;
Ok(())
}
2017-06-26 14:27:29 +03:00
}
2018-06-29 19:01:06 +08:00
fn inject_grow_counter(instructions: &mut elements::Instructions, grow_counter_func: u32) -> usize {
use parity_wasm::elements::Instruction::*;
2018-02-05 00:10:53 +03:00
let mut counter = 0;
2018-06-29 19:01:06 +08:00
for instruction in instructions.elements_mut() {
if let GrowMemory(_) = *instruction {
*instruction = Call(grow_counter_func);
counter += 1;
2018-02-05 00:10:53 +03:00
}
}
counter
}
fn add_grow_counter(module: elements::Module, rules: &rules::Set, gas_func: u32) -> elements::Module {
2018-06-29 19:01:06 +08:00
use parity_wasm::elements::Instruction::*;
2018-02-05 00:10:53 +03:00
let mut b = builder::from_module(module);
b.push_function(
builder::function()
2018-02-05 15:27:32 +03:00
.signature().params().i32().build().with_return_type(Some(elements::ValueType::I32)).build()
2018-02-05 00:10:53 +03:00
.body()
2018-06-29 19:01:06 +08:00
.with_instructions(elements::Instructions::new(vec![
2018-02-05 15:27:32 +03:00
GetLocal(0),
2018-02-05 00:10:53 +03:00
GetLocal(0),
I32Const(rules.grow_cost() as i32),
I32Mul,
// todo: there should be strong guarantee that it does not return anything on stack?
Call(gas_func),
GrowMemory(0),
2018-02-05 00:40:28 +03:00
End,
2018-02-05 00:10:53 +03:00
]))
.build()
.build()
);
b.build()
}
pub fn inject_counter(
2018-06-29 19:01:06 +08:00
instructions: &mut elements::Instructions,
2018-02-05 00:10:53 +03:00
rules: &rules::Set,
gas_func: u32,
2018-02-19 19:28:12 +03:00
) -> Result<(), ()> {
2018-06-29 19:01:06 +08:00
use parity_wasm::elements::Instruction::*;
2017-06-26 14:27:29 +03:00
2018-04-24 14:51:54 +03:00
let mut counter = Counter::new();
2017-06-26 14:27:29 +03:00
2018-04-24 14:51:54 +03:00
// Begin an implicit function (i.e. `func...end`) block.
counter.begin(0);
2017-06-26 14:27:29 +03:00
2018-06-29 19:01:06 +08:00
for cursor in 0..instructions.elements().len() {
let instruction = &instructions.elements()[cursor];
match *instruction {
2018-04-24 14:51:54 +03:00
Block(_) | If(_) | Loop(_) => {
// Increment previous block with the cost of the current opcode.
2018-06-29 19:01:06 +08:00
let instruction_cost = rules.process(instruction)?;
counter.increment(instruction_cost)?;
2018-04-24 14:51:54 +03:00
// Begin new block. The cost of the following opcodes until `End` or `Else` will
// be included into this block.
counter.begin(cursor + 1);
2017-06-26 14:27:29 +03:00
}
2018-04-24 14:51:54 +03:00
End => {
// Just finalize current block.
counter.finalize()?;
2017-05-19 16:54:53 +03:00
},
2018-04-24 14:51:54 +03:00
Else => {
// `Else` opcode is being encountered. So the case we are looking at:
//
// if
// ...
// else <-- cursor
// ...
// end
//
// Finalize the current block ('then' part of the if statement),
// and begin another one for the 'else' part.
counter.finalize()?;
counter.begin(cursor + 1);
}
_ => {
// An ordinal non control flow instruction. Just increment the cost of the current block.
2018-06-29 19:01:06 +08:00
let instruction_cost = rules.process(instruction)?;
counter.increment(instruction_cost)?;
2018-04-24 14:51:54 +03:00
}
2017-05-19 16:54:53 +03:00
}
}
2018-02-19 19:28:12 +03:00
2018-04-24 14:51:54 +03:00
// Then insert metering calls.
let mut cumulative_offset = 0;
for block in counter.blocks {
let effective_pos = block.start_pos + cumulative_offset;
2018-06-29 19:01:06 +08:00
instructions.elements_mut().insert(effective_pos, I32Const(block.cost as i32));
instructions.elements_mut().insert(effective_pos+1, Call(gas_func));
2018-04-24 14:51:54 +03:00
// Take into account these two inserted instructions.
cumulative_offset += 2;
}
2018-02-19 19:28:12 +03:00
Ok(())
2017-05-04 17:38:09 +03:00
}
2018-02-19 19:28:12 +03:00
/// Injects gas counter.
///
/// Can only fail if encounters operation forbidden by gas rules,
/// in this case it returns error with the original module.
pub fn inject_gas_counter(module: elements::Module, rules: &rules::Set)
-> Result<elements::Module, elements::Module>
{
2017-05-19 16:54:53 +03:00
// Injecting gas counting external
let mut mbuilder = builder::from_module(module);
let import_sig = mbuilder.push_signature(
builder::signature()
.param().i32()
.build_sig()
);
2017-05-04 17:38:09 +03:00
2018-02-05 00:10:53 +03:00
mbuilder.push_import(
2017-05-19 16:54:53 +03:00
builder::import()
.module("env")
.field("gas")
.external().func(import_sig)
.build()
);
2017-05-04 17:38:09 +03:00
2017-05-19 16:54:53 +03:00
// back to plain module
let mut module = mbuilder.build();
2017-05-04 17:38:09 +03:00
2017-05-19 16:54:53 +03:00
// calculate actual function index of the imported definition
// (substract all imports that are NOT functions)
2017-05-04 17:38:09 +03:00
2018-02-05 00:10:53 +03:00
let gas_func = module.import_count(elements::ImportCountType::Function) as u32 - 1;
let total_func = module.functions_space() as u32;
let mut need_grow_counter = false;
2018-02-19 19:28:12 +03:00
let mut error = false;
2017-05-04 17:38:09 +03:00
2017-05-19 16:54:53 +03:00
// Updating calling addresses (all calls to function index >= `gas_func` should be incremented)
for section in module.sections_mut() {
match section {
&mut elements::Section::Code(ref mut code_section) => {
for ref mut func_body in code_section.bodies_mut() {
update_call_index(func_body.code_mut(), gas_func);
2018-02-19 19:28:12 +03:00
if let Err(_) = inject_counter(func_body.code_mut(), rules, gas_func) {
error = true;
break;
}
2018-02-05 00:10:53 +03:00
if rules.grow_cost() > 0 {
if inject_grow_counter(func_body.code_mut(), total_func) > 0 {
need_grow_counter = true;
}
}
2017-05-19 16:54:53 +03:00
}
},
&mut elements::Section::Export(ref mut export_section) => {
for ref mut export in export_section.entries_mut() {
if let &mut elements::Internal::Function(ref mut func_index) = export.internal_mut() {
if *func_index >= gas_func { *func_index += 1}
2017-07-25 18:43:12 +03:00
}
2017-05-19 16:54:53 +03:00
}
},
&mut elements::Section::Element(ref mut elements_section) => {
for ref mut segment in elements_section.entries_mut() {
// update all indirect call addresses initial values
for func_index in segment.members_mut() {
if *func_index >= gas_func { *func_index += 1}
}
}
},
&mut elements::Section::Start(ref mut start_idx) => {
if *start_idx >= gas_func { *start_idx += 1}
},
2017-05-19 16:54:53 +03:00
_ => { }
}
}
2017-05-04 17:38:09 +03:00
2018-02-19 19:28:12 +03:00
if error { return Err(module); }
if need_grow_counter { Ok(add_grow_counter(module, rules, gas_func)) } else { Ok(module) }
2017-06-26 16:03:27 +03:00
}
#[cfg(test)]
mod tests {
2018-02-05 00:40:28 +03:00
extern crate wabt;
use parity_wasm::{serialize, builder, elements};
2017-06-26 16:03:27 +03:00
use super::*;
2018-02-05 00:10:53 +03:00
use rules;
#[test]
fn simple_grow() {
2018-06-29 19:06:33 +08:00
use parity_wasm::elements::Instruction::*;
2018-02-05 00:10:53 +03:00
let module = builder::module()
.global()
.value_type().i32()
.build()
.function()
.signature().param().i32().build()
.body()
2018-06-29 19:06:33 +08:00
.with_instructions(elements::Instructions::new(
2018-02-05 00:10:53 +03:00
vec![
GetGlobal(0),
GrowMemory(0),
End
]
))
.build()
.build()
.build();
2018-02-19 19:28:12 +03:00
let injected_module = inject_gas_counter(module, &rules::Set::default().with_grow_cost(10000)).unwrap();
2018-02-05 00:10:53 +03:00
assert_eq!(
&vec![
I32Const(3),
Call(0),
GetGlobal(0),
Call(2),
End
][..],
injected_module
.code_section().expect("function section should exist").bodies()[0]
.code().elements()
);
assert_eq!(
&vec![
2018-02-05 15:27:32 +03:00
GetLocal(0),
2018-02-05 00:10:53 +03:00
GetLocal(0),
I32Const(10000),
I32Mul,
Call(0),
GrowMemory(0),
2018-02-05 00:40:28 +03:00
End,
2018-02-05 00:10:53 +03:00
][..],
injected_module
.code_section().expect("function section should exist").bodies()[1]
.code().elements()
);
2018-02-05 00:40:28 +03:00
let binary = serialize(injected_module).expect("serialization failed");
self::wabt::wasm2wat(&binary).unwrap();
2018-02-05 00:10:53 +03:00
}
#[test]
fn grow_no_gas_no_track() {
2018-06-29 19:06:33 +08:00
use parity_wasm::elements::Instruction::*;
2018-02-05 00:10:53 +03:00
let module = builder::module()
.global()
.value_type().i32()
.build()
.function()
.signature().param().i32().build()
.body()
2018-06-29 19:06:33 +08:00
.with_instructions(elements::Instructions::new(
2018-02-05 00:10:53 +03:00
vec![
GetGlobal(0),
GrowMemory(0),
End
]
))
.build()
.build()
.build();
2018-02-19 19:28:12 +03:00
let injected_module = inject_gas_counter(module, &rules::Set::default()).unwrap();
2018-02-05 00:10:53 +03:00
assert_eq!(
&vec![
I32Const(3),
Call(0),
GetGlobal(0),
GrowMemory(0),
End
][..],
injected_module
.code_section().expect("function section should exist").bodies()[0]
.code().elements()
);
assert_eq!(injected_module.functions_space(), 2);
2018-02-05 00:40:28 +03:00
let binary = serialize(injected_module).expect("serialization failed");
self::wabt::wasm2wat(&binary).unwrap();
2018-02-05 00:10:53 +03:00
}
2017-06-26 16:03:27 +03:00
#[test]
fn simple() {
2018-06-29 19:06:33 +08:00
use parity_wasm::elements::Instruction::*;
2017-06-26 16:03:27 +03:00
let module = builder::module()
.global()
.value_type().i32()
.build()
.function()
.signature().param().i32().build()
.body()
2018-06-29 19:06:33 +08:00
.with_instructions(elements::Instructions::new(
2017-06-26 16:03:27 +03:00
vec![
GetGlobal(0),
End
]
))
.build()
.build()
.build();
2018-02-19 19:28:12 +03:00
let injected_module = inject_gas_counter(module, &Default::default()).unwrap();
2017-06-26 16:03:27 +03:00
assert_eq!(
&vec![
I32Const(2),
Call(0),
GetGlobal(0),
End
2017-07-25 18:43:12 +03:00
][..],
2017-06-26 16:03:27 +03:00
injected_module
.code_section().expect("function section should exist").bodies()[0]
.code().elements()
2017-07-25 18:43:12 +03:00
);
2017-06-26 16:03:27 +03:00
}
#[test]
fn nested() {
2018-06-29 19:06:33 +08:00
use parity_wasm::elements::Instruction::*;
2017-06-26 16:03:27 +03:00
let module = builder::module()
.global()
.value_type().i32()
.build()
.function()
.signature().param().i32().build()
.body()
2018-06-29 19:06:33 +08:00
.with_instructions(elements::Instructions::new(
2017-06-26 16:03:27 +03:00
vec![
GetGlobal(0),
Block(elements::BlockType::NoResult),
GetGlobal(0),
GetGlobal(0),
GetGlobal(0),
End,
2017-07-25 18:43:12 +03:00
GetGlobal(0),
2017-06-26 16:03:27 +03:00
End
]
))
.build()
.build()
.build();
2018-02-19 19:28:12 +03:00
let injected_module = inject_gas_counter(module, &Default::default()).unwrap();
2017-07-25 18:43:12 +03:00
2017-06-26 16:03:27 +03:00
assert_eq!(
2017-06-26 16:09:56 +03:00
&vec![
I32Const(4),
Call(0),
GetGlobal(0),
Block(elements::BlockType::NoResult),
I32Const(4),
Call(0),
GetGlobal(0),
GetGlobal(0),
GetGlobal(0),
End,
GetGlobal(0),
End
2017-07-25 18:43:12 +03:00
][..],
2017-06-26 16:03:27 +03:00
injected_module
.code_section().expect("function section should exist").bodies()[0]
2017-06-26 16:09:56 +03:00
.code().elements()
2017-06-26 16:03:27 +03:00
);
2017-06-26 16:09:56 +03:00
}
#[test]
fn ifelse() {
2018-06-29 19:06:33 +08:00
use parity_wasm::elements::Instruction::*;
2017-06-26 16:09:56 +03:00
let module = builder::module()
.global()
.value_type().i32()
.build()
.function()
.signature().param().i32().build()
.body()
2018-06-29 19:06:33 +08:00
.with_instructions(elements::Instructions::new(
2017-06-26 16:09:56 +03:00
vec![
GetGlobal(0),
If(elements::BlockType::NoResult),
GetGlobal(0),
GetGlobal(0),
GetGlobal(0),
Else,
GetGlobal(0),
2017-07-25 18:43:12 +03:00
GetGlobal(0),
2017-06-26 16:09:56 +03:00
End,
2017-07-25 18:43:12 +03:00
GetGlobal(0),
2017-06-26 16:09:56 +03:00
End
]
))
.build()
.build()
.build();
2018-02-19 19:28:12 +03:00
let injected_module = inject_gas_counter(module, &Default::default()).unwrap();
2017-06-26 16:09:56 +03:00
2017-06-26 16:03:27 +03:00
assert_eq!(
&vec![
I32Const(4),
Call(0),
GetGlobal(0),
2017-06-26 16:09:56 +03:00
If(elements::BlockType::NoResult),
2017-06-26 16:03:27 +03:00
I32Const(4),
Call(0),
GetGlobal(0),
GetGlobal(0),
GetGlobal(0),
2017-06-26 16:09:56 +03:00
Else,
I32Const(3),
Call(0),
GetGlobal(0),
GetGlobal(0),
2017-06-26 16:03:27 +03:00
End,
GetGlobal(0),
End
2017-07-25 18:43:12 +03:00
][..],
2017-06-26 16:03:27 +03:00
injected_module
.code_section().expect("function section should exist").bodies()[0]
.code().elements()
);
2017-07-25 18:43:12 +03:00
}
2017-06-26 16:03:27 +03:00
2017-06-26 16:09:56 +03:00
#[test]
fn call_index() {
2018-06-29 19:06:33 +08:00
use parity_wasm::elements::Instruction::*;
2017-06-26 16:09:56 +03:00
let module = builder::module()
.global()
.value_type().i32()
.build()
.function()
.signature().param().i32().build()
.body().build()
.build()
.function()
.signature().param().i32().build()
.body()
2018-06-29 19:06:33 +08:00
.with_instructions(elements::Instructions::new(
2017-06-26 16:09:56 +03:00
vec![
Call(0),
If(elements::BlockType::NoResult),
Call(0),
Call(0),
Call(0),
Else,
Call(0),
2017-07-25 18:43:12 +03:00
Call(0),
2017-06-26 16:09:56 +03:00
End,
2017-07-25 18:43:12 +03:00
Call(0),
2017-06-26 16:09:56 +03:00
End
]
))
.build()
.build()
.build();
2018-02-19 19:28:12 +03:00
let injected_module = inject_gas_counter(module, &Default::default()).unwrap();
2017-06-26 16:09:56 +03:00
assert_eq!(
&vec![
I32Const(4),
Call(0),
Call(1),
If(elements::BlockType::NoResult),
I32Const(4),
Call(0),
Call(1),
Call(1),
Call(1),
Else,
I32Const(3),
Call(0),
Call(1),
Call(1),
End,
Call(1),
End
2017-07-25 18:43:12 +03:00
][..],
2017-06-26 16:09:56 +03:00
injected_module
.code_section().expect("function section should exist").bodies()[1]
.code().elements()
);
2017-07-25 18:43:12 +03:00
}
2017-06-26 16:09:56 +03:00
2018-02-19 19:28:12 +03:00
#[test]
fn forbidden() {
2018-06-29 19:06:33 +08:00
use parity_wasm::elements::Instruction::*;
2018-02-19 19:28:12 +03:00
let module = builder::module()
.global()
.value_type().i32()
.build()
.function()
.signature().param().i32().build()
.body()
2018-06-29 19:06:33 +08:00
.with_instructions(elements::Instructions::new(
2018-02-19 19:28:12 +03:00
vec![
F32Const(555555),
End
]
))
.build()
.build()
.build();
let rules = rules::Set::default().with_forbidden_floats();
if let Err(_) = inject_gas_counter(module, &rules) { }
else { panic!("Should be error because of the forbidden operation")}
}
2018-04-24 14:51:54 +03:00
}