-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples(allocation): free memory after unmarshalling a result from t…
…he guest (cgo) Signed-off-by: Luca Burgazzoli <[email protected]>
- Loading branch information
1 parent
c5871c7
commit 4f23ede
Showing
16 changed files
with
261 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
-371 Bytes
(99%)
imports/wasi_snapshot_preview1/example/testdata/tinygo/cat.wasm
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package vs | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/tetratelabs/wazero/internal/testing/require" | ||
) | ||
|
||
var ( | ||
// allocationWasm is compiled from ../../../examples/allocation/tinygo/testdata/src/greet.go | ||
// We can't use go:embed as it is outside this directory. Copying it isn't ideal due to size and drift. | ||
allocationCGOWasmPath = "../../../examples/allocation/tinygo/testdata/greet.wasm" | ||
allocationCGOWasm []byte | ||
allocationCGOParam = "wazero" | ||
allocationCGOResult = []byte("wasm >> Hello, wazero!") | ||
allocationCGOConfig *RuntimeConfig | ||
) | ||
|
||
func init() { | ||
allocationCGOWasm = readRelativeFile(allocationCGOWasmPath) | ||
allocationCGOConfig = &RuntimeConfig{ | ||
ModuleName: "greeting", | ||
ModuleWasm: allocationCGOWasm, | ||
FuncNames: []string{"malloc", "free", "greeting"}, | ||
NeedsWASI: true, // Needed for TinyGo | ||
LogFn: func(buf []byte) error { | ||
if !bytes.Equal(allocationCGOResult, buf) { | ||
return fmt.Errorf("expected %q, but was %q", allocationCGOResult, buf) | ||
} | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func AllocationCGOCall(m Module, _ int) error { | ||
nameSize := uint32(len(allocationCGOParam)) | ||
// Instead of an arbitrary memory offset, use TinyGo's allocator. Notice | ||
// there is nothing string-specific in this allocation function. The same | ||
// function could be used to pass binary serialized data to Wasm. | ||
namePtr, err := m.CallI32_I32(testCtx, "malloc", nameSize) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// The pointer is a linear memory offset, which is where we write the name. | ||
if err = m.WriteMemory(namePtr, []byte(allocationCGOParam)); err != nil { | ||
return err | ||
} | ||
|
||
// Now, we can call "greeting", which reads the string we wrote to memory! | ||
ptrSize, fnErr := m.CallI32I32_I64(testCtx, "greeting", namePtr, nameSize) | ||
if fnErr != nil { | ||
return fnErr | ||
} | ||
|
||
// This pointer was allocated by TinyGo, but owned by Go, So, we have to | ||
// deallocate it when finished | ||
if err := m.CallI32_V(testCtx, "free", namePtr); err != nil { | ||
return err | ||
} | ||
|
||
// This pointer was allocated by TinyGo, but owned by Go, So, we have to | ||
// deallocate it when finished | ||
if err := m.CallI32_V(testCtx, "free", uint32(ptrSize>>32)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func RunTestAllocationCGO(t *testing.T, runtime func() Runtime) { | ||
testCall(t, runtime, allocationCGOConfig, testAllocationCGOCall) | ||
} | ||
|
||
func testAllocationCGOCall(t *testing.T, m Module, instantiation, iteration int) { | ||
err := AllocationCGOCall(m, iteration) | ||
require.NoError(t, err, "instantiation[%d] iteration[%d] failed: %v", instantiation, iteration, err) | ||
} | ||
|
||
func RunTestBenchmarkAllocationCGO_Call_CompilerFastest(t *testing.T, vsRuntime Runtime) { | ||
runTestBenchmark_Call_CompilerFastest(t, allocationCGOConfig, "Allocation", AllocationCGOCall, vsRuntime) | ||
} | ||
|
||
func RunBenchmarkAllocationCGO(b *testing.B, runtime func() Runtime) { | ||
benchmark(b, runtime, allocationCGOConfig, AllocationCGOCall) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters