Skip to content

Commit 94e6981

Browse files
managed dealloc - implementation
1 parent 38d100e commit 94e6981

File tree

20 files changed

+120
-33
lines changed

20 files changed

+120
-33
lines changed

framework/base/src/api/managed_types/managed_type_api_impl.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ pub trait ManagedTypeApiImpl:
6666
super::token_identifier_util::get_token_ticker_len(token_id_len)
6767
}
6868

69-
fn drop_managed_buffer(&self, handle: Self::ManagedBufferHandle) {
70-
drop(handle)
71-
}
72-
69+
fn drop_managed_buffer(&self, _handle: Self::ManagedBufferHandle) {}
7370
fn drop_big_float(&self, _handle: Self::BigFloatHandle) {}
7471
fn drop_big_int(&self, _handle: Self::BigIntHandle) {}
7572
fn drop_elliptic_curve(&self, _handle: Self::EllipticCurveHandle) {}

framework/base/src/types/managed/basic/managed_buffer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ impl<M: ManagedTypeApi> Clone for ManagedBuffer<M> {
352352

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

framework/scenario/src/api/impl_vh/debug_api.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::sync::Arc;
22

33
use multiversx_chain_vm::{
4-
executor::{BreakpointValue, VMHooks},
4+
executor::BreakpointValue,
55
tx_mock::{TxContext, TxContextRef, TxContextStack, TxPanic},
66
vm_hooks::{DebugApiVMHooksHandler, VMHooksDispatcher},
77
};
88
use multiversx_sc::{chain_core::types::ReturnCode, err_msg};
99

10-
use crate::debug_executor::{StaticVarData, StaticVarStack};
10+
use crate::debug_executor::{StaticVarData, StaticVarStack, VMHooksDebugger};
1111

1212
use super::{DebugHandle, VMHooksApi, VMHooksApiBackend};
1313

@@ -19,7 +19,7 @@ impl VMHooksApiBackend for DebugApiBackend {
1919

2020
fn with_vm_hooks<R, F>(f: F) -> R
2121
where
22-
F: FnOnce(&dyn VMHooks) -> R,
22+
F: FnOnce(&dyn VMHooksDebugger) -> R,
2323
{
2424
let top_context = TxContextStack::static_peek();
2525
let wrapper = DebugApiVMHooksHandler::new(top_context);
@@ -29,7 +29,7 @@ impl VMHooksApiBackend for DebugApiBackend {
2929

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

3939
fn with_vm_hooks_ctx_2<R, F>(handle1: Self::HandleType, handle2: Self::HandleType, f: F) -> R
4040
where
41-
F: FnOnce(&dyn VMHooks) -> R,
41+
F: FnOnce(&dyn VMHooksDebugger) -> R,
4242
{
4343
assert_handles_on_same_context(&handle1, &handle2);
4444
Self::with_vm_hooks_ctx_1(handle1, f)
@@ -51,7 +51,7 @@ impl VMHooksApiBackend for DebugApiBackend {
5151
f: F,
5252
) -> R
5353
where
54-
F: FnOnce(&dyn VMHooks) -> R,
54+
F: FnOnce(&dyn VMHooksDebugger) -> R,
5555
{
5656
assert_handles_on_same_context(&handle1, &handle2);
5757
assert_handles_on_same_context(&handle1, &handle3);

framework/scenario/src/api/impl_vh/single_tx_api.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::sync::Mutex;
22

33
use multiversx_chain_vm::{
4-
executor::VMHooks,
54
types::VMAddress,
65
vm_hooks::{SingleTxApiData, SingleTxApiVMHooksHandler, VMHooksDispatcher},
76
world_mock::AccountData,
87
};
98
use multiversx_sc::api::RawHandle;
109

11-
use crate::debug_executor::StaticVarData;
10+
use crate::debug_executor::{StaticVarData, VMHooksDebugger};
1211

1312
use super::{VMHooksApi, VMHooksApiBackend};
1413

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

2726
fn with_vm_hooks<R, F>(f: F) -> R
2827
where
29-
F: FnOnce(&dyn VMHooks) -> R,
28+
F: FnOnce(&dyn VMHooksDebugger) -> R,
3029
{
3130
SINGLE_TX_API_VH_CELL.with(|cell| {
3231
let handler = cell.lock().unwrap().clone();

framework/scenario/src/api/impl_vh/static_api.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
use multiversx_chain_vm::{
2-
executor::VMHooks,
3-
vm_hooks::{StaticApiVMHooksHandler, VMHooksDispatcher, VMHooksHandler},
4-
};
1+
use multiversx_chain_vm::vm_hooks::{StaticApiVMHooksHandler, VMHooksDispatcher, VMHooksHandler};
52
use multiversx_sc::{api::RawHandle, types::Address};
63
use std::sync::Mutex;
74

8-
use crate::debug_executor::StaticVarData;
5+
use crate::debug_executor::{StaticVarData, VMHooksDebugger};
96

107
use super::{VMHooksApi, VMHooksApiBackend};
118

@@ -28,7 +25,7 @@ impl VMHooksApiBackend for StaticApiBackend {
2825

2926
fn with_vm_hooks<R, F>(f: F) -> R
3027
where
31-
F: FnOnce(&dyn VMHooks) -> R,
28+
F: FnOnce(&dyn VMHooksDebugger) -> R,
3229
{
3330
STATIC_API_VH_CELL.with(|vh_mutex| {
3431
let vh = vh_mutex.lock().unwrap();

framework/scenario/src/api/impl_vh/vm_hooks_api.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::debug_executor::StaticVarData;
1+
use crate::debug_executor::{StaticVarData, VMHooksDebugger};
22

33
use super::VMHooksApiBackend;
44

55
use std::marker::PhantomData;
66

7-
use multiversx_chain_vm::executor::{MemPtr, VMHooks};
7+
use multiversx_chain_vm::executor::MemPtr;
88
use multiversx_sc::api::{HandleTypeInfo, ManagedBufferApiImpl};
99

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

3030
/// Works with the VM hooks given by the context of 1 handle.
3131
pub fn with_vm_hooks_ctx_1<R, F>(&self, handle: &VHB::HandleType, f: F) -> R
3232
where
33-
F: FnOnce(&dyn VMHooks) -> R,
33+
F: FnOnce(&dyn VMHooksDebugger) -> R,
3434
{
3535
VHB::with_vm_hooks_ctx_1(handle.clone(), f)
3636
}
@@ -43,7 +43,7 @@ impl<VHB: VMHooksApiBackend> VMHooksApi<VHB> {
4343
f: F,
4444
) -> R
4545
where
46-
F: FnOnce(&dyn VMHooks) -> R,
46+
F: FnOnce(&dyn VMHooksDebugger) -> R,
4747
{
4848
VHB::with_vm_hooks_ctx_2(handle1.clone(), handle2.clone(), f)
4949
}
@@ -57,7 +57,7 @@ impl<VHB: VMHooksApiBackend> VMHooksApi<VHB> {
5757
f: F,
5858
) -> R
5959
where
60-
F: FnOnce(&dyn VMHooks) -> R,
60+
F: FnOnce(&dyn VMHooksDebugger) -> R,
6161
{
6262
VHB::with_vm_hooks_ctx_3(handle1.clone(), handle2.clone(), handle3.clone(), f)
6363
}

framework/scenario/src/api/impl_vh/vm_hooks_backend.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use multiversx_chain_vm::executor::VMHooks;
21
use multiversx_sc::api::HandleConstraints;
32

4-
use crate::debug_executor::StaticVarData;
3+
use crate::debug_executor::{StaticVarData, VMHooksDebugger};
54

65
pub trait VMHooksApiBackend: Clone + Send + Sync + 'static {
76
/// We use a single handle type for all handles.
@@ -10,18 +9,18 @@ pub trait VMHooksApiBackend: Clone + Send + Sync + 'static {
109
/// All communication with the VM happens via this method.
1110
fn with_vm_hooks<R, F>(f: F) -> R
1211
where
13-
F: FnOnce(&dyn VMHooks) -> R;
12+
F: FnOnce(&dyn VMHooksDebugger) -> R;
1413

1514
fn with_vm_hooks_ctx_1<R, F>(_handle: Self::HandleType, f: F) -> R
1615
where
17-
F: FnOnce(&dyn VMHooks) -> R,
16+
F: FnOnce(&dyn VMHooksDebugger) -> R,
1817
{
1918
Self::with_vm_hooks(f)
2019
}
2120

2221
fn with_vm_hooks_ctx_2<R, F>(_handle1: Self::HandleType, _handle2: Self::HandleType, f: F) -> R
2322
where
24-
F: FnOnce(&dyn VMHooks) -> R,
23+
F: FnOnce(&dyn VMHooksDebugger) -> R,
2524
{
2625
Self::with_vm_hooks(f)
2726
}
@@ -33,7 +32,7 @@ pub trait VMHooksApiBackend: Clone + Send + Sync + 'static {
3332
f: F,
3433
) -> R
3534
where
36-
F: FnOnce(&dyn VMHooks) -> R,
35+
F: FnOnce(&dyn VMHooksDebugger) -> R,
3736
{
3837
Self::with_vm_hooks(f)
3938
}

framework/scenario/src/api/managed_type_api_vh.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,28 @@ impl<VHB: VMHooksApiBackend> ManagedTypeApiImpl for VMHooksApi<VHB> {
9494
)
9595
});
9696
}
97+
98+
fn drop_managed_buffer(&self, handle: Self::ManagedBufferHandle) {
99+
self.with_vm_hooks_ctx_1(&handle, |vh| {
100+
vh.drop_managed_buffer(handle.get_raw_handle_unchecked())
101+
});
102+
}
103+
fn drop_big_float(&self, handle: Self::BigFloatHandle) {
104+
self.with_vm_hooks_ctx_1(&handle, |vh| {
105+
vh.drop_big_float(handle.get_raw_handle_unchecked())
106+
});
107+
}
108+
fn drop_big_int(&self, handle: Self::BigIntHandle) {
109+
self.with_vm_hooks_ctx_1(&handle, |vh| {
110+
vh.drop_big_int(handle.get_raw_handle_unchecked())
111+
});
112+
}
113+
fn drop_elliptic_curve(&self, _handle: Self::EllipticCurveHandle) {
114+
// TODO
115+
}
116+
fn drop_managed_map(&self, handle: Self::ManagedMapHandle) {
117+
self.with_vm_hooks_ctx_1(&handle, |vh| {
118+
vh.drop_managed_map(handle.get_raw_handle_unchecked())
119+
});
120+
}
97121
}

framework/scenario/src/debug_executor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod contract_container;
33
mod contract_map;
44
mod static_var_stack;
55
mod tx_static_vars;
6+
mod vm_hooks_debugger;
67

78
pub use catch_tx_panic::catch_tx_panic;
89
pub use contract_container::{
@@ -11,3 +12,4 @@ pub use contract_container::{
1112
pub use contract_map::{ContractMap, ContractMapRef};
1213
pub use static_var_stack::{StaticVarData, StaticVarStack};
1314
pub use tx_static_vars::TxStaticVars;
15+
pub use vm_hooks_debugger::VMHooksDebugger;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use multiversx_chain_vm::vm_hooks::VMHooksDispatcher;
2+
use multiversx_chain_vm_executor::VMHooks;
3+
4+
pub trait VMHooksDebugger: VMHooks {
5+
fn drop_managed_buffer(&self, handle: i32);
6+
fn drop_big_float(&self, handle: i32);
7+
fn drop_big_int(&self, handle: i32);
8+
fn drop_elliptic_curve(&self, handle: i32);
9+
fn drop_managed_map(&self, handle: i32);
10+
}
11+
12+
impl VMHooksDebugger for VMHooksDispatcher {
13+
fn drop_managed_buffer(&self, handle: i32) {
14+
self.handler.mb_drop(handle);
15+
}
16+
17+
fn drop_big_float(&self, handle: i32) {
18+
self.handler.bf_drop(handle);
19+
}
20+
21+
fn drop_big_int(&self, handle: i32) {
22+
self.handler.bi_drop(handle);
23+
}
24+
25+
fn drop_elliptic_curve(&self, _handle: i32) {
26+
// TODO: not implemented
27+
}
28+
29+
fn drop_managed_map(&self, handle: i32) {
30+
self.handler.mm_drop(handle);
31+
}
32+
}

0 commit comments

Comments
 (0)