Skip to content

Commit

Permalink
symmetric list memory fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Sep 1, 2024
1 parent 166460f commit 034e7c6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 53 deletions.
65 changes: 24 additions & 41 deletions crates/cpp/tests/symmetric_lists/rust_b/src/w.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Generated by `wit-bindgen` 0.30.0. DO NOT EDIT!
// Options used:
#[allow(dead_code)]
pub mod exports {
#[allow(dead_code)]
Expand All @@ -12,7 +10,6 @@ pub mod exports {
#[doc(hidden)]
static __FORCE_SECTION_REF: fn() =
super::super::super::super::__link_custom_section_describing_imports;

use super::super::super::super::_rt;
#[doc(hidden)]
#[allow(non_snake_case)]
Expand All @@ -31,12 +28,10 @@ pub mod exports {
let string2 = String::from(
std::str::from_utf8(std::slice::from_raw_parts(l0, len2)).unwrap(),
);

string2
};
result3.push(e3);
}
//_rt::cabi_dealloc(base3, len3 * (2*core::mem::size_of::<*const u8>()), core::mem::size_of::<*const u8>());
let result4 = T::f(result3);
let vec6 = result4;
let len6 = vec6.len();
Expand Down Expand Up @@ -74,11 +69,11 @@ pub mod exports {
_rt::run_ctors_once();
let len0 = arg1;
let result1 =
T::g(unsafe{std::slice::from_raw_parts(arg0.cast(), len0)}.to_vec());
let vec2 = result1;
T::g(unsafe { std::slice::from_raw_parts(arg0.cast(), len0) }.to_vec());
let vec2 = (result1).into_boxed_slice();
let ptr2 = vec2.as_ptr().cast::<u8>();
let len2 = vec2.len();
vec2.leak();
::core::mem::forget(vec2);
*arg2.add(core::mem::size_of::<*const u8>()).cast::<usize>() = len2;
*arg2.add(0).cast::<*mut u8>() = ptr2.cast_mut();
}
Expand All @@ -87,47 +82,35 @@ pub mod exports {
fn g(a: _rt::Vec<u8>) -> _rt::Vec<u8>;
}
#[doc(hidden)]

macro_rules! __export_test_test_i_cabi{
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {

#[cfg_attr(target_arch = "wasm32", export_name = "f")]
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
unsafe extern "C" fn testX3AtestX2FiX00f(arg0: *mut u8,arg1: usize,arg2: *mut u8,) {
$($path_to_types)*::_export_f_cabi::<$ty>(arg0, arg1, arg2)
}
#[cfg_attr(target_arch = "wasm32", export_name = "g")]
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
unsafe extern "C" fn testX3AtestX2FiX00g(arg0: *mut u8,arg1: usize,arg2: *mut u8,) {
$($path_to_types)*::_export_g_cabi::<$ty>(arg0, arg1, arg2)
}
};);
}
macro_rules! __export_test_test_i_cabi {
($ty:ident with_types_in $($path_to_types:tt)*) => {
const _ : () = { #[cfg_attr(target_arch = "wasm32", export_name =
"f")] #[cfg_attr(not(target_arch = "wasm32"), no_mangle)] unsafe
extern "C" fn testX3AtestX2FiX00f(arg0 : * mut u8, arg1 : usize,
arg2 : * mut u8,) { $($path_to_types)*:: _export_f_cabi::<$ty >
(arg0, arg1, arg2) } #[cfg_attr(target_arch = "wasm32",
export_name = "g")] #[cfg_attr(not(target_arch = "wasm32"),
no_mangle)] unsafe extern "C" fn testX3AtestX2FiX00g(arg0 : * mut
u8, arg1 : usize, arg2 : * mut u8,) { $($path_to_types)*::
_export_g_cabi::<$ty > (arg0, arg1, arg2) } };
};
}
#[doc(hidden)]
pub(crate) use __export_test_test_i_cabi;
}
}
}
}
mod _rt {

#[cfg(target_arch = "wasm32")]
pub fn run_ctors_once() {
wit_bindgen::rt::run_ctors_once();
}
pub use alloc_crate::vec::Vec;
pub unsafe fn cabi_dealloc(ptr: *mut u8, size: usize, align: usize) {
if size == 0 {
return;
}
let layout = alloc::Layout::from_size_align_unchecked(size, align);
alloc::dealloc(ptr, layout);
}
pub use alloc_crate::alloc;
pub use alloc_crate::string::String;
pub use alloc_crate::vec::Vec;
extern crate alloc as alloc_crate;
}

/// Generates `#[no_mangle]` functions to export the specified type as the
/// root implementation of all generated traits.
///
Expand All @@ -146,16 +129,17 @@ mod _rt {
/// ```
#[allow(unused_macros)]
#[doc(hidden)]

macro_rules! __export_w_impl {
($ty:ident) => (self::export!($ty with_types_in self););
($ty:ident with_types_in $($path_to_types_root:tt)*) => (
$($path_to_types_root)*::exports::test::test::i::__export_test_test_i_cabi!($ty with_types_in $($path_to_types_root)*::exports::test::test::i);
)
($ty:ident) => {
self::export!($ty with_types_in self);
};
($ty:ident with_types_in $($path_to_types_root:tt)*) => {
$($path_to_types_root)*:: exports::test::test::i::__export_test_test_i_cabi!($ty
with_types_in $($path_to_types_root)*:: exports::test::test::i);
};
}
#[doc(inline)]
pub(crate) use __export_w_impl as export;

#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:wit-bindgen:0.30.0:test:test:w:encoded world"]
#[doc(hidden)]
Expand All @@ -165,7 +149,6 @@ B\x06\x01ps\x01@\x01\x01a\0\0\0\x04\0\x01f\x01\x01\x01p}\x01@\x01\x01a\x02\0\x02
\x04\0\x01g\x01\x03\x04\x01\x0btest:test/i\x05\0\x04\x01\x0btest:test/w\x04\0\x0b\
\x07\x01\0\x01w\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\
\x070.216.0\x10wit-bindgen-rust\x060.30.0";

#[inline(never)]
#[doc(hidden)]
pub fn __link_custom_section_describing_imports() {
Expand Down
33 changes: 21 additions & 12 deletions crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,15 +711,15 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let val = format!("vec{}", tmp);
let ptr = format!("ptr{}", tmp);
let len = format!("len{}", tmp);
if realloc.is_none() || self.gen.gen.opts.symmetric {
if realloc.is_none() || (self.gen.in_import && self.gen.gen.opts.symmetric) {
self.push_str(&format!("let {} = {};\n", val, operands[0]));
} else {
let op0 = operands.pop().unwrap();
self.push_str(&format!("let {} = ({}).into_boxed_slice();\n", val, op0));
}
self.push_str(&format!("let {} = {}.as_ptr().cast::<u8>();\n", ptr, val));
self.push_str(&format!("let {} = {}.len();\n", len, val));
if realloc.is_some() && !self.gen.gen.opts.symmetric {
if realloc.is_some() && !(self.gen.in_import && self.gen.gen.opts.symmetric) {
self.push_str(&format!("::core::mem::forget({});\n", val));
}
results.push(format!("{ptr}.cast_mut()"));
Expand All @@ -731,10 +731,17 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let len = format!("len{}", tmp);
self.push_str(&format!("let {} = {};\n", len, operands[1]));
let vec = self.gen.path_to_vec();
let result = format!(
"{vec}::from_raw_parts({}.cast(), {1}, {1})",
operands[0], len
);
let result = if !self.gen.gen.opts.symmetric || self.gen.in_import {
format!(
"{vec}::from_raw_parts({}.cast(), {1}, {1})",
operands[0], len
)
} else {
format!(
"unsafe {{ std::slice::from_raw_parts({}.cast(), {1}) }}.to_vec()",
operands[0], len
)
};
results.push(result);
}

Expand Down Expand Up @@ -871,12 +878,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
uwriteln!(self.src, "{result}.push(e{tmp});");
uwriteln!(self.src, "}}");
results.push(result);
let dealloc = self.gen.path_to_cabi_dealloc();
self.push_str(&format!(
"{dealloc}({base}, {len} * {size}, {align});\n",
size = size.format(POINTER_SIZE_EXPRESSION),
align = align.format(POINTER_SIZE_EXPRESSION)
));
if !self.gen.gen.opts.symmetric || self.gen.in_import {
let dealloc = self.gen.path_to_cabi_dealloc();
self.push_str(&format!(
"{dealloc}({base}, {len} * {size}, {align});\n",
size = size.format(POINTER_SIZE_EXPRESSION),
align = align.format(POINTER_SIZE_EXPRESSION)
));
}
}

Instruction::IterElem { .. } => results.push("e".to_string()),
Expand Down

0 comments on commit 034e7c6

Please sign in to comment.