diff --git a/runtime/near-vm-runner/src/logic/logic.rs b/runtime/near-vm-runner/src/logic/logic.rs index af8f2c3f6af..257fd3ded45 100644 --- a/runtime/near-vm-runner/src/logic/logic.rs +++ b/runtime/near-vm-runner/src/logic/logic.rs @@ -43,7 +43,7 @@ pub struct VMLogic<'a> { /// results of the methods that made the callback are stored in this collection. promise_results: &'a [PromiseResult], /// Pointer to the guest memory. - memory: super::vmstate::Memory<'a>, + memory: super::vmstate::Memory, /// Keeping track of the current account balance, which can decrease when we create promises /// and attach balance to them. @@ -135,7 +135,7 @@ impl<'a> VMLogic<'a> { config: &'a Config, fees_config: &'a RuntimeFeesConfig, promise_results: &'a [PromiseResult], - memory: &'a mut dyn MemoryLike, + memory: impl MemoryLike + 'static, ) -> Self { // Overflow should be checked before calling VMLogic. let current_account_balance = context.account_balance + context.attached_deposit; @@ -194,7 +194,7 @@ impl<'a> VMLogic<'a> { } #[cfg(test)] - pub(super) fn memory(&mut self) -> &mut super::vmstate::Memory<'a> { + pub(super) fn memory(&mut self) -> &mut super::vmstate::Memory { &mut self.memory } diff --git a/runtime/near-vm-runner/src/logic/mocks/mock_memory.rs b/runtime/near-vm-runner/src/logic/mocks/mock_memory.rs index 94d4b135470..51340e450ff 100644 --- a/runtime/near-vm-runner/src/logic/mocks/mock_memory.rs +++ b/runtime/near-vm-runner/src/logic/mocks/mock_memory.rs @@ -2,6 +2,7 @@ use crate::logic::{MemSlice, MemoryLike}; use std::borrow::Cow; +#[derive(Clone)] pub struct MockedMemory(Box<[u8]>); impl MockedMemory { diff --git a/runtime/near-vm-runner/src/logic/tests/vm_logic_builder.rs b/runtime/near-vm-runner/src/logic/tests/vm_logic_builder.rs index b3358b1fa6b..14864d2fe0c 100644 --- a/runtime/near-vm-runner/src/logic/tests/vm_logic_builder.rs +++ b/runtime/near-vm-runner/src/logic/tests/vm_logic_builder.rs @@ -43,7 +43,7 @@ impl VMLogicBuilder { &self.config, &self.fees_config, &self.promise_results, - &mut self.memory, + self.memory.clone(), )) } diff --git a/runtime/near-vm-runner/src/logic/vmstate.rs b/runtime/near-vm-runner/src/logic/vmstate.rs index a1ac607f697..bd72ff0f7f7 100644 --- a/runtime/near-vm-runner/src/logic/vmstate.rs +++ b/runtime/near-vm-runner/src/logic/vmstate.rs @@ -19,7 +19,7 @@ type Result = ::std::result::Result; /// the compiler can deconstruct the access to each field of [`VMLogic`] and do /// more granular lifetime analysis. In particular, this design is what allows /// us to forgo copying register value in [`VMLogic::read_register`]. -pub(super) struct Memory<'a>(&'a mut dyn MemoryLike); +pub(super) struct Memory(Box); macro_rules! memory_get { ($_type:ty, $name:ident) => { @@ -48,9 +48,9 @@ macro_rules! memory_set { }; } -impl<'a> Memory<'a> { - pub(super) fn new(mem: &'a mut dyn MemoryLike) -> Self { - Self(mem) +impl Memory { + pub(super) fn new(mem: impl MemoryLike + 'static) -> Self { + Self(Box::new(mem)) } /// Returns view of the guest memory. @@ -237,13 +237,13 @@ impl Registers { /// of gas counter, memory and registers separately. This allows `VMLogic` to /// borrow value from a register and then continue constructing mutable /// references to other fields in the structure.. -pub(super) fn get_memory_or_register<'a, 'b>( +pub(super) fn get_memory_or_register<'a>( gas_counter: &mut GasCounter, - memory: &'b Memory<'a>, - registers: &'b Registers, + memory: &'a Memory, + registers: &'a Registers, ptr: u64, len: u64, -) -> Result> { +) -> Result> { if len == u64::MAX { registers.get(gas_counter, ptr).map(Cow::Borrowed) } else { diff --git a/runtime/near-vm-runner/src/near_vm_runner/runner.rs b/runtime/near-vm-runner/src/near_vm_runner/runner.rs index 203f468ff83..050bba91ebd 100644 --- a/runtime/near-vm-runner/src/near_vm_runner/runner.rs +++ b/runtime/near-vm-runner/src/near_vm_runner/runner.rs @@ -304,7 +304,7 @@ impl NearVM { crate::metrics::record_compiled_contract_cache_lookup(is_cache_hit); - let mut memory = NearVmMemory::new( + let memory = NearVmMemory::new( self.config.limit_config.initial_memory_pages, self.config.limit_config.max_memory_pages, ) @@ -313,7 +313,7 @@ impl NearVM { // Note that we don't clone the actual backing memory, just increase the RC. let vmmemory = memory.vm(); let mut logic = - VMLogic::new(ext, context, &self.config, fees_config, promise_results, &mut memory); + VMLogic::new(ext, context, &self.config, fees_config, promise_results, memory); let result = logic.before_loading_executable(method_name, wasm_bytes); if let Err(e) = result { diff --git a/runtime/near-vm-runner/src/wasmer2_runner.rs b/runtime/near-vm-runner/src/wasmer2_runner.rs index 8d0798f0e9f..dcf3e7e173e 100644 --- a/runtime/near-vm-runner/src/wasmer2_runner.rs +++ b/runtime/near-vm-runner/src/wasmer2_runner.rs @@ -574,7 +574,7 @@ impl crate::runner::VM for Wasmer2VM { let Some(code) = ext.get_contract() else { return Err(VMRunnerError::ContractCodeNotPresent); }; - let mut memory = Wasmer2Memory::new( + let memory = Wasmer2Memory::new( self.config.limit_config.initial_memory_pages, self.config.limit_config.max_memory_pages, ) @@ -584,7 +584,7 @@ impl crate::runner::VM for Wasmer2VM { // Note that we don't clone the actual backing memory, just increase the RC. let vmmemory = memory.vm(); let mut logic = - VMLogic::new(ext, context, &self.config, fees_config, promise_results, &mut memory); + VMLogic::new(ext, context, &self.config, fees_config, promise_results, memory); let result = logic.before_loading_executable(method_name, code.code().len() as u64); if let Err(e) = result { diff --git a/runtime/near-vm-runner/src/wasmer_runner.rs b/runtime/near-vm-runner/src/wasmer_runner.rs index f0b7a2552fb..b9a983f6946 100644 --- a/runtime/near-vm-runner/src/wasmer_runner.rs +++ b/runtime/near-vm-runner/src/wasmer_runner.rs @@ -438,7 +438,7 @@ impl crate::runner::VM for Wasmer0VM { panic!("AVX support is required in order to run Wasmer VM Singlepass backend."); } - let mut memory = WasmerMemory::new( + let memory = WasmerMemory::new( self.config.limit_config.initial_memory_pages, self.config.limit_config.max_memory_pages, ); @@ -446,7 +446,7 @@ impl crate::runner::VM for Wasmer0VM { let memory_copy = memory.clone(); let mut logic = - VMLogic::new(ext, context, &self.config, fees_config, promise_results, &mut memory); + VMLogic::new(ext, context, &self.config, fees_config, promise_results, memory); let result = logic.before_loading_executable(method_name, code.code().len() as u64); if let Err(e) = result { diff --git a/runtime/near-vm-runner/src/wasmtime_runner.rs b/runtime/near-vm-runner/src/wasmtime_runner.rs index 32c79c2b343..d9ebcbdb714 100644 --- a/runtime/near-vm-runner/src/wasmtime_runner.rs +++ b/runtime/near-vm-runner/src/wasmtime_runner.rs @@ -250,7 +250,7 @@ impl WasmtimeVM { )?; let mut store = Store::new(&self.engine, ()); - let mut memory = WasmtimeMemory::new( + let memory = WasmtimeMemory::new( &mut store, self.config.limit_config.initial_memory_pages, self.config.limit_config.max_memory_pages, @@ -258,7 +258,7 @@ impl WasmtimeVM { .unwrap(); let memory_copy = memory.0; let mut logic = - VMLogic::new(ext, context, &self.config, fees_config, promise_results, &mut memory); + VMLogic::new(ext, context, &self.config, fees_config, promise_results, memory); let result = logic.before_loading_executable(method_name, wasm_bytes); if let Err(e) = result { return Ok(VMOutcome::abort(logic, e));