Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 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/getrandom.h"
#include "src/__support/libc_errno.h"
#include "src/sys/random/getrandom.h"
#endif

namespace LIBC_NAMESPACE_DECL {
Expand All @@ -39,7 +39,7 @@ 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);
ssize_t len = internal::getrandom(buffer, count, 0);
if (len == -1) {
if (libc_errno == ENOSYS)
break;
Expand Down
23 changes: 23 additions & 0 deletions libc/src/__support/OSUtil/getrandom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===------------ 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 "src/__support/macros/config.h"
#include <sys/random.h>

namespace LIBC_NAMESPACE_DECL {
namespace internal {

ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);

} // namespace internal
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H
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 @@ -9,6 +9,7 @@ add_object_library(
SRCS
exit.cpp
fcntl.cpp
getrandom.cpp
HDRS
io.h
syscall.h
Expand Down
30 changes: 30 additions & 0 deletions libc/src/__support/OSUtil/linux/getrandom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===------------ Linux implementation of getrandom -------------*- 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
//
//===----------------------------------------------------------------------===//

#include "src/__support/OSUtil/getrandom.h"
#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {
namespace internal {

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;
}

} // namespace internal
} // namespace LIBC_NAMESPACE_DECL
9 changes: 2 additions & 7 deletions libc/src/sys/random/linux/getrandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

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

Expand All @@ -19,13 +20,7 @@ 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;
return internal::getrandom(buf, buflen, flags);
}

} // namespace LIBC_NAMESPACE_DECL
Loading