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.
35extern "C" [[gnu::weak]] int __cxa_atexit(void (*callback)(void *),
36 void *payload, void *);
37
38namespace LIBC_NAMESPACE {
39
40constexpr static size_t MAX_AUXV_ENTRIES = 64;
41
42// Helper to recover or set errno
43class AuxvErrnoGuard {
44public:
45 AuxvErrnoGuard() : saved(libc_errno), failure(false) {}
46 ~AuxvErrnoGuard() { libc_errno = failure ? ENOENT : saved; }
47 void mark_failure() { failure = true; }
48
49private:
50 int saved;
51 bool failure;
52};
53
54// Helper to manage the memory
55static AuxEntry *auxv = nullptr;
56
57class AuxvMMapGuard {
58public:
59 constexpr static size_t AUXV_MMAP_SIZE = sizeof(AuxEntry) * MAX_AUXV_ENTRIES;
60
61 AuxvMMapGuard()
62 : ptr(mmap(addr: nullptr, size: AUXV_MMAP_SIZE, PROT_READ | PROT_WRITE,
63 MAP_PRIVATE | MAP_ANONYMOUS, fd: -1, offset: 0)) {}
64 ~AuxvMMapGuard() {
65 if (ptr != MAP_FAILED)
66 munmap(addr: ptr, size: 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 callback: [](void *) {
72 munmap(addr: auxv, size: AUXV_MMAP_SIZE);
73 auxv = nullptr;
74 },
75 payload: 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
86private:
87 void *ptr;
88};
89
90class AuxvFdGuard {
91public:
92 AuxvFdGuard() : fd(open(path: "/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
100private:
101 int fd;
102};
103
104static 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: fd_guard.get(), buf, count: 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
155static 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, buf: ptr, count: 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
175LLVM_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(flag: reinterpret_cast<CallOnceFlag *>(&once_flag), callback: 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: 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
218

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