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 memory leak on boxed bytes #1629

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 6 additions & 27 deletions framework/base/src/types/heap/boxed_bytes.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,23 +17,7 @@ impl BoxedBytes {
}

pub fn zeros(len: usize) -> Self {
unsafe {
let layout = Layout::from_size_align(len, core::mem::align_of::<u8>()).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 {
andrei-marinica marked this conversation as resolved.
Show resolved Hide resolved
let layout = Layout::from_size_align(len, core::mem::align_of::<u8>()).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)
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ impl<VHB: VMHooksApiBackend> ManagedBufferApiImpl for VMHooksApi<VHB> {
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
})
}

Expand Down
2 changes: 1 addition & 1 deletion framework/wasm-adapter/src/api/endpoint_arg_api_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Loading