[libc-commits] [libc] df0325a - [libc] add getrandom vDSO symbol (#151630)

via libc-commits libc-commits at lists.llvm.org
Wed Aug 6 07:55:45 PDT 2025


Author: Schrodinger ZHU Yifan
Date: 2025-08-06T10:55:41-04:00
New Revision: df0325a3e580613b53803ac03ab1e2bda00f744f

URL: https://github.com/llvm/llvm-project/commit/df0325a3e580613b53803ac03ab1e2bda00f744f
DIFF: https://github.com/llvm/llvm-project/commit/df0325a3e580613b53803ac03ab1e2bda00f744f.diff

LOG: [libc] add getrandom vDSO symbol (#151630)

Added: 
    

Modified: 
    libc/src/__support/OSUtil/linux/aarch64/vdso.h
    libc/src/__support/OSUtil/linux/vdso_sym.h
    libc/src/__support/OSUtil/linux/x86_64/vdso.h
    libc/test/src/__support/OSUtil/linux/vdso_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/OSUtil/linux/aarch64/vdso.h b/libc/src/__support/OSUtil/linux/aarch64/vdso.h
index 3c4c6205071da..ee5777ad67f6d 100644
--- a/libc/src/__support/OSUtil/linux/aarch64/vdso.h
+++ b/libc/src/__support/OSUtil/linux/aarch64/vdso.h
@@ -23,6 +23,8 @@ LIBC_INLINE constexpr cpp::string_view symbol_name(VDSOSym sym) {
     return "__kernel_clock_gettime";
   case VDSOSym::ClockGetRes:
     return "__kernel_clock_getres";
+  case VDSOSym::GetRandom:
+    return "__kernel_getrandom";
   default:
     return "";
   }

diff  --git a/libc/src/__support/OSUtil/linux/vdso_sym.h b/libc/src/__support/OSUtil/linux/vdso_sym.h
index 968e1536c4d27..01f0b72a4ed9c 100644
--- a/libc/src/__support/OSUtil/linux/vdso_sym.h
+++ b/libc/src/__support/OSUtil/linux/vdso_sym.h
@@ -35,7 +35,8 @@ enum class VDSOSym {
   RTSigReturn,
   FlushICache,
   RiscvHwProbe,
-  VDSOSymCount
+  GetRandom,
+  VDSOSymCount,
 };
 
 template <VDSOSym sym> LIBC_INLINE constexpr auto dispatcher() {
@@ -60,6 +61,9 @@ template <VDSOSym sym> LIBC_INLINE constexpr auto dispatcher() {
   else if constexpr (sym == VDSOSym::RiscvHwProbe)
     return static_cast<int (*)(riscv_hwprobe *, size_t, size_t, cpu_set_t *,
                                unsigned)>(nullptr);
+  else if constexpr (sym == VDSOSym::GetRandom)
+    return static_cast<int (*)(void *, size_t, unsigned int, void *, size_t)>(
+        nullptr);
   else
     return static_cast<void *>(nullptr);
 }

diff  --git a/libc/src/__support/OSUtil/linux/x86_64/vdso.h b/libc/src/__support/OSUtil/linux/x86_64/vdso.h
index abe7c33e07cfa..f46fcb038f2e6 100644
--- a/libc/src/__support/OSUtil/linux/x86_64/vdso.h
+++ b/libc/src/__support/OSUtil/linux/x86_64/vdso.h
@@ -29,6 +29,8 @@ LIBC_INLINE constexpr cpp::string_view symbol_name(VDSOSym sym) {
     return "__vdso_time";
   case VDSOSym::ClockGetRes:
     return "__vdso_clock_getres";
+  case VDSOSym::GetRandom:
+    return "__vdso_getrandom";
   default:
     return "";
   }

diff  --git a/libc/test/src/__support/OSUtil/linux/vdso_test.cpp b/libc/test/src/__support/OSUtil/linux/vdso_test.cpp
index 2f68470e8f678..71892a055daba 100644
--- a/libc/test/src/__support/OSUtil/linux/vdso_test.cpp
+++ b/libc/test/src/__support/OSUtil/linux/vdso_test.cpp
@@ -110,8 +110,8 @@ TEST(LlvmLibcOSUtilVDSOTest, RtSigReturn) {
   using namespace testing::ErrnoSetterMatcher;
   // must use struct since there is a function of the same name in the same
   // scope.
-  struct sigaction sa {};
-  struct sigaction old_sa {};
+  struct sigaction sa{};
+  struct sigaction old_sa{};
   sa.sa_handler = sigprof_handler;
   sa.sa_flags = SA_RESTORER;
   vdso::TypedSymbol<vdso::VDSOSym::RTSigReturn> symbol;
@@ -158,4 +158,30 @@ TEST(LlvmLibcOSUtilVDSOTest, RiscvHwProbe) {
   }
 }
 
+TEST(LlvmLibcOSUtilVDSOTest, GetRandom) {
+  using namespace testing::ErrnoSetterMatcher;
+  vdso::TypedSymbol<vdso::VDSOSym::GetRandom> symbol;
+  if (!symbol)
+    return;
+  // This structure exists in kernel UAPI header; but we define it on our own to
+  // make sure we can test it even on platform without support.
+  struct VGetrandomOpaqueParams {
+    uint32_t size_of_opaque_states;
+    uint32_t mmap_prot;
+    uint32_t mmap_flags;
+    uint32_t reserved[13];
+  };
+  VGetrandomOpaqueParams param{0, 0, 0, {}};
+  // When getrandom vDSO symbol is called with special parameters (~0 for state
+  // size), it populates the desired configuration into VGetrandomOpaqueParams.
+  int res = symbol(
+      /*buf=*/nullptr, /*count=*/0, /*flags=*/0,
+      /*opaque_states=*/&param,
+      /*size_of_opaque_states=*/~0);
+  // Test that the size of the states are correctly populated after a successful
+  // call.
+  EXPECT_EQ(res, 0);
+  EXPECT_GT(param.size_of_opaque_states, 0u);
+}
+
 } // namespace LIBC_NAMESPACE_DECL


        


More information about the libc-commits mailing list