From 8837e0376ee326fc75d1630636a267f862c20f10 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 20 Sep 2025 23:11:50 +0200 Subject: [PATCH 1/8] + fixes: https://github.com/bytecodealliance/wit-bindgen/issues/1377 --- crates/csharp/src/function.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/csharp/src/function.rs b/crates/csharp/src/function.rs index 55c4fc8e3..2008bbcec 100644 --- a/crates/csharp/src/function.rs +++ b/crates/csharp/src/function.rs @@ -838,10 +838,23 @@ impl Bindgen for FunctionBindgen<'_, '_> { } Instruction::StringLift { .. } => { - results.push(format!( + let str = self.locals.tmp("str"); + let op0 = &operands[0]; + + let get_str = format!( "global::System.Text.Encoding.UTF8.GetString((byte*){}, {})", - operands[0], operands[1] - )); + op0, operands[1] + ); + + uwriteln!( + self.src, + " + var {str} = {get_str}; + global::System.Runtime.InteropServices.NativeMemory.Free((void*){op0}); + " + ); + + results.push(str); } Instruction::ListLower { element, realloc } => { From c836395f7fe4296f954646a58941efb9f3a4aec1 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 20 Sep 2025 23:43:34 +0200 Subject: [PATCH 2/8] + free only when the string is valid --- crates/csharp/src/function.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/csharp/src/function.rs b/crates/csharp/src/function.rs index 2008bbcec..4707fd0c3 100644 --- a/crates/csharp/src/function.rs +++ b/crates/csharp/src/function.rs @@ -850,7 +850,9 @@ impl Bindgen for FunctionBindgen<'_, '_> { self.src, " var {str} = {get_str}; - global::System.Runtime.InteropServices.NativeMemory.Free((void*){op0}); + if (!string.IsNullOrEmpty({str})) {{ + global::System.Runtime.InteropServices.NativeMemory.Free((void*){op0}); + }} " ); From 4054933dc87a188136d1827d15f0ba59bc28c2bd Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sun, 21 Sep 2025 00:02:07 +0200 Subject: [PATCH 3/8] + free when ptr != 0 --- crates/csharp/src/function.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/csharp/src/function.rs b/crates/csharp/src/function.rs index 4707fd0c3..45bce9e75 100644 --- a/crates/csharp/src/function.rs +++ b/crates/csharp/src/function.rs @@ -850,7 +850,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { self.src, " var {str} = {get_str}; - if (!string.IsNullOrEmpty({str})) {{ + if ({op0} != 0) {{ global::System.Runtime.InteropServices.NativeMemory.Free((void*){op0}); }} " From 73b3e2c42c618c977c2fc498fe13c354fb82bb1e Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sun, 21 Sep 2025 00:35:25 +0200 Subject: [PATCH 4/8] use the "list" way to free a ptr --- crates/csharp/src/function.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/csharp/src/function.rs b/crates/csharp/src/function.rs index 45bce9e75..ca5a7da41 100644 --- a/crates/csharp/src/function.rs +++ b/crates/csharp/src/function.rs @@ -850,8 +850,8 @@ impl Bindgen for FunctionBindgen<'_, '_> { self.src, " var {str} = {get_str}; - if ({op0} != 0) {{ - global::System.Runtime.InteropServices.NativeMemory.Free((void*){op0}); + if (global::System.BitConverter.ToInt32(new global::System.Span((void*)({op0}), 4)) != 0) {{ + global::System.Runtime.InteropServices.NativeMemory.Free((void*)global::System.BitConverter.ToInt32(new global::System.Span((void*)({op0}), 4))); }} " ); From a9ae045599ae2efd4d716c24791f819954c4f730 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sun, 21 Sep 2025 22:52:06 +0200 Subject: [PATCH 5/8] + free when size > 0 --- crates/csharp/src/function.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/csharp/src/function.rs b/crates/csharp/src/function.rs index ca5a7da41..272c0d036 100644 --- a/crates/csharp/src/function.rs +++ b/crates/csharp/src/function.rs @@ -840,18 +840,19 @@ impl Bindgen for FunctionBindgen<'_, '_> { Instruction::StringLift { .. } => { let str = self.locals.tmp("str"); let op0 = &operands[0]; + let op1 = &operands[1]; let get_str = format!( "global::System.Text.Encoding.UTF8.GetString((byte*){}, {})", - op0, operands[1] + op0, op1 ); uwriteln!( self.src, " var {str} = {get_str}; - if (global::System.BitConverter.ToInt32(new global::System.Span((void*)({op0}), 4)) != 0) {{ - global::System.Runtime.InteropServices.NativeMemory.Free((void*)global::System.BitConverter.ToInt32(new global::System.Span((void*)({op0}), 4))); + if ({op1} > 0) {{ + global::System.Runtime.InteropServices.NativeMemory.Free((void*){op0}); }} " ); From 8f989b207036ca639237d2895e5eb676e4044089 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Mon, 22 Sep 2025 19:52:10 +0200 Subject: [PATCH 6/8] + free ptr for cases: ListCanonLift and ListLift --- crates/csharp/src/function.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/csharp/src/function.rs b/crates/csharp/src/function.rs index 272c0d036..3272480f2 100644 --- a/crates/csharp/src/function.rs +++ b/crates/csharp/src/function.rs @@ -788,6 +788,10 @@ impl Bindgen for FunctionBindgen<'_, '_> { " var {array} = new {ty}[{length}]; new global::System.Span<{ty}>((void*)({address}), {length}).CopyTo(new global::System.Span<{ty}>({array})); + + if ({length} > 0) {{ + global::System.Runtime.InteropServices.NativeMemory.Free((void*){address}); + }} " ); @@ -957,6 +961,10 @@ impl Bindgen for FunctionBindgen<'_, '_> { {body} {array}.Add({result}); }} + + if ({length} > 0) {{ + global::System.Runtime.InteropServices.NativeMemory.Free((void*){address}); + }} " ); From badb5f1a2ce1fd22a78ad173a9837285fe336e61 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Tue, 23 Sep 2025 22:06:23 +0200 Subject: [PATCH 7/8] + emit deallocations at the end of the function when possible --- crates/core/src/abi.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/core/src/abi.rs b/crates/core/src/abi.rs index 234209440..bd5e83618 100644 --- a/crates/core/src/abi.rs +++ b/crates/core/src/abi.rs @@ -1235,6 +1235,19 @@ impl<'a, B: Bindgen> Generator<'a, B> { }, } + // Free things + let mut offset = 0; + for (_, ty) in func.params.iter() { + let types = flat_types(self.resolve, ty, Some(max_flat_params)) + .expect(&format!("direct parameter load failed to produce types during generation of fn call (func name: '{}')", func.name)); + for _ in 0..types.len() { + self.emit(&Instruction::GetArg { nth: offset }); + offset += 1; + } + + self.deallocate(ty, Deallocate::Lists); + } + // Build and emit the appropriate return match (variant, async_flat_results) { // Async guest imports always return a i32 status code From fd53ac688972b2efed4f5382cd4faa1b4e4dfdce Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Wed, 24 Sep 2025 12:35:36 +0200 Subject: [PATCH 8/8] + ignore sig.indirect_params --- crates/core/src/abi.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/core/src/abi.rs b/crates/core/src/abi.rs index bd5e83618..d5e70fdf5 100644 --- a/crates/core/src/abi.rs +++ b/crates/core/src/abi.rs @@ -1236,16 +1236,25 @@ impl<'a, B: Bindgen> Generator<'a, B> { } // Free things - let mut offset = 0; - for (_, ty) in func.params.iter() { - let types = flat_types(self.resolve, ty, Some(max_flat_params)) - .expect(&format!("direct parameter load failed to produce types during generation of fn call (func name: '{}')", func.name)); - for _ in 0..types.len() { - self.emit(&Instruction::GetArg { nth: offset }); - offset += 1; - } + if sig.indirect_params { + // self.emit(&Instruction::GetArg { nth: 0 }); + // let ElementInfo { size, align } = self + // .bindgen + // .sizes() + // .record(func.params.iter().map(|t| &t.1)); + // self.emit(&Instruction::GuestDeallocate { size, align }); + } else { + let mut offset = 0; + for (_, ty) in func.params.iter() { + let types = flat_types(self.resolve, ty, Some(max_flat_params)) + .expect(&format!("direct parameter load failed to produce types during generation of fn call (func name: '{}')", func.name)); + for _ in 0..types.len() { + self.emit(&Instruction::GetArg { nth: offset }); + offset += 1; + } - self.deallocate(ty, Deallocate::Lists); + self.deallocate(ty, Deallocate::Lists); + } } // Build and emit the appropriate return