From 0b11eec45a467749055376c482cfdba7dce7b49f Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Fri, 16 Jun 2023 11:54:47 +0900 Subject: [PATCH] Export malloc/free by default to match TinyGo 0.28 behavior (#16) --- bench/bench_test.go | 29 +++++++++++++++++++++++++++++ init.go | 4 +++- magefiles/magefile.go | 22 ++++++++++++++++------ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/bench/bench_test.go b/bench/bench_test.go index 372e090..c9579a8 100644 --- a/bench/bench_test.go +++ b/bench/bench_test.go @@ -13,6 +13,35 @@ import ( "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" ) +// TestExportsMalloc is in the bench package since it already provides a fully packaged binary. +// This could be cleaned up by using a different package, but for now we'll stick with being a little +// lazy. +func TestExportsMalloc(t *testing.T) { + wasm, err := os.ReadFile(filepath.Join("..", "build", "bench.wasm")) + if err != nil { + t.Fatal(err) + } + + ctx := context.Background() + r := wazero.NewRuntime(ctx) + wasi_snapshot_preview1.MustInstantiate(ctx, r) + + mod, err := r.InstantiateModuleFromBinary(ctx, wasm) + if err != nil { + t.Fatal(err) + } + + malloc := mod.ExportedFunction("malloc") + free := mod.ExportedFunction("free") + + if malloc == nil { + t.Error("malloc is not exported") + } + if free == nil { + t.Error("free is not exported") + } +} + func BenchmarkGC(b *testing.B) { tests := []string{"bench.wasm", "benchref.wasm"} for _, tc := range tests { diff --git a/init.go b/init.go index ef006cc..efce564 100644 --- a/init.go +++ b/init.go @@ -1,9 +1,11 @@ // Copyright wasilibs authors // SPDX-License-Identifier: MIT +//go:build tinygo + package nottinygc /* -#cgo LDFLAGS: -Lwasm -lgc -lmimalloc -lclang_rt.builtins-wasm32 +#cgo LDFLAGS: -Lwasm -lgc -lmimalloc -lclang_rt.builtins-wasm32 --export=malloc --export=free */ import "C" diff --git a/magefiles/magefile.go b/magefiles/magefile.go index 15ca06f..67435a0 100644 --- a/magefiles/magefile.go +++ b/magefiles/magefile.go @@ -40,6 +40,14 @@ func Test() error { return fmt.Errorf("unexpected error message: %s", s) } + if err := buildBenchExecutable(); err != nil { + return err + } + + if err := sh.RunV("go", "test", "./bench"); err != nil { + return err + } + return nil } @@ -69,8 +77,6 @@ func Format() error { return nil } -var errMissingCopyrightHeaders = errors.New("missing copyright headers, use go run mage.go format") - func Lint() error { if _, err := sh.Exec(map[string]string{}, io.Discard, io.Discard, "go", "run", fmt.Sprintf("github.com/google/addlicense@%s", verAddLicense), "-check", @@ -81,7 +87,7 @@ func Lint() error { "-ignore", "**/*.yml", "-ignore", "**/*.yaml", "."); err != nil { - return errMissingCopyrightHeaders + return fmt.Errorf("missing copyright headers, use go run mage.go format: %w", err) } return sh.RunV("go", "run", fmt.Sprintf("github.com/golangci/golangci-lint/cmd/golangci-lint@%s", verGolancCILint), "run") @@ -112,7 +118,7 @@ func UpdateLibs() error { // Bench runs benchmarks. func Bench() error { - if err := os.MkdirAll("build", 0o755); err != nil { + if err := buildBenchExecutable(); err != nil { return err } @@ -120,11 +126,15 @@ func Bench() error { return err } - if err := sh.RunV("tinygo", "build", "-gc=custom", "-tags=custommalloc", "-scheduler=none", "-target=wasi", "-o", "build/bench.wasm", "./bench"); err != nil { + return sh.RunV("go", "test", "-bench=.", "-benchtime=10s", "./bench") +} + +func buildBenchExecutable() error { + if err := os.MkdirAll("build", 0o755); err != nil { return err } - return sh.RunV("go", "test", "-bench=.", "-benchtime=10s", "./bench") + return sh.RunV("tinygo", "build", "-gc=custom", "-tags=custommalloc", "-scheduler=none", "-target=wasi", "-o", "build/bench.wasm", "./bench") } var Default = Test