Commit 9f5405cb10 for golang
commit 9f5405cb10388d27afa4b6c774bca19017a64c3a
Author: Zxilly <zhouxinyu1001@gmail.com>
Date: Mon Sep 7 20:00:36 2026 +0800
runtime, syscall: use typed WASI pointers on wasm32
Use *byte in WASI I/O vectors and argument and environment pointer
arrays on wasm32 so the GC can track the referenced buffers. Continue
using uint32 on wasm, whose Go pointers are wider than WASI pointers.
Rebased and reviewed and uploaded by drchase@google.com .
Updates #63131
Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64-longtest,gotip-linux-amd64-longtest,gotip-linux-amd64-longtest-race,gotip-wasip1-wasm_wazero
Change-Id: Iaf2e259844ade8b1bf20b38c757d556c1577892a
Reviewed-on: https://go-review.googlesource.com/c/go/+/836656
TryBot-Bypass: David Chase <drchase@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
diff --git a/src/crypto/rand/rand.go b/src/crypto/rand/rand.go
index b6e17dc3ec..5185117c00 100644
--- a/src/crypto/rand/rand.go
+++ b/src/crypto/rand/rand.go
@@ -28,7 +28,7 @@ import (
// - On NetBSD, Reader uses the kern.arandom sysctl.
// - On Windows, Reader uses the ProcessPrng API.
// - On js/wasm, Reader uses the Web Crypto API.
-// - On wasip1/wasm, Reader uses random_get.
+// - On wasip1, Reader uses random_get.
//
// In FIPS 140-3 mode, the output passes through an SP 800-90A Rev. 1
// Deterministric Random Bit Generator (DRBG).
diff --git a/src/net/net_fake.go b/src/net/net_fake.go
index cc35d90a26..5b9a503d86 100644
--- a/src/net/net_fake.go
+++ b/src/net/net_fake.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// Fake networking for js/wasm and wasip1/wasm.
+// Fake networking for js/wasm and wasip1.
// It is intended to allow tests of other package to pass.
//go:build js || wasip1
diff --git a/src/os/wait_unimp.go b/src/os/wait_unimp.go
index 810e35da63..2547333dfc 100644
--- a/src/os/wait_unimp.go
+++ b/src/os/wait_unimp.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// aix, darwin, js/wasm, openbsd, solaris and wasip1/wasm don't implement
+// aix, darwin, js/wasm, openbsd, solaris and wasip1 don't implement
// waitid/wait6.
//go:build aix || darwin || (js && wasm) || openbsd || solaris || wasip1
diff --git a/src/runtime/os_wasip1.go b/src/runtime/os_wasip1.go
index 34f1dd6da0..3a3cda302c 100644
--- a/src/runtime/os_wasip1.go
+++ b/src/runtime/os_wasip1.go
@@ -11,15 +11,6 @@ import (
"unsafe"
)
-// GOARCH=wasm currently has 64 bits pointers, but the WebAssembly host expects
-// pointers to be 32 bits so we use this type alias to represent pointers in
-// structs and arrays passed as arguments to WASI functions.
-//
-// Note that the use of an integer type prevents the compiler from tracking
-// pointers passed to WASI functions, so we must use KeepAlive to explicitly
-// retain the objects that could otherwise be reclaimed by the GC.
-type uintptr32 = uint32
-
// https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-size-u32
type size = uint32
@@ -42,7 +33,8 @@ const (
// https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-iovec-record
type iovec struct {
- buf uintptr32
+ _ structs.HostLayout
+ buf wasiPtr
bufLen size
}
@@ -51,7 +43,7 @@ func exit(code int32)
//go:wasmimport wasi_snapshot_preview1 args_get
//go:noescape
-func args_get(argv *uintptr32, argvBuf *byte) errno
+func args_get(argv *wasiPtr, argvBuf *byte) errno
//go:wasmimport wasi_snapshot_preview1 args_sizes_get
//go:noescape
@@ -63,7 +55,7 @@ func clock_time_get(clock_id clockid, precision timestamp, time *timestamp) errn
//go:wasmimport wasi_snapshot_preview1 environ_get
//go:noescape
-func environ_get(environ *uintptr32, environBuf *byte) errno
+func environ_get(environ *wasiPtr, environBuf *byte) errno
//go:wasmimport wasi_snapshot_preview1 environ_sizes_get
//go:noescape
@@ -71,7 +63,7 @@ func environ_sizes_get(environCount, environBufLen *size) errno
//go:wasmimport wasi_snapshot_preview1 fd_write
//go:noescape
-func fd_write(fd int32, iovs unsafe.Pointer, iovsLen size, nwritten *size) errno
+func fd_write(fd int32, iovs *iovec, iovsLen size, nwritten *size) errno
//go:wasmimport wasi_snapshot_preview1 random_get
//go:noescape
@@ -160,11 +152,11 @@ func poll_oneoff(in *subscription, out *event, nsubscriptions size, nevents *siz
func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
iov := iovec{
- buf: uintptr32(uintptr(p)),
+ buf: makeWASIPtr((*byte)(p)),
bufLen: size(n),
}
var nwritten size
- if fd_write(int32(fd), unsafe.Pointer(&iov), 1, &nwritten) != 0 {
+ if fd_write(int32(fd), &iov, 1, &nwritten) != 0 {
throw("fd_write failed")
}
return int32(nwritten)
@@ -196,6 +188,12 @@ func readRandom(r []byte) int {
}
func goenvs() {
+ // On wasm32, argv and environ are []*byte slices, and args_get/environ_get
+ // write Go pointers into them from the WASI host without Go write barriers.
+ // This is safe here because goenvs runs during schedinit before GC is
+ // enabled and the slices are freshly zeroed; do not copy this pattern into
+ // post-startup WASI calls.
+
// arguments
var argc size
var argvBufLen size
@@ -205,14 +203,14 @@ func goenvs() {
argslice = make([]string, argc)
if argc > 0 {
- argv := make([]uintptr32, argc)
+ argv := make([]wasiPtr, argc)
argvBuf := make([]byte, argvBufLen)
if args_get(&argv[0], &argvBuf[0]) != 0 {
throw("args_get failed")
}
for i := range argslice {
- start := argv[i] - uintptr32(uintptr(unsafe.Pointer(&argvBuf[0])))
+ start := wasiPtrOffset(argv[i], &argvBuf[0])
end := start
for argvBuf[end] != 0 {
end++
@@ -230,14 +228,14 @@ func goenvs() {
envs = make([]string, environCount)
if environCount > 0 {
- environ := make([]uintptr32, environCount)
+ environ := make([]wasiPtr, environCount)
environBuf := make([]byte, environBufLen)
if environ_get(&environ[0], &environBuf[0]) != 0 {
throw("environ_get failed")
}
for i := range envs {
- start := environ[i] - uintptr32(uintptr(unsafe.Pointer(&environBuf[0])))
+ start := wasiPtrOffset(environ[i], &environBuf[0])
end := start
for environBuf[end] != 0 {
end++
diff --git a/src/runtime/os_wasip1_wasm.go b/src/runtime/os_wasip1_wasm.go
new file mode 100644
index 0000000000..fc654eb71e
--- /dev/null
+++ b/src/runtime/os_wasip1_wasm.go
@@ -0,0 +1,21 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime
+
+import "unsafe"
+
+// wasiPtr represents a 32-bit host pointer on wasm, where Go pointers are 64 bits.
+// Because it is an integer type untracked by the GC, callers in shared WASI code
+// must still use KeepAlive to retain referenced buffers across host calls.
+type wasiPtr = uint32
+
+//go:nosplit
+func makeWASIPtr(p *byte) wasiPtr {
+ return uint32(uintptr(unsafe.Pointer(p)))
+}
+
+func wasiPtrOffset(p wasiPtr, base *byte) uint32 {
+ return p - uint32(uintptr(unsafe.Pointer(base)))
+}
diff --git a/src/runtime/os_wasip1_wasm32.go b/src/runtime/os_wasip1_wasm32.go
new file mode 100644
index 0000000000..778e9b36ca
--- /dev/null
+++ b/src/runtime/os_wasip1_wasm32.go
@@ -0,0 +1,19 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime
+
+import "unsafe"
+
+// wasiPtr is a Go pointer so the GC can track buffers referenced by WASI data.
+type wasiPtr = *byte
+
+//go:nosplit
+func makeWASIPtr(p *byte) wasiPtr {
+ return p
+}
+
+func wasiPtrOffset(p wasiPtr, base *byte) uint32 {
+ return uint32(uintptr(unsafe.Pointer(p)) - uintptr(unsafe.Pointer(base)))
+}
diff --git a/src/syscall/fs_wasip1.go b/src/syscall/fs_wasip1.go
index 2be32b2413..2835f0e434 100644
--- a/src/syscall/fs_wasip1.go
+++ b/src/syscall/fs_wasip1.go
@@ -27,7 +27,6 @@ func init() {
SetNonblock(2, true)
}
-type uintptr32 = uint32
type size = uint32
type fdflags = uint32
type filesize = uint64
@@ -42,7 +41,7 @@ type fstflags = uint32
type iovec struct {
_ structs.HostLayout
- buf uintptr32
+ buf wasiPtr
bufLen size
}
@@ -892,7 +891,7 @@ func Fsync(fd int) error {
func makeIOVec(b []byte) *iovec {
return &iovec{
- buf: uintptr32(uintptr(unsafe.Pointer(unsafe.SliceData(b)))),
+ buf: makeWASIPtr(unsafe.SliceData(b)),
bufLen: size(len(b)),
}
}
diff --git a/src/syscall/fs_wasip1_wasm.go b/src/syscall/fs_wasip1_wasm.go
new file mode 100644
index 0000000000..dc11ab63d3
--- /dev/null
+++ b/src/syscall/fs_wasip1_wasm.go
@@ -0,0 +1,16 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package syscall
+
+import "unsafe"
+
+// wasiPtr represents a 32-bit host pointer on wasm, where Go pointers are 64 bits.
+// Because it is an integer type untracked by the GC, callers in shared WASI code
+// must still use KeepAlive to retain referenced buffers across host calls.
+type wasiPtr = uint32
+
+func makeWASIPtr(p *byte) wasiPtr {
+ return uint32(uintptr(unsafe.Pointer(p)))
+}
diff --git a/src/syscall/fs_wasip1_wasm32.go b/src/syscall/fs_wasip1_wasm32.go
new file mode 100644
index 0000000000..6999b5b10d
--- /dev/null
+++ b/src/syscall/fs_wasip1_wasm32.go
@@ -0,0 +1,12 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package syscall
+
+// wasiPtr is a Go pointer so the GC can track buffers referenced by WASI data.
+type wasiPtr = *byte
+
+func makeWASIPtr(p *byte) wasiPtr {
+ return p
+}
diff --git a/src/syscall/net_fake.go b/src/syscall/net_fake.go
index 549f2bece0..9e981c4f1e 100644
--- a/src/syscall/net_fake.go
+++ b/src/syscall/net_fake.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// Fake networking for js/wasm and wasip1/wasm.
+// Fake networking for js/wasm and wasip1.
//go:build js || wasip1