Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/base/src/types/managed/managed_type_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait ManagedType<M: ManagedTypeApi>: Sized {
}

fn get_raw_handle(&self) -> RawHandle {
self.get_handle().cast_or_signal_error::<M, _>()
self.get_handle().get_raw_handle()
}

fn get_raw_handle_unchecked(&self) -> RawHandle {
Expand Down
3 changes: 2 additions & 1 deletion framework/scenario/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ mod vm_api_vh;

pub(crate) use impl_vh::i32_to_bool;
pub use impl_vh::{
DebugApi, DebugApiBackend, DebugHandle, SingleTxApi, StaticApi, VMHooksApi, VMHooksApiBackend,
DebugApi, DebugApiBackend, DebugHandle, SingleTxApi, StaticApi, StaticApiHandle, VMHooksApi,
VMHooksApiBackend,
};
6 changes: 4 additions & 2 deletions framework/scenario/src/api/impl_vh.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
mod debug_api;
mod debug_handle_vh;
mod debug_handle;
mod single_tx_api;
mod static_api;
mod static_api_handle;
mod vh_single_tx_api;
mod vh_static_api;
mod vm_hooks_api;
mod vm_hooks_backend;

pub use debug_api::{DebugApi, DebugApiBackend};
pub use debug_handle_vh::DebugHandle;
pub use debug_handle::DebugHandle;
pub use single_tx_api::SingleTxApi;
pub use static_api::StaticApi;
pub use static_api_handle::StaticApiHandle;
pub use vh_single_tx_api::{SingleTxApiData, SingleTxApiVMHooksContext};
pub use vh_static_api::StaticApiVMHooksContext;
pub use vm_hooks_api::VMHooksApi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::marker::PhantomData;
use std::sync::Weak;

use multiversx_chain_vm::host::context::{TxContext, TxContextRef};
Expand All @@ -14,6 +15,12 @@ pub struct DebugHandle {
/// Using the pointer after the context is released will panic.
pub(crate) context: Weak<TxContext>,
raw_handle: RawHandle,

/// This field causes DebugHandle not to be `Send` or `Sync`,
/// which is desirable since the handle is only valid on the thread of the original context.
///
/// This restriction is not enough to ensure safety (the context also helps), but it is an additional line of defense against misuse.
_phantom: PhantomData<*const ()>,
}

impl DebugHandle {
Expand All @@ -22,6 +29,7 @@ impl DebugHandle {
Self {
context,
raw_handle,
_phantom: PhantomData,
}
}

Expand Down
9 changes: 6 additions & 3 deletions framework/scenario/src/api/impl_vh/static_api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use multiversx_chain_vm::host::vm_hooks::VMHooksDispatcher;
use multiversx_chain_vm_executor::VMHooksEarlyExit;
use multiversx_sc::{api::RawHandle, types::Address};
use multiversx_sc::types::Address;
use std::sync::Mutex;

use crate::executor::debug::{StaticVarData, VMHooksDebugger};
use crate::{
api::StaticApiHandle,
executor::debug::{StaticVarData, VMHooksDebugger},
};

use super::{StaticApiVMHooksContext, VMHooksApi, VMHooksApiBackend};

Expand All @@ -21,7 +24,7 @@ thread_local! {
pub struct StaticApiBackend;

impl VMHooksApiBackend for StaticApiBackend {
type HandleType = RawHandle;
type HandleType = StaticApiHandle;

fn with_vm_hooks<R, F>(f: F) -> R
where
Expand Down
66 changes: 66 additions & 0 deletions framework/scenario/src/api/impl_vh/static_api_handle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use core::marker::PhantomData;

use multiversx_sc::{
api::{HandleConstraints, RawHandle},
codec::TryStaticCast,
};

#[derive(Clone)]
pub struct StaticApiHandle {
raw_handle: RawHandle,
_phantom: PhantomData<*const ()>,
}

impl StaticApiHandle {
/// Should almost never call directly, only used directly in a test.
pub fn new(raw_handle: RawHandle) -> Self {
Self {
raw_handle,
_phantom: PhantomData,
}
}
}

impl core::fmt::Debug for StaticApiHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
RawHandle::fmt(&self.raw_handle, f)
}
}

impl HandleConstraints for StaticApiHandle {
fn new(handle: multiversx_sc::api::RawHandle) -> Self {
StaticApiHandle::new(handle)
}

fn to_be_bytes(&self) -> [u8; 4] {
self.raw_handle.to_be_bytes()
}

fn get_raw_handle(&self) -> RawHandle {
self.raw_handle
}

fn get_raw_handle_unchecked(&self) -> RawHandle {
self.raw_handle
}
}

impl PartialEq<RawHandle> for StaticApiHandle {
fn eq(&self, other: &RawHandle) -> bool {
&self.raw_handle == other
}
}

impl PartialEq<StaticApiHandle> for StaticApiHandle {
fn eq(&self, other: &StaticApiHandle) -> bool {
self.raw_handle == other.raw_handle
}
}

impl From<i32> for StaticApiHandle {
fn from(handle: i32) -> Self {
StaticApiHandle::new(handle)
}
}

impl TryStaticCast for StaticApiHandle {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use multiversx_sc::{
api::ManagedTypeApi,
api::{HandleConstraints, ManagedTypeApi},
codec::{
self,
derive::{NestedDecode, NestedEncode, TopDecode, TopEncode},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use multiversx_sc::{
api::ManagedTypeApi,
api::{HandleConstraints, ManagedTypeApi},
codec::{
self,
derive::{NestedDecode, NestedEncode, TopDecode, TopEncode},
Expand Down
8 changes: 8 additions & 0 deletions tools/managed-mem-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ version = "0.1.0"
edition = "2024"
publish = false

[[bin]]
name = "bench-leak"
path = "src/bench_leak.rs"

[[bin]]
name = "bench-threading"
path = "src/bench_threading.rs"

[dependencies.multiversx-sc]
version = "0.65.0"
path = "../../framework/base"
Expand Down
Loading
Loading