[libc-commits] [libc] [libc] implement sys/getauxval (PR #78493)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Mon Jan 22 16:50:13 PST 2024


https://github.com/SchrodingerZhu updated https://github.com/llvm/llvm-project/pull/78493

>From 579c5ae0a3a91ff381828dd2839b94c20fb7e29c Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Tue, 19 Dec 2023 00:39:04 -0500
Subject: [PATCH 01/10] [libc] implement sys/getauxval

---
 libc/config/linux/aarch64/entrypoints.txt |   3 +
 libc/config/linux/app.h                   |   2 +-
 libc/config/linux/arm/entrypoints.txt     |   3 +
 libc/config/linux/riscv/entrypoints.txt   |   3 +
 libc/config/linux/x86_64/entrypoints.txt  |   3 +
 libc/src/sys/CMakeLists.txt               |   1 +
 libc/src/sys/auxv/CMakeLists.txt          |  10 ++
 libc/src/sys/auxv/getauxval.h             |  20 +++
 libc/src/sys/auxv/linux/CMakeLists.txt    |  18 ++
 libc/src/sys/auxv/linux/getauxval.cpp     | 190 ++++++++++++++++++++++
 10 files changed, 252 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/sys/auxv/CMakeLists.txt
 create mode 100644 libc/src/sys/auxv/getauxval.h
 create mode 100644 libc/src/sys/auxv/linux/CMakeLists.txt
 create mode 100644 libc/src/sys/auxv/linux/getauxval.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 625fa6bffe63c6..3f66a582f5e3ee 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -168,6 +168,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/prctl.h entrypoints
     libc.src.sys.prctl.prctl
 
+    # sys/auxv.h entrypoints
+    libc.src.sys.auxv.getauxval
+
     # termios.h entrypoints
     libc.src.termios.cfgetispeed
     libc.src.termios.cfgetospeed
diff --git a/libc/config/linux/app.h b/libc/config/linux/app.h
index 1b3523deb1b23e..766cd49e88f6f7 100644
--- a/libc/config/linux/app.h
+++ b/libc/config/linux/app.h
@@ -93,7 +93,7 @@ struct AppProperties {
   AuxEntry *auxv_ptr;
 };
 
-extern AppProperties app;
+[[gnu::weak]] extern AppProperties app;
 
 // The descriptor of a thread's TLS area.
 struct TLSDescriptor {
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index c75ac2302d4ac4..301870d337ca00 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -95,6 +95,9 @@ set(TARGET_LIBC_ENTRYPOINTS
 
     # sys/prctl.h entrypoints
     libc.src.sys.prctl.prctl
+
+    # sys/auxv.h entrypoints
+    libc.src.sys.auxv.getauxval
 )
 
 set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index ec2a16f5cf473b..0331ef782cf74a 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -174,6 +174,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/prctl.h entrypoints
     libc.src.sys.prctl.prctl
 
+    # sys/auxv.h entrypoints
+    libc.src.sys.auxv.getauxval
+
     # termios.h entrypoints
     libc.src.termios.cfgetispeed
     libc.src.termios.cfgetospeed
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 094bdde2e1589c..d5ab891674a2d8 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -174,6 +174,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/prctl.h entrypoints
     libc.src.sys.prctl.prctl
 
+    # sys/auxv.h entrypoints
+    libc.src.sys.auxv.getauxval
+
     # termios.h entrypoints
     libc.src.termios.cfgetispeed
     libc.src.termios.cfgetospeed
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 12e2020f013ab1..81098294176ad5 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -1,3 +1,4 @@
+add_subdirectory(auxv)
 add_subdirectory(mman)
 add_subdirectory(random)
 add_subdirectory(resource)
diff --git a/libc/src/sys/auxv/CMakeLists.txt b/libc/src/sys/auxv/CMakeLists.txt
new file mode 100644
index 00000000000000..4065761064b129
--- /dev/null
+++ b/libc/src/sys/auxv/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  getauxval
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.getauxval
+)
diff --git a/libc/src/sys/auxv/getauxval.h b/libc/src/sys/auxv/getauxval.h
new file mode 100644
index 00000000000000..7c9fb846e91984
--- /dev/null
+++ b/libc/src/sys/auxv/getauxval.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for getauxval function ------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_AUXV_GETAUXVAL_H
+#define LLVM_LIBC_SRC_SYS_AUXV_GETAUXVAL_H
+
+#include <sys/auxv.h>
+
+namespace LIBC_NAMESPACE {
+
+unsigned long getauxval(unsigned long id);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_SYS_AUXV_GETAUXVAL_H
diff --git a/libc/src/sys/auxv/linux/CMakeLists.txt b/libc/src/sys/auxv/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..b38d63ee0329c7
--- /dev/null
+++ b/libc/src/sys/auxv/linux/CMakeLists.txt
@@ -0,0 +1,18 @@
+add_entrypoint_object(
+  getauxval
+  SRCS
+  getauxval.cpp
+  HDRS
+    ../getauxval.h
+  DEPENDS
+    libc.src.sys.prctl.prctl
+    libc.src.sys.mman.mmap
+    libc.src.sys.mman.munmap
+    libc.src.__support.threads.callonce
+    libc.src.__support.common
+    libc.src.errno.errno
+    libc.config.linux.app_h
+    libc.src.fcntl.open
+    libc.src.unistd.read
+    libc.src.unistd.close
+)
diff --git a/libc/src/sys/auxv/linux/getauxval.cpp b/libc/src/sys/auxv/linux/getauxval.cpp
new file mode 100644
index 00000000000000..b1d962698fe025
--- /dev/null
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -0,0 +1,190 @@
+//===-- Implementation file for getauxval function --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/auxv/getauxval.h"
+#include "config/linux/app.h"
+#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
+#include <linux/auxvec.h>
+
+// for guarded initialization
+#include "src/__support/threads/callonce.h"
+#include "src/__support/threads/linux/futex_word.h"
+
+// for mallocing the global auxv
+#include "src/sys/mman/mmap.h"
+#include "src/sys/mman/munmap.h"
+
+// for reading /proc/self/auxv
+#include "src/fcntl/open.h"
+#include "src/sys/prctl/prctl.h"
+#include "src/unistd/close.h"
+#include "src/unistd/read.h"
+
+// getauxval will work either with or without atexit support.
+// In order to detect if atexit is supported, we define a weak symbol.
+extern "C" [[gnu::weak]] int atexit(void (*)(void));
+
+namespace LIBC_NAMESPACE {
+
+constexpr static size_t MAX_AUXV_ENTRIES = 64;
+
+// Helper to recover or set errno
+struct AuxvErrnoGuard {
+  int saved;
+  bool failure;
+  AuxvErrnoGuard() : saved(libc_errno), failure(false) {}
+  ~AuxvErrnoGuard() { libc_errno = failure ? ENOENT : saved; }
+  void mark_failure() { failure = true; }
+};
+
+// Helper to manage the memory
+static AuxEntry *auxv = nullptr;
+
+struct AuxvMMapGuard {
+  constexpr static size_t AUXV_MMAP_SIZE = sizeof(AuxEntry) * MAX_AUXV_ENTRIES;
+  void *ptr;
+  AuxvMMapGuard(size_t size)
+      : ptr(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, -1, 0)) {}
+  ~AuxvMMapGuard() {
+    if (ptr != MAP_FAILED) {
+      munmap(ptr, AUXV_MMAP_SIZE);
+    }
+  }
+  void submit_to_global() {
+    // atexit may fail, we do not set it to global in that case.
+    int ret = atexit([]() {
+      munmap(auxv, AUXV_MMAP_SIZE);
+      auxv = nullptr;
+    });
+
+    if (ret != 0)
+      return;
+
+    auxv = reinterpret_cast<AuxEntry *>(ptr);
+    ptr = MAP_FAILED;
+  }
+  bool allocated() { return ptr != MAP_FAILED; }
+};
+
+struct AuxvFdGuard {
+  int fd;
+  AuxvFdGuard() : fd(open("/proc/self/auxv", O_RDONLY | O_CLOEXEC)) {}
+  ~AuxvFdGuard() {
+    if (fd != -1) {
+      close(fd);
+    }
+  }
+  bool valid() { return fd != -1; }
+};
+
+static void initialize_auxv_once(void) {
+  // if we cannot get atexit, we cannot register the cleanup function.
+  if (&atexit == nullptr)
+    return;
+
+  AuxvMMapGuard mmap_guard(AuxvMMapGuard::AUXV_MMAP_SIZE);
+  if (!mmap_guard.allocated())
+    return;
+  auto *ptr = reinterpret_cast<AuxEntry *>(mmap_guard.ptr);
+
+  // We get one less than the max size to make sure the search always
+  // terminates. MMAP private pages are zeroed out already.
+  size_t available_size = AuxvMMapGuard::AUXV_MMAP_SIZE - sizeof(AuxEntryType);
+#if defined(PR_GET_AUXV)
+  int ret = prctl(PR_GET_AUXV, reinterpret_cast<unsigned long>(ptr),
+                  available_size, 0, 0);
+  if (ret >= 0) {
+    mmap_guard.submit_to_global();
+    return;
+  }
+#endif
+  AuxvFdGuard fd_guard;
+  if (!fd_guard.valid())
+    return;
+  auto *buf = reinterpret_cast<char *>(ptr);
+  libc_errno = 0;
+  bool error_detected = false;
+  while (available_size != 0) {
+    ssize_t bytes_read = read(fd_guard.fd, buf, available_size);
+    if (bytes_read <= 0) {
+      if (libc_errno == EINTR)
+        continue;
+      error_detected = bytes_read < 0;
+      break;
+    }
+    available_size -= bytes_read;
+  }
+  if (!error_detected) {
+    mmap_guard.submit_to_global();
+  }
+}
+
+static AuxEntry read_entry(int fd) {
+  AuxEntry buf;
+  ssize_t size = sizeof(AuxEntry);
+  while (size > 0) {
+    ssize_t ret = read(fd, &buf, size);
+    if (ret < 0) {
+      if (libc_errno == EINTR)
+        continue;
+      buf.id = AT_NULL;
+      buf.value = AT_NULL;
+      break;
+    }
+    size -= ret;
+  }
+  return buf;
+}
+
+LLVM_LIBC_FUNCTION(unsigned long, getauxval, (unsigned long id)) {
+  // Fast path when libc is loaded by its own initialization code. In this case,
+  // app.auxv_ptr is already set to the auxv passed on the initial stack of the
+  // process.
+  AuxvErrnoGuard errno_guard;
+
+  auto search_auxv = [&errno_guard](AuxEntry *auxv,
+                                    unsigned long id) -> AuxEntryType {
+    for (auto *ptr = auxv; ptr->id != AT_NULL; ptr++) {
+      if (ptr->id == id) {
+        return ptr->value;
+      }
+    }
+    errno_guard.mark_failure();
+    return AT_NULL;
+  };
+
+  // App is a weak symbol that is only defined if libc is linked to its own
+  // initialization routine. We need to check if it is null.
+  if (&app != nullptr) {
+    return search_auxv(app.auxv_ptr, id);
+  }
+
+  static FutexWordType once_flag;
+  callonce(reinterpret_cast<CallOnceFlag *>(&once_flag), initialize_auxv_once);
+  if (auxv != nullptr) {
+    return search_auxv(auxv, id);
+  }
+
+  // fallback to use read without mmap
+  AuxvFdGuard fd_guard;
+  if (fd_guard.valid()) {
+    while (true) {
+      AuxEntry buf = read_entry(fd_guard.fd);
+      if (buf.id == AT_NULL)
+        break;
+      if (buf.id == id)
+        return buf.value;
+    }
+  }
+
+  // cannot find the entry after all methods, mark failure and return 0
+  errno_guard.mark_failure();
+  return AT_NULL;
+}
+} // namespace LIBC_NAMESPACE

>From 0e2fcff29f980500e3069d60926f65dd914da81c Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 22 Jan 2024 14:24:43 -0500
Subject: [PATCH 02/10] fix bugs

---
 libc/src/sys/auxv/linux/getauxval.cpp         | 28 +++++++++++--------
 libc/test/src/sys/CMakeLists.txt              |  1 +
 libc/test/src/sys/auxv/CMakeLists.txt         |  3 ++
 libc/test/src/sys/auxv/linux/CMakeLists.txt   | 14 ++++++++++
 .../src/sys/auxv/linux/getauxval_test.cpp     | 27 ++++++++++++++++++
 5 files changed, 62 insertions(+), 11 deletions(-)
 create mode 100644 libc/test/src/sys/auxv/CMakeLists.txt
 create mode 100644 libc/test/src/sys/auxv/linux/CMakeLists.txt
 create mode 100644 libc/test/src/sys/auxv/linux/getauxval_test.cpp

diff --git a/libc/src/sys/auxv/linux/getauxval.cpp b/libc/src/sys/auxv/linux/getauxval.cpp
index b1d962698fe025..4b2aabe40c39b5 100644
--- a/libc/src/sys/auxv/linux/getauxval.cpp
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -26,9 +26,12 @@
 #include "src/unistd/close.h"
 #include "src/unistd/read.h"
 
-// getauxval will work either with or without atexit support.
-// In order to detect if atexit is supported, we define a weak symbol.
-extern "C" [[gnu::weak]] int atexit(void (*)(void));
+// getauxval will work either with or without __cxa_atexit support.
+// In order to detect if __cxa_atexit is supported, we define a weak symbol.
+// We prefer __cxa_atexit as it is always defined as a C symbol whileas atexit
+// may not be created via objcopy yet.
+extern "C" [[gnu::weak]] int __cxa_atexit(void (*callback)(void *),
+                                          void *payload, void *);
 
 namespace LIBC_NAMESPACE {
 
@@ -49,8 +52,9 @@ static AuxEntry *auxv = nullptr;
 struct AuxvMMapGuard {
   constexpr static size_t AUXV_MMAP_SIZE = sizeof(AuxEntry) * MAX_AUXV_ENTRIES;
   void *ptr;
-  AuxvMMapGuard(size_t size)
-      : ptr(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, -1, 0)) {}
+  AuxvMMapGuard()
+      : ptr(mmap(nullptr, AUXV_MMAP_SIZE, PROT_READ | PROT_WRITE,
+                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) {}
   ~AuxvMMapGuard() {
     if (ptr != MAP_FAILED) {
       munmap(ptr, AUXV_MMAP_SIZE);
@@ -58,10 +62,12 @@ struct AuxvMMapGuard {
   }
   void submit_to_global() {
     // atexit may fail, we do not set it to global in that case.
-    int ret = atexit([]() {
-      munmap(auxv, AUXV_MMAP_SIZE);
-      auxv = nullptr;
-    });
+    int ret = __cxa_atexit(
+        [](void *) {
+          munmap(auxv, AUXV_MMAP_SIZE);
+          auxv = nullptr;
+        },
+        nullptr, nullptr);
 
     if (ret != 0)
       return;
@@ -85,10 +91,10 @@ struct AuxvFdGuard {
 
 static void initialize_auxv_once(void) {
   // if we cannot get atexit, we cannot register the cleanup function.
-  if (&atexit == nullptr)
+  if (&__cxa_atexit == nullptr)
     return;
 
-  AuxvMMapGuard mmap_guard(AuxvMMapGuard::AUXV_MMAP_SIZE);
+  AuxvMMapGuard mmap_guard;
   if (!mmap_guard.allocated())
     return;
   auto *ptr = reinterpret_cast<AuxEntry *>(mmap_guard.ptr);
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index a87e77da7d2cdb..c7095456383b30 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -8,3 +8,4 @@ add_subdirectory(stat)
 add_subdirectory(utsname)
 add_subdirectory(wait)
 add_subdirectory(prctl)
+add_subdirectory(auxv)
diff --git a/libc/test/src/sys/auxv/CMakeLists.txt b/libc/test/src/sys/auxv/CMakeLists.txt
new file mode 100644
index 00000000000000..b4bbe81c92ff2e
--- /dev/null
+++ b/libc/test/src/sys/auxv/CMakeLists.txt
@@ -0,0 +1,3 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/sys/auxv/linux/CMakeLists.txt b/libc/test/src/sys/auxv/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..c1e82a1f0a46c3
--- /dev/null
+++ b/libc/test/src/sys/auxv/linux/CMakeLists.txt
@@ -0,0 +1,14 @@
+add_custom_target(libc_sys_auxv_unittests)
+add_libc_unittest(
+  getauxval_test
+  SUITE
+    libc_sys_auxv_unittests
+  SRCS
+    getauxval_test.cpp
+  DEPENDS
+    libc.include.sys_auxv
+    libc.src.errno.errno
+    libc.src.sys.auxv.getauxval
+    libc.test.UnitTest.ErrnoSetterMatcher
+    libc.src.string.strstr
+)
diff --git a/libc/test/src/sys/auxv/linux/getauxval_test.cpp b/libc/test/src/sys/auxv/linux/getauxval_test.cpp
new file mode 100644
index 00000000000000..3b0c4e1b4175f2
--- /dev/null
+++ b/libc/test/src/sys/auxv/linux/getauxval_test.cpp
@@ -0,0 +1,27 @@
+//===-- Unittests for getaxuval -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include "src/errno/libc_errno.h"
+#include "src/sys/auxv/getauxval.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+#include <src/string/strstr.h>
+#include <sys/auxv.h>
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+TEST(LlvmLibcGetauxvalTest, Basic) {
+  EXPECT_THAT(LIBC_NAMESPACE::getauxval(AT_PAGESZ), returns(GT(0ul)));
+  const char *filename;
+  auto getfilename = [&filename]() {
+    auto value = LIBC_NAMESPACE::getauxval(AT_EXECFN);
+    filename = reinterpret_cast<const char *>(value);
+    return value;
+  };
+  EXPECT_THAT(getfilename(), returns(NE(0ul)));
+  ASSERT_TRUE(LIBC_NAMESPACE::strstr(filename, "getauxval_test") != nullptr);
+}

>From 6ec61d7f95e600cd40bb829804c70ade25f4d757 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 22 Jan 2024 14:35:10 -0500
Subject: [PATCH 03/10] more comments

---
 libc/src/sys/auxv/linux/getauxval.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libc/src/sys/auxv/linux/getauxval.cpp b/libc/src/sys/auxv/linux/getauxval.cpp
index 4b2aabe40c39b5..dedd2c39119153 100644
--- a/libc/src/sys/auxv/linux/getauxval.cpp
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -29,7 +29,9 @@
 // getauxval will work either with or without __cxa_atexit support.
 // In order to detect if __cxa_atexit is supported, we define a weak symbol.
 // We prefer __cxa_atexit as it is always defined as a C symbol whileas atexit
-// may not be created via objcopy yet.
+// may not be created via objcopy yet. Also, for glibc, atexit is provided via
+// libc_nonshared.a rather than libc.so. So, it is may not be made ready for
+// overlay builds.
 extern "C" [[gnu::weak]] int __cxa_atexit(void (*callback)(void *),
                                           void *payload, void *);
 

>From 82dd9efdda2d1b9219275bd5b2735e0ae56c5a6e Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 22 Jan 2024 16:29:51 -0500
Subject: [PATCH 04/10] address CR

---
 libc/src/sys/auxv/linux/getauxval.cpp | 64 +++++++++++++++++----------
 1 file changed, 40 insertions(+), 24 deletions(-)

diff --git a/libc/src/sys/auxv/linux/getauxval.cpp b/libc/src/sys/auxv/linux/getauxval.cpp
index dedd2c39119153..96c0a2ba814186 100644
--- a/libc/src/sys/auxv/linux/getauxval.cpp
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -40,20 +40,24 @@ namespace LIBC_NAMESPACE {
 constexpr static size_t MAX_AUXV_ENTRIES = 64;
 
 // Helper to recover or set errno
-struct AuxvErrnoGuard {
-  int saved;
-  bool failure;
+class AuxvErrnoGuard {
+public:
   AuxvErrnoGuard() : saved(libc_errno), failure(false) {}
   ~AuxvErrnoGuard() { libc_errno = failure ? ENOENT : saved; }
   void mark_failure() { failure = true; }
+
+private:
+  int saved;
+  bool failure;
 };
 
 // Helper to manage the memory
 static AuxEntry *auxv = nullptr;
 
-struct AuxvMMapGuard {
+class AuxvMMapGuard {
+public:
   constexpr static size_t AUXV_MMAP_SIZE = sizeof(AuxEntry) * MAX_AUXV_ENTRIES;
-  void *ptr;
+
   AuxvMMapGuard()
       : ptr(mmap(nullptr, AUXV_MMAP_SIZE, PROT_READ | PROT_WRITE,
                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) {}
@@ -77,29 +81,37 @@ struct AuxvMMapGuard {
     auxv = reinterpret_cast<AuxEntry *>(ptr);
     ptr = MAP_FAILED;
   }
-  bool allocated() { return ptr != MAP_FAILED; }
+  bool allocated() const { return ptr != MAP_FAILED; }
+  void *get() const { return ptr; }
+
+private:
+  void *ptr;
 };
 
-struct AuxvFdGuard {
-  int fd;
+class AuxvFdGuard {
+public:
   AuxvFdGuard() : fd(open("/proc/self/auxv", O_RDONLY | O_CLOEXEC)) {}
   ~AuxvFdGuard() {
     if (fd != -1) {
       close(fd);
     }
   }
-  bool valid() { return fd != -1; }
+  bool valid() const { return fd != -1; }
+  int get() const { return fd; }
+
+private:
+  int fd;
 };
 
 static void initialize_auxv_once(void) {
-  // if we cannot get atexit, we cannot register the cleanup function.
+  // If we cannot get atexit, we cannot register the cleanup function.
   if (&__cxa_atexit == nullptr)
     return;
 
   AuxvMMapGuard mmap_guard;
   if (!mmap_guard.allocated())
     return;
-  auto *ptr = reinterpret_cast<AuxEntry *>(mmap_guard.ptr);
+  auto *ptr = reinterpret_cast<AuxEntry *>(mmap_guard.get());
 
   // We get one less than the max size to make sure the search always
   // terminates. MMAP private pages are zeroed out already.
@@ -118,16 +130,20 @@ static void initialize_auxv_once(void) {
   auto *buf = reinterpret_cast<char *>(ptr);
   libc_errno = 0;
   bool error_detected = false;
+  // Read until we use up all the available space or we finish reading the file.
   while (available_size != 0) {
-    ssize_t bytes_read = read(fd_guard.fd, buf, available_size);
+    ssize_t bytes_read = read(fd_guard.get(), buf, available_size);
     if (bytes_read <= 0) {
       if (libc_errno == EINTR)
         continue;
-      error_detected = bytes_read < 0;
+      if (bytes_read == -1)
+        error_detected = true;
       break;
     }
+    buf += bytes_read;
     available_size -= bytes_read;
   }
+  // If we get out of the loop without an error, the auxv is ready.
   if (!error_detected) {
     mmap_guard.submit_to_global();
   }
@@ -136,15 +152,18 @@ static void initialize_auxv_once(void) {
 static AuxEntry read_entry(int fd) {
   AuxEntry buf;
   ssize_t size = sizeof(AuxEntry);
+  char *ptr = reinterpret_cast<char *>(&buf);
   while (size > 0) {
-    ssize_t ret = read(fd, &buf, size);
+    ssize_t ret = read(fd, ptr, size);
     if (ret < 0) {
       if (libc_errno == EINTR)
         continue;
+      // Error detected, return AT_NULL
       buf.id = AT_NULL;
       buf.value = AT_NULL;
       break;
     }
+    ptr += ret;
     size -= ret;
   }
   return buf;
@@ -158,32 +177,29 @@ LLVM_LIBC_FUNCTION(unsigned long, getauxval, (unsigned long id)) {
 
   auto search_auxv = [&errno_guard](AuxEntry *auxv,
                                     unsigned long id) -> AuxEntryType {
-    for (auto *ptr = auxv; ptr->id != AT_NULL; ptr++) {
-      if (ptr->id == id) {
+    for (auto *ptr = auxv; ptr->id != AT_NULL; ptr++)
+      if (ptr->id == id)
         return ptr->value;
-      }
-    }
+
     errno_guard.mark_failure();
     return AT_NULL;
   };
 
   // App is a weak symbol that is only defined if libc is linked to its own
   // initialization routine. We need to check if it is null.
-  if (&app != nullptr) {
+  if (&app != nullptr)
     return search_auxv(app.auxv_ptr, id);
-  }
 
   static FutexWordType once_flag;
   callonce(reinterpret_cast<CallOnceFlag *>(&once_flag), initialize_auxv_once);
-  if (auxv != nullptr) {
+  if (auxv != nullptr)
     return search_auxv(auxv, id);
-  }
 
-  // fallback to use read without mmap
+  // Fallback to use read without mmap
   AuxvFdGuard fd_guard;
   if (fd_guard.valid()) {
     while (true) {
-      AuxEntry buf = read_entry(fd_guard.fd);
+      AuxEntry buf = read_entry(fd_guard.get());
       if (buf.id == AT_NULL)
         break;
       if (buf.id == id)

>From 77e33b2a116226840167d80773717873dd15e6e8 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 22 Jan 2024 16:32:20 -0500
Subject: [PATCH 05/10] address CR

---
 libc/src/sys/auxv/linux/getauxval.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libc/src/sys/auxv/linux/getauxval.cpp b/libc/src/sys/auxv/linux/getauxval.cpp
index 96c0a2ba814186..27fb68f6860d2b 100644
--- a/libc/src/sys/auxv/linux/getauxval.cpp
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -116,7 +116,11 @@ static void initialize_auxv_once(void) {
   // We get one less than the max size to make sure the search always
   // terminates. MMAP private pages are zeroed out already.
   size_t available_size = AuxvMMapGuard::AUXV_MMAP_SIZE - sizeof(AuxEntryType);
-#if defined(PR_GET_AUXV)
+  // PR_GET_AUXV is only available on Linux kernel 6.1 and above. If this is not
+  // defined, we direcly fall back to reading /proc/self/auxv. In case the libc
+  // is compiled and run on separate kernels, we also check the return value of
+  // prctl.
+#ifdef(PR_GET_AUXV)
   int ret = prctl(PR_GET_AUXV, reinterpret_cast<unsigned long>(ptr),
                   available_size, 0, 0);
   if (ret >= 0) {

>From 93f2e7b3251b45f51b87187a9952fb074229cfd6 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 22 Jan 2024 16:35:15 -0500
Subject: [PATCH 06/10] address CR

---
 libc/src/sys/auxv/linux/getauxval.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/sys/auxv/linux/getauxval.cpp b/libc/src/sys/auxv/linux/getauxval.cpp
index 27fb68f6860d2b..73520a422a1b2a 100644
--- a/libc/src/sys/auxv/linux/getauxval.cpp
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -120,7 +120,7 @@ static void initialize_auxv_once(void) {
   // defined, we direcly fall back to reading /proc/self/auxv. In case the libc
   // is compiled and run on separate kernels, we also check the return value of
   // prctl.
-#ifdef(PR_GET_AUXV)
+#ifdef PR_GET_AUXV
   int ret = prctl(PR_GET_AUXV, reinterpret_cast<unsigned long>(ptr),
                   available_size, 0, 0);
   if (ret >= 0) {

>From 117c5ccd88f2480ad224458e2254ec80d87cf3a4 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 22 Jan 2024 16:44:53 -0500
Subject: [PATCH 07/10] address CR

---
 libc/src/sys/auxv/linux/getauxval.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/src/sys/auxv/linux/getauxval.cpp b/libc/src/sys/auxv/linux/getauxval.cpp
index 73520a422a1b2a..1b31e70680c18f 100644
--- a/libc/src/sys/auxv/linux/getauxval.cpp
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -148,9 +148,8 @@ static void initialize_auxv_once(void) {
     available_size -= bytes_read;
   }
   // If we get out of the loop without an error, the auxv is ready.
-  if (!error_detected) {
+  if (!error_detected)
     mmap_guard.submit_to_global();
-  }
 }
 
 static AuxEntry read_entry(int fd) {

>From 01013880ad6f426e2c88de64ed95dbdaeb908788 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 22 Jan 2024 16:49:23 -0500
Subject: [PATCH 08/10] clarify error_detected

---
 libc/src/sys/auxv/linux/getauxval.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/src/sys/auxv/linux/getauxval.cpp b/libc/src/sys/auxv/linux/getauxval.cpp
index 1b31e70680c18f..3018fa5c3ed192 100644
--- a/libc/src/sys/auxv/linux/getauxval.cpp
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -140,6 +140,8 @@ static void initialize_auxv_once(void) {
     if (bytes_read <= 0) {
       if (libc_errno == EINTR)
         continue;
+      // Now, we either have an non-recoverable error or we have reached the end
+      // of the file. Mark `error_detected` accordingly.
       if (bytes_read == -1)
         error_detected = true;
       break;

>From 2a9758ab7e3135f94d380b0b03ff2b57e7b87933 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 22 Jan 2024 17:08:27 -0500
Subject: [PATCH 09/10] clean code style

---
 libc/src/sys/auxv/linux/getauxval.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/libc/src/sys/auxv/linux/getauxval.cpp b/libc/src/sys/auxv/linux/getauxval.cpp
index 3018fa5c3ed192..b0db36732c06e1 100644
--- a/libc/src/sys/auxv/linux/getauxval.cpp
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -62,9 +62,8 @@ class AuxvMMapGuard {
       : ptr(mmap(nullptr, AUXV_MMAP_SIZE, PROT_READ | PROT_WRITE,
                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) {}
   ~AuxvMMapGuard() {
-    if (ptr != MAP_FAILED) {
+    if (ptr != MAP_FAILED)
       munmap(ptr, AUXV_MMAP_SIZE);
-    }
   }
   void submit_to_global() {
     // atexit may fail, we do not set it to global in that case.
@@ -92,9 +91,8 @@ class AuxvFdGuard {
 public:
   AuxvFdGuard() : fd(open("/proc/self/auxv", O_RDONLY | O_CLOEXEC)) {}
   ~AuxvFdGuard() {
-    if (fd != -1) {
+    if (fd != -1)
       close(fd);
-    }
   }
   bool valid() const { return fd != -1; }
   int get() const { return fd; }

>From 83fcd4903903efd2038541877b7335cdfa20760b Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 22 Jan 2024 19:49:55 -0500
Subject: [PATCH 10/10] fix UB

---
 libc/test/src/sys/auxv/linux/getauxval_test.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/test/src/sys/auxv/linux/getauxval_test.cpp b/libc/test/src/sys/auxv/linux/getauxval_test.cpp
index 3b0c4e1b4175f2..8811fd8dfbc3a4 100644
--- a/libc/test/src/sys/auxv/linux/getauxval_test.cpp
+++ b/libc/test/src/sys/auxv/linux/getauxval_test.cpp
@@ -15,13 +15,14 @@
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
 
 TEST(LlvmLibcGetauxvalTest, Basic) {
-  EXPECT_THAT(LIBC_NAMESPACE::getauxval(AT_PAGESZ), returns(GT(0ul)));
+  EXPECT_THAT(LIBC_NAMESPACE::getauxval(AT_PAGESZ),
+              returns(GT(0ul)).with_errno(EQ(0)));
   const char *filename;
   auto getfilename = [&filename]() {
     auto value = LIBC_NAMESPACE::getauxval(AT_EXECFN);
     filename = reinterpret_cast<const char *>(value);
     return value;
   };
-  EXPECT_THAT(getfilename(), returns(NE(0ul)));
+  EXPECT_THAT(getfilename(), returns(NE(0ul)).with_errno(EQ(0)));
   ASSERT_TRUE(LIBC_NAMESPACE::strstr(filename, "getauxval_test") != nullptr);
 }



More information about the libc-commits mailing list