Skip to content

Commit

Permalink
unix: use vDSO for getrandom() on linux
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
zx2c4 committed Sep 28, 2024
1 parent 48aad76 commit 981de40
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
21 changes: 20 additions & 1 deletion unix/syscall_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,26 @@ func Getpgrp() (pid int) {
//sysnb Getpid() (pid int)
//sysnb Getppid() (ppid int)
//sys Getpriority(which int, who int) (prio int, err error)
//sys Getrandom(buf []byte, flags int) (n int, err error)

func Getrandom(buf []byte, flags int) (n int, err error) {
vdsoRet, supported := vgetrandom(buf, uint32(flags))
if supported {
if vdsoRet < 0 {
return 0, errnoErr(syscall.Errno(-vdsoRet))
}
return vdsoRet, nil
}
var p *byte
if len(buf) > 0 {
p = &buf[0]
}
r, _, e := Syscall(SYS_GETRANDOM, uintptr(unsafe.Pointer(p)), uintptr(len(buf)), uintptr(flags))
if e != 0 {
return 0, errnoErr(e)
}
return int(r), nil
}

//sysnb Getrusage(who int, rusage *Rusage) (err error)
//sysnb Getsid(pid int) (sid int, err error)
//sysnb Gettid() (tid int)
Expand Down
12 changes: 12 additions & 0 deletions unix/vgetrandom_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 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.

//go:build linux && go1.24

package unix

import _ "unsafe"

//go:linkname vgetrandom runtime.vgetrandom
func vgetrandom(p []byte, flags uint32) (ret int, supported bool)
11 changes: 11 additions & 0 deletions unix/vgetrandom_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2024 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.

//go:build !linux || !go1.24

package unix

func vgetrandom(p []byte, flags uint32) (ret int, supported bool) {
return -1, false
}
17 changes: 0 additions & 17 deletions unix/zsyscall_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 981de40

Please sign in to comment.