Skip to content

[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
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

sribee8
Copy link
Contributor

@sribee8 sribee8 commented Jun 16, 2025

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

Implemented an internal getrandom to avoid calls to the public one in table.h
@llvmbot llvmbot added the libc label Jun 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 16, 2025

@llvm/pr-subscribers-libc

Author: None (sribee8)

Changes

Implemented 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:

  • (modified) libc/src/__support/HashTable/randomness.h (+2-2)
  • (added) libc/src/__support/OSUtil/getrandom.h (+23)
  • (modified) libc/src/__support/OSUtil/linux/CMakeLists.txt (+1)
  • (added) libc/src/__support/OSUtil/linux/getrandom.cpp (+30)
  • (modified) libc/src/sys/random/linux/getrandom.cpp (+2-7)
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

Copy link

github-actions bot commented Jun 16, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants