Skip to content

Commit 4beb723

Browse files
[libc] implement sys/getauxval (#78493)
This PR implements `sys/getauxval` that can be used in both overlay builds and full builds.
1 parent 23edf78 commit 4beb723

File tree

14 files changed

+325
-1
lines changed

14 files changed

+325
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ set(TARGET_LIBC_ENTRYPOINTS
168168
# sys/prctl.h entrypoints
169169
libc.src.sys.prctl.prctl
170170

171+
# sys/auxv.h entrypoints
172+
libc.src.sys.auxv.getauxval
173+
171174
# termios.h entrypoints
172175
libc.src.termios.cfgetispeed
173176
libc.src.termios.cfgetospeed

libc/config/linux/app.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct AppProperties {
9393
AuxEntry *auxv_ptr;
9494
};
9595

96-
extern AppProperties app;
96+
[[gnu::weak]] extern AppProperties app;
9797

9898
// The descriptor of a thread's TLS area.
9999
struct TLSDescriptor {

libc/config/linux/arm/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ set(TARGET_LIBC_ENTRYPOINTS
9595

9696
# sys/prctl.h entrypoints
9797
libc.src.sys.prctl.prctl
98+
99+
# sys/auxv.h entrypoints
100+
libc.src.sys.auxv.getauxval
98101
)
99102

100103
set(TARGET_LIBM_ENTRYPOINTS

libc/config/linux/riscv/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ set(TARGET_LIBC_ENTRYPOINTS
174174
# sys/prctl.h entrypoints
175175
libc.src.sys.prctl.prctl
176176

177+
# sys/auxv.h entrypoints
178+
libc.src.sys.auxv.getauxval
179+
177180
# termios.h entrypoints
178181
libc.src.termios.cfgetispeed
179182
libc.src.termios.cfgetospeed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ set(TARGET_LIBC_ENTRYPOINTS
174174
# sys/prctl.h entrypoints
175175
libc.src.sys.prctl.prctl
176176

177+
# sys/auxv.h entrypoints
178+
libc.src.sys.auxv.getauxval
179+
177180
# termios.h entrypoints
178181
libc.src.termios.cfgetispeed
179182
libc.src.termios.cfgetospeed

libc/src/sys/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(auxv)
12
add_subdirectory(mman)
23
add_subdirectory(random)
34
add_subdirectory(resource)

libc/src/sys/auxv/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
3+
endif()
4+
5+
add_entrypoint_object(
6+
getauxval
7+
ALIAS
8+
DEPENDS
9+
.${LIBC_TARGET_OS}.getauxval
10+
)

libc/src/sys/auxv/getauxval.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for getauxval function ------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SYS_AUXV_GETAUXVAL_H
10+
#define LLVM_LIBC_SRC_SYS_AUXV_GETAUXVAL_H
11+
12+
#include <sys/auxv.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
unsigned long getauxval(unsigned long id);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_SYS_AUXV_GETAUXVAL_H
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
add_entrypoint_object(
2+
getauxval
3+
SRCS
4+
getauxval.cpp
5+
HDRS
6+
../getauxval.h
7+
DEPENDS
8+
libc.src.sys.prctl.prctl
9+
libc.src.sys.mman.mmap
10+
libc.src.sys.mman.munmap
11+
libc.src.__support.threads.callonce
12+
libc.src.__support.common
13+
libc.src.errno.errno
14+
libc.config.linux.app_h
15+
libc.src.fcntl.open
16+
libc.src.unistd.read
17+
libc.src.unistd.close
18+
)

libc/src/sys/auxv/linux/getauxval.cpp

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
//===-- Implementation file for getauxval function --------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/auxv/getauxval.h"
10+
#include "config/linux/app.h"
11+
#include "src/__support/common.h"
12+
#include "src/errno/libc_errno.h"
13+
#include <linux/auxvec.h>
14+
15+
// for guarded initialization
16+
#include "src/__support/threads/callonce.h"
17+
#include "src/__support/threads/linux/futex_word.h"
18+
19+
// for mallocing the global auxv
20+
#include "src/sys/mman/mmap.h"
21+
#include "src/sys/mman/munmap.h"
22+
23+
// for reading /proc/self/auxv
24+
#include "src/fcntl/open.h"
25+
#include "src/sys/prctl/prctl.h"
26+
#include "src/unistd/close.h"
27+
#include "src/unistd/read.h"
28+
29+
// getauxval will work either with or without __cxa_atexit support.
30+
// In order to detect if __cxa_atexit is supported, we define a weak symbol.
31+
// We prefer __cxa_atexit as it is always defined as a C symbol whileas atexit
32+
// may not be created via objcopy yet. Also, for glibc, atexit is provided via
33+
// libc_nonshared.a rather than libc.so. So, it is may not be made ready for
34+
// overlay builds.
35+
extern "C" [[gnu::weak]] int __cxa_atexit(void (*callback)(void *),
36+
void *payload, void *);
37+
38+
namespace LIBC_NAMESPACE {
39+
40+
constexpr static size_t MAX_AUXV_ENTRIES = 64;
41+
42+
// Helper to recover or set errno
43+
class AuxvErrnoGuard {
44+
public:
45+
AuxvErrnoGuard() : saved(libc_errno), failure(false) {}
46+
~AuxvErrnoGuard() { libc_errno = failure ? ENOENT : saved; }
47+
void mark_failure() { failure = true; }
48+
49+
private:
50+
int saved;
51+
bool failure;
52+
};
53+
54+
// Helper to manage the memory
55+
static AuxEntry *auxv = nullptr;
56+
57+
class AuxvMMapGuard {
58+
public:
59+
constexpr static size_t AUXV_MMAP_SIZE = sizeof(AuxEntry) * MAX_AUXV_ENTRIES;
60+
61+
AuxvMMapGuard()
62+
: ptr(mmap(nullptr, AUXV_MMAP_SIZE, PROT_READ | PROT_WRITE,
63+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) {}
64+
~AuxvMMapGuard() {
65+
if (ptr != MAP_FAILED)
66+
munmap(ptr, AUXV_MMAP_SIZE);
67+
}
68+
void submit_to_global() {
69+
// atexit may fail, we do not set it to global in that case.
70+
int ret = __cxa_atexit(
71+
[](void *) {
72+
munmap(auxv, AUXV_MMAP_SIZE);
73+
auxv = nullptr;
74+
},
75+
nullptr, nullptr);
76+
77+
if (ret != 0)
78+
return;
79+
80+
auxv = reinterpret_cast<AuxEntry *>(ptr);
81+
ptr = MAP_FAILED;
82+
}
83+
bool allocated() const { return ptr != MAP_FAILED; }
84+
void *get() const { return ptr; }
85+
86+
private:
87+
void *ptr;
88+
};
89+
90+
class AuxvFdGuard {
91+
public:
92+
AuxvFdGuard() : fd(open("/proc/self/auxv", O_RDONLY | O_CLOEXEC)) {}
93+
~AuxvFdGuard() {
94+
if (fd != -1)
95+
close(fd);
96+
}
97+
bool valid() const { return fd != -1; }
98+
int get() const { return fd; }
99+
100+
private:
101+
int fd;
102+
};
103+
104+
static void initialize_auxv_once(void) {
105+
// If we cannot get atexit, we cannot register the cleanup function.
106+
if (&__cxa_atexit == nullptr)
107+
return;
108+
109+
AuxvMMapGuard mmap_guard;
110+
if (!mmap_guard.allocated())
111+
return;
112+
auto *ptr = reinterpret_cast<AuxEntry *>(mmap_guard.get());
113+
114+
// We get one less than the max size to make sure the search always
115+
// terminates. MMAP private pages are zeroed out already.
116+
size_t available_size = AuxvMMapGuard::AUXV_MMAP_SIZE - sizeof(AuxEntryType);
117+
// PR_GET_AUXV is only available on Linux kernel 6.1 and above. If this is not
118+
// defined, we direcly fall back to reading /proc/self/auxv. In case the libc
119+
// is compiled and run on separate kernels, we also check the return value of
120+
// prctl.
121+
#ifdef PR_GET_AUXV
122+
int ret = prctl(PR_GET_AUXV, reinterpret_cast<unsigned long>(ptr),
123+
available_size, 0, 0);
124+
if (ret >= 0) {
125+
mmap_guard.submit_to_global();
126+
return;
127+
}
128+
#endif
129+
AuxvFdGuard fd_guard;
130+
if (!fd_guard.valid())
131+
return;
132+
auto *buf = reinterpret_cast<char *>(ptr);
133+
libc_errno = 0;
134+
bool error_detected = false;
135+
// Read until we use up all the available space or we finish reading the file.
136+
while (available_size != 0) {
137+
ssize_t bytes_read = read(fd_guard.get(), buf, available_size);
138+
if (bytes_read <= 0) {
139+
if (libc_errno == EINTR)
140+
continue;
141+
// Now, we either have an non-recoverable error or we have reached the end
142+
// of the file. Mark `error_detected` accordingly.
143+
if (bytes_read == -1)
144+
error_detected = true;
145+
break;
146+
}
147+
buf += bytes_read;
148+
available_size -= bytes_read;
149+
}
150+
// If we get out of the loop without an error, the auxv is ready.
151+
if (!error_detected)
152+
mmap_guard.submit_to_global();
153+
}
154+
155+
static AuxEntry read_entry(int fd) {
156+
AuxEntry buf;
157+
ssize_t size = sizeof(AuxEntry);
158+
char *ptr = reinterpret_cast<char *>(&buf);
159+
while (size > 0) {
160+
ssize_t ret = read(fd, ptr, size);
161+
if (ret < 0) {
162+
if (libc_errno == EINTR)
163+
continue;
164+
// Error detected, return AT_NULL
165+
buf.id = AT_NULL;
166+
buf.value = AT_NULL;
167+
break;
168+
}
169+
ptr += ret;
170+
size -= ret;
171+
}
172+
return buf;
173+
}
174+
175+
LLVM_LIBC_FUNCTION(unsigned long, getauxval, (unsigned long id)) {
176+
// Fast path when libc is loaded by its own initialization code. In this case,
177+
// app.auxv_ptr is already set to the auxv passed on the initial stack of the
178+
// process.
179+
AuxvErrnoGuard errno_guard;
180+
181+
auto search_auxv = [&errno_guard](AuxEntry *auxv,
182+
unsigned long id) -> AuxEntryType {
183+
for (auto *ptr = auxv; ptr->id != AT_NULL; ptr++)
184+
if (ptr->id == id)
185+
return ptr->value;
186+
187+
errno_guard.mark_failure();
188+
return AT_NULL;
189+
};
190+
191+
// App is a weak symbol that is only defined if libc is linked to its own
192+
// initialization routine. We need to check if it is null.
193+
if (&app != nullptr)
194+
return search_auxv(app.auxv_ptr, id);
195+
196+
static FutexWordType once_flag;
197+
callonce(reinterpret_cast<CallOnceFlag *>(&once_flag), initialize_auxv_once);
198+
if (auxv != nullptr)
199+
return search_auxv(auxv, id);
200+
201+
// Fallback to use read without mmap
202+
AuxvFdGuard fd_guard;
203+
if (fd_guard.valid()) {
204+
while (true) {
205+
AuxEntry buf = read_entry(fd_guard.get());
206+
if (buf.id == AT_NULL)
207+
break;
208+
if (buf.id == id)
209+
return buf.value;
210+
}
211+
}
212+
213+
// cannot find the entry after all methods, mark failure and return 0
214+
errno_guard.mark_failure();
215+
return AT_NULL;
216+
}
217+
} // namespace LIBC_NAMESPACE

libc/test/src/sys/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ add_subdirectory(stat)
88
add_subdirectory(utsname)
99
add_subdirectory(wait)
1010
add_subdirectory(prctl)
11+
add_subdirectory(auxv)

libc/test/src/sys/auxv/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
add_subdirectory(${LIBC_TARGET_OS})
3+
endif()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_custom_target(libc_sys_auxv_unittests)
2+
add_libc_unittest(
3+
getauxval_test
4+
SUITE
5+
libc_sys_auxv_unittests
6+
SRCS
7+
getauxval_test.cpp
8+
DEPENDS
9+
libc.include.sys_auxv
10+
libc.src.errno.errno
11+
libc.src.sys.auxv.getauxval
12+
libc.test.UnitTest.ErrnoSetterMatcher
13+
libc.src.string.strstr
14+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Unittests for getaxuval -------------------------------------------===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#include "src/errno/libc_errno.h"
9+
#include "src/sys/auxv/getauxval.h"
10+
#include "test/UnitTest/ErrnoSetterMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include <src/string/strstr.h>
13+
#include <sys/auxv.h>
14+
15+
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
16+
17+
TEST(LlvmLibcGetauxvalTest, Basic) {
18+
EXPECT_THAT(LIBC_NAMESPACE::getauxval(AT_PAGESZ),
19+
returns(GT(0ul)).with_errno(EQ(0)));
20+
const char *filename;
21+
auto getfilename = [&filename]() {
22+
auto value = LIBC_NAMESPACE::getauxval(AT_EXECFN);
23+
filename = reinterpret_cast<const char *>(value);
24+
return value;
25+
};
26+
EXPECT_THAT(getfilename(), returns(NE(0ul)).with_errno(EQ(0)));
27+
ASSERT_TRUE(LIBC_NAMESPACE::strstr(filename, "getauxval_test") != nullptr);
28+
}

0 commit comments

Comments
 (0)