Skip to content

Commit

Permalink
quick fix to remove the leak from symmetric calls
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Sep 3, 2024
1 parent 797ca6c commit f661f88
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,11 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.push_str(&format!(
"let ptr = {alloc}::alloc({layout}).cast::<u8>();\n",
));
if self.gen.gen.opts.symmetric && self.gen.in_import {
self.push_str(&format!(
"if !ptr.is_null() {{ _deallocate.push((ptr, {layout})); }}\n"
));
}
self.push_str(&format!(
"if ptr.is_null()\n{{\n{alloc}::handle_alloc_error({layout});\n}}\nptr\n}}",
));
Expand Down Expand Up @@ -909,6 +914,9 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.push_str("(");
self.push_str(&operands.join(", "));
self.push_str(");\n");
if self.gen.gen.opts.symmetric && self.gen.in_import {
self.push_str(&format!("for (ptr,layout) in _deallocate.drain(..) {{ _rt::alloc::dealloc(ptr, layout); }}\n"));
}
}

Instruction::AsyncCallWasm { name, size, align } => {
Expand Down
6 changes: 6 additions & 0 deletions crates/rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,12 @@ impl {async_support}::StreamPayload for {name} {{
self.src.push_str("#[allow(unused_unsafe, clippy::all)]\n");
let params = self.print_signature(func, false, &sig, true);
self.src.push_str("{\n");
if self.gen.opts.symmetric {
uwriteln!(
self.src,
"let mut _deallocate: Vec<(*mut u8, _rt::alloc::Layout)> = Vec::new();"
);
}
self.src.push_str("unsafe {\n");

self.generate_guest_import_body(&self.wasm_import_module, func, params, async_);
Expand Down

0 comments on commit f661f88

Please sign in to comment.