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

source code of libc/src/sys/auxv/linux/getauxval.cpp