From 073e50e22d7d0493c017068964af8254ca1d6803 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Wed, 12 Oct 2022 15:05:18 +0800 Subject: [PATCH 1/5] chore: improve call wasm --- crates/wasmi/src/engine/mod.rs | 4 +++- crates/wasmi/src/engine/stack/frames.rs | 14 +++----------- crates/wasmi/src/engine/stack/mod.rs | 7 ++++--- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/crates/wasmi/src/engine/mod.rs b/crates/wasmi/src/engine/mod.rs index 78b3047ac3..9149d0ee32 100644 --- a/crates/wasmi/src/engine/mod.rs +++ b/crates/wasmi/src/engine/mod.rs @@ -376,7 +376,9 @@ impl EngineInner { CallOutcome::NestedCall(called_func) => { match called_func.as_internal(ctx.as_context()) { FuncEntityInternal::Wasm(wasm_func) => { - self.stack.call_wasm(frame, wasm_func, &self.code_map)?; + *frame = + self.stack + .call_wasm(frame.clone(), wasm_func, &self.code_map)?; } FuncEntityInternal::Host(host_func) => { cache.reset_default_memory_bytes(); diff --git a/crates/wasmi/src/engine/stack/frames.rs b/crates/wasmi/src/engine/stack/frames.rs index 59a948e333..9732234b01 100644 --- a/crates/wasmi/src/engine/stack/frames.rs +++ b/crates/wasmi/src/engine/stack/frames.rs @@ -3,7 +3,6 @@ use super::{err_stack_overflow, DEFAULT_MAX_RECURSION_DEPTH}; use crate::{core::TrapCode, engine::code_map::InstructionPtr, Instance}; use alloc::vec::Vec; -use core::mem::replace; /// A function frame of a function on the call stack. #[derive(Debug, Copy, Clone)] @@ -72,20 +71,13 @@ impl CallStack { FuncFrame::new(ip, instance) } - /// Pushes a Wasm function onto the [`CallStack`]. - pub(crate) fn push( - &mut self, - caller: &mut FuncFrame, - ip: InstructionPtr, - instance: Instance, - ) -> Result { + /// Pushes a Wasm caller function onto the [`CallStack`]. + pub(crate) fn push(&mut self, caller: FuncFrame) -> Result<(), TrapCode> { if self.len() == self.recursion_limit { return Err(err_stack_overflow()); } - let frame = FuncFrame::new(ip, instance); - let caller = replace(caller, frame); self.frames.push(caller); - Ok(frame) + Ok(()) } /// Pops the last [`FuncFrame`] from the [`CallStack`] if any. diff --git a/crates/wasmi/src/engine/stack/mod.rs b/crates/wasmi/src/engine/stack/mod.rs index 362b6dd921..803a827249 100644 --- a/crates/wasmi/src/engine/stack/mod.rs +++ b/crates/wasmi/src/engine/stack/mod.rs @@ -134,16 +134,17 @@ impl Stack { Ok(self.frames.init(iref, instance)) } - /// Prepares the [`Stack`] for the given Wasm function call. + /// Prepares the [`Stack`] for the given Wasm function call and return the callee. pub(crate) fn call_wasm<'engine>( &mut self, - caller: &mut FuncFrame, + caller: FuncFrame, wasm_func: &WasmFuncEntity, code_map: &'engine CodeMap, ) -> Result { let ip = self.call_wasm_impl(wasm_func, code_map)?; let instance = wasm_func.instance(); - let frame = self.frames.push(caller, ip, instance)?; + self.frames.push(caller)?; + let frame = FuncFrame::new(ip, instance); Ok(frame) } From 8ff7837df5ba5ff19efb277f4c2720efeebb3c4a Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Wed, 12 Oct 2022 15:21:12 +0800 Subject: [PATCH 2/5] chore: improve call wasm --- crates/wasmi/src/engine/mod.rs | 4 +--- crates/wasmi/src/engine/stack/mod.rs | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/wasmi/src/engine/mod.rs b/crates/wasmi/src/engine/mod.rs index 9149d0ee32..4175c3f6b7 100644 --- a/crates/wasmi/src/engine/mod.rs +++ b/crates/wasmi/src/engine/mod.rs @@ -376,9 +376,7 @@ impl EngineInner { CallOutcome::NestedCall(called_func) => { match called_func.as_internal(ctx.as_context()) { FuncEntityInternal::Wasm(wasm_func) => { - *frame = - self.stack - .call_wasm(frame.clone(), wasm_func, &self.code_map)?; + *frame = self.stack.call_wasm(frame, wasm_func, &self.code_map)?; } FuncEntityInternal::Host(host_func) => { cache.reset_default_memory_bytes(); diff --git a/crates/wasmi/src/engine/stack/mod.rs b/crates/wasmi/src/engine/stack/mod.rs index 803a827249..888f5abf70 100644 --- a/crates/wasmi/src/engine/stack/mod.rs +++ b/crates/wasmi/src/engine/stack/mod.rs @@ -137,13 +137,13 @@ impl Stack { /// Prepares the [`Stack`] for the given Wasm function call and return the callee. pub(crate) fn call_wasm<'engine>( &mut self, - caller: FuncFrame, + caller: &FuncFrame, wasm_func: &WasmFuncEntity, code_map: &'engine CodeMap, ) -> Result { let ip = self.call_wasm_impl(wasm_func, code_map)?; + self.frames.push(caller.clone())?; let instance = wasm_func.instance(); - self.frames.push(caller)?; let frame = FuncFrame::new(ip, instance); Ok(frame) } From 7669be2947cb347431507fb028e373e5a4f6d230 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Wed, 12 Oct 2022 15:25:05 +0800 Subject: [PATCH 3/5] chore: improve call wasm --- crates/wasmi/src/engine/stack/frames.rs | 2 +- crates/wasmi/src/engine/stack/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/wasmi/src/engine/stack/frames.rs b/crates/wasmi/src/engine/stack/frames.rs index 9732234b01..28fb456a61 100644 --- a/crates/wasmi/src/engine/stack/frames.rs +++ b/crates/wasmi/src/engine/stack/frames.rs @@ -73,7 +73,7 @@ impl CallStack { /// Pushes a Wasm caller function onto the [`CallStack`]. pub(crate) fn push(&mut self, caller: FuncFrame) -> Result<(), TrapCode> { - if self.len() == self.recursion_limit { + if self.len() >= self.recursion_limit { return Err(err_stack_overflow()); } self.frames.push(caller); diff --git a/crates/wasmi/src/engine/stack/mod.rs b/crates/wasmi/src/engine/stack/mod.rs index 888f5abf70..055c27136f 100644 --- a/crates/wasmi/src/engine/stack/mod.rs +++ b/crates/wasmi/src/engine/stack/mod.rs @@ -134,7 +134,7 @@ impl Stack { Ok(self.frames.init(iref, instance)) } - /// Prepares the [`Stack`] for the given Wasm function call and return the callee. + /// Prepares the [`Stack`] for the given Wasm function call. pub(crate) fn call_wasm<'engine>( &mut self, caller: &FuncFrame, From 6c21a193aa6de1f4ec7b16eede24358850983deb Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Wed, 12 Oct 2022 15:29:10 +0800 Subject: [PATCH 4/5] clippy --- crates/wasmi/src/engine/stack/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasmi/src/engine/stack/mod.rs b/crates/wasmi/src/engine/stack/mod.rs index 055c27136f..919c0e7dec 100644 --- a/crates/wasmi/src/engine/stack/mod.rs +++ b/crates/wasmi/src/engine/stack/mod.rs @@ -142,7 +142,7 @@ impl Stack { code_map: &'engine CodeMap, ) -> Result { let ip = self.call_wasm_impl(wasm_func, code_map)?; - self.frames.push(caller.clone())?; + self.frames.push(*caller)?; let instance = wasm_func.instance(); let frame = FuncFrame::new(ip, instance); Ok(frame) From 7bd3acc3fe32babb947cea7da9080d19177ae828 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Thu, 13 Oct 2022 14:46:21 +0800 Subject: [PATCH 5/5] use `==` to check recursion_limit --- crates/wasmi/src/engine/stack/frames.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasmi/src/engine/stack/frames.rs b/crates/wasmi/src/engine/stack/frames.rs index 28fb456a61..9732234b01 100644 --- a/crates/wasmi/src/engine/stack/frames.rs +++ b/crates/wasmi/src/engine/stack/frames.rs @@ -73,7 +73,7 @@ impl CallStack { /// Pushes a Wasm caller function onto the [`CallStack`]. pub(crate) fn push(&mut self, caller: FuncFrame) -> Result<(), TrapCode> { - if self.len() >= self.recursion_limit { + if self.len() == self.recursion_limit { return Err(err_stack_overflow()); } self.frames.push(caller);