Skip to content

Commit

Permalink
managed dealloc - implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 27, 2024
1 parent 38d100e commit 2284ffe
Show file tree
Hide file tree
Showing 22 changed files with 131 additions and 33 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ pub trait ManagedTypeApiImpl:
super::token_identifier_util::get_token_ticker_len(token_id_len)
}

fn drop_managed_buffer(&self, handle: Self::ManagedBufferHandle) {
drop(handle)
}

fn drop_managed_buffer(&self, _handle: Self::ManagedBufferHandle) {}
fn drop_big_float(&self, _handle: Self::BigFloatHandle) {}
fn drop_big_int(&self, _handle: Self::BigIntHandle) {}
fn drop_elliptic_curve(&self, _handle: Self::EllipticCurveHandle) {}
Expand Down
5 changes: 3 additions & 2 deletions framework/base/src/types/managed/basic/managed_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
abi::{TypeAbi, TypeAbiFrom, TypeName},
api::{
use_raw_handle, ErrorApiImpl, HandleConstraints, InvalidSliceError, ManagedBufferApiImpl,
ManagedTypeApi, StaticVarApiImpl,
ManagedTypeApi, ManagedTypeApiImpl, StaticVarApiImpl,

Check failure on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Proxy compare - newly generated vs present in file tree

unused import: `ManagedTypeApiImpl`

Check failure on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Template tool test - released templates

unused import: `ManagedTypeApiImpl`

Check failure on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Plotter tests

unused import: `ManagedTypeApiImpl`

Check failure on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Template tool test - current (unreleased) templates

unused import: `ManagedTypeApiImpl`

Check failure on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Wasm tests

unused import: `ManagedTypeApiImpl`

Check failure on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Contracts / Interactor tests

unused import: `ManagedTypeApiImpl`

Check warning on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Contracts / Rust tests

unused import: `ManagedTypeApiImpl`

Check warning on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Rust tests

unused import: `ManagedTypeApiImpl`

Check failure on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Contracts / Test Coverage

unused import: `ManagedTypeApiImpl`

Check failure on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Test Coverage

unused import: `ManagedTypeApiImpl`

Check failure on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

unused import: `ManagedTypeApiImpl`

Check warning on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] framework/base/src/types/managed/basic/managed_buffer.rs#L5

warning: unused import: `ManagedTypeApiImpl` --> framework/base/src/types/managed/basic/managed_buffer.rs:5:25 | 5 | ManagedTypeApi, ManagedTypeApiImpl, StaticVarApiImpl, | ^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
Raw output
framework/base/src/types/managed/basic/managed_buffer.rs:5:25:w:warning: unused import: `ManagedTypeApiImpl`
 --> framework/base/src/types/managed/basic/managed_buffer.rs:5:25
  |
5 |         ManagedTypeApi, ManagedTypeApiImpl, StaticVarApiImpl,
  |                         ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default


__END__

Check warning on line 5 in framework/base/src/types/managed/basic/managed_buffer.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] framework/base/src/types/managed/basic/managed_buffer.rs#L5

warning: unused import: `ManagedTypeApiImpl` --> framework/base/src/types/managed/basic/managed_buffer.rs:5:25 | 5 | ManagedTypeApi, ManagedTypeApiImpl, StaticVarApiImpl, | ^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
Raw output
framework/base/src/types/managed/basic/managed_buffer.rs:5:25:w:warning: unused import: `ManagedTypeApiImpl`
 --> framework/base/src/types/managed/basic/managed_buffer.rs:5:25
  |
5 |         ManagedTypeApi, ManagedTypeApiImpl, StaticVarApiImpl,
  |                         ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default


__END__
},
codec::{
DecodeErrorHandler, Empty, EncodeErrorHandler, NestedDecode, NestedDecodeInput,
Expand Down Expand Up @@ -352,7 +352,8 @@ impl<M: ManagedTypeApi> Clone for ManagedBuffer<M> {

impl<M: ManagedTypeApi> Drop for ManagedBuffer<M> {
fn drop(&mut self) {
let _ = core::mem::replace(&mut self.get_handle(), unsafe { core::mem::zeroed() });
// TODO: enable, after fixing all ownership issues
// M::managed_type_impl().drop_managed_buffer(self.handle.clone());
}
}

Expand Down
15 changes: 9 additions & 6 deletions framework/scenario/src/api/impl_vh/debug_api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::sync::Arc;

use multiversx_chain_vm::{
executor::{BreakpointValue, VMHooks},
executor::BreakpointValue,
tx_mock::{TxContext, TxContextRef, TxContextStack, TxPanic},
vm_hooks::{DebugApiVMHooksHandler, VMHooksDispatcher},
};
use multiversx_sc::{chain_core::types::ReturnCode, err_msg};
use multiversx_sc::{
chain_core::{types::ReturnCode, VMHooksDebugger},
err_msg,
};

use crate::debug_executor::{StaticVarData, StaticVarStack};

Expand All @@ -19,7 +22,7 @@ impl VMHooksApiBackend for DebugApiBackend {

fn with_vm_hooks<R, F>(f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
let top_context = TxContextStack::static_peek();
let wrapper = DebugApiVMHooksHandler::new(top_context);
Expand All @@ -29,7 +32,7 @@ impl VMHooksApiBackend for DebugApiBackend {

fn with_vm_hooks_ctx_1<R, F>(handle: Self::HandleType, f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
let wrapper = DebugApiVMHooksHandler::new(handle.context);
let dispatcher = VMHooksDispatcher::new(Box::new(wrapper));
Expand All @@ -38,7 +41,7 @@ impl VMHooksApiBackend for DebugApiBackend {

fn with_vm_hooks_ctx_2<R, F>(handle1: Self::HandleType, handle2: Self::HandleType, f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
assert_handles_on_same_context(&handle1, &handle2);
Self::with_vm_hooks_ctx_1(handle1, f)
Expand All @@ -51,7 +54,7 @@ impl VMHooksApiBackend for DebugApiBackend {
f: F,
) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
assert_handles_on_same_context(&handle1, &handle2);
assert_handles_on_same_context(&handle1, &handle3);
Expand Down
5 changes: 2 additions & 3 deletions framework/scenario/src/api/impl_vh/single_tx_api.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::sync::Mutex;

use multiversx_chain_vm::{
executor::VMHooks,
types::VMAddress,
vm_hooks::{SingleTxApiData, SingleTxApiVMHooksHandler, VMHooksDispatcher},
world_mock::AccountData,
};
use multiversx_sc::api::RawHandle;
use multiversx_sc::{api::RawHandle, chain_core::VMHooksDebugger};

use crate::debug_executor::StaticVarData;

Expand All @@ -26,7 +25,7 @@ impl VMHooksApiBackend for SingleTxApiBackend {

fn with_vm_hooks<R, F>(f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
SINGLE_TX_API_VH_CELL.with(|cell| {
let handler = cell.lock().unwrap().clone();
Expand Down
9 changes: 3 additions & 6 deletions framework/scenario/src/api/impl_vh/static_api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use multiversx_chain_vm::{
executor::VMHooks,
vm_hooks::{StaticApiVMHooksHandler, VMHooksDispatcher, VMHooksHandler},
};
use multiversx_sc::{api::RawHandle, types::Address};
use multiversx_chain_vm::vm_hooks::{StaticApiVMHooksHandler, VMHooksDispatcher, VMHooksHandler};
use multiversx_sc::{api::RawHandle, chain_core::VMHooksDebugger, types::Address};
use std::sync::Mutex;

use crate::debug_executor::StaticVarData;
Expand All @@ -28,7 +25,7 @@ impl VMHooksApiBackend for StaticApiBackend {

fn with_vm_hooks<R, F>(f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
STATIC_API_VH_CELL.with(|vh_mutex| {
let vh = vh_mutex.lock().unwrap();
Expand Down
15 changes: 9 additions & 6 deletions framework/scenario/src/api/impl_vh/vm_hooks_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use super::VMHooksApiBackend;

use std::marker::PhantomData;

use multiversx_chain_vm::executor::{MemPtr, VMHooks};
use multiversx_sc::api::{HandleTypeInfo, ManagedBufferApiImpl};
use multiversx_chain_vm::executor::MemPtr;
use multiversx_sc::{
api::{HandleTypeInfo, ManagedBufferApiImpl},
chain_core::VMHooksDebugger,
};

#[derive(Clone, Debug)]
pub struct VMHooksApi<S: VMHooksApiBackend> {
Expand All @@ -22,15 +25,15 @@ impl<VHB: VMHooksApiBackend> VMHooksApi<VHB> {
/// All communication with the VM happens via this method.
pub fn with_vm_hooks<R, F>(&self, f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
VHB::with_vm_hooks(f)
}

/// Works with the VM hooks given by the context of 1 handle.
pub fn with_vm_hooks_ctx_1<R, F>(&self, handle: &VHB::HandleType, f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
VHB::with_vm_hooks_ctx_1(handle.clone(), f)
}
Expand All @@ -43,7 +46,7 @@ impl<VHB: VMHooksApiBackend> VMHooksApi<VHB> {
f: F,
) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
VHB::with_vm_hooks_ctx_2(handle1.clone(), handle2.clone(), f)
}
Expand All @@ -57,7 +60,7 @@ impl<VHB: VMHooksApiBackend> VMHooksApi<VHB> {
f: F,
) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
VHB::with_vm_hooks_ctx_3(handle1.clone(), handle2.clone(), handle3.clone(), f)
}
Expand Down
11 changes: 5 additions & 6 deletions framework/scenario/src/api/impl_vh/vm_hooks_backend.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use multiversx_chain_vm::executor::VMHooks;
use multiversx_sc::api::HandleConstraints;
use multiversx_sc::{api::HandleConstraints, chain_core::VMHooksDebugger};

use crate::debug_executor::StaticVarData;

Expand All @@ -10,18 +9,18 @@ pub trait VMHooksApiBackend: Clone + Send + Sync + 'static {
/// All communication with the VM happens via this method.
fn with_vm_hooks<R, F>(f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R;
F: FnOnce(&dyn VMHooksDebugger) -> R;

fn with_vm_hooks_ctx_1<R, F>(_handle: Self::HandleType, f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
Self::with_vm_hooks(f)
}

fn with_vm_hooks_ctx_2<R, F>(_handle1: Self::HandleType, _handle2: Self::HandleType, f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
Self::with_vm_hooks(f)
}
Expand All @@ -33,7 +32,7 @@ pub trait VMHooksApiBackend: Clone + Send + Sync + 'static {
f: F,
) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
Self::with_vm_hooks(f)
}
Expand Down
24 changes: 24 additions & 0 deletions framework/scenario/src/api/managed_type_api_vh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,28 @@ impl<VHB: VMHooksApiBackend> ManagedTypeApiImpl for VMHooksApi<VHB> {
)
});
}

fn drop_managed_buffer(&self, handle: Self::ManagedBufferHandle) {
self.with_vm_hooks_ctx_1(&handle, |vh| {
vh.drop_managed_buffer(handle.get_raw_handle_unchecked())
});
}
fn drop_big_float(&self, handle: Self::BigFloatHandle) {
self.with_vm_hooks_ctx_1(&handle, |vh| {
vh.drop_big_float(handle.get_raw_handle_unchecked())
});
}
fn drop_big_int(&self, handle: Self::BigIntHandle) {
self.with_vm_hooks_ctx_1(&handle, |vh| {
vh.drop_big_int(handle.get_raw_handle_unchecked())
});
}
fn drop_elliptic_curve(&self, _handle: Self::EllipticCurveHandle) {
// TODO
}
fn drop_managed_map(&self, handle: Self::ManagedMapHandle) {
self.with_vm_hooks_ctx_1(&handle, |vh| {
vh.drop_managed_map(handle.get_raw_handle_unchecked())
});
}
}
3 changes: 3 additions & 0 deletions vm-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ bitflags = "=2.6.0"
version = "=0.21.0"
path = "../data/codec"
features = ["derive"]

[dependencies.multiversx-chain-vm-executor]
version = "0.2.0"
3 changes: 3 additions & 0 deletions vm-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ extern crate alloc;

pub mod builtin_func_names;
pub mod types;
mod vm_hooks_debugger;

/// Re-exported for convenience.
pub use multiversx_sc_codec as codec;

pub use vm_hooks_debugger::VMHooksDebugger;
9 changes: 9 additions & 0 deletions vm-core/src/vm_hooks_debugger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use multiversx_chain_vm_executor::VMHooks;

pub trait VMHooksDebugger: VMHooks {
fn drop_managed_buffer(&self, handle: i32);
fn drop_big_float(&self, handle: i32);
fn drop_big_int(&self, handle: i32);
fn drop_elliptic_curve(&self, handle: i32);
fn drop_managed_map(&self, handle: i32);
}
4 changes: 4 additions & 0 deletions vm/src/tx_mock/tx_managed_types/handle_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ impl<V> HandleMap<V> {
pub fn insert(&mut self, handle: RawHandle, value: V) {
let _ = self.map.insert(handle, value);
}

pub fn remove_handle(&mut self, handle: RawHandle) {
let _ = self.map.remove(&handle);
}
}
4 changes: 4 additions & 0 deletions vm/src/tx_mock/tx_managed_types/tx_big_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ impl TxManagedTypes {
pub fn bf_overwrite(&mut self, handle: RawHandle, value: f64) {
self.big_float_map.insert(handle, value);
}

pub fn bf_remove(&mut self, handle: RawHandle) {
self.big_float_map.remove_handle(handle);
}
}
4 changes: 4 additions & 0 deletions vm/src/tx_mock/tx_managed_types/tx_big_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ impl TxManagedTypes {
self.big_int_map.insert_new_handle_raw(value)
}

pub fn bi_remove(&mut self, handle: RawHandle) {
self.big_int_map.remove_handle(handle);
}

pub fn bi_overwrite(&mut self, destination: RawHandle, value: num_bigint::BigInt) {
self.big_int_map.insert(destination, value);
}
Expand Down
4 changes: 4 additions & 0 deletions vm/src/tx_mock/tx_managed_types/tx_managed_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ impl TxManagedTypes {
self.mb_append_bytes(dest_handle, &handle_to_be_bytes(amount_handle)[..]);
}
}

pub fn mb_remove(&mut self, handle: RawHandle) {
self.managed_buffer_map.remove_handle(handle);
}
}

pub fn handle_to_be_bytes(handle: RawHandle) -> [u8; 4] {
Expand Down
4 changes: 4 additions & 0 deletions vm/src/tx_mock/tx_managed_types/tx_managed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ impl TxManagedTypes {
let mmap = self.managed_map_map.get_mut(map_handle);
mmap.remove(key).unwrap_or_default()
}

pub fn mm_remove(&mut self, handle: RawHandle) {
self.managed_map_map.remove_handle(handle);
}
}
23 changes: 23 additions & 0 deletions vm/src/vm_hooks/vh_dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ffi::c_void;

use multiversx_chain_core::VMHooksDebugger;
use multiversx_chain_vm_executor::{MemLength, MemPtr, VMHooks};

use crate::mem_conv;
Expand Down Expand Up @@ -1860,3 +1861,25 @@ impl VMHooks for VMHooksDispatcher {
panic!("Unavailable: elliptic_curve_get_values")
}
}

impl VMHooksDebugger for VMHooksDispatcher {
fn drop_managed_buffer(&self, handle: i32) {
self.handler.mb_drop(handle);
}

fn drop_big_float(&self, handle: i32) {
self.handler.bf_drop(handle);
}

fn drop_big_int(&self, handle: i32) {
self.handler.bi_drop(handle);
}

fn drop_elliptic_curve(&self, _handle: i32) {
// TODO: not implemented
}

fn drop_managed_map(&self, handle: i32) {
self.handler.mm_drop(handle);
}
}
4 changes: 4 additions & 0 deletions vm/src/vm_hooks/vh_handler/vh_managed_types/vh_big_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,8 @@ pub trait VMHooksBigFloat: VMHooksHandlerSource + VMHooksError {
fn bf_get_const_e(&self, dest: RawHandle) {
self.m_types_lock().bf_overwrite(dest, std::f64::consts::E);
}

fn bf_drop(&self, map_handle: RawHandle) {
self.m_types_lock().bf_remove(map_handle);
}
}
4 changes: 4 additions & 0 deletions vm/src/vm_hooks/vh_handler/vh_managed_types/vh_big_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,8 @@ pub trait VMHooksBigInt: VMHooksHandlerSource + VMHooksError {
let result = bi_x.shl(bits);
self.m_types_lock().bi_overwrite(dest, result);
}

fn bi_drop(&self, map_handle: RawHandle) {
self.m_types_lock().bi_remove(map_handle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ pub trait VMHooksManagedBuffer: VMHooksHandlerSource {
self.m_types_lock()
.mb_set(dest_handle, encoded.into_bytes());
}

fn mb_drop(&self, handle: RawHandle) {
self.m_types_lock().mb_remove(handle);
}
}
4 changes: 4 additions & 0 deletions vm/src/vm_hooks/vh_handler/vh_managed_types/vh_managed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ pub trait VMHooksManagedMap: VMHooksHandlerSource {
let key = self.m_types_lock().mb_get(key_handle).to_vec();
self.m_types_lock().mm_contains(map_handle, key.as_slice())
}

fn mm_drop(&self, map_handle: RawHandle) {
self.m_types_lock().mm_remove(map_handle);
}
}

0 comments on commit 2284ffe

Please sign in to comment.