From af6193d9800f1e2964580fe023d81cb760140141 Mon Sep 17 00:00:00 2001 From: Matthew Suozzo Date: Fri, 6 Nov 2026 15:17:17 +0000 Subject: [PATCH 1/2] Widen 32-bit linear memory addresses to uint64 in popAddr under -unsafe. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Generated 32-bit memory accesses use `uint32(addr)` at offset 0 and `uint64(uint32(addr))+offset` otherwise, so Go's type-keyed SSA cannot share zero-extensions or bounds-check proofs across uses of `addr`. Under `-unsafe`, where load and store helpers inline into large callers, always widen 32-bit addresses to `uint64(uint32(addr))`. Safe mode keeps `uint32(addr)` at offset 0: its helpers are not inlined into large callers, so widening shares nothing across the call and only adds zero-extensions and spills in the caller. The safe-mode carve-out can be removed once the safe helpers inline, see #69. Benchmarks on `go-sqlite3` (`wasm2go -unsafe`): goos: linux goarch: amd64 pkg: github.com/ncruces/go-sqlite3/driver cpu: AMD EPYC 7B13 │ before │ after │ │ sec/op │ sec/op vs base │ _loop 543.5m ± 2% 537.3m ± 2% -1.14% (p=0.001 n=8) pkg: github.com/ncruces/go-sqlite3/ext/stats │ before │ after │ │ sec/op │ sec/op vs base │ _average 84.39n ± 1% 81.96n ± 1% -2.88% (p=0.003 n=8) _variance 99.65n ± 1% 98.34n ± 8% ~ (p=0.129 n=8) geomean 91.70n 89.78n -2.10% pkg: github.com/ncruces/go-sqlite3/vfs/tests/speedtest1 │ before │ after │ │ sec/op │ sec/op vs base │ _speedtest1 69.11m ± 1% 68.58m ± 2% -0.77% (p=0.050 n=8) --- fncompiler.go | 13 ++++++++----- testdata/regression/shared_export/shared_export.go | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/fncompiler.go b/fncompiler.go index 37e12ed7..54e24648 100644 --- a/fncompiler.go +++ b/fncompiler.go @@ -301,16 +301,19 @@ func (fn *funcCompiler) popAddr(offset uint64) ast.Expr { if fn.memory.is64 { fn.helpers.add("p64") addr = convert(addr, "p64") - } else { + } else if offset == 0 && !*unsafe { + // Keep zero-offset safe addresses uint32: safe helpers are not inlined + // into large callers, so the uint64 used for -unsafe and nonzero offsets + // cannot share bounds-check proofs across calls. Remove this branch once + // safe helpers inline (#69). addr = convert(addr, "uint32") + } else { + // Widen so inlined accesses share a type for bounds-check proofs. + addr = convert(addr, "uint32", "uint64") } if offset == 0 { return addr } - - if !fn.memory.is64 { - addr = convert(addr, "uint64") - } return &ast.BinaryExpr{Op: token.ADD, X: addr, Y: uintLit(offset)} } diff --git a/testdata/regression/shared_export/shared_export.go b/testdata/regression/shared_export/shared_export.go index dc288461..395aa915 100644 --- a/testdata/regression/shared_export/shared_export.go +++ b/testdata/regression/shared_export/shared_export.go @@ -45,7 +45,7 @@ func (m *wasmMemory) Waiters() *sync.Map { return m.waiters } func (m *Module) Xwait0() int32 { - t0 := atomic_wait32(m.memory, uint32(i32(0)), uint32(i32(0)), i64(0), m.waiters) + t0 := atomic_wait32(m.memory, uint64(uint32(i32(0))), uint32(i32(0)), i64(0), m.waiters) return t0 } func (m *Module) Xmemory() Memory { From b8c89cf0fd445a3a1c6121db762b94017e223f3e Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 9 Nov 2026 15:24:10 +0000 Subject: [PATCH 2/2] Invert condition --- fncompiler.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/fncompiler.go b/fncompiler.go index 54e24648..c02d9895 100644 --- a/fncompiler.go +++ b/fncompiler.go @@ -301,15 +301,13 @@ func (fn *funcCompiler) popAddr(offset uint64) ast.Expr { if fn.memory.is64 { fn.helpers.add("p64") addr = convert(addr, "p64") - } else if offset == 0 && !*unsafe { - // Keep zero-offset safe addresses uint32: safe helpers are not inlined - // into large callers, so the uint64 used for -unsafe and nonzero offsets - // cannot share bounds-check proofs across calls. Remove this branch once - // safe helpers inline (#69). - addr = convert(addr, "uint32") - } else { + } else if *unsafe || offset != 0 { // Widen so inlined accesses share a type for bounds-check proofs. addr = convert(addr, "uint32", "uint64") + } else { + // Keep safe zero-offset addresses uint32. + // Remove this branch once safe helpers inline (#69). + addr = convert(addr, "uint32") } if offset == 0 { return addr