[libc] Internal getrandom implementation (#144427)

Implemented an internal getrandom to avoid calls to the public one in
table.h

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
This commit is contained in:
sribee8
2025-06-18 10:56:57 -07:00
committed by GitHub
parent 835d3034fe
commit 6f4e4ea177
5 changed files with 63 additions and 17 deletions

View File

@@ -15,7 +15,8 @@ if (NOT ${getrandom_index} EQUAL -1)
message(STATUS "Using getrandom for hashtable randomness")
set(randomness_compile_flags -DLIBC_HASHTABLE_USE_GETRANDOM)
set(randomness_extra_depends
libc.src.sys.random.getrandom libc.src.errno.errno)
libc.src.__support.OSUtil.linux.getrandom
libc.hdr.errno_macros)
endif()

View File

@@ -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/libc_errno.h"
#include "src/sys/random/getrandom.h"
#include "hdr/errno_macros.h"
#include "src/__support/OSUtil/linux/getrandom.h"
#endif
namespace LIBC_NAMESPACE_DECL {
@@ -35,20 +35,18 @@ LIBC_INLINE uint64_t next_random_seed() {
entropy[0] = reinterpret_cast<uint64_t>(&entropy);
entropy[1] = reinterpret_cast<uint64_t>(&state);
#if defined(LIBC_HASHTABLE_USE_GETRANDOM)
int errno_backup = libc_errno;
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.has_value()) {
if (len.error() == ENOSYS)
break;
continue;
}
count -= len;
buffer += len;
count -= len.value();
buffer += len.value();
}
libc_errno = errno_backup;
#endif
state.update(&entropy, sizeof(entropy));
}

View File

@@ -24,6 +24,19 @@ add_object_library(
libc.include.sys_syscall
)
add_header_library(
getrandom
HDRS
getrandom.h
DEPENDS
libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.__support.error_or
libc.src.__support.macros.config
libc.hdr.types.ssize_t
libc.include.sys_syscall
)
add_header_library(
vdso_sym
HDRS

View File

@@ -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 {
LIBC_INLINE static 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

View File

@@ -8,24 +8,23 @@
#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.
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.has_value()) {
libc_errno = static_cast<int>(rand.error());
return -1;
}
return ret;
return rand.value();
}
} // namespace LIBC_NAMESPACE_DECL