runtime: add runtime.rand function

This function is needed for Go 1.22, and is used from various packages
like math/rand.

When there is no random number generator available, it falls back to a
static sequence of numbers. I think this is fine, because as far as I
can see it is only used for non-cryptographic needs.
This commit is contained in:
Ayke van Laethem
2024-01-18 21:27:08 +01:00
parent 204659bdcd
commit e9003e2deb
11 changed files with 102 additions and 1 deletions
+13
View File
@@ -120,3 +120,16 @@ func libc_getpagesize() int
func syscall_Getpagesize() int {
return libc_getpagesize()
}
func hardwareRand() (n uint64, ok bool) {
read := libc_getrandom(unsafe.Pointer(&n), 8, 0)
if read != 8 {
return 0, false
}
return n, true
}
// ssize_t getrandom(void buf[.buflen], size_t buflen, unsigned int flags);
//
//export getrandom
func libc_getrandom(buf unsafe.Pointer, buflen uintptr, flags uint32) uint32