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

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Wed Jan 17 11:31:51 PST 2024


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

None

>From 047864138a4a1b227998121d79d80e62d60a362f 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] [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    |  19 +++
 libc/src/sys/auxv/linux/getauxval.cpp     | 176 ++++++++++++++++++++++
 10 files changed, 239 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 ab54633006979b..37197a26e16d69 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 274d5aa5a0057d..5bcf86a8063e26 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..f57100e422cb93
--- /dev/null
+++ b/libc/src/sys/auxv/linux/CMakeLists.txt
@@ -0,0 +1,19 @@
+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.src.stdlib.atexit
+    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..9558aa903fb7e3
--- /dev/null
+++ b/libc/src/sys/auxv/linux/getauxval.cpp
@@ -0,0 +1,176 @@
+//===-- 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/CPP/atomic.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/callonce.h"
+#include "src/__support/threads/linux/futex_word.h"
+#include "src/errno/libc_errno.h"
+#include "src/fcntl/open.h"
+#include "src/stdlib/atexit.h"
+#include "src/sys/mman/mmap.h"
+#include "src/sys/mman/munmap.h"
+#include "src/sys/prctl/prctl.h"
+#include "src/unistd/close.h"
+#include "src/unistd/read.h"
+#include <linux/auxvec.h>
+
+namespace LIBC_NAMESPACE {
+
+constexpr static size_t AUXV_SIZE = 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) * AUXV_SIZE;
+  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) {
+  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.
+  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 (true) {
+    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) {
+    for (auto *ptr = auxv; ptr->id != AT_NULL; ptr++) {
+      if (ptr->id == id) {
+        return ptr->value;
+      }
+    }
+    errno_guard.mark_failure();
+    return AuxEntryType{0};
+  };
+
+  if (&app != nullptr) {
+    return search_auxv(app.auxv_ptr, id);
+  }
+
+  static cpp::Atomic<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
+  do {
+    AuxvFdGuard fd_guard;
+    if (!fd_guard.valid())
+      break;
+
+    while (true) {
+      AuxEntry buf = read_entry(fd_guard.fd);
+      if (buf.id == AT_NULL)
+        break;
+      if (buf.id == id)
+        return buf.value;
+    }
+
+  } while (0);
+  errno_guard.mark_failure();
+  return 0;
+}
+} // namespace LIBC_NAMESPACE



More information about the libc-commits mailing list