Skip to content

Commit

Permalink
examples(allocation): ensure message outlives host call
Browse files Browse the repository at this point in the history
Also, incorporate guidance from TinyGo to use unsafe.String
rather than unsafe.SliceHeader.

Signed-off-by: Nuno Cruces <[email protected]>
  • Loading branch information
ncruces committed May 4, 2023
1 parent 5380321 commit 3c01f86
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions examples/allocation/tinygo/testdata/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "C"

import (
"fmt"
"reflect"
"runtime"
"unsafe"
)

Expand All @@ -21,6 +21,7 @@ func greet(name string) {
func log(message string) {
ptr, size := stringToPtr(message)
_log(ptr, size)
runtime.KeepAlive(message) // keep message alive until ptr is no longer needed.
}

// _log is a WebAssembly import which prints a string (linear memory offset,
Expand All @@ -30,7 +31,7 @@ func log(message string) {
//
//go:wasm-module env
//export log
func _log(ptr uint32, size uint32)
func _log(ptr, size uint32)

// greeting gets a greeting for the name.
func greeting(name string) string {
Expand Down Expand Up @@ -63,32 +64,24 @@ func _greeting(ptr, size uint32) (ptrSize uint64) {
// ptrToString returns a string from WebAssembly compatible numeric types
// representing its pointer and length.
func ptrToString(ptr uint32, size uint32) string {
// Get a slice view of the underlying bytes in the stream. We use SliceHeader, not StringHeader
// as it allows us to fix the capacity to what was allocated.
return *(*string)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(ptr),
Len: uintptr(size), // Tinygo requires these as uintptrs even if they are int fields.
Cap: uintptr(size), // ^^ See https://github.com/tinygo-org/tinygo/issues/1284
}))
return unsafe.String((*byte)(unsafe.Pointer(uintptr(ptr))), size)
}

// stringToPtr returns a pointer and size pair for the given string in a way
// compatible with WebAssembly numeric types.
// The returned pointer aliases the string hence the string must be kept alive
// until ptr is no longer needed.
func stringToPtr(s string) (uint32, uint32) {
buf := []byte(s)
ptr := &buf[0]
unsafePtr := uintptr(unsafe.Pointer(ptr))
return uint32(unsafePtr), uint32(len(buf))
ptr := unsafe.Pointer(unsafe.StringData(s))
return uint32(uintptr(ptr)), uint32(len(s))
}

// stringToLeakedPtr returns a pointer and size pair for the given string in a way
// compatible with WebAssembly numeric types. The pointer is not automatically
// managed by TinyGo hence it must be freed by the host.
// compatible with WebAssembly numeric types.
// The pointer is not automatically managed by TinyGo hence it must be freed by the host.
func stringToLeakedPtr(s string) (uint32, uint32) {
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))
copy(unsafe.Slice((*byte)(ptr), size), s)
return uint32(uintptr(ptr)), uint32(size)
}
Binary file modified examples/allocation/tinygo/testdata/greet.wasm
Binary file not shown.

0 comments on commit 3c01f86

Please sign in to comment.