mirror of
https://github.com/hanzoai/wasm2go.git
synced 2026-10-30 15:06:48 +00:00
* Back shared linear memories with anonymous mappings, not the Go heap A shared memory is allocated at its declared ceiling LENGTH once (agents deref the base pointer concurrently, so the array must never move). On the Go heap that size is a trap: the FIRST make is lazily paged, but a freed ceiling-sized span gets zeroed when the allocator reuses it — so the second instance a process builds and discards (e.g. the private builder engines behind the copy-on-write image tiers) memclrs the whole ceiling. With llama's 8 GiB ceiling that measured ~8 GB of dirty resident pages for memory nothing had touched, on every process, regardless of model size; guests with small ceilings (spidermonkey's 256 MiB) had the same behaviour hidden under the noise floor. Generated constructors for SHARED memories now call the sharedimage runtime's AllocSharedMemory: anonymous, private, zero-fill-on-demand pages outside the GC heap, fresh from the OS on every call, with a finalizer on the module preserving the heap allocation's reclaim semantics (a module kept reachable — say by a parked agent goroutine — keeps its memory). Platforms without mmap keep the historical make. The sharedimage aux files thereby become a compile-time dependency of shared-memory output, which they were always documented to be: the gcasm capture tree and the test snippet runners now include Result.AuxFiles instead of silently dropping them. Pinned by a fixture with a 1 GiB ceiling: the constructor must route through AllocSharedMemory, and building + discarding three instances must hold process RSS far under one ceiling. Co-Authored-By: Claude Fable 5 <[email protected]> * Add in-place image builders: the builder writes the image file directly The image tiers built their template by booting a throwaway instance on private memory, copying its linear memory into a heap buffer, and writing that to the image file — the same bytes crossing memory up to three times, and (before the off-heap change) the trigger for the ceiling-zeroing regression. Elide all of it: NewSharedImageInPlace / NewSharedSnapshotInPlace hand the builder a MAP_SHARED mapping of the (sparse) image file, so every page the build touches is written once, straight into the file's page cache, and IS the image. Building now dirties exactly the touched pages, and they are file-backed — the kernel can write them back and evict under pressure, which anonymous pages cannot offer. The data-segment kind keeps its contract (carry NOTHING above the segments): the builder's BSS is punched back out of the file (truncate down, re-extend sparse) before instances map it. The builder's mapping stays valid until the module is unreachable, via the same finalizer discipline as AllocSharedMemory. Constructing a module ON caller memory needs the declared initial size, so generated packages now export InitialMemoryBytes: what NewWithMemory's memSize must be when the provided memory is fresh zeros rather than an existing image. Pinned by the sharedmem_ceiling fixture: a snapshot instance inherits the builder's BSS write, a data-segment-image instance must not. Co-Authored-By: Claude Fable 5 <[email protected]> * Drop the dead make-expression path for shared memories Review feedback on the constructor emission: the make() call expression was built for every memory and then discarded whenever the shared branch (AllocSharedMemory) or the caller-memory branch replaced it, and the shared branch assigned a capArg nothing consumes. Select the memory expression in one switch instead — each variant builds only what it emits. Co-Authored-By: Claude Fable 5 <[email protected]> * Remove AllocSharedMemory: the in-place builders are the fix Review feedback: with the image tiers built in place, no default path allocates a discarded ceiling-length shared memory any more, and the paths the anonymous-mmap constructor change still claimed to protect do not hold up. The fallback engines exist precisely because mmap is unavailable or failing — an mmap-based allocator cannot be their safety net (its no-mmap variant was plain make anyway), and the debugging escape hatch is not a production path to engineer for. Embedders still building images through throwaway plain-constructor engines migrate to the in-place builders when their bundles roll forward. Generated constructors go back to make for shared memories, the capture tree no longer needs the aux files (nothing in generated constructors references them; the snippet harnesses keep them because embedder-level tests exercise the image API), and the off-heap constructor test goes away with the machinery it pinned. Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]>
246 lines
8.4 KiB
Go
246 lines
8.4 KiB
Go
package codegen_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/goccy/wasm2go/internal/codegen"
|
|
"github.com/goccy/wasm2go/internal/testfixture"
|
|
"github.com/goccy/wasm2go/internal/wasm"
|
|
"github.com/tetratelabs/wazero"
|
|
)
|
|
|
|
// TestWexportsBulkDispatch drives the bulk-export fixture through
|
|
// the single-file output path with the bulk-export-prefix knob. Each
|
|
// `w_<svc>_<mt>` export becomes a standalone Inv_<svc>_<mt> function
|
|
// that the linker can drop independently — the consolidated
|
|
// InvokeExport switch form has been retired.
|
|
func TestWexportsBulkDispatch(t *testing.T) {
|
|
mod := readFixture(t, "wexports.wasm")
|
|
var buf bytes.Buffer
|
|
res, err := codegen.Translate(&buf, mod, codegen.Options{
|
|
Package: "pkg",
|
|
OutputImportPath: "gentest/pkg",
|
|
BulkExportPrefix: "w_",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("translate: %v", err)
|
|
}
|
|
out := buf.String()
|
|
if !strings.Contains(out, "func Inv_") {
|
|
t.Errorf("output missing per-export Inv_<svc>_<mt> function:\n%s", out)
|
|
}
|
|
if strings.Contains(out, "func (m *Module) InvokeExport(") {
|
|
t.Errorf("consolidated InvokeExport method should be gone in the per-export layout")
|
|
}
|
|
assertSingleFileCompiles(t, out, res.Sidecars, res.AuxFiles, res.Files)
|
|
}
|
|
|
|
// TestNativeWASIDefault drives the wasi fixture through the auto-on
|
|
// native wasip1 implementation. DefaultWASI() must appear in the
|
|
// generated source and NewWithWASI() must be exposed.
|
|
func TestNativeWASIDefault(t *testing.T) {
|
|
mod := readFixture(t, "cg_wasi.wasm")
|
|
var buf bytes.Buffer
|
|
if _, err := codegen.Translate(&buf, mod, codegen.Options{
|
|
Package: "pkg",
|
|
OutputImportPath: "gentest/pkg",
|
|
}); err != nil {
|
|
t.Fatalf("translate: %v", err)
|
|
}
|
|
src := buf.String()
|
|
if !strings.Contains(src, "DefaultWASI") {
|
|
t.Errorf("native wasi output missing DefaultWASI(): %s", src)
|
|
}
|
|
if !strings.Contains(src, "NewWithWASI") {
|
|
t.Errorf("native wasi output missing NewWithWASI(): %s", src)
|
|
}
|
|
}
|
|
|
|
// TestNativeWASIReserve verifies the initial-linear-memory reservation API:
|
|
// the full-body constructor is NewWithWASIReserve(..., reserveBytes int), the
|
|
// memory make() takes a capacity argument so start-up grows are zero-copy
|
|
// reslices, and the plain NewWithWASI delegates with a default headroom.
|
|
func TestNativeWASIReserve(t *testing.T) {
|
|
mod := readFixture(t, "cg_wasi.wasm")
|
|
var buf bytes.Buffer
|
|
if _, err := codegen.Translate(&buf, mod, codegen.Options{
|
|
Package: "pkg",
|
|
OutputImportPath: "gentest/pkg",
|
|
}); err != nil {
|
|
t.Fatalf("translate: %v", err)
|
|
}
|
|
src := buf.String()
|
|
if !strings.Contains(src, "func NewWithWASIReserve(") {
|
|
t.Errorf("output missing NewWithWASIReserve(): %s", src)
|
|
}
|
|
if !strings.Contains(src, "reserveBytes int") {
|
|
t.Errorf("NewWithWASIReserve missing reserveBytes param: %s", src)
|
|
}
|
|
// 3-arg make: make([]byte, <min>, <cap>) — two commas before the close.
|
|
if !regexp.MustCompile(`make\(\[\]byte,[^,)]+,[^,)]+\)`).MatchString(src) {
|
|
t.Errorf("memory make() is not 3-arg (no capacity reservation): %s", src)
|
|
}
|
|
// Clamp so cap is never below len (make would panic otherwise).
|
|
if !strings.Contains(src, "__memcap") {
|
|
t.Errorf("missing reserveBytes->cap clamp: %s", src)
|
|
}
|
|
// NewWithWASI must delegate to NewWithWASIReserve with a default headroom.
|
|
if !regexp.MustCompile(`return NewWithWASIReserve\(.*,\s*\d+\)`).MatchString(src) {
|
|
t.Errorf("NewWithWASI does not delegate to NewWithWASIReserve with a default cap: %s", src)
|
|
}
|
|
}
|
|
|
|
// TestBulkMemoryRuntime verifies the 0xFC instruction family: saturating
|
|
// float truncation, memory.copy and memory.fill. Each result is checked
|
|
// against wazero.
|
|
func TestBulkMemoryRuntime(t *testing.T) {
|
|
bin := testfixture.Wasm(t, "cg_bulkmem")
|
|
mod, err := wasm.Parse(bytes.NewReader(bin))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
r := wazero.NewRuntime(ctx)
|
|
t.Cleanup(func() {
|
|
if err := r.Close(ctx); err != nil {
|
|
t.Errorf("wazero runtime close: %v", err)
|
|
}
|
|
})
|
|
wmod, err := r.Instantiate(ctx, bin)
|
|
if err != nil {
|
|
t.Fatalf("wazero instantiate: %v", err)
|
|
}
|
|
|
|
// All integer-result, integer-arg exports — keeps the generated main
|
|
// trivial. Float-arg saturating truncs are exercised by the compile
|
|
// matrix (TestMatrixSingleFile); here we drive the integer-arg ones.
|
|
type tc struct {
|
|
export string
|
|
args []uint64
|
|
}
|
|
cases := []tc{
|
|
{"mem_copy", []uint64{20, 0, 5}},
|
|
{"mem_copy", []uint64{30, 2, 3}},
|
|
{"mem_fill", []uint64{40, 0x5a, 8}},
|
|
{"mem_fill", []uint64{50, 0xff, 1}},
|
|
{"load16_s", []uint64{0}},
|
|
{"load64", []uint64{0}},
|
|
}
|
|
|
|
var mainSB strings.Builder
|
|
mainSB.WriteString("package main\n\nimport (\n\t\"fmt\"\n\t\"gentest/pkg\"\n)\n\nfunc main() {\n\tm := pkg.New()\n")
|
|
var want []string
|
|
for _, c := range cases {
|
|
res, err := wmod.ExportedFunction(c.export).Call(ctx, c.args...)
|
|
if err != nil {
|
|
t.Fatalf("wazero %s%v: %v", c.export, c.args, err)
|
|
}
|
|
var argList []string
|
|
for _, a := range c.args {
|
|
argList = append(argList, fmt.Sprintf("int32(%d)", int32(a)))
|
|
}
|
|
var v int64
|
|
if c.export == "load64" {
|
|
v = int64(res[0])
|
|
} else {
|
|
v = int64(int32(res[0]))
|
|
}
|
|
want = append(want, fmt.Sprintf("%s=%d", c.export, v))
|
|
fmt.Fprintf(&mainSB, "\tfmt.Printf(\"%s=%%d\\n\", m.%s(%s))\n",
|
|
c.export, codegen.ExportMethodName(c.export), strings.Join(argList, ", "))
|
|
}
|
|
mainSB.WriteString("}\n")
|
|
|
|
var buf bytes.Buffer
|
|
res, err := codegen.Translate(&buf, mod, codegen.Options{Package: "pkg", OutputImportPath: "gentest/pkg"})
|
|
if err != nil {
|
|
t.Fatalf("translate: %v", err)
|
|
}
|
|
out := strings.TrimSpace(runGoSnippet(t, buf.String(), mainSB.String(), res.Sidecars, filesWithAux(res)))
|
|
gotLines := strings.Split(out, "\n")
|
|
if len(gotLines) != len(want) {
|
|
t.Fatalf("got %d lines want %d\n%s", len(gotLines), len(want), out)
|
|
}
|
|
for i := range want {
|
|
if strings.TrimSpace(gotLines[i]) != want[i] {
|
|
t.Errorf("line %d got %q want %q", i, gotLines[i], want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestDispatchIfRuntime drives cg_dispatchif — a br_table dispatcher whose
|
|
// case bodies wrap their shared-epilogue branch inside an if/else — through
|
|
// wasm2go and checks every (selector, x) result against wazero. This
|
|
// exercises the compound-statement exit-goto inlining in dispatch_split.
|
|
func TestDispatchIfRuntime(t *testing.T) {
|
|
bin := testfixture.Wasm(t, "cg_dispatchif")
|
|
mod, err := wasm.Parse(bytes.NewReader(bin))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
r := wazero.NewRuntime(ctx)
|
|
t.Cleanup(func() {
|
|
if err := r.Close(ctx); err != nil {
|
|
t.Errorf("wazero runtime close: %v", err)
|
|
}
|
|
})
|
|
wmod, err := r.Instantiate(ctx, bin)
|
|
if err != nil {
|
|
t.Fatalf("wazero instantiate: %v", err)
|
|
}
|
|
|
|
type call struct{ sel, x int32 }
|
|
calls := []call{
|
|
{0, 1}, {0, -1}, {5, 1}, {5, 0}, {17, 7}, {35, -3}, {40, 1}, {99, 0},
|
|
}
|
|
var mainSB strings.Builder
|
|
mainSB.WriteString("package main\n\nimport (\n\t\"fmt\"\n\t\"gentest/pkg\"\n)\n\nfunc main() {\n\tm := pkg.New()\n")
|
|
var want []int32
|
|
for _, c := range calls {
|
|
if _, err := wmod.ExportedFunction("dispatch").Call(ctx, u32(c.sel), u32(c.x)); err != nil {
|
|
t.Fatalf("wazero dispatch(%d,%d): %v", c.sel, c.x, err)
|
|
}
|
|
o, err := wmod.ExportedFunction("get_out").Call(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want = append(want, int32(o[0]))
|
|
fmt.Fprintf(&mainSB, "\tm.Dispatch(int32(%d), int32(%d))\n\tfmt.Println(m.GetOut())\n", c.sel, c.x)
|
|
}
|
|
mainSB.WriteString("}\n")
|
|
|
|
var buf bytes.Buffer
|
|
res, err := codegen.Translate(&buf, mod, codegen.Options{Package: "pkg", OutputImportPath: "gentest/pkg"})
|
|
if err != nil {
|
|
t.Fatalf("translate: %v", err)
|
|
}
|
|
// NOTE: dispatch-split previously kicked in on the legacy compiler's
|
|
// `switch` emission for br_table. The SSA pipeline currently lowers
|
|
// br_table as a chain of equality-If blocks, so the splitter's
|
|
// pattern-match does not fire. The semantics of the dispatcher are
|
|
// still asserted below; a future change should either round-trip
|
|
// br_table through a BlockSwitch SSA op or extend matchDispatcher
|
|
// to recognise the if-chain shape, at which point this assertion
|
|
// can be restored.
|
|
out := strings.TrimSpace(runGoSnippet(t, buf.String(), mainSB.String(), res.Sidecars, filesWithAux(res)))
|
|
gotLines := strings.Fields(out)
|
|
if len(gotLines) != len(want) {
|
|
t.Fatalf("got %d lines want %d\n%s", len(gotLines), len(want), out)
|
|
}
|
|
for i, c := range calls {
|
|
var g int32
|
|
if _, err := fmt.Sscanf(gotLines[i], "%d", &g); err != nil {
|
|
t.Fatalf("Sscanf %q: %v", gotLines[i], err)
|
|
}
|
|
if g != want[i] {
|
|
t.Errorf("dispatch(%d,%d): got %d want %d", c.sel, c.x, g, want[i])
|
|
}
|
|
}
|
|
}
|