Skip to content

Commit e07914b

Browse files
[libc] fix sysconf test for rv32 (#162685)
1 parent 1b627da commit e07914b

File tree

7 files changed

+21
-6
lines changed

7 files changed

+21
-6
lines changed

libc/src/__support/OSUtil/linux/auxv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <linux/auxvec.h> // For AT_ macros
1818
#include <linux/mman.h> // For mmap flags
19+
#include <linux/param.h> // For EXEC_PAGESIZE
1920
#include <linux/prctl.h> // For prctl
2021
#include <sys/syscall.h> // For syscall numbers
2122

@@ -90,7 +91,7 @@ LIBC_INLINE void Vector::fallback_initialize_unsync() {
9091
PROT_READ | PROT_WRITE,
9192
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
9293
// We do not proceed if mmap fails.
93-
if (mmap_ret <= 0)
94+
if (!linux_utils::is_valid_mmap(mmap_ret))
9495
return;
9596

9697
// Initialize the auxv array with AT_NULL entries.

libc/src/__support/OSUtil/linux/syscall.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/CPP/bit.h"
1313
#include "src/__support/common.h"
1414
#include "src/__support/macros/config.h"
15+
#include "src/__support/macros/optimization.h"
1516
#include "src/__support/macros/properties/architectures.h"
1617

1718
#ifdef LIBC_TARGET_ARCH_IS_X86_32
@@ -34,6 +35,19 @@ LIBC_INLINE R syscall_impl(long __number, Ts... ts) {
3435
return cpp::bit_or_static_cast<R>(syscall_impl(__number, (long)ts...));
3536
}
3637

38+
// Linux-specific function for checking
39+
namespace linux_utils {
40+
LIBC_INLINE_VAR constexpr unsigned long MAX_ERRNO = 4095;
41+
// Ideally, this should be defined using PAGE_OFFSET
42+
// However, that is a configurable parameter. We mimic kernel's behavior
43+
// by checking against MAX_ERRNO.
44+
template <typename PointerLike>
45+
LIBC_INLINE constexpr bool is_valid_mmap(PointerLike ptr) {
46+
long addr = cpp::bit_cast<long>(ptr);
47+
return LIBC_LIKELY(addr > 0 || addr < -static_cast<long>(MAX_ERRNO));
48+
}
49+
} // namespace linux_utils
50+
3751
} // namespace LIBC_NAMESPACE_DECL
3852

3953
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_H

libc/src/__support/threads/linux/thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ LIBC_INLINE ErrorOr<void *> alloc_stack(size_t stacksize, size_t guardsize) {
100100
-1, // Not backed by any file
101101
0 // No offset
102102
);
103-
if (mmap_result < 0 && (uintptr_t(mmap_result) >= UINTPTR_MAX - size))
103+
if (!linux_utils::is_valid_mmap(mmap_result))
104104
return Error{int(-mmap_result)};
105105

106106
if (guardsize) {

libc/src/sys/mman/linux/mmap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ LLVM_LIBC_FUNCTION(void *, mmap,
5656
// However, since a valid return address cannot be within the last page, a
5757
// return value corresponding to a location in the last page is an error
5858
// value.
59-
if (ret < 0 && ret > -EXEC_PAGESIZE) {
59+
if (!linux_utils::is_valid_mmap(ret)) {
6060
libc_errno = static_cast<int>(-ret);
6161
return MAP_FAILED;
6262
}

libc/startup/linux/aarch64/tls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void init_tls(TLSDescriptor &tls_descriptor) {
6262
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
6363
// We cannot check the return value with MAP_FAILED as that is the return
6464
// of the mmap function and not the mmap syscall.
65-
if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.page_size)
65+
if (!linux_utils::is_valid_mmap(mmap_ret_val))
6666
syscall_impl<long>(SYS_exit, 1);
6767
uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
6868
uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;

libc/startup/linux/riscv/tls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void init_tls(TLSDescriptor &tls_descriptor) {
5050
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
5151
// We cannot check the return value with MAP_FAILED as that is the return
5252
// of the mmap function and not the mmap syscall.
53-
if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.page_size)
53+
if (!linux_utils::is_valid_mmap(mmap_ret_val))
5454
syscall_impl<long>(SYS_exit, 1);
5555
uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
5656
uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;

libc/startup/linux/x86_64/tls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void init_tls(TLSDescriptor &tls_descriptor) {
5353
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
5454
// We cannot check the return value with MAP_FAILED as that is the return
5555
// of the mmap function and not the mmap syscall.
56-
if (mmap_retval < 0 && static_cast<uintptr_t>(mmap_retval) > -app.page_size)
56+
if (!linux_utils::is_valid_mmap(mmap_retval))
5757
syscall_impl<long>(SYS_exit, 1);
5858
uintptr_t *tls_addr = reinterpret_cast<uintptr_t *>(mmap_retval);
5959

0 commit comments

Comments
 (0)