Skip to content

[libc] Internal getrandom implementation #144427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions libc/src/__support/HashTable/randomness.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
#if defined(LIBC_HASHTABLE_USE_GETRANDOM)
#include "src/__support/OSUtil/linux/getrandom.h"
#include "src/__support/libc_errno.h"
#include "src/sys/random/getrandom.h"
#endif

namespace LIBC_NAMESPACE_DECL {
Expand All @@ -39,14 +39,14 @@ LIBC_INLINE uint64_t next_random_seed() {
size_t count = sizeof(entropy);
uint8_t *buffer = reinterpret_cast<uint8_t *>(entropy);
while (count > 0) {
ssize_t len = getrandom(buffer, count, 0);
if (len == -1) {
if (libc_errno == ENOSYS)
auto len = internal::getrandom(buffer, count, 0);
if (len.error()) {
if (len.error() == ENOSYS)
break;
continue;
}
count -= len;
buffer += len;
count -= len.value();
buffer += len.value();
}
libc_errno = errno_backup;
#endif
Expand Down
13 changes: 13 additions & 0 deletions libc/src/__support/OSUtil/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ add_object_library(
libc.include.sys_syscall
)

add_header_library(
getrandom
HDRS
getrandom.h
DEPENDS
libc.src.__support.OSUtil.linux.syscall
libc.src.__support.common
libc.src.__support.error_or
libc.src.__support.macros.config.h
libc.hdr.types.ssize_t
libc.include.sys_syscall
)

add_header_library(
vdso_sym
HDRS
Expand Down
35 changes: 35 additions & 0 deletions libc/src/__support/OSUtil/linux/getrandom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===------------ Implementation of getrandom function ----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H

#include "hdr/types/ssize_t.h"
#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
#include <sys/syscall.h> // For syscall numbers

namespace LIBC_NAMESPACE_DECL {
namespace internal {

static inline ErrorOr<ssize_t> getrandom(void *buf, size_t buflen,
unsigned int flags) {
ssize_t ret =
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_getrandom, buf, buflen, flags);
if (ret < 0) {
return Error(-static_cast<int>(ret));
}
return ret;
}

} // namespace internal
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H
12 changes: 6 additions & 6 deletions libc/src/sys/random/linux/getrandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

#include "src/sys/random/getrandom.h"

#include "src/__support/OSUtil/linux/getrandom.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"

#include "src/__support/error_or.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include <sys/syscall.h> // For syscall numbers.
Expand All @@ -19,13 +20,12 @@ namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(ssize_t, getrandom,
(void *buf, size_t buflen, unsigned int flags)) {
ssize_t ret =
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_getrandom, buf, buflen, flags);
if (ret < 0) {
libc_errno = static_cast<int>(-ret);
auto rand = internal::getrandom(buf, buflen, flags);
if (rand.error()) {
libc_errno = static_cast<int>(rand.error());
return -1;
}
return ret;
return rand.value();
}

} // namespace LIBC_NAMESPACE_DECL
Loading