Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 5 additions & 5 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) {
auto len = internal::getrandom(buffer, count, 0);
if (!len.has_value()) {
if (libc_errno == ENOSYS)
break;
continue;
}
count -= len;
buffer += len;
count -= len.value();
buffer += len.value();
}
libc_errno = errno_backup;
#endif
Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/OSUtil/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_object_library(
HDRS
io.h
syscall.h
getrandom.h
DEPENDS
.${LIBC_TARGET_ARCHITECTURE}.linux_${LIBC_TARGET_ARCHITECTURE}_util
libc.src.__support.common
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
14 changes: 7 additions & 7 deletions libc/src/sys/random/linux/getrandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +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"
Expand All @@ -19,13 +21,11 @@ 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);
return -1;
}
return ret;
auto rand = internal::getrandom(buf, buflen, flags);
if (rand.has_value())
return rand.value();
libc_errno = static_cast<int>(-(rand.error()));
return -1;
}

} // namespace LIBC_NAMESPACE_DECL
Loading