Skip to content

Commit

Permalink
vmlogic: maintain memory by a Box
Browse files Browse the repository at this point in the history
This is a part of an effort to drop the lifetime from the `VMLogic`
structure in order to make it more straightforward to have it live for a
longer duration alongside the instance and not force our hand in pretty
much "create-execute contract-drop" flow.
  • Loading branch information
nagisa committed Jun 19, 2024
1 parent 3e3137f commit 14b3679
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions runtime/near-vm-runner/src/logic/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 8 additions & 8 deletions runtime/near-vm-runner/src/logic/vmstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Result<T> = ::std::result::Result<T, VMLogicError>;
/// 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<dyn MemoryLike>);

macro_rules! memory_get {
($_type:ty, $name:ident) => {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<Cow<'b, [u8]>> {
) -> Result<Cow<'a, [u8]>> {
if len == u64::MAX {
registers.get(gas_counter, ptr).map(Cow::Borrowed)
} else {
Expand Down
4 changes: 2 additions & 2 deletions runtime/near-vm-runner/src/near_vm_runner/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions runtime/near-vm-runner/src/wasmer2_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions runtime/near-vm-runner/src/wasmer_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,15 +438,15 @@ 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,
);
// Note that we don't clone the actual backing memory, just increase the RC.
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 {
Expand Down
4 changes: 2 additions & 2 deletions runtime/near-vm-runner/src/wasmtime_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ 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,
)
.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));
Expand Down

0 comments on commit 14b3679

Please sign in to comment.