Skip to content

Commit df0325a

Browse files
[libc] add getrandom vDSO symbol (#151630)
1 parent dfd506b commit df0325a

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

libc/src/__support/OSUtil/linux/aarch64/vdso.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ LIBC_INLINE constexpr cpp::string_view symbol_name(VDSOSym sym) {
2323
return "__kernel_clock_gettime";
2424
case VDSOSym::ClockGetRes:
2525
return "__kernel_clock_getres";
26+
case VDSOSym::GetRandom:
27+
return "__kernel_getrandom";
2628
default:
2729
return "";
2830
}

libc/src/__support/OSUtil/linux/vdso_sym.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ enum class VDSOSym {
3535
RTSigReturn,
3636
FlushICache,
3737
RiscvHwProbe,
38-
VDSOSymCount
38+
GetRandom,
39+
VDSOSymCount,
3940
};
4041

4142
template <VDSOSym sym> LIBC_INLINE constexpr auto dispatcher() {
@@ -60,6 +61,9 @@ template <VDSOSym sym> LIBC_INLINE constexpr auto dispatcher() {
6061
else if constexpr (sym == VDSOSym::RiscvHwProbe)
6162
return static_cast<int (*)(riscv_hwprobe *, size_t, size_t, cpu_set_t *,
6263
unsigned)>(nullptr);
64+
else if constexpr (sym == VDSOSym::GetRandom)
65+
return static_cast<int (*)(void *, size_t, unsigned int, void *, size_t)>(
66+
nullptr);
6367
else
6468
return static_cast<void *>(nullptr);
6569
}

libc/src/__support/OSUtil/linux/x86_64/vdso.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ LIBC_INLINE constexpr cpp::string_view symbol_name(VDSOSym sym) {
2929
return "__vdso_time";
3030
case VDSOSym::ClockGetRes:
3131
return "__vdso_clock_getres";
32+
case VDSOSym::GetRandom:
33+
return "__vdso_getrandom";
3234
default:
3335
return "";
3436
}

libc/test/src/__support/OSUtil/linux/vdso_test.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ TEST(LlvmLibcOSUtilVDSOTest, RtSigReturn) {
110110
using namespace testing::ErrnoSetterMatcher;
111111
// must use struct since there is a function of the same name in the same
112112
// scope.
113-
struct sigaction sa {};
114-
struct sigaction old_sa {};
113+
struct sigaction sa{};
114+
struct sigaction old_sa{};
115115
sa.sa_handler = sigprof_handler;
116116
sa.sa_flags = SA_RESTORER;
117117
vdso::TypedSymbol<vdso::VDSOSym::RTSigReturn> symbol;
@@ -158,4 +158,30 @@ TEST(LlvmLibcOSUtilVDSOTest, RiscvHwProbe) {
158158
}
159159
}
160160

161+
TEST(LlvmLibcOSUtilVDSOTest, GetRandom) {
162+
using namespace testing::ErrnoSetterMatcher;
163+
vdso::TypedSymbol<vdso::VDSOSym::GetRandom> symbol;
164+
if (!symbol)
165+
return;
166+
// This structure exists in kernel UAPI header; but we define it on our own to
167+
// make sure we can test it even on platform without support.
168+
struct VGetrandomOpaqueParams {
169+
uint32_t size_of_opaque_states;
170+
uint32_t mmap_prot;
171+
uint32_t mmap_flags;
172+
uint32_t reserved[13];
173+
};
174+
VGetrandomOpaqueParams param{0, 0, 0, {}};
175+
// When getrandom vDSO symbol is called with special parameters (~0 for state
176+
// size), it populates the desired configuration into VGetrandomOpaqueParams.
177+
int res = symbol(
178+
/*buf=*/nullptr, /*count=*/0, /*flags=*/0,
179+
/*opaque_states=*/&param,
180+
/*size_of_opaque_states=*/~0);
181+
// Test that the size of the states are correctly populated after a successful
182+
// call.
183+
EXPECT_EQ(res, 0);
184+
EXPECT_GT(param.size_of_opaque_states, 0u);
185+
}
186+
161187
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)