From 29304aae89304968335056aee440193fa4f3d96f Mon Sep 17 00:00:00 2001 From: Fusee Date: Wed, 15 May 2024 15:02:24 +0200 Subject: [PATCH] fixed memory leak on boxed bytes --- framework/base/src/types/heap/boxed_bytes.rs | 33 ++++--------------- .../managed_buffer_api_vh.rs | 16 ++++----- .../src/api/endpoint_arg_api_node.rs | 2 +- .../managed_types/elliptic_curve_api_node.rs | 6 ++-- .../managed_types/managed_buffer_api_node.rs | 2 +- 5 files changed, 18 insertions(+), 41 deletions(-) diff --git a/framework/base/src/types/heap/boxed_bytes.rs b/framework/base/src/types/heap/boxed_bytes.rs index 338af818c2..3bc0d690c6 100644 --- a/framework/base/src/types/heap/boxed_bytes.rs +++ b/framework/base/src/types/heap/boxed_bytes.rs @@ -1,12 +1,9 @@ +use alloc::{alloc::{alloc, Layout, realloc}, boxed::Box, vec, vec::Vec}; + use crate::{ abi::{TypeAbi, TypeAbiFrom, TypeName}, codec::*, }; -use alloc::{ - alloc::{alloc, alloc_zeroed, realloc, Layout}, - boxed::Box, - vec::Vec, -}; /// Simple wrapper around a boxed byte slice, /// but with a lot of optimized methods for manipulating it. @@ -20,23 +17,7 @@ impl BoxedBytes { } pub fn zeros(len: usize) -> Self { - unsafe { - let layout = Layout::from_size_align(len, core::mem::align_of::()).unwrap(); - let bytes_ptr = alloc_zeroed(layout); - let bytes_box = Box::from_raw(core::slice::from_raw_parts_mut(bytes_ptr, len)); - BoxedBytes(bytes_box) - } - } - - /// Allocates an uninitialized BoxedBytes to heap. - /// - /// # Safety - /// - /// Should only be called if the contents are initialized immediately afterwards, e.g. via a FFI call. - pub unsafe fn allocate(len: usize) -> Self { - let layout = Layout::from_size_align(len, core::mem::align_of::()).unwrap(); - let bytes_ptr = alloc(layout); - let bytes_box = Box::from_raw(core::slice::from_raw_parts_mut(bytes_ptr, len)); + let bytes_box = Box::from(vec![0u8; len]); BoxedBytes(bytes_box) } @@ -222,11 +203,9 @@ impl NestedDecode for BoxedBytes { H: DecodeErrorHandler, { let size = usize::dep_decode_or_handle_err(input, h)?; - unsafe { - let mut result = BoxedBytes::allocate(size); - input.read_into(result.as_mut_slice(), h)?; - Ok(result) - } + let mut result = BoxedBytes::zeros(size); + input.read_into(result.as_mut_slice(), h)?; + Ok(result) } } diff --git a/framework/scenario/src/api/managed_type_api_vh/managed_buffer_api_vh.rs b/framework/scenario/src/api/managed_type_api_vh/managed_buffer_api_vh.rs index 09e7d9137f..22a0c152cc 100644 --- a/framework/scenario/src/api/managed_type_api_vh/managed_buffer_api_vh.rs +++ b/framework/scenario/src/api/managed_type_api_vh/managed_buffer_api_vh.rs @@ -29,16 +29,14 @@ impl ManagedBufferApiImpl for VMHooksApi { fn mb_to_boxed_bytes(&self, handle: Self::ManagedBufferHandle) -> BoxedBytes { self.with_vm_hooks_ctx_1(&handle, |vh| { let len = vh.mbuffer_get_length(handle.get_raw_handle_unchecked()) as usize; - unsafe { - let mut res = BoxedBytes::allocate(len); - if len > 0 { - let _ = vh.mbuffer_get_bytes( - handle.get_raw_handle_unchecked(), - res.as_mut_ptr() as MemPtr, - ); - } - res + let mut res = BoxedBytes::zeros(len); + if len > 0 { + let _ = vh.mbuffer_get_bytes( + handle.get_raw_handle_unchecked(), + res.as_mut_ptr() as MemPtr, + ); } + res }) } diff --git a/framework/wasm-adapter/src/api/endpoint_arg_api_node.rs b/framework/wasm-adapter/src/api/endpoint_arg_api_node.rs index 48135f65ae..6c4627fa60 100644 --- a/framework/wasm-adapter/src/api/endpoint_arg_api_node.rs +++ b/framework/wasm-adapter/src/api/endpoint_arg_api_node.rs @@ -56,7 +56,7 @@ impl EndpointArgumentApiImpl for VmApiImpl { fn get_argument_boxed_bytes(&self, arg_index: i32) -> BoxedBytes { let len = self.get_argument_len(arg_index); unsafe { - let mut res = BoxedBytes::allocate(len); + let mut res = BoxedBytes::zeros(len); if len > 0 { getArgument(arg_index, res.as_mut_ptr()); } diff --git a/framework/wasm-adapter/src/api/managed_types/elliptic_curve_api_node.rs b/framework/wasm-adapter/src/api/managed_types/elliptic_curve_api_node.rs index fe35a339f9..3fb800e85f 100644 --- a/framework/wasm-adapter/src/api/managed_types/elliptic_curve_api_node.rs +++ b/framework/wasm-adapter/src/api/managed_types/elliptic_curve_api_node.rs @@ -314,7 +314,7 @@ impl EllipticCurveApiImpl for crate::api::VmApiImpl { ) -> BoxedBytes { unsafe { let byte_length = (getCurveLengthEC(ec_handle) + 7) / 8; - let mut result = BoxedBytes::allocate(1 + 2 * byte_length as usize); + let mut result = BoxedBytes::zeros(1 + 2 * byte_length as usize); marshalEC(x_pair_handle, y_pair_handle, ec_handle, result.as_mut_ptr()); result } @@ -340,7 +340,7 @@ impl EllipticCurveApiImpl for crate::api::VmApiImpl { ) -> BoxedBytes { unsafe { let byte_length = (getCurveLengthEC(ec_handle) + 7) / 8; - let mut result = BoxedBytes::allocate(1 + byte_length as usize); + let mut result = BoxedBytes::zeros(1 + byte_length as usize); marshalCompressedEC(x_pair_handle, y_pair_handle, ec_handle, result.as_mut_ptr()); result } @@ -426,7 +426,7 @@ impl EllipticCurveApiImpl for crate::api::VmApiImpl { ) -> BoxedBytes { unsafe { let priv_key_length = getPrivKeyByteLengthEC(ec_handle); - let mut private_key = BoxedBytes::allocate(priv_key_length as usize); + let mut private_key = BoxedBytes::zeros(priv_key_length as usize); generateKeyEC( x_pub_key_handle, y_pub_key_handle, diff --git a/framework/wasm-adapter/src/api/managed_types/managed_buffer_api_node.rs b/framework/wasm-adapter/src/api/managed_types/managed_buffer_api_node.rs index 120d06e734..ee0390ffb0 100644 --- a/framework/wasm-adapter/src/api/managed_types/managed_buffer_api_node.rs +++ b/framework/wasm-adapter/src/api/managed_types/managed_buffer_api_node.rs @@ -58,7 +58,7 @@ impl ManagedBufferApiImpl for crate::api::VmApiImpl { fn mb_to_boxed_bytes(&self, handle: Self::ManagedBufferHandle) -> BoxedBytes { unsafe { let len = mBufferGetLength(handle); - let mut res = BoxedBytes::allocate(len as usize); + let mut res = BoxedBytes::zeros(len as usize); if len > 0 { let _ = mBufferGetBytes(handle, res.as_mut_ptr()); }