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]>
191 lines
6.1 KiB
Go
191 lines
6.1 KiB
Go
package codegen_test
|
|
|
|
// Memory64 counterparts of the wasi-threads and single-agent atomics tests:
|
|
// the same guests with i64 addressing on a shared memory64, so every atomic
|
|
// routes through the _m64 helper family and the i64 start_arg spawn path.
|
|
// Like the wasm32 originals, the generated programs run under the race
|
|
// detector — a wasm thread is a goroutine, so unsynchronised access to the
|
|
// shared linear memory (above all a grow that relocated it) surfaces as a
|
|
// race, not a flake.
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/goccy/wasm2go/internal/codegen"
|
|
"github.com/goccy/wasm2go/internal/testfixture"
|
|
"github.com/goccy/wasm2go/internal/wasm"
|
|
)
|
|
|
|
func TestThreadsM64(t *testing.T) {
|
|
bin := testfixture.Wasm(t, "cg_threads_m64.wasm")
|
|
mod, err := wasm.Parse(bytes.NewReader(bin))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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)
|
|
}
|
|
|
|
main := `package main
|
|
|
|
import (
|
|
"fmt"
|
|
"gentest/pkg"
|
|
)
|
|
|
|
func main() {
|
|
// The only wasi import (thread-spawn) is goroutine-backed and internal,
|
|
// so the generated constructor takes no host parameters at all.
|
|
m := pkg.New()
|
|
// 8 agents, each +1 on the counter, joined through atomic wait/notify.
|
|
fmt.Printf("joined -> %d\n", m.SpawnAndJoin(8))
|
|
// Growing a shared memory must preserve both contents and pointers:
|
|
// 2 pages * 65536 + 0xbeef = 131072 + 48879.
|
|
fmt.Printf("grown -> %d\n", m.GrowShared())
|
|
}
|
|
`
|
|
got := runGoSnippetRaceDetector(t, buf.String(), main, res.Sidecars, filesWithAux(res))
|
|
want := "joined -> 8\ngrown -> 179951\n"
|
|
if got != want {
|
|
t.Errorf("output mismatch\n--want--\n%s\n--got--\n%s", want, got)
|
|
}
|
|
if strings.Contains(got, "DATA RACE") {
|
|
t.Errorf("race detected:\n%s", got)
|
|
}
|
|
}
|
|
|
|
func TestThreadsVisibilityM64(t *testing.T) {
|
|
// A plain store published by an atomic store must be visible to a
|
|
// goroutine-agent that acquires via an atomic load — on i64 addresses.
|
|
bin := testfixture.Wasm(t, "cg_threads_visibility_m64.wasm")
|
|
mod, err := wasm.Parse(bytes.NewReader(bin))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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)
|
|
}
|
|
main := "package main\n\nimport (\n\t\"fmt\"\n\t\"gentest/pkg\"\n)\n\nfunc main() {\n\tm := pkg.New()\n\tfmt.Println(m.Run())\n}\n"
|
|
got := runGoSnippetRaceDetector(t, buf.String(), main, res.Sidecars, filesWithAux(res))
|
|
if strings.TrimSpace(got) != "4242" {
|
|
t.Errorf("cross-thread plain-store visibility (memory64): got %q, want 4242", got)
|
|
}
|
|
}
|
|
|
|
// TestAtomicsM64 drives the single-agent atomics matrix on a shared memory64.
|
|
// The wasm32 twin (cg_atomics.wat) is diffed against wazero by the fixtures
|
|
// matrix; this mirror asserts the same deterministic values, which pins the
|
|
// _m64 helper routing without needing a memory64+threads reference engine.
|
|
func TestAtomicsM64(t *testing.T) {
|
|
bin := testfixture.Wasm(t, "cg_atomics_m64.wasm")
|
|
mod, err := wasm.Parse(bytes.NewReader(bin))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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)
|
|
}
|
|
main := `package main
|
|
|
|
import (
|
|
"fmt"
|
|
"gentest/pkg"
|
|
)
|
|
|
|
func main() {
|
|
m := pkg.New()
|
|
fmt.Println("rmw32", m.Rmw32())
|
|
fmt.Println("rmw64", m.Rmw64())
|
|
fmt.Println("xchg", m.Xchg())
|
|
fmt.Println("cmpxchg", m.Cmpxchg())
|
|
fmt.Println("subword", m.Subword())
|
|
fmt.Println("subword_cmpxchg", m.SubwordCmpxchg())
|
|
fmt.Println("wait_notify", m.WaitNotify())
|
|
fmt.Println("fenced_load", m.FencedLoad())
|
|
fmt.Println("store_neg", m.StoreNeg())
|
|
}
|
|
`
|
|
got := runGoSnippetRaceDetector(t, buf.String(), main, res.Sidecars, filesWithAux(res))
|
|
want := `rmw32 361
|
|
rmw64 8000000000
|
|
xchg 16
|
|
cmpxchg 6
|
|
subword 305441536
|
|
subword_cmpxchg 9
|
|
wait_notify 1
|
|
fenced_load 42
|
|
store_neg -1
|
|
`
|
|
if got != want {
|
|
t.Errorf("output mismatch\n--want--\n%s\n--got--\n%s", want, got)
|
|
}
|
|
}
|
|
|
|
// TestMem64UnsharedAtomics locks the regression where every 0xFE opcode on a
|
|
// memory64 module was rejected — atomics on a plain (unshared) memory64 are
|
|
// legal wasm and must both transpile and run.
|
|
func TestMem64UnsharedAtomics(t *testing.T) {
|
|
bin := testfixture.Wasm(t, "cg_mem64_atomics_unshared.wasm")
|
|
mod, err := wasm.Parse(bytes.NewReader(bin))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var buf bytes.Buffer
|
|
res, err := codegen.Translate(&buf, mod, codegen.Options{Package: "pkg", OutputImportPath: "gentest/pkg"})
|
|
if err != nil {
|
|
t.Fatalf("translate (unshared memory64 with atomics): %v", err)
|
|
}
|
|
main := `package main
|
|
|
|
import (
|
|
"fmt"
|
|
"gentest/pkg"
|
|
)
|
|
|
|
func main() {
|
|
m := pkg.New()
|
|
fmt.Println(m.Bump())
|
|
// A wait on an unshared memory never parks: not-equal result.
|
|
fmt.Println(m.WaitUnshared())
|
|
}
|
|
`
|
|
got := runGoSnippetRaceDetector(t, buf.String(), main, res.Sidecars, filesWithAux(res))
|
|
if want := "42\n1\n"; got != want {
|
|
t.Errorf("unshared memory64 atomics: got %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
// TestSharedMem64RequiresMax: a shared memory64 with no declared maximum has
|
|
// no reservable growth ceiling (the fallback would be the mem64 hard cap),
|
|
// so translation must fail with a clear error. The spec requires shared
|
|
// memories to declare a maximum, but the parser tolerates its absence, so
|
|
// the binary is hand-assembled (wat2wasm refuses to emit it).
|
|
func TestSharedMem64RequiresMax(t *testing.T) {
|
|
bin := []byte{
|
|
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, // header
|
|
0x05, 0x03, // memory section, 3 bytes
|
|
0x01, // one memory
|
|
0x06, 0x01, // limits: shared|mem64 flags, min 1, NO max
|
|
}
|
|
mod, err := wasm.Parse(bytes.NewReader(bin))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var buf bytes.Buffer
|
|
_, err = codegen.Translate(&buf, mod, codegen.Options{Package: "pkg", OutputImportPath: "gentest/pkg"})
|
|
if err == nil {
|
|
t.Fatal("shared memory64 without a maximum translated; want an error")
|
|
}
|
|
if !strings.Contains(err.Error(), "shared memory64 requires a declared maximum") {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
}
|