From 5aafcc4836498eb2919148214d268b82cfcf28a2 Mon Sep 17 00:00:00 2001 From: Luca Burgazzoli Date: Tue, 18 Apr 2023 07:38:10 +0200 Subject: [PATCH] examples(allocation): free memory after unmarshalling a result from the guest (#1368) Signed-off-by: Luca Burgazzoli --- examples/allocation/tinygo/greet.go | 8 ++++++- examples/allocation/tinygo/testdata/greet.go | 20 +++++++++++++----- .../allocation/tinygo/testdata/greet.wasm | Bin 54293 -> 54257 bytes 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/examples/allocation/tinygo/greet.go b/examples/allocation/tinygo/greet.go index 09e1831b01..14d4d882d5 100644 --- a/examples/allocation/tinygo/greet.go +++ b/examples/allocation/tinygo/greet.go @@ -90,9 +90,15 @@ func main() { if err != nil { log.Panicln(err) } - // Note: This pointer is still owned by TinyGo, so don't try to free it! + greetingPtr := uint32(ptrSize[0] >> 32) greetingSize := uint32(ptrSize[0]) + + // We don't need the memory after deserialization: make sure it is freed. + if greetingPtr != 0 { + defer free.Call(ctx, uint64(greetingPtr)) + } + // The pointer is a linear memory offset, which is where we write the name. if bytes, ok := mod.Memory().Read(greetingPtr, greetingSize); !ok { log.Panicf("Memory.Read(%d, %d) out of range of memory size %d", diff --git a/examples/allocation/tinygo/testdata/greet.go b/examples/allocation/tinygo/testdata/greet.go index d858ab38fe..cfc460a027 100644 --- a/examples/allocation/tinygo/testdata/greet.go +++ b/examples/allocation/tinygo/testdata/greet.go @@ -6,6 +6,9 @@ import ( "unsafe" ) +// #include +import "C" + // main is required for TinyGo to compile to Wasm. func main() {} @@ -70,10 +73,17 @@ func ptrToString(ptr uint32, size uint32) string { } // stringToPtr returns a pointer and size pair for the given string in a way -// compatible with WebAssembly numeric types. +// compatible with WebAssembly numeric types. The pointer is not automatically +// managed by tinygo but must be freed by the host. func stringToPtr(s string) (uint32, uint32) { - buf := []byte(s) - ptr := &buf[0] - unsafePtr := uintptr(unsafe.Pointer(ptr)) - return uint32(unsafePtr), uint32(len(buf)) + if len(s) == 0 { + return 0, 0 + } + + size := C.ulong(len(s)) + ptr := unsafe.Pointer(C.malloc(size)) + + copy(unsafe.Slice((*byte)(ptr), size), []byte(s)) + + return uint32(uintptr(ptr)), uint32(len(s)) } diff --git a/examples/allocation/tinygo/testdata/greet.wasm b/examples/allocation/tinygo/testdata/greet.wasm index 12efdc1fa2d4b444f1412f4f9de54c0a6d3a6159..bd91540c7de86f9202c00f8b924d2f073ffb996d 100755 GIT binary patch delta 8355 zcmbVRdw5mVmEU`xbMDQ3gd8A1NJ4V%O+s!UFJ3nfsL7@Tf`EiqVJa3Pq`*yhB>{0n zZmLuRL=A4KQK^%uk~QA35mXECTU^yFSbHXc}V%6NYIpj}Xkz z4PEzWJ}!8gx|=U7EZ4u;-qOM-SZ zewDDq!<-kW7qn^qq5F-9U?E)&sUhu`!3v9+0Tz(>e@OjCuTS<1R3{iqMqc600E^18 zvUi_~8wFnhh$W(eSsYjvmzA+X7Whc#-nhjY^m=*eej_6w&)sj(Ux~&n_T=s=jmbf@ z45~MbvHXns)QC=Sn8_|+va3rsC(>8;KV+B_wR(A3&G+P#oweDu_yWCNxc@9}7RxJq z`v_fQTzCSgN8|+{Ur+}<6a0~&25}(ZX?4s~J^Hi_K&Y)!+p;*_W0ok*8!Eeu39Obe z>8ubET0LYGi_>`$Uutu8w3(CSS+&HQlV2kF_J(vGaA#Q5jfJ$J2w0j~tPXf5uRoJi zjIfN>u#nuGY-u2+h=n+WLcguCd4wzldb#B23i3d}5>oFZMq`)=l??@$C7{OM1G-E% z3uu!<-he!#veWVk3Zb)l3@pSL$)aMGC1#5%FsvCm2=)kF z>JDgqYax?<40z8sG2M|coE8;naww7K|2pRt>gGTRA5xD5WU2vw5Lh5!u``1q^xY7w zO7@ZQKOFoc8uz6Sq&C)#_~$&=BW%+SL3x_RqrwEwA%lEG%V@SwUh<2Syf4-Khu zb3w9`9O!m)X@1=o{HK2(b{Cq;vUgw{##}UOk`iTLAJ9@evQqT#OSFw_d*)#kBCO?7A zjxq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