diff --git a/vm-executor-wasmer/Cargo.toml b/vm-executor-wasmer/Cargo.toml index b4ad51f..6b8815c 100644 --- a/vm-executor-wasmer/Cargo.toml +++ b/vm-executor-wasmer/Cargo.toml @@ -11,15 +11,15 @@ version = "0.2.0" path = "../vm-executor" [dependencies] -wasmer = { git = "https://github.com/multiversx/wasmer", rev = "cdd1550", default-features = false, features = [ +wasmer = { git = "https://github.com/multiversx/wasmer", rev = "090ad17927fd1cbecb253a7b123d21e453fa13df", default-features = false, features = [ "singlepass", "sys", "universal", "wat", ] } -wasmer-vm = { git = "https://github.com/multiversx/wasmer", rev = "cdd1550" } -wasmer-types = { git = "https://github.com/multiversx/wasmer", rev = "cdd1550" } +wasmer-vm = { git = "https://github.com/multiversx/wasmer", rev = "090ad17927fd1cbecb253a7b123d21e453fa13df" } +wasmer-types = { git = "https://github.com/multiversx/wasmer", rev = "090ad17927fd1cbecb253a7b123d21e453fa13df" } chrono = "0.4.23" log = "0.4.17" diff --git a/vm-executor-wasmer/src/wasmer_instance.rs b/vm-executor-wasmer/src/wasmer_instance.rs index 764e4ac..49acbbb 100644 --- a/vm-executor-wasmer/src/wasmer_instance.rs +++ b/vm-executor-wasmer/src/wasmer_instance.rs @@ -180,6 +180,7 @@ fn push_middlewares( // Create opcode_control middleware let opcode_control_middleware = Arc::new(OpcodeControl::new( + 100, // TODO: should be compilation_options.max_memory_grow_count, compilation_options.max_memory_grow, compilation_options.max_memory_grow_delta, breakpoints_middleware.clone(), diff --git a/vm-executor-wasmer/src/wasmer_opcode_control.rs b/vm-executor-wasmer/src/wasmer_opcode_control.rs index 195bca4..7c1cbe8 100644 --- a/vm-executor-wasmer/src/wasmer_opcode_control.rs +++ b/vm-executor-wasmer/src/wasmer_opcode_control.rs @@ -26,6 +26,8 @@ struct OpcodeControlGlobalIndexes { #[derive(Debug)] pub(crate) struct OpcodeControl { + total_memory_grow_count: Arc>, + max_memory_grow_count: usize, max_memory_grow: usize, max_memory_grow_delta: usize, breakpoints_middleware: Arc, @@ -34,11 +36,14 @@ pub(crate) struct OpcodeControl { impl OpcodeControl { pub(crate) fn new( + max_memory_grow_count: usize, max_memory_grow: usize, max_memory_grow_delta: usize, breakpoints_middleware: Arc, ) -> Self { Self { + total_memory_grow_count: Arc::new(Mutex::new(0)), + max_memory_grow_count, max_memory_grow, max_memory_grow_delta, breakpoints_middleware, @@ -81,6 +86,8 @@ impl ModuleMiddleware for OpcodeControl { _local_function_index: LocalFunctionIndex, ) -> Box { Box::new(FunctionOpcodeControl { + total_memory_grow_count: self.total_memory_grow_count.clone(), + max_memory_grow_count: self.max_memory_grow_count, max_memory_grow: self.max_memory_grow, max_memory_grow_delta: self.max_memory_grow_delta, breakpoints_middleware: self.breakpoints_middleware.clone(), @@ -117,6 +124,8 @@ impl MiddlewareWithProtectedGlobals for OpcodeControl { #[derive(Debug)] struct FunctionOpcodeControl { + total_memory_grow_count: Arc>, + max_memory_grow_count: usize, max_memory_grow: usize, max_memory_grow_delta: usize, breakpoints_middleware: Arc, @@ -193,6 +202,12 @@ impl FunctionMiddleware for FunctionOpcodeControl { state: &mut MiddlewareReaderState<'b>, ) -> Result<(), MiddlewareError> { if matches!(operator, Operator::MemoryGrow { .. }) { + let mut grow_count = self.total_memory_grow_count.lock().map_err(|_| MiddlewareError::new("MemoryGrowLimit", "failed to lock counter"))?; + *grow_count += 1; + if *grow_count > self.max_memory_grow_count { + return Err(MiddlewareError::new("MemoryGrowLimit", "memory.grow limit exceeded")); + } + self.inject_memory_grow_check(state); }