internal/syscall/unix: implement GetRandom for WASI targets

Go 1.26's crypto/internal/sysrand uses internal/syscall/unix.GetRandom
on Linux-like targets (including wasip2 which sets GOOS=linux). The
existing stub panicked with 'todo: unix.GetRandom', causing crypto/ecdsa
tests to fail on wasip2.

Implement GetRandom on WASI targets (wasip1, wasip2) by calling the
arc4random_buf libc function that TinyGo's runtime already provides.
For other TinyGo targets, return ENOSYS so sysrand can fall back to
/dev/urandom.
This commit is contained in:
deadprogram
2026-04-10 14:08:43 +02:00
committed by Ron Evans
parent c1f48fc79a
commit 59feac28b9
2 changed files with 32 additions and 1 deletions
+7 -1
View File
@@ -1,5 +1,9 @@
//go:build !tinygo.wasm
package unix
import "syscall"
type GetRandomFlag uintptr
const (
@@ -8,5 +12,7 @@ const (
)
func GetRandom(p []byte, flags GetRandomFlag) (n int, err error) {
panic("todo: unix.GetRandom")
// Not supported on most TinyGo targets.
// On real Linux the sysrand package will fall back to /dev/urandom.
return 0, syscall.ENOSYS
}
@@ -0,0 +1,25 @@
//go:build tinygo.wasm
package unix
import "unsafe"
type GetRandomFlag uintptr
const (
GRND_NONBLOCK GetRandomFlag = 0x0001
GRND_RANDOM GetRandomFlag = 0x0002
)
func GetRandom(p []byte, flags GetRandomFlag) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
libc_arc4random_buf(unsafe.Pointer(&p[0]), uint(len(p)))
return len(p), nil
}
// void arc4random_buf(void *buf, size_t buflen);
//
//export arc4random_buf
func libc_arc4random_buf(buf unsafe.Pointer, buflen uint)