Skip to contentSkip to conversation

linux-user: fix issues with mmap on 4K guest on 16K host and library load

PATCH In review Andreas Kemnade 3 participants 4 messages Aug 5 – Aug 8

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: fix issues with mmap on 4K guest on 16K host and library load

Strace in qemu:
198005 openat(AT_FDCWD,"/lib/arm-linux-gnueabihf/libselinux.so.1",O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3 198005 read(3,0x40802570,512) = 512
198005 statx(3,"",AT_EMPTY_PATH|AT_NO_AUTOMOUNT|AT_STATX_SYNC_AS_STAT,STATX_BASIC_STATS,0x408022a8) = 0 198005 mmap2(NULL,269428,PROT_NONE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_DENYWRITE,-1,0) = 0x40868000
198005 mmap2(0x40870000,203892,PROT_EXEC|PROT_READ,MAP_PRIVATE|MAP_DENYWRITE|MAP_FIXED,3,0) = 0x40870000 198005 munmap(0x40868000,32768) = 0
198005 munmap(0x408a2000,31860) = 0
198005 mprotect(0x40890000,61440,PROT_NONE) = 0
198005 mmap2(0x4089f000,8192,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_DENYWRITE|MAP_FIXED,3,0x1f) = -1 errno=14 (Bad address)

strace outside qemu:
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib/arm-linux-gnueabihf", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib/arm-linux-gnueabihf/libselinux.so.1", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] faccessat(AT_FDCWD, "/usr/gnemul/qemu-arm/lib/arm-linux-gnueabihf/libselinux.so.1", F_OK) = -1 ENOENT (No such file or directory)
[pid 195331] openat(AT_FDCWD, "/lib/arm-linux-gnueabihf/libselinux.so.1", O_RDONLY|O_CLOEXEC) = 3
[pid 195331] read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\0\0\0\0004\0\0\0"..., 512) = 512
[pid 195331] statx(3, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_BASIC_STATS, {stx_mask=STATX_ALL|STATX_MNT_ID|STATX_SUBVOL, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=132612, ...}) = 0 [pid 195331] mmap(0x408a8000, 16384, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_DENYWRITE, -1, 0) = 0x408a8000
[pid 195331] mprotect(0x408a8000, 16384, PROT_WRITE) = 0
[pid 195331] mprotect(0x408a8000, 16384, PROT_NONE) = 0
[pid 195331] mmap(0x40868000, 262144, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_DENYWRITE, -1, 0) = 0x40868000
[pid 195331] mprotect(0x408a0000, 16384, PROT_WRITE) = 0
[pid 195331] pread64(3, "", 8192, 196608) = 0
[pid 195331] mprotect(0x408a0000, 16384, PROT_READ) = 0
[pid 195331] mmap(0x40870000, 196608, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0) = 0x40870000

[pid 195331] mmap(0x40868000, 32768, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x40868000
[pid 195331] mmap(0x408a4000, 32768, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x408a4000

[pid 195331] mprotect(0x4089c000, 16384, PROT_READ) = 0
[pid 195331] mprotect(0x40890000, 49152, PROT_NONE) = 0
[pid 195331] mprotect(0x4089c000, 16384, PROT_WRITE) = 0
[pid 195331] pread64(3, 0x4089f000, 4096, 126976) = -1 EFAULT (Bad address)

That makes simple commands like ls --version to fail. Tested was arm32 on a arm64 only host.

Fix that issue by truncating file mapping and replacing out-of-file mappings by anonymous mappings.

---
 linux-user/mmap.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
linux-user/mmap.c +26 -0
index 423c77856a..4fce6fae3c 100644
@@ -796,6 +796,32 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len,
     bool misaligned_offset = false;
     size_t host_len;
 
+    if (!(flags & MAP_ANONYMOUS)) {
+        struct stat sb;
+
+        if (fstat(fd, &sb) == -1) {
+            return -1;
+        }
+        if (offset >= sb.st_size) {
+            /*
+             * The entire map is beyond the end of the file.
+             * Transform it to an anonymous mapping.
+             */
+            flags |= MAP_ANONYMOUS;
+            fd = -1;
+            offset = 0;
+        } else if (offset + len > sb.st_size) {
+            /*
+             * A portion of the map is beyond the end of the file.
+             * Truncate the file portion of the allocation.
+             */
+            len = sb.st_size - offset;
+            len = len + host_page_size - 1;
+            len /= host_page_size;
+            len *= host_page_size;
+        }
+    }
+
     if (start || (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) {
         want_p = g2h_untagged(start);
     }
Signature · 1 line
2.47.3
#1 lore raw
Quoted 34 lines · Andreas Kemnade

Strace in qemu:
198005 openat(AT_FDCWD,"/lib/arm-linux-gnueabihf/libselinux.so.1",O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3 198005 read(3,0x40802570,512) = 512
198005 statx(3,"",AT_EMPTY_PATH|AT_NO_AUTOMOUNT|AT_STATX_SYNC_AS_STAT,STATX_BASIC_STATS,0x408022a8) = 0 198005 mmap2(NULL,269428,PROT_NONE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_DENYWRITE,-1,0) = 0x40868000
198005 mmap2(0x40870000,203892,PROT_EXEC|PROT_READ,MAP_PRIVATE|MAP_DENYWRITE|MAP_FIXED,3,0) = 0x40870000 198005 munmap(0x40868000,32768) = 0
198005 munmap(0x408a2000,31860) = 0
198005 mprotect(0x40890000,61440,PROT_NONE) = 0
198005 mmap2(0x4089f000,8192,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_DENYWRITE|MAP_FIXED,3,0x1f) = -1 errno=14 (Bad address)

strace outside qemu:
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib/arm-linux-gnueabihf", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib/arm-linux-gnueabihf/libselinux.so.1", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] faccessat(AT_FDCWD, "/usr/gnemul/qemu-arm/lib/arm-linux-gnueabihf/libselinux.so.1", F_OK) = -1 ENOENT (No such file or directory)
[pid 195331] openat(AT_FDCWD, "/lib/arm-linux-gnueabihf/libselinux.so.1", O_RDONLY|O_CLOEXEC) = 3
[pid 195331] read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\0\0\0\0004\0\0\0"..., 512) = 512
[pid 195331] statx(3, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_BASIC_STATS, {stx_mask=STATX_ALL|STATX_MNT_ID|STATX_SUBVOL, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=132612, ...}) = 0 [pid 195331] mmap(0x408a8000, 16384, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_DENYWRITE, -1, 0) = 0x408a8000
[pid 195331] mprotect(0x408a8000, 16384, PROT_WRITE) = 0
[pid 195331] mprotect(0x408a8000, 16384, PROT_NONE) = 0
[pid 195331] mmap(0x40868000, 262144, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_DENYWRITE, -1, 0) = 0x40868000
[pid 195331] mprotect(0x408a0000, 16384, PROT_WRITE) = 0
[pid 195331] pread64(3, "", 8192, 196608) = 0
[pid 195331] mprotect(0x408a0000, 16384, PROT_READ) = 0
[pid 195331] mmap(0x40870000, 196608, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0) = 0x40870000

[pid 195331] mmap(0x40868000, 32768, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x40868000
[pid 195331] mmap(0x408a4000, 32768, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x408a4000

[pid 195331] mprotect(0x4089c000, 16384, PROT_READ) = 0
[pid 195331] mprotect(0x40890000, 49152, PROT_NONE) = 0
[pid 195331] mprotect(0x4089c000, 16384, PROT_WRITE) = 0
[pid 195331] pread64(3, 0x4089f000, 4096, 126976) = -1 EFAULT (Bad address)

Quoted 5 lines

That makes simple commands like ls --version to fail. Tested was arm32 on a arm64 only host.

Fix that issue by truncating file mapping and replacing out-of-file mappings by anonymous mappings.

Could you please explain "the issue" and not just referring to the strace? It's not that easy to see the problem.

Thanks!
Helge

Btw: you patch might fix this problem, but you may run into other problems too, see e.g.: https://gitlab.com/qemu-project/qemu/-/work_items/3410

Quoted 42 lines
---
  linux-user/mmap.c | 26 ++++++++++++++++++++++++++
  1 file changed, 26 insertions(+)
linux-user/mmap.c +26 -0
index 423c77856a..4fce6fae3c 100644
@@ -796,6 +796,32 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len,
      bool misaligned_offset = false;
      size_t host_len;
 
+    if (!(flags & MAP_ANONYMOUS)) {
+        struct stat sb;
+
+        if (fstat(fd, &sb) == -1) {
+            return -1;
+        }
+        if (offset >= sb.st_size) {
+            /*
+             * The entire map is beyond the end of the file.
+             * Transform it to an anonymous mapping.
+             */
+            flags |= MAP_ANONYMOUS;
+            fd = -1;
+            offset = 0;
+        } else if (offset + len > sb.st_size) {
+            /*
+             * A portion of the map is beyond the end of the file.
+             * Truncate the file portion of the allocation.
+             */
+            len = sb.st_size - offset;
+            len = len + host_page_size - 1;
+            len /= host_page_size;
+            len *= host_page_size;
+        }
+    }
+
      if (start || (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) {
          want_p = g2h_untagged(start);
      }
#2 lore raw
Quoted 69 lines · Helge Deller
Quoted 34 lines · Andreas Kemnade

Strace in qemu:
198005 openat(AT_FDCWD,"/lib/arm-linux-gnueabihf/libselinux.so.1",O_RDONLY|O_LARGEFILE| O_CLOEXEC) = 3 198005 read(3,0x40802570,512) = 512
198005 statx(3,"",AT_EMPTY_PATH|AT_NO_AUTOMOUNT| AT_STATX_SYNC_AS_STAT,STATX_BASIC_STATS,0x408022a8) = 0 198005 mmap2(NULL,269428,PROT_NONE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_DENYWRITE,-1,0) = 0x40868000
198005 mmap2(0x40870000,203892,PROT_EXEC|PROT_READ,MAP_PRIVATE|MAP_DENYWRITE| MAP_FIXED,3,0) = 0x40870000 198005 munmap(0x40868000,32768) = 0
198005 munmap(0x408a2000,31860) = 0
198005 mprotect(0x40890000,61440,PROT_NONE) = 0
198005 mmap2(0x4089f000,8192,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_DENYWRITE| MAP_FIXED,3,0x1f) = -1 errno=14 (Bad address)

strace outside qemu:
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib/arm-linux-gnueabihf", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib/arm-linux-gnueabihf/libselinux.so.1", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] faccessat(AT_FDCWD, "/usr/gnemul/qemu-arm/lib/arm-linux-gnueabihf/ libselinux.so.1", F_OK) = -1 ENOENT (No such file or directory)
[pid 195331] openat(AT_FDCWD, "/lib/arm-linux-gnueabihf/libselinux.so.1", O_RDONLY| O_CLOEXEC) = 3
[pid 195331] read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\0\0\0\0004\0\0\0"..., 512) = 512
[pid 195331] statx(3, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_BASIC_STATS, {stx_mask=STATX_ALL|STATX_MNT_ID|STATX_SUBVOL, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=132612, ...}) = 0 [pid 195331] mmap(0x408a8000, 16384, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS| MAP_DENYWRITE, -1, 0) = 0x408a8000
[pid 195331] mprotect(0x408a8000, 16384, PROT_WRITE) = 0
[pid 195331] mprotect(0x408a8000, 16384, PROT_NONE) = 0
[pid 195331] mmap(0x40868000, 262144, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS| MAP_DENYWRITE, -1, 0) = 0x40868000
[pid 195331] mprotect(0x408a0000, 16384, PROT_WRITE) = 0
[pid 195331] pread64(3, "", 8192, 196608) = 0
[pid 195331] mprotect(0x408a0000, 16384, PROT_READ) = 0
[pid 195331] mmap(0x40870000, 196608, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0) = 0x40870000

[pid 195331] mmap(0x40868000, 32768, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS| MAP_NORESERVE, -1, 0) = 0x40868000
[pid 195331] mmap(0x408a4000, 32768, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS| MAP_NORESERVE, -1, 0) = 0x408a4000

[pid 195331] mprotect(0x4089c000, 16384, PROT_READ) = 0
[pid 195331] mprotect(0x40890000, 49152, PROT_NONE) = 0
[pid 195331] mprotect(0x4089c000, 16384, PROT_WRITE) = 0
[pid 195331] pread64(3, 0x4089f000, 4096, 126976) = -1 EFAULT (Bad address)

Quoted 5 lines

That makes simple commands like ls --version to fail. Tested was arm32 on a arm64 only host.

Fix that issue by truncating file mapping and replacing out-of-file mappings by anonymous mappings.

Could you please explain "the issue" and not just referring to the strace? It's not that easy to see the problem.

Thanks!
Helge

Btw: you patch might fix this problem, but you may run into other problems too, see e.g.: https://gitlab.com/qemu-project/qemu/-/work_items/3410

Quoted 18 lines

---
  linux-user/mmap.c | 26 ++++++++++++++++++++++++++

  1 file changed, 26 insertions(+)
linux-user/mmap.c +6 -0
index 423c77856a..4fce6fae3c 100644
@@ -796,6 +796,32 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len,
       bool misaligned_offset = false;
       size_t host_len;
+    if (!(flags & MAP_ANONYMOUS)) {
+        struct stat sb;
+
+        if (fstat(fd, &sb) == -1) {
+            return -1;
+        }
Quoted 8 lines

linux-user/mmap.c static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len, · line 812

Quoted 8 lines
linux-user/mmap.c +8 -0
+        if (offset >= sb.st_size) {
+            /*
+             * The entire map is beyond the end of the file.
+             * Transform it to an anonymous mapping.
+             */
+            flags |= MAP_ANONYMOUS;
+            fd = -1;
+            offset = 0;

This is incorrect. Even natively, accessing past the end of the file should SIGBUS.
For host page size > guest page size, as we are here, you should never encounter that particular emulation problem.

I've no idea what you're trying to solve.

r~

#3 lore raw
In reply to Helge Deller
Quoted 7 lines · On Fri, 7 Aug 2026 23:26:07 +0200 Helge Deller <[email protected]> wrote:
Quoted 6 lines · Andreas Kemnade

Strace in qemu:
198005 openat(AT_FDCWD,"/lib/arm-linux-gnueabihf/libselinux.so.1",O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3 198005 read(3,0x40802570,512) = 512
198005 statx(3,"",AT_EMPTY_PATH|AT_NO_AUTOMOUNT|AT_STATX_SYNC_AS_STAT,STATX_BASIC_STATS,0x408022a8) = 0 198005 mmap2(NULL,269428,PROT_NONE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_DENYWRITE,-1,0) = 0x40868000 198005 mmap2(0x40870000,203892,PROT_EXEC|PROT_READ,MAP_PRIVATE|MAP_DENYWRITE|MAP_FIXED,3,0) = 0x40870000

Size of the file: 132612: allocated: 203892

End address: 0x408a1c74

Quoted 3 lines
Quoted 3 lines

198005 munmap(0x40868000,32768) = 0
198005 munmap(0x408a2000,31860) = 0
198005 mprotect(0x40890000,61440,PROT_NONE) = 0

which just ends at 0x4089f000 (not 16K aligned)

Quoted 2 lines
Quoted 2 lines

198005 mmap2(0x4089f000,8192,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_DENYWRITE|MAP_FIXED,3,0x1f) = -1 errno=14 (Bad address)

mmap2 sitting in that area mapping a part of the same file.

Quoted 12 lines
Quoted 12 lines

strace outside qemu:
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib/arm-linux-gnueabihf", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] readlinkat(AT_FDCWD, "/usr/lib/arm-linux-gnueabihf/libselinux.so.1", 0x7fffd1a7fd00, 1023) = -1 EINVAL (Invalid argument)
[pid 195331] faccessat(AT_FDCWD, "/usr/gnemul/qemu-arm/lib/arm-linux-gnueabihf/libselinux.so.1", F_OK) = -1 ENOENT (No such file or directory)
[pid 195331] openat(AT_FDCWD, "/lib/arm-linux-gnueabihf/libselinux.so.1", O_RDONLY|O_CLOEXEC) = 3
[pid 195331] read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\0\0\0\0004\0\0\0"..., 512) = 512
[pid 195331] statx(3, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_BASIC_STATS, {stx_mask=STATX_ALL|STATX_MNT_ID|STATX_SUBVOL, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=132612, ...}) = 0 [pid 195331] mmap(0x408a8000, 16384, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_DENYWRITE, -1, 0) = 0x408a8000
[pid 195331] mprotect(0x408a8000, 16384, PROT_WRITE) = 0
[pid 195331] mprotect(0x408a8000, 16384, PROT_NONE) = 0
[pid 195331] mmap(0x40868000, 262144, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_DENYWRITE, -1, 0) = 0x40868000

Quoted 8 lines
Quoted 8 lines

[pid 195331] mprotect(0x408a0000, 16384, PROT_WRITE) = 0
[pid 195331] pread64(3, "", 8192, 196608) = 0
[pid 195331] mprotect(0x408a0000, 16384, PROT_READ) = 0
[pid 195331] mmap(0x40870000, 196608, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0) = 0x40870000

[pid 195331] mmap(0x40868000, 32768, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x40868000
[pid 195331] mmap(0x408a4000, 32768, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x408a4000

[pid 195331] mprotect(0x4089c000, 16384, PROT_READ) = 0 [pid 195331] mprotect(0x40890000, 49152, PROT_NONE) = 0

ends at 0x4089c000

Quoted 3 lines
Quoted 3 lines

[pid 195331] mprotect(0x4089c000, 16384, PROT_WRITE) = 0
[pid 195331] pread64(3, 0x4089f000, 4096, 126976) = -1 EFAULT (Bad address)

reads from write-only area created by the mprotect before.

Quoted 6 lines
Quoted 5 lines

That makes simple commands like ls --version to fail. Tested was arm32 on a arm64 only host.

Fix that issue by truncating file mapping and replacing out-of-file mappings by anonymous mappings.

Quoted 9 lines

Could you please explain "the issue" and not just referring to the strace? It's not that easy to see the problem.

Thanks!
Helge

Btw: you patch might fix this problem, but you may run into other problems too, see e.g.: https://gitlab.com/qemu-project/qemu/-/work_items/3410

That is correct, but at least increases the amount of platforms to be debootstrappable. Or maybe some better chances for same bitbake run to get through.

Regards,
Andreas