Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a memory leak issue when using StaticApi #1545

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@
}
},
{
"step": "scCall",
"id": "codec_err_finish",
"step": "scQuery",
"id": "codec_err_storage_key",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "codec_err_finish",
"arguments": [],
"gasLimit": "50,000,000",
"gasPrice": "0"
"function": "codec_err_storage_key",
"arguments": []
},
"expect": {
"out": [],
"status": "4",
"message": "str:endpoint result encode error: deliberate top encode error"
"message": "str:storage key encode error: deliberate nested encode error"
}
},
{
"step": "scQuery",
"id": "codec_err_storage_key",
"step": "scCall",
"id": "codec_err_finish",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "codec_err_storage_key",
"arguments": []
"function": "codec_err_finish",
"arguments": [],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "4",
"message": "str:storage key encode error: deliberate nested encode error"
"message": "str:endpoint result encode error: deliberate top encode error"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub trait StorageLoadFeatures {
Self::Api::storage_read_api_impl().storage_load_from_address(
address.get_handle(),
key.get_handle(),
value_handle.clone(),
&value_handle,
);
ManagedBuffer::from_handle(value_handle)
}
Expand Down
3 changes: 2 additions & 1 deletion contracts/modules/src/esdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ pub trait EsdtModule {
fn nft_create<T: TopEncode>(&self, amount: &BigUint, attributes: &T) -> u64 {
let token_id = self.token_id().get();
let empty_buffer = ManagedBuffer::new();
let empty_vec = ManagedVec::from_handle(empty_buffer.get_handle());
let empty_vec = ManagedVec::from_handle(empty_buffer.take_handle());
let empty_buffer = ManagedBuffer::new();

self.send().esdt_nft_create(
&token_id,
Expand Down
48 changes: 24 additions & 24 deletions framework/base/src/api/blockchain_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,47 @@ pub trait BlockchainApi: ManagedTypeApi {
pub trait BlockchainApiImpl: ManagedTypeApiImpl {
fn get_caller_legacy(&self) -> Address;

fn load_caller_managed(&self, dest: Self::ManagedBufferHandle) {
fn load_caller_managed(&self, dest: &Self::ManagedBufferHandle) {
self.mb_overwrite(dest, self.get_caller_legacy().as_bytes());
}

fn get_sc_address_legacy(&self) -> Address;

fn load_sc_address_managed(&self, dest: Self::ManagedBufferHandle) {
fn load_sc_address_managed(&self, dest: &Self::ManagedBufferHandle) {
self.mb_overwrite(dest, self.get_sc_address_legacy().as_bytes())
}

fn load_owner_address_managed(&self, dest: Self::ManagedBufferHandle);
fn load_owner_address_managed(&self, dest: &Self::ManagedBufferHandle);

fn get_shard_of_address_legacy(&self, address: &Address) -> u32;

fn get_shard_of_address(&self, address_handle: Self::ManagedBufferHandle) -> u32 {
fn get_shard_of_address(&self, address_handle: &Self::ManagedBufferHandle) -> u32 {
let mut address = Address::zero();
let _ = self.mb_load_slice(address_handle, 0, address.as_mut());
self.get_shard_of_address_legacy(&address)
}

fn is_smart_contract_legacy(&self, address: &Address) -> bool;

fn is_smart_contract(&self, address_handle: Self::ManagedBufferHandle) -> bool {
fn is_smart_contract(&self, address_handle: &Self::ManagedBufferHandle) -> bool {
let mut address = Address::zero();
let _ = self.mb_load_slice(address_handle, 0, address.as_mut());
self.is_smart_contract_legacy(&address)
}

fn load_balance_legacy(&self, dest: Self::BigIntHandle, address: &Address);
fn load_balance_legacy(&self, dest: &Self::BigIntHandle, address: &Address);

fn load_balance(&self, dest: Self::BigIntHandle, address_handle: Self::ManagedBufferHandle) {
fn load_balance(&self, dest: &Self::BigIntHandle, address_handle: &Self::ManagedBufferHandle) {
let mut address = Address::zero();
let _ = self.mb_load_slice(address_handle, 0, address.as_mut());
self.load_balance_legacy(dest, &address);
}

fn load_state_root_hash_managed(&self, dest: Self::ManagedBufferHandle);
fn load_state_root_hash_managed(&self, dest: &Self::ManagedBufferHandle);

fn get_tx_hash_legacy(&self) -> H256;

fn load_tx_hash_managed(&self, dest: Self::ManagedBufferHandle) {
fn load_tx_hash_managed(&self, dest: &Self::ManagedBufferHandle) {
self.mb_overwrite(dest, self.get_tx_hash_legacy().as_bytes());
}

Expand All @@ -79,7 +79,7 @@ pub trait BlockchainApiImpl: ManagedTypeApiImpl {

fn get_block_epoch(&self) -> u64;

fn load_block_random_seed_managed(&self, dest: Self::ManagedBufferHandle);
fn load_block_random_seed_managed(&self, dest: &Self::ManagedBufferHandle);

fn get_prev_block_timestamp(&self) -> u64;

Expand All @@ -91,22 +91,22 @@ pub trait BlockchainApiImpl: ManagedTypeApiImpl {

fn get_prev_block_random_seed_legacy(&self) -> Box<[u8; 48]>;

fn load_prev_block_random_seed_managed(&self, dest: Self::ManagedBufferHandle) {
fn load_prev_block_random_seed_managed(&self, dest: &Self::ManagedBufferHandle) {
self.mb_overwrite(dest, self.get_prev_block_random_seed_legacy().as_slice());
}

fn get_current_esdt_nft_nonce(
&self,
address_handle: Self::ManagedBufferHandle,
token_id_handle: Self::ManagedBufferHandle,
address_handle: &Self::ManagedBufferHandle,
token_id_handle: &Self::ManagedBufferHandle,
) -> u64;

fn load_esdt_balance(
&self,
address_handle: Self::ManagedBufferHandle,
token_id_handle: Self::ManagedBufferHandle,
address_handle: &Self::ManagedBufferHandle,
token_id_handle: &Self::ManagedBufferHandle,
nonce: u64,
dest: Self::BigIntHandle,
dest: &Self::BigIntHandle,
);

#[allow(clippy::too_many_arguments)]
Expand All @@ -133,25 +133,25 @@ pub trait BlockchainApiImpl: ManagedTypeApiImpl {

fn check_esdt_frozen(
&self,
address_handle: Self::ManagedBufferHandle,
token_id_handle: Self::ManagedBufferHandle,
address_handle: &Self::ManagedBufferHandle,
token_id_handle: &Self::ManagedBufferHandle,
nonce: u64,
) -> bool;

fn check_esdt_paused(&self, token_id_handle: Self::ManagedBufferHandle) -> bool;
fn check_esdt_paused(&self, token_id_handle: &Self::ManagedBufferHandle) -> bool;

fn check_esdt_limited_transfer(&self, token_id_handle: Self::ManagedBufferHandle) -> bool;
fn check_esdt_limited_transfer(&self, token_id_handle: &Self::ManagedBufferHandle) -> bool;

fn load_esdt_local_roles(
&self,
token_id_handle: Self::ManagedBufferHandle,
token_id_handle: &Self::ManagedBufferHandle,
) -> EsdtLocalRoleFlags;

fn managed_get_code_metadata(
&self,
address_handle: Self::ManagedBufferHandle,
response_handle: Self::ManagedBufferHandle,
address_handle: &Self::ManagedBufferHandle,
response_handle: &Self::ManagedBufferHandle,
);

fn managed_is_builtin_function(&self, function_name_handle: Self::ManagedBufferHandle) -> bool;
fn managed_is_builtin_function(&self, function_name_handle: &Self::ManagedBufferHandle) -> bool;
}
4 changes: 2 additions & 2 deletions framework/base/src/api/call_value_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub trait CallValueApiImpl: ErrorApiImpl + ManagedTypeApiImpl + Sized {

/// Retrieves the EGLD call value from the VM.
/// Will return 0 in case of an ESDT transfer (cannot have both EGLD and ESDT transfer simultaneously).
fn load_egld_value(&self, dest_handle: Self::BigIntHandle);
fn load_egld_value(&self, dest_handle: &Self::BigIntHandle);

/// Loads all ESDT call values into a managed vec. Overwrites destination.
fn load_all_esdt_transfers(&self, dest_handle: Self::ManagedBufferHandle);
fn load_all_esdt_transfers(&self, dest_handle: &Self::ManagedBufferHandle);

/// Gets the total number of ESDT transfers (Fungible/SFT/NFT).
///
Expand Down
42 changes: 21 additions & 21 deletions framework/base/src/api/crypto_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,57 @@ pub trait CryptoApi: ManagedTypeApi {
pub trait CryptoApiImpl: ManagedTypeApiImpl {
fn sha256_managed(
&self,
dest: Self::ManagedBufferHandle,
data_handle: Self::ManagedBufferHandle,
dest: &Self::ManagedBufferHandle,
data_handle: &Self::ManagedBufferHandle,
);

fn keccak256_managed(
&self,
dest: Self::ManagedBufferHandle,
data_handle: Self::ManagedBufferHandle,
dest: &Self::ManagedBufferHandle,
data_handle: &Self::ManagedBufferHandle,
);

fn ripemd160_managed(
&self,
dest: Self::ManagedBufferHandle,
data_handle: Self::ManagedBufferHandle,
dest: &Self::ManagedBufferHandle,
data_handle: &Self::ManagedBufferHandle,
);

fn verify_bls_managed(
&self,
key: Self::ManagedBufferHandle,
message: Self::ManagedBufferHandle,
signature: Self::ManagedBufferHandle,
key: &Self::ManagedBufferHandle,
message: &Self::ManagedBufferHandle,
signature: &Self::ManagedBufferHandle,
) -> bool;

fn verify_ed25519_managed(
&self,
key: Self::ManagedBufferHandle,
message: Self::ManagedBufferHandle,
signature: Self::ManagedBufferHandle,
key: &Self::ManagedBufferHandle,
message: &Self::ManagedBufferHandle,
signature: &Self::ManagedBufferHandle,
);

/// Note: the signature is minimum 2 bytes in length,
/// the second byte encodes the length of the remaining signature bytes.
fn verify_secp256k1_managed(
&self,
key: Self::ManagedBufferHandle,
message: Self::ManagedBufferHandle,
signature: Self::ManagedBufferHandle,
key: &Self::ManagedBufferHandle,
message: &Self::ManagedBufferHandle,
signature: &Self::ManagedBufferHandle,
) -> bool;

fn verify_custom_secp256k1_managed(
&self,
key: Self::ManagedBufferHandle,
message: Self::ManagedBufferHandle,
signature: Self::ManagedBufferHandle,
key: &Self::ManagedBufferHandle,
message: &Self::ManagedBufferHandle,
signature: &Self::ManagedBufferHandle,
hash_type: MessageHashType,
) -> bool;

fn encode_secp256k1_der_signature_managed(
&self,
r: Self::ManagedBufferHandle,
s: Self::ManagedBufferHandle,
dest: Self::ManagedBufferHandle,
r: &Self::ManagedBufferHandle,
s: &Self::ManagedBufferHandle,
dest: &Self::ManagedBufferHandle,
);
}
32 changes: 16 additions & 16 deletions framework/base/src/api/endpoint_arg_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,41 @@ pub trait EndpointArgumentApi: HandleTypeInfo {
pub trait EndpointArgumentApiImpl: ErrorApi + ManagedTypeApi {
fn get_num_arguments(&self) -> i32;

fn load_argument_managed_buffer(&self, arg_index: i32, dest: Self::ManagedBufferHandle);
fn load_argument_managed_buffer(&self, arg_index: i32, dest: &Self::ManagedBufferHandle);

fn get_argument_len(&self, arg_index: i32) -> usize {
let mbuf_temp_1: Self::ManagedBufferHandle =
use_raw_handle(const_handles::MBUF_TEMPORARY_1);
self.load_argument_managed_buffer(arg_index, mbuf_temp_1.clone());
Self::managed_type_impl().mb_len(mbuf_temp_1)
self.load_argument_managed_buffer(arg_index, &mbuf_temp_1);
Self::managed_type_impl().mb_len(&mbuf_temp_1)
}

fn get_argument_boxed_bytes(&self, arg_index: i32) -> BoxedBytes {
let mbuf_temp_1: Self::ManagedBufferHandle =
use_raw_handle(const_handles::MBUF_TEMPORARY_1);
self.load_argument_managed_buffer(arg_index, mbuf_temp_1.clone());
Self::managed_type_impl().mb_to_boxed_bytes(mbuf_temp_1)
self.load_argument_managed_buffer(arg_index, &mbuf_temp_1);
Self::managed_type_impl().mb_to_boxed_bytes(&mbuf_temp_1)
}

fn load_argument_big_int_unsigned(&self, arg_index: i32, dest: Self::BigIntHandle) {
fn load_argument_big_int_unsigned(&self, arg_index: i32, dest: &Self::BigIntHandle) {
let mbuf_temp_1: Self::ManagedBufferHandle =
use_raw_handle(const_handles::MBUF_TEMPORARY_1);
self.load_argument_managed_buffer(arg_index, mbuf_temp_1.clone());
Self::managed_type_impl().mb_to_big_int_unsigned(mbuf_temp_1, dest);
self.load_argument_managed_buffer(arg_index, &mbuf_temp_1);
Self::managed_type_impl().mb_to_big_int_unsigned(&mbuf_temp_1, dest);
}

fn load_argument_big_int_signed(&self, arg_index: i32, dest: Self::BigIntHandle) {
fn load_argument_big_int_signed(&self, arg_index: i32, dest: &Self::BigIntHandle) {
let mbuf_temp_1: Self::ManagedBufferHandle =
use_raw_handle(const_handles::MBUF_TEMPORARY_1);
self.load_argument_managed_buffer(arg_index, mbuf_temp_1.clone());
Self::managed_type_impl().mb_to_big_int_signed(mbuf_temp_1, dest);
self.load_argument_managed_buffer(arg_index, &mbuf_temp_1);
Self::managed_type_impl().mb_to_big_int_signed(&mbuf_temp_1, dest);
}

fn get_argument_u64(&self, arg_index: i32) -> u64 {
// TODO: this implementation doesn't work for arguments > i64::MAX, fix it!
let big_int_temp_1: Self::BigIntHandle = use_raw_handle(const_handles::BIG_INT_TEMPORARY_1);
self.load_argument_big_int_unsigned(arg_index, big_int_temp_1.clone());
if let Some(value) = Self::managed_type_impl().bi_to_i64(big_int_temp_1) {
self.load_argument_big_int_unsigned(arg_index, &big_int_temp_1);
if let Some(value) = Self::managed_type_impl().bi_to_i64(&big_int_temp_1) {
value as u64
} else {
Self::error_api_impl().signal_error(err_msg::ARG_OUT_OF_RANGE)
Expand All @@ -65,13 +65,13 @@ pub trait EndpointArgumentApiImpl: ErrorApi + ManagedTypeApi {

fn get_argument_i64(&self, arg_index: i32) -> i64 {
let big_int_temp_1: Self::BigIntHandle = use_raw_handle(const_handles::BIG_INT_TEMPORARY_1);
self.load_argument_big_int_signed(arg_index, big_int_temp_1.clone());
if let Some(value) = Self::managed_type_impl().bi_to_i64(big_int_temp_1) {
self.load_argument_big_int_signed(arg_index, &big_int_temp_1);
if let Some(value) = Self::managed_type_impl().bi_to_i64(&big_int_temp_1) {
value
} else {
Self::error_api_impl().signal_error(err_msg::ARG_OUT_OF_RANGE)
}
}

fn load_callback_closure_buffer(&self, dest: Self::ManagedBufferHandle);
fn load_callback_closure_buffer(&self, dest: &Self::ManagedBufferHandle);
}
6 changes: 3 additions & 3 deletions framework/base/src/api/endpoint_finish_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub trait EndpointFinishApi: HandleTypeInfo + ErrorApi {
pub trait EndpointFinishApiImpl: HandleTypeInfo {
fn finish_slice_u8(&self, slice: &[u8]);

fn finish_big_int_raw(&self, handle: Self::BigIntHandle);
fn finish_big_int_raw(&self, handle: &Self::BigIntHandle);

fn finish_big_uint_raw(&self, handle: Self::BigIntHandle);
fn finish_big_uint_raw(&self, handle: &Self::BigIntHandle);

fn finish_managed_buffer_raw(&self, handle: Self::ManagedBufferHandle);
fn finish_managed_buffer_raw(&self, handle: &Self::ManagedBufferHandle);

fn finish_u64(&self, value: u64);

Expand Down
Loading
Loading