Skip to content

Commit

Permalink
examples(allocation): ensure the data use to call host functions is n…
Browse files Browse the repository at this point in the history
…ot fred before the host function is invoked
  • Loading branch information
lburgazzoli committed May 4, 2023
1 parent 6dd0cef commit 808dbb5
Showing 2 changed files with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions examples/allocation/tinygo/testdata/greet.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import "C"
import (
"fmt"
"reflect"
"runtime"
"unsafe"
)

@@ -21,6 +22,7 @@ func greet(name string) {
func log(message string) {
ptr, size := stringToPtr(message)
_log(ptr, size)
runtime.KeepAlive(message)
}

// _log is a WebAssembly import which prints a string (linear memory offset,
@@ -72,13 +74,13 @@ 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.
// stringToPtr returns a pointer and size pair to the underlying bytes of the given
// string in a way compatible with WebAssembly numeric types. Since Go strings are
// immutable, the pointer returned by stringToPtr must not be modified.
func stringToPtr(s string) (uint32, uint32) {
buf := []byte(s)
ptr := &buf[0]
ptr := unsafe.StringData(s)
unsafePtr := uintptr(unsafe.Pointer(ptr))
return uint32(unsafePtr), uint32(len(buf))
return uint32(unsafePtr), uint32(len(s))
}

// stringToLeakedPtr returns a pointer and size pair for the given string in a way
Binary file modified examples/allocation/tinygo/testdata/greet.wasm
Binary file not shown.

0 comments on commit 808dbb5

Please sign in to comment.