1 //===-- Unittests for VDSO ------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "hdr/signal_macros.h"
10 #include "hdr/time_macros.h"
11 #include "hdr/types/clockid_t.h"
12 #include "hdr/types/struct_sigaction.h"
13 #include "hdr/types/struct_timespec.h"
14 #include "hdr/types/struct_timeval.h"
15 #include "hdr/types/time_t.h"
16 #include "src/__support/OSUtil/linux/vdso.h"
17 #include "src/__support/OSUtil/syscall.h"
18 #include "src/__support/macros/properties/architectures.h"
19 #include "src/signal/raise.h"
20 #include "src/signal/sigaction.h"
21 #include "test/UnitTest/ErrnoSetterMatcher.h"
22 #include "test/UnitTest/Test.h"
23 #include <linux/time_types.h>
24 #include <sys/syscall.h>
26 struct riscv_hwprobe
{
31 namespace LIBC_NAMESPACE_DECL
{
32 // For x86_64, we explicitly test some traditional vdso symbols are indeed
35 TEST(LlvmLibcOSUtilVDSOTest
, GetTimeOfDay
) {
36 vdso::TypedSymbol
<vdso::VDSOSym::GetTimeOfDay
> symbol
;
40 EXPECT_EQ(symbol(&tv
, nullptr), 0);
41 // hopefully people are not building time machines using our libc.
42 EXPECT_GT(tv
.tv_sec
, static_cast<decltype(tv
.tv_sec
)>(0));
45 TEST(LlvmLibcOSUtilVDSOTest
, Time
) {
46 vdso::TypedSymbol
<vdso::VDSOSym::Time
> symbol
;
50 EXPECT_GT(symbol(&a
), static_cast<time_t>(0));
51 EXPECT_GT(symbol(&b
), static_cast<time_t>(0));
55 TEST(LlvmLibcOSUtilVDSOTest
, ClockGetTime
) {
56 vdso::TypedSymbol
<vdso::VDSOSym::ClockGetTime
> symbol
;
60 EXPECT_EQ(symbol(CLOCK_MONOTONIC
, &a
), 0);
61 EXPECT_EQ(symbol(CLOCK_MONOTONIC
, &b
), 0);
62 if (a
.tv_sec
== b
.tv_sec
) {
63 EXPECT_LT(a
.tv_nsec
, b
.tv_nsec
);
65 EXPECT_LT(a
.tv_sec
, b
.tv_sec
);
69 TEST(LlvmLibcOSUtilVDSOTest
, ClockGetTime64
) {
70 vdso::TypedSymbol
<vdso::VDSOSym::ClockGetTime64
> symbol
;
74 // https://elixir.bootlin.com/linux/latest/source/tools/testing/selftests/vDSO/vdso_test_correctness.c#L155
75 __kernel_timespec a
, b
;
76 EXPECT_EQ(symbol(CLOCK_MONOTONIC
, &a
), 0);
77 EXPECT_EQ(symbol(CLOCK_MONOTONIC
, &b
), 0);
78 if (a
.tv_sec
== b
.tv_sec
) {
79 EXPECT_LT(a
.tv_nsec
, b
.tv_nsec
);
81 EXPECT_LT(a
.tv_sec
, b
.tv_sec
);
85 TEST(LlvmLibcOSUtilVDSOTest
, ClockGetRes
) {
86 vdso::TypedSymbol
<vdso::VDSOSym::ClockGetRes
> symbol
;
90 EXPECT_EQ(symbol(CLOCK_MONOTONIC
, &res
), 0);
91 EXPECT_TRUE(res
.tv_sec
> 0 || res
.tv_nsec
> 0);
94 TEST(LlvmLibcOSUtilVDSOTest
, GetCpu
) {
95 // The kernel system call has a third argument, which should be passed as
97 vdso::TypedSymbol
<vdso::VDSOSym::GetCpu
> symbol
;
100 unsigned cpu
= static_cast<unsigned>(-1), node
= static_cast<unsigned>(-1);
101 EXPECT_EQ(symbol(&cpu
, &node
, nullptr), 0);
106 static bool flag
= false;
107 static void sigprof_handler
[[gnu::used
]] (int) { flag
= true; }
109 TEST(LlvmLibcOSUtilVDSOTest
, RtSigReturn
) {
110 using namespace testing::ErrnoSetterMatcher
;
111 // must use struct since there is a function of the same name in the same
113 struct sigaction sa
{};
114 struct sigaction old_sa
{};
115 sa
.sa_handler
= sigprof_handler
;
116 sa
.sa_flags
= SA_RESTORER
;
117 vdso::TypedSymbol
<vdso::VDSOSym::RTSigReturn
> symbol
;
120 sa
.sa_restorer
= symbol
;
121 ASSERT_THAT(LIBC_NAMESPACE::sigaction(SIGPROF
, &sa
, &old_sa
), Succeeds());
125 ASSERT_THAT(LIBC_NAMESPACE::sigaction(SIGPROF
, &old_sa
, nullptr), Succeeds());
128 TEST(LlvmLibcOSUtilVDSOTest
, FlushICache
) {
129 vdso::TypedSymbol
<vdso::VDSOSym::FlushICache
> symbol
;
133 // we just check that the flush will not panic the program.
134 // the flags part only take 0/1 as up to kernel 6.10, which is used to
135 // indicate whether the flush is local to the core or global.
136 symbol(buf
, buf
+ sizeof(buf
), 0);
137 symbol(buf
, buf
+ sizeof(buf
), 1);
140 // https://docs.kernel.org/6.5/riscv/hwprobe.html
141 TEST(LlvmLibcOSUtilVDSOTest
, RiscvHwProbe
) {
142 using namespace testing::ErrnoSetterMatcher
;
143 vdso::TypedSymbol
<vdso::VDSOSym::RiscvHwProbe
> symbol
;
146 // If a key is unknown to the kernel, its key field will be cleared to -1, and
147 // its value set to 0. We expect probes.value are all 0.
148 // Usermode can supply NULL for cpus and 0 for cpu_count as a shortcut for all
150 riscv_hwprobe probes
[2] = {{-1, 1}, {-1, 1}};
151 ASSERT_THAT(symbol(/*pairs=*/probes
, /*count=*/2, /*cpusetsize=*/0,
155 for (auto &probe
: probes
) {
156 EXPECT_EQ(probe
.key
, static_cast<decltype(probe
.key
)>(-1));
157 EXPECT_EQ(probe
.value
, static_cast<decltype(probe
.value
)>(0));
161 } // namespace LIBC_NAMESPACE_DECL