mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-06-26 13:01:37 +00:00
Merge pull request #24 from paritytech/inject-rules
Inject gas using rules
This commit is contained in:
@ -16,7 +16,7 @@ fn main() {
|
|||||||
// Loading module
|
// Loading module
|
||||||
let module = parity_wasm::deserialize_file(&args[1]).expect("Module deserialization to succeed");
|
let module = parity_wasm::deserialize_file(&args[1]).expect("Module deserialization to succeed");
|
||||||
|
|
||||||
let result = wasm_utils::inject_gas_counter(module);
|
let result = wasm_utils::inject_gas_counter(module, &Default::default());
|
||||||
|
|
||||||
parity_wasm::serialize_to_file(&args[2], result).expect("Module serialization to succeed")
|
parity_wasm::serialize_to_file(&args[2], result).expect("Module serialization to succeed")
|
||||||
}
|
}
|
||||||
|
31
src/gas.rs
31
src/gas.rs
@ -1,4 +1,5 @@
|
|||||||
use parity_wasm::{elements, builder};
|
use parity_wasm::{elements, builder};
|
||||||
|
use rules;
|
||||||
|
|
||||||
pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) {
|
pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Opcode::*;
|
||||||
@ -13,13 +14,13 @@ pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum InjectAction {
|
enum InjectAction {
|
||||||
Spawn,
|
Spawn(u32),
|
||||||
Continue,
|
Continue(u32),
|
||||||
Increment,
|
Increment,
|
||||||
IncrementSpawn,
|
IncrementSpawn,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inject_counter(opcodes: &mut elements::Opcodes, gas_func: u32) {
|
pub fn inject_counter(opcodes: &mut elements::Opcodes, rules: &rules::Set, gas_func: u32) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Opcode::*;
|
||||||
|
|
||||||
let mut stack: Vec<(usize, usize)> = Vec::new();
|
let mut stack: Vec<(usize, usize)> = Vec::new();
|
||||||
@ -37,7 +38,7 @@ pub fn inject_counter(opcodes: &mut elements::Opcodes, gas_func: u32) {
|
|||||||
let opcode = &opcodes.elements()[cursor];
|
let opcode = &opcodes.elements()[cursor];
|
||||||
match *opcode {
|
match *opcode {
|
||||||
Block(_) | If(_) | Loop(_) => {
|
Block(_) | If(_) | Loop(_) => {
|
||||||
InjectAction::Spawn
|
InjectAction::Spawn(rules.process(opcode))
|
||||||
},
|
},
|
||||||
Else => {
|
Else => {
|
||||||
InjectAction::IncrementSpawn
|
InjectAction::IncrementSpawn
|
||||||
@ -46,7 +47,7 @@ pub fn inject_counter(opcodes: &mut elements::Opcodes, gas_func: u32) {
|
|||||||
InjectAction::Increment
|
InjectAction::Increment
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
InjectAction::Continue
|
InjectAction::Continue(rules.process(opcode))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -65,14 +66,14 @@ pub fn inject_counter(opcodes: &mut elements::Opcodes, gas_func: u32) {
|
|||||||
cursor += 3;
|
cursor += 3;
|
||||||
stack.push((cursor, 1));
|
stack.push((cursor, 1));
|
||||||
},
|
},
|
||||||
InjectAction::Continue => {
|
InjectAction::Continue(val) => {
|
||||||
cursor += 1;
|
cursor += 1;
|
||||||
let (pos, ops) = last_entry;
|
let (pos, ops) = last_entry;
|
||||||
stack.push((pos, ops+1));
|
stack.push((pos, ops + val as usize));
|
||||||
},
|
},
|
||||||
InjectAction::Spawn => {
|
InjectAction::Spawn(val) => {
|
||||||
let (pos, ops) = last_entry;
|
let (pos, ops) = last_entry;
|
||||||
stack.push((pos, ops+1));
|
stack.push((pos, ops + val as usize));
|
||||||
|
|
||||||
cursor += 1;
|
cursor += 1;
|
||||||
stack.push((cursor, 1));
|
stack.push((cursor, 1));
|
||||||
@ -81,7 +82,7 @@ pub fn inject_counter(opcodes: &mut elements::Opcodes, gas_func: u32) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inject_gas_counter(module: elements::Module) -> elements::Module {
|
pub fn inject_gas_counter(module: elements::Module, rules: &rules::Set) -> elements::Module {
|
||||||
// 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 = mbuilder.push_signature(
|
let import_sig = mbuilder.push_signature(
|
||||||
@ -117,7 +118,7 @@ pub fn inject_gas_counter(module: elements::Module) -> elements::Module {
|
|||||||
&mut elements::Section::Code(ref mut code_section) => {
|
&mut elements::Section::Code(ref mut code_section) => {
|
||||||
for ref mut func_body in code_section.bodies_mut() {
|
for ref mut func_body in code_section.bodies_mut() {
|
||||||
update_call_index(func_body.code_mut(), gas_func);
|
update_call_index(func_body.code_mut(), gas_func);
|
||||||
inject_counter(func_body.code_mut(), gas_func);
|
inject_counter(func_body.code_mut(), rules, gas_func);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
&mut elements::Section::Export(ref mut export_section) => {
|
&mut elements::Section::Export(ref mut export_section) => {
|
||||||
@ -172,7 +173,7 @@ mod tests {
|
|||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let injected_module = inject_gas_counter(module);
|
let injected_module = inject_gas_counter(module, &Default::default());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&vec![
|
&vec![
|
||||||
@ -214,7 +215,7 @@ mod tests {
|
|||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let injected_module = inject_gas_counter(module);
|
let injected_module = inject_gas_counter(module, &Default::default());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&vec![
|
&vec![
|
||||||
@ -267,7 +268,7 @@ mod tests {
|
|||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let injected_module = inject_gas_counter(module);
|
let injected_module = inject_gas_counter(module, &Default::default());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&vec![
|
&vec![
|
||||||
@ -329,7 +330,7 @@ mod tests {
|
|||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let injected_module = inject_gas_counter(module);
|
let injected_module = inject_gas_counter(module, &Default::default());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&vec![
|
&vec![
|
||||||
|
Reference in New Issue
Block a user