ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.66k stars 2.53k forks source link

Support `vgetrandom` on Linux 6.11+ #21590

Open The-King-of-Toasters opened 3 weeks ago

The-King-of-Toasters commented 3 weeks ago

Linux 6.11 added getrandom to the vdso. The supported architectures are:

The API is:

ssize_t vgetrandom(void *buffer, size_t len, unsigned int flags,
                   void *opaque_state, size_t opaque_len);
// If buffer = len = flags = 0 && opaque_len = ~0, opaque_state is set to the struct:
struct vgetrandom_opaque_params {
    __u32 size_of_opaque_state;
    __u32 mmap_prot;
    __u32 mmap_flags;
    __u32 reserved[13];
};

Note that it isn't as simple as calling vgetrandom, callers need to pre-allocate memory to pass to the syscall.

Go recently implemented support in golang/go/issues/69577 (see https://go-review.googlesource.com/c/go/+/614835 for the patchset). Note that this impl allocates a pool of states per the number of CPUs, and uses a lock to fetch an unused state.

The-King-of-Toasters commented 3 weeks ago

Tagging @jedisct1 since this effects the std.crypto namespace.