Skip to content

Commit 981de40

Browse files
committed
unix: use vDSO for getrandom() on linux
With CL 614835 adding support in the runtime for calling into the getrandom() vDSO function, wire up x/sys/unix's Getrandom() function to it, so that callers can benefit from the increased speed and shared vDSO state with the runtime. Updates golang/go#69577. Change-Id: I17734409982c51bb984a6337f4ffa8f60414ebee Reviewed-on: https://go-review.googlesource.com/c/sys/+/615335 Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 48aad76 commit 981de40

4 files changed

+43
-18
lines changed

unix/syscall_linux.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,26 @@ func Getpgrp() (pid int) {
20012001
//sysnb Getpid() (pid int)
20022002
//sysnb Getppid() (ppid int)
20032003
//sys Getpriority(which int, who int) (prio int, err error)
2004-
//sys Getrandom(buf []byte, flags int) (n int, err error)
2004+
2005+
func Getrandom(buf []byte, flags int) (n int, err error) {
2006+
vdsoRet, supported := vgetrandom(buf, uint32(flags))
2007+
if supported {
2008+
if vdsoRet < 0 {
2009+
return 0, errnoErr(syscall.Errno(-vdsoRet))
2010+
}
2011+
return vdsoRet, nil
2012+
}
2013+
var p *byte
2014+
if len(buf) > 0 {
2015+
p = &buf[0]
2016+
}
2017+
r, _, e := Syscall(SYS_GETRANDOM, uintptr(unsafe.Pointer(p)), uintptr(len(buf)), uintptr(flags))
2018+
if e != 0 {
2019+
return 0, errnoErr(e)
2020+
}
2021+
return int(r), nil
2022+
}
2023+
20052024
//sysnb Getrusage(who int, rusage *Rusage) (err error)
20062025
//sysnb Getsid(pid int) (sid int, err error)
20072026
//sysnb Gettid() (tid int)

unix/vgetrandom_linux.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux && go1.24
6+
7+
package unix
8+
9+
import _ "unsafe"
10+
11+
//go:linkname vgetrandom runtime.vgetrandom
12+
func vgetrandom(p []byte, flags uint32) (ret int, supported bool)

unix/vgetrandom_unsupported.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !linux || !go1.24
6+
7+
package unix
8+
9+
func vgetrandom(p []byte, flags uint32) (ret int, supported bool) {
10+
return -1, false
11+
}

unix/zsyscall_linux.go

-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)