[libc-commits] [libc] d2d4c68 - [libc] Leave mmap offsets with the top bit set to the kernel (#239985)

via libc-commits libc-commits at lists.llvm.org
Mon Nov 16 01:26:05 PST 2026


Author: Mike Lothian
Date: 2026-11-16T10:25:58+01:00
New Revision: d2d4c6885e53b0a355fd50ba46101dfa49b83eb0

URL: https://github.com/llvm/llvm-project/commit/d2d4c6885e53b0a355fd50ba46101dfa49b83eb0
DIFF: https://github.com/llvm/llvm-project/commit/d2d4c6885e53b0a355fd50ba46101dfa49b83eb0.diff

LOG: [libc] Leave mmap offsets with the top bit set to the kernel (#239985)

The kernel reads the mmap offset as unsigned, and drivers use its top
bits
to tell mappings apart: KFD's doorbells sit at 0xebff000000000000.
Rejecting
negative offsets in libc made ROCr's doorbell mapping fail, and HIP then
died on its first dispatch. mmap2 keeps the check, as there the offset
is
divided before it reaches the kernel.

AI disclosure: this patch was written with Claude Code. I reviewed the
change.

Assisted-by: Claude

Added: 
    

Modified: 
    libc/src/__support/OSUtil/linux/syscall_wrappers/mmap.h
    libc/test/src/sys/mman/linux/mmap_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/mmap.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/mmap.h
index 0bda412368133e..a507cdda078a0e 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/mmap.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/mmap.h
@@ -22,9 +22,9 @@ namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<void *> mmap(void *addr, size_t size, int prot, int flags,
                                  int fd, off_t offset) {
+#ifdef SYS_mmap2
   if (offset < 0)
     return Error(EINVAL);
-#ifdef SYS_mmap2
   // The mmap2 syscall uses 4k units, regardless of the actual page, size on
   // almost every architecture. If porting to a new architecture (Openrisc,
   // hexagon?), please confirm this code is correct.
@@ -34,6 +34,8 @@ LIBC_INLINE ErrorOr<void *> mmap(void *addr, size_t size, int prot, int flags,
   offset /= MMAP2_FACTOR;
   long syscall_number = SYS_mmap2;
 #elif defined(SYS_mmap)
+  // The kernel takes the offset as unsigned, and some drivers expect large
+  // offsets with the top bit set. Leave validating the offset to the kernel.
   long syscall_number = SYS_mmap;
 #else
 #error "mmap or mmap2 syscalls not available."

diff  --git a/libc/test/src/sys/mman/linux/mmap_test.cpp b/libc/test/src/sys/mman/linux/mmap_test.cpp
index eb1edd13f6467b..ae141e7e3e4ae8 100644
--- a/libc/test/src/sys/mman/linux/mmap_test.cpp
+++ b/libc/test/src/sys/mman/linux/mmap_test.cpp
@@ -16,6 +16,8 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
+#include <sys/syscall.h> // For SYS_mmap2
+
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
 using LlvmLibcMMapTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
@@ -57,10 +59,25 @@ TEST_F(LlvmLibcMMapTest, FileOffsets) {
   ASSERT_ERRNO_SUCCESS();
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, 47), Succeeds());
 
-  // Mapping negative offsets fails.
+  // Mapping negative offsets fails. -1 is not page aligned, so this holds
+  // whichever of libc or the kernel looks at it.
   EXPECT_THAT(LIBC_NAMESPACE::mmap(nullptr, 47, PROT_READ, MAP_PRIVATE, fd, -1),
               Fails(EINVAL, MAP_FAILED));
 
+#ifndef SYS_mmap2
+  if constexpr (sizeof(long) == sizeof(off_t)) {
+    // An aligned offset with the top bit set is the kernel's to judge: it is
+    // unsigned there, and drivers such as KFD encode the kind of mapping in
+    // it. A memfd cannot be that large, so the kernel says EOVERFLOW, where
+    // libc rejecting the offset itself would say EINVAL. Targets that use
+    // mmap2 reject a negative offset in libc, as before.
+    EXPECT_THAT(
+        LIBC_NAMESPACE::mmap(nullptr, 47, PROT_READ, MAP_PRIVATE, fd,
+                             static_cast<off_t>(1) << (sizeof(off_t) * 8 - 1)),
+        Fails(EOVERFLOW, MAP_FAILED));
+  }
+#endif
+
   // So do offsets that are not page aligned. This should be rejected in the
   // kernel or by our mmap2 rounding code. Note that POSIX permits (but does not
   // require) mapping at unaligned offsets, but linux does not support it.


        


More information about the libc-commits mailing list