From 4a51f16874c703c7cfc9e02f6b24dc8e0106fa27 Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Mon, 6 Jun 2022 15:42:52 +0100 Subject: [PATCH] handle debug info (#16) --- CHANGELOG.md | 4 ++++ Cargo.toml | 4 ++-- src/gas_metering/mod.rs | 16 ++++++++++++++-- tests/diff.rs | 5 ++++- tests/expectations/gas/branch.wat | 8 ++++---- tests/expectations/gas/call.wat | 12 ++++++------ tests/expectations/gas/start.wat | 10 +++++----- tests/expectations/stack-height/global.wat | 6 +++--- tests/expectations/stack-height/imports.wat | 4 ++-- tests/expectations/stack-height/many_locals.wat | 4 ++-- tests/expectations/stack-height/start.wat | 4 ++-- tests/expectations/stack-height/table.wat | 4 ++-- 12 files changed, 50 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2632219..b79a077 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ 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. In other words: Upgrading this pallet will not break pre-existing contracts. +## [v0.1.2] 2022-06-06 +- Adjust debug information (if already parsed) when injecting gas metering +[#16](https://github.com/paritytech/wasm-instrument/pull/16) + ## [v0.1.1] 2022-01-18 ### Fixed diff --git a/Cargo.toml b/Cargo.toml index c5fef18..b6a2f3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasm-instrument" -version = "0.1.1" +version = "0.1.2" edition = "2021" rust-version = "1.56.1" authors = ["Parity Technologies "] @@ -20,7 +20,7 @@ lto = "fat" codegen-units = 1 [dependencies] -parity-wasm = { version = "0.42", default-features = false } +parity-wasm = { version = "0.45", default-features = false } [dev-dependencies] binaryen = "0.12" diff --git a/src/gas_metering/mod.rs b/src/gas_metering/mod.rs index b3cbe13..2425552 100644 --- a/src/gas_metering/mod.rs +++ b/src/gas_metering/mod.rs @@ -11,7 +11,7 @@ use alloc::{vec, vec::Vec}; use core::{cmp::min, mem, num::NonZeroU32}; use parity_wasm::{ builder, - elements::{self, Instruction, ValueType}, + elements::{self, IndexMap, Instruction, ValueType}, }; /// An interface that describes instruction costs. @@ -129,7 +129,8 @@ impl Rules for ConstantCostRules { /// /// The above transformations are performed for every function body defined in the module. This /// function also rewrites all function indices references by code, table elements, etc., since -/// the addition of an imported functions changes the indices of module-defined functions. +/// the addition of an imported functions changes the indices of module-defined functions. If the +/// the module has a NameSection, added by calling `parse_names`, the indices will also be updated. /// /// This routine runs in time linear in the size of the input module. /// @@ -212,6 +213,17 @@ pub fn inject( if *start_idx >= gas_func { *start_idx += 1 }, + elements::Section::Name(s) => + for functions in s.functions_mut() { + *functions.names_mut() = + IndexMap::from_iter(functions.names().iter().map(|(mut idx, name)| { + if idx >= gas_func { + idx += 1; + } + + (idx, name.clone()) + })); + }, _ => {}, } } diff --git a/tests/diff.rs b/tests/diff.rs index c880a72..ebd097e 100644 --- a/tests/diff.rs +++ b/tests/diff.rs @@ -1,3 +1,4 @@ +use parity_wasm::elements::Module; use std::{ fs, io::{self, Read, Write}, @@ -101,8 +102,10 @@ mod gas { run_diff_test("gas", concat!(stringify!($name), ".wat"), |input| { let rules = instrument::gas_metering::ConstantCostRules::default(); - let module = + let module: Module = elements::deserialize_buffer(input).expect("Failed to deserialize"); + let module = module.parse_names().expect("Failed to parse names"); + let instrumented = instrument::gas_metering::inject(module, &rules, "env") .expect("Failed to instrument with gas metering"); elements::serialize(instrumented).expect("Failed to serialize") diff --git a/tests/expectations/gas/branch.wat b/tests/expectations/gas/branch.wat index 8a733f2..ab92e95 100644 --- a/tests/expectations/gas/branch.wat +++ b/tests/expectations/gas/branch.wat @@ -1,11 +1,11 @@ (module (type (;0;) (func (result i32))) (type (;1;) (func (param i32))) - (import "env" "gas" (func $fibonacci_with_break (type 1))) - (func (;1;) (type 0) (result i32) + (import "env" "gas" (func (;0;) (type 1))) + (func $fibonacci_with_break (;1;) (type 0) (result i32) (local i32 i32) i32.const 13 - call $fibonacci_with_break + call 0 block ;; label = @1 i32.const 0 local.set 0 @@ -19,7 +19,7 @@ i32.const 1 br_if 0 (;@1;) i32.const 5 - call $fibonacci_with_break + call 0 local.get 0 local.get 1 local.tee 0 diff --git a/tests/expectations/gas/call.wat b/tests/expectations/gas/call.wat index dd8f6e3..7cf6eca 100644 --- a/tests/expectations/gas/call.wat +++ b/tests/expectations/gas/call.wat @@ -1,20 +1,20 @@ (module (type (;0;) (func (param i32 i32) (result i32))) (type (;1;) (func (param i32))) - (import "env" "gas" (func $add_locals (type 1))) - (func $add (type 0) (param $x i32) (param $y i32) (result i32) + (import "env" "gas" (func (;0;) (type 1))) + (func $add_locals (;1;) (type 0) (param $x i32) (param $y i32) (result i32) (local i32) i32.const 5 - call $add_locals + call 0 local.get $x local.get $y - call 2 + call $add local.set 2 local.get 2 ) - (func (;2;) (type 0) (param i32 i32) (result i32) + (func $add (;2;) (type 0) (param i32 i32) (result i32) i32.const 3 - call $add_locals + call 0 local.get 0 local.get 1 i32.add diff --git a/tests/expectations/gas/start.wat b/tests/expectations/gas/start.wat index cd3b7cd..c05a47c 100644 --- a/tests/expectations/gas/start.wat +++ b/tests/expectations/gas/start.wat @@ -2,12 +2,12 @@ (type (;0;) (func (param i32 i32))) (type (;1;) (func)) (type (;2;) (func (param i32))) - (import "env" "ext_return" (func $ext_return (type 0))) + (import "env" "ext_return" (func $ext_return (;0;) (type 0))) (import "env" "memory" (memory (;0;) 1 1)) - (import "env" "gas" (func $start (type 2))) - (func (;2;) (type 1) + (import "env" "gas" (func (;1;) (type 2))) + (func $start (;2;) (type 1) i32.const 4 - call $start + call 1 i32.const 8 i32.const 4 call $ext_return @@ -15,6 +15,6 @@ ) (func (;3;) (type 1)) (export "call" (func 3)) - (start 2) + (start $start) (data (;0;) (i32.const 8) "\01\02\03\04") ) \ No newline at end of file diff --git a/tests/expectations/stack-height/global.wat b/tests/expectations/stack-height/global.wat index cba8282..4add98c 100644 --- a/tests/expectations/stack-height/global.wat +++ b/tests/expectations/stack-height/global.wat @@ -2,8 +2,8 @@ (type (;0;) (func)) (type (;1;) (func (param i32 i32) (result i32))) (type (;2;) (func (param i32))) - (import "env" "foo" (func $foo (type 0))) - (func $i32.add (type 1) (param i32 i32) (result i32) + (import "env" "foo" (func $foo (;0;) (type 0))) + (func $i32.add (;1;) (type 1) (param i32 i32) (result i32) local.get 0 local.get 1 i32.add @@ -53,7 +53,7 @@ i32.sub global.set 1 ) - (global $counter (mut i32) i32.const 1) + (global $counter (;0;) (mut i32) i32.const 1) (global (;1;) (mut i32) i32.const 0) (export "i32.add" (func 3)) ) \ No newline at end of file diff --git a/tests/expectations/stack-height/imports.wat b/tests/expectations/stack-height/imports.wat index 79178ce..9879822 100644 --- a/tests/expectations/stack-height/imports.wat +++ b/tests/expectations/stack-height/imports.wat @@ -1,8 +1,8 @@ (module (type (;0;) (func)) (type (;1;) (func (param i32 i32) (result i32))) - (import "env" "foo" (func $foo (type 0))) - (import "env" "boo" (func $boo (type 0))) + (import "env" "foo" (func $foo (;0;) (type 0))) + (import "env" "boo" (func $boo (;1;) (type 0))) (func (;2;) (type 1) (param i32 i32) (result i32) call $foo call $boo diff --git a/tests/expectations/stack-height/many_locals.wat b/tests/expectations/stack-height/many_locals.wat index 76c1f2c..8893fdd 100644 --- a/tests/expectations/stack-height/many_locals.wat +++ b/tests/expectations/stack-height/many_locals.wat @@ -1,9 +1,9 @@ (module (type (;0;) (func)) - (func $one-group-many-locals (type 0) + (func $one-group-many-locals (;0;) (type 0) (local i64 i64 i32) ) - (func $main (type 0) + (func $main (;1;) (type 0) global.get 0 i32.const 5 i32.add diff --git a/tests/expectations/stack-height/start.wat b/tests/expectations/stack-height/start.wat index 037dcc8..1e82a84 100644 --- a/tests/expectations/stack-height/start.wat +++ b/tests/expectations/stack-height/start.wat @@ -1,9 +1,9 @@ (module (type (;0;) (func (param i32 i32))) (type (;1;) (func)) - (import "env" "ext_return" (func $ext_return (type 0))) + (import "env" "ext_return" (func $ext_return (;0;) (type 0))) (import "env" "memory" (memory (;0;) 1 1)) - (func $start (type 1) + (func $start (;1;) (type 1) (local i32) ) (func (;2;) (type 1)) diff --git a/tests/expectations/stack-height/table.wat b/tests/expectations/stack-height/table.wat index 23477d3..56334c1 100644 --- a/tests/expectations/stack-height/table.wat +++ b/tests/expectations/stack-height/table.wat @@ -2,7 +2,7 @@ (type (;0;) (func)) (type (;1;) (func (param i32))) (type (;2;) (func (param i32 i32) (result i32))) - (import "env" "foo" (func $foo (type 0))) + (import "env" "foo" (func $foo (;0;) (type 0))) (func (;1;) (type 1) (param i32) local.get 0 i32.const 0 @@ -23,7 +23,7 @@ global.set 0 drop ) - (func $i32.add (type 2) (param i32 i32) (result i32) + (func $i32.add (;2;) (type 2) (param i32 i32) (result i32) local.get 0 local.get 1 i32.add