Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
14 changes: 6 additions & 8 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 @@ -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));
}
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 {

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
13 changes: 6 additions & 7 deletions libc/src/sys/random/linux/getrandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading