Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stack-limiter): support multi-value feature #8

Draft
wants to merge 2 commits into
base: gear-stable-v0.3.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ codegen-units = 1

[dependencies]
# parity-wasm = { version = "0.45.1", git = "https://github.com/gear-tech/parity-wasm", branch = "v0.45.1-sign-ext", default-features = false }
parity-wasm = { package = "gear-wasm", version = "0.45.1", default-features = false }
# TODO: add multi_value feature to gear-wasm as default, #[cfg(feature = "multi_value")] -> // #[cfg(feature = "multi_value")]
parity-wasm = { package = "gear-wasm", version = "0.45.1", features = ["multi_value"], default-features = false }

[dev-dependencies]
binaryen = "0.12"
Expand Down
12 changes: 10 additions & 2 deletions src/stack_limiter/max_height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,20 @@ where
match opcode {
Nop => {},
Block(ty) | Loop(ty) | If(ty) => {
let end_arity = if *ty == BlockType::NoResult { 0 } else { 1 };
let (param_arity, end_arity) = match ty {
BlockType::NoResult => (0, 0),
BlockType::Value(_) => (0, 1),
BlockType::TypeIndex(x) => {
let Type::Function(ty) =
type_section.types().get(*x as usize).ok_or("Type not found")?;
(ty.params().len() as u32, ty.results().len() as u32)
},
};
let branch_arity = if let Loop(_) = *opcode { 0 } else { end_arity };
if let If(_) = *opcode {
stack.pop_values(1)?;
}
let height = stack.height();
let height = stack.height().checked_sub(param_arity).ok_or("Empty stack")?;
stack.push_frame(Frame {
is_polymorphic: false,
end_arity,
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn rewrite_sections_after_insertion(
*start_idx += inserted_count
},
Section::Name(s) =>
for functions in s.functions_mut() {
if let Some(functions) = s.functions_mut() {
*functions.names_mut() =
IndexMap::from_iter(functions.names().iter().map(|(mut idx, name)| {
if idx >= inserted_index {
Expand Down
1 change: 1 addition & 0 deletions tests/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ mod stack_height {
def_stack_height_test!(imports);
def_stack_height_test!(many_locals);
def_stack_height_test!(empty_functions);
def_stack_height_test!(multi_value_if);
}

mod gas {
Expand Down
52 changes: 52 additions & 0 deletions tests/expectations/stack-height/multi_value_if.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(module
(type (;0;) (func (param i32) (result i32)))
(type (;1;) (func (param i32 i32) (result i32)))
(type (;2;) (func (result i32)))
(func $f (;0;) (type 0) (param $param i32) (result i32)
i32.const 1
i32.const 2
local.get $param
if (type 1) (param i32 i32) (result i32) ;; label = @1
i32.add
else
i32.sub
end
)
(func $main (;1;) (type 2) (result i32)
i32.const 0
global.get 0
i32.const 5
i32.add
global.set 0
global.get 0
i32.const 1024
i32.gt_u
if ;; label = @1
unreachable
end
call $f
global.get 0
i32.const 5
i32.sub
global.set 0
)
(func (;2;) (type 2) (result i32)
global.get 0
i32.const 10
i32.add
global.set 0
global.get 0
i32.const 1024
i32.gt_u
if ;; label = @1
unreachable
end
call $main
global.get 0
i32.const 10
i32.sub
global.set 0
)
(global (;0;) (mut i32) i32.const 0)
(export "main" (func 2))
)
13 changes: 13 additions & 0 deletions tests/fixtures/stack-height/multi_value_if.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(module
(func $f (param $param i32) (result i32)
(i32.const 1)
(i32.const 2)
(if (param i32 i32) (result i32) (local.get $param)
(then (i32.add))
(else (i32.sub))
)
)
(func $main (export "main") (result i32)
(call $f (i32.const 0))
)
)