Skip to content

Commit 0e2fcff

Browse files
fix bugs
1 parent 579c5ae commit 0e2fcff

File tree

5 files changed

+62
-11
lines changed

5 files changed

+62
-11
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
#include "src/unistd/close.h"
2727
#include "src/unistd/read.h"
2828

29-
// getauxval will work either with or without atexit support.
30-
// In order to detect if atexit is supported, we define a weak symbol.
31-
extern "C" [[gnu::weak]] int atexit(void (*)(void));
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.
33+
extern "C" [[gnu::weak]] int __cxa_atexit(void (*callback)(void *),
34+
void *payload, void *);
3235

3336
namespace LIBC_NAMESPACE {
3437

@@ -49,19 +52,22 @@ static AuxEntry *auxv = nullptr;
4952
struct AuxvMMapGuard {
5053
constexpr static size_t AUXV_MMAP_SIZE = sizeof(AuxEntry) * MAX_AUXV_ENTRIES;
5154
void *ptr;
52-
AuxvMMapGuard(size_t size)
53-
: ptr(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, -1, 0)) {}
55+
AuxvMMapGuard()
56+
: ptr(mmap(nullptr, AUXV_MMAP_SIZE, PROT_READ | PROT_WRITE,
57+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) {}
5458
~AuxvMMapGuard() {
5559
if (ptr != MAP_FAILED) {
5660
munmap(ptr, AUXV_MMAP_SIZE);
5761
}
5862
}
5963
void submit_to_global() {
6064
// atexit may fail, we do not set it to global in that case.
61-
int ret = atexit([]() {
62-
munmap(auxv, AUXV_MMAP_SIZE);
63-
auxv = nullptr;
64-
});
65+
int ret = __cxa_atexit(
66+
[](void *) {
67+
munmap(auxv, AUXV_MMAP_SIZE);
68+
auxv = nullptr;
69+
},
70+
nullptr, nullptr);
6571

6672
if (ret != 0)
6773
return;
@@ -85,10 +91,10 @@ struct AuxvFdGuard {
8591

8692
static void initialize_auxv_once(void) {
8793
// if we cannot get atexit, we cannot register the cleanup function.
88-
if (&atexit == nullptr)
94+
if (&__cxa_atexit == nullptr)
8995
return;
9096

91-
AuxvMMapGuard mmap_guard(AuxvMMapGuard::AUXV_MMAP_SIZE);
97+
AuxvMMapGuard mmap_guard;
9298
if (!mmap_guard.allocated())
9399
return;
94100
auto *ptr = reinterpret_cast<AuxEntry *>(mmap_guard.ptr);

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)
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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), returns(GT(0ul)));
19+
const char *filename;
20+
auto getfilename = [&filename]() {
21+
auto value = LIBC_NAMESPACE::getauxval(AT_EXECFN);
22+
filename = reinterpret_cast<const char *>(value);
23+
return value;
24+
};
25+
EXPECT_THAT(getfilename(), returns(NE(0ul)));
26+
ASSERT_TRUE(LIBC_NAMESPACE::strstr(filename, "getauxval_test") != nullptr);
27+
}

0 commit comments

Comments
 (0)