-
Notifications
You must be signed in to change notification settings - Fork 14k
[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
Open
sribee8
wants to merge
10
commits into
llvm:main
Choose a base branch
from
sribee8:internal-getrandom
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+60
−15
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Implemented an internal getrandom to avoid calls to the public one in table.h
@llvm/pr-subscribers-libc Author: None (sribee8) ChangesImplemented an internal getrandom to avoid calls to the public one in table.h Full diff: https://github.com/llvm/llvm-project/pull/144427.diff 5 Files Affected:
diff --git a/libc/src/__support/HashTable/randomness.h b/libc/src/__support/HashTable/randomness.h
index 6b58a4125f785..54fb9e51a5d67 100644
--- a/libc/src/__support/HashTable/randomness.h
+++ b/libc/src/__support/HashTable/randomness.h
@@ -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 {
@@ -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;
diff --git a/libc/src/__support/OSUtil/getrandom.h b/libc/src/__support/OSUtil/getrandom.h
new file mode 100644
index 0000000000000..69468d7a5882c
--- /dev/null
+++ b/libc/src/__support/OSUtil/getrandom.h
@@ -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
diff --git a/libc/src/__support/OSUtil/linux/CMakeLists.txt b/libc/src/__support/OSUtil/linux/CMakeLists.txt
index 4681d8c2bb73c..a725a7a6b318b 100644
--- a/libc/src/__support/OSUtil/linux/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/CMakeLists.txt
@@ -9,6 +9,7 @@ add_object_library(
SRCS
exit.cpp
fcntl.cpp
+ getrandom.cpp
HDRS
io.h
syscall.h
diff --git a/libc/src/__support/OSUtil/linux/getrandom.cpp b/libc/src/__support/OSUtil/linux/getrandom.cpp
new file mode 100644
index 0000000000000..225c381e3540f
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/getrandom.cpp
@@ -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
diff --git a/libc/src/sys/random/linux/getrandom.cpp b/libc/src/sys/random/linux/getrandom.cpp
index 0b8471ed8b374..c58c4c1857037 100644
--- a/libc/src/sys/random/linux/getrandom.cpp
+++ b/libc/src/sys/random/linux/getrandom.cpp
@@ -10,6 +10,7 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "src/__support/OSUtil/getrandom.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
@@ -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
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
lntue
reviewed
Jun 16, 2025
lntue
reviewed
Jun 16, 2025
lntue
reviewed
Jun 16, 2025
lntue
reviewed
Jun 16, 2025
lntue
reviewed
Jun 16, 2025
SchrodingerZhu
requested changes
Jun 17, 2025
SchrodingerZhu
approved these changes
Jun 18, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implemented an internal getrandom to avoid calls to the public one in table.h