Fixes for unfixed mmap beyond reserved_va
cover Fixes for unfixed mmap beyond reserved_va
For reserved_va & ~MAP_FIXED mmap, linux-user and bsd-user translates it into a fixed mmap within [mmap_min_addr, reserved_va]. However, the previous implementation didn't care about where the address hint provided by user is greater than reserved_va and will run into an assertion failure.
This series fixed it for linux-user and bsd-user and added test coverage. For start > reserved_va, skip [start, reserved_va] and just search within [min_addr, reserved_va].
I actually thought that I am adding too many CC's and causing unnecessary noise but b4 is asking me to run auto-to-cc (:
---
Bingwu Zhang (4):
linux-user: Deal with mmap where start > reserved_va
bsd-user: Deal with mmap where start > reserved_va tests/tcg/multiarch/test-mmap: Print more details tests/tcg/multiarch/test-mmap: Check mmaps beyond reserved_va
bsd-user/mmap.c | 8 +++++--- linux-user/mmap.c | 9 ++++++--- tests/tcg/multiarch/test-mmap.c | 27 ++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 9 deletions(-)
---
base-commit: d8a9d97317d03190b34498741f98f22e2a9afe3e
change-id: 20260227-abyss-mmap-relocation-ccba2ab8902b
Best regards,
Signature · 1 line
Bingwu Zhang <[email protected]>
PATCH 1/4 linux-user: Deal with mmap where start > reserved_va
From: Bingwu Zhang <[email protected]>
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3310
--- linux-user/mmap.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
linux-user/mmap.c +6 -3
@@ -423,12 +423,15 @@ abi_ulong mmap_next_start;static abi_ulong mmap_find_vma_reserved (abi_ulong start,abi_ulong size,abi_ulong align) { -target_ulong ret; +target_ulong ret =-1 ; - ret =page_find_range_empty (start, reserved_va, size, align); +if (start <= reserved_va) { + ret =page_find_range_empty (start, reserved_va, size, align); + }if (ret ==-1 && start > mmap_min_addr) {/* Restart at the beginning of the address space. */ - ret =page_find_range_empty (mmap_min_addr, start -1 , size, align); + ret =page_find_range_empty (mmap_min_addr,MIN (start -1 , reserved_va), + size, align); }return ret;
Signature · 1 line
2.52.0
Re: [PATCH 1/4] linux-user: Deal with mmap where start > reserved_va
Quoted 5 lines · Bingwu Zhang
From: Bingwu Zhang <[email protected]>
Fixes 4c13048e02d9 ("linux-user: Use page_find_range_empty for mmap_find_vma_reserved")Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3310
Signed-off-by Bingwu Zhang <[email protected]>
After staring some time at this code, I believe this is correct, so:
Helge
Quoted 28 lines
--- linux-user/mmap.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)linux-user/mmap.c +6
-3@@ -423,12 +423,15 @@ abi_ulong mmap_next_start;static abi_ulong mmap_find_vma_reserved (abi_ulong start,abi_ulong size,abi_ulong align) { -target_ulong ret; +target_ulong ret =-1 ; - ret =page_find_range_empty (start, reserved_va, size, align); +if (start <= reserved_va) { + ret =page_find_range_empty (start, reserved_va, size, align); + }if (ret ==-1 && start > mmap_min_addr) {/* Restart at the beginning of the address space. */ - ret =page_find_range_empty (mmap_min_addr, start -1 , size, align); + ret =page_find_range_empty (mmap_min_addr,MIN (start -1 , reserved_va), + size, align); }return ret;
PATCH 2/4 bsd-user: Deal with mmap where start > reserved_va
In reply to Bingwu ZhangFrom: Bingwu Zhang <[email protected]>
--- bsd-user/mmap.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
bsd-user/mmap.c +5 -3
@@ -258,12 +258,14 @@ abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;static abi_ulong mmap_find_vma_reserved (abi_ulong start,abi_ulong size,abi_ulong alignment) { -abi_ulong ret; +abi_ulong ret =-1 ; - ret =page_find_range_empty (start, reserved_va, size, alignment); +if (start <= reserved_va) { + ret =page_find_range_empty (start, reserved_va, size, alignment); + }if (ret ==-1 && start >TARGET_PAGE_SIZE ) {/* Restart at the beginning of the address space. */ - ret =page_find_range_empty (TARGET_PAGE_SIZE , start -1 , + ret =page_find_range_empty (TARGET_PAGE_SIZE ,MIN (start -1 , reserved_va), size, alignment); }
Signature · 1 line
2.52.0
Re: [PATCH 2/4] bsd-user: Deal with mmap where start > reserved_va
Quoted 9 lines · Bingwu Zhang
From: Bingwu Zhang <[email protected]>
Fixes f12294b5bd21 ("bsd-user: Use page_find_range_empty for
mmap_find_vma_reserved")Signed-off-by Bingwu Zhang <[email protected]>--- bsd-user/mmap.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
Have you run FreeBSD binaries with this?
Warner
Quoted 28 lines
bsd-user/mmap.c +5
-4@@ -258,12 +258,14 @@ abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;static abi_ulong mmap_find_vma_reserved (abi_ulong start,abi_ulong size,abi_ulong alignment) { -abi_ulong ret; +abi_ulong ret =-1 ; - ret =page_find_range_empty (start, reserved_va, size, alignment); +if (start <= reserved_va) { + ret =page_find_range_empty (start, reserved_va, size, alignment); + }if (ret ==-1 && start >TARGET_PAGE_SIZE ) {/* Restart at the beginning of the address space. */ - ret =page_find_range_empty (TARGET_PAGE_SIZE , start -1 , + ret =page_find_range_empty (TARGET_PAGE_SIZE ,MIN (start -1 , reserved_va), size, alignment); } --2.52.0
Re: [PATCH 2/4] bsd-user: Deal with mmap where start > reserved_va
Quoted 1 line · China Standard Time Warner Losh
Reviewed-by Warner Losh <[email protected]>
Thank you!
Quoted 1 line
Have you run FreeBSD binaries with this?
No I didn't. I only ran {i386,riscv32,mips}-linux-user, failed to compile tests/tcg/multiarch/test-mmap.c for i386 freebsd, compiler saying mremap is undeclared.
Signature · 1 line
xtex (Bingwu Zhang) @ Sat, 28 Feb 2026 00:41:49 +0000
Re: [PATCH 2/4] bsd-user: Deal with mmap where start > reserved_va
Quoted 4 lines · Bingwu Zhang
Quoted 1 line · China Standard Time Warner Losh
Reviewed-by Warner Losh <[email protected]>
Quoted 9 lines
Thank you!
Quoted 1 line
Have you run FreeBSD binaries with this?
No I didn't. I only ran {i386,riscv32,mips}-linux-user, failed to compile
tests/tcg/multiarch/test-mmap.cfor i386 freebsd, compiler saying mremap is
undeclared.
Ok. I'm planning on sending my pull request later tonight. You'll likely need to rebase around that when it lands.... If linux works, you've not broken bsd-user
Warner
Quoted 5 lines
xtex (Bingwu Zhang) @ Sat, 28 Feb 2026 00:41:49 +0000
Re: [PATCH 2/4] bsd-user: Deal with mmap where start > reserved_va
In reply to Bingwu ZhangQuoted 7 lines · Bingwu Zhang
From: Bingwu Zhang <[email protected]>
Fixes f12294b5bd21 ("bsd-user: Use page_find_range_empty for mmap_find_vma_reserved")Signed-off-by Bingwu Zhang <[email protected]>--- bsd-user/mmap.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
Helge
Quoted 24 lines
bsd-user/mmap.c +5
-3@@ -258,12 +258,14 @@ abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;static abi_ulong mmap_find_vma_reserved (abi_ulong start,abi_ulong size,abi_ulong alignment) { -abi_ulong ret; +abi_ulong ret =-1 ; - ret =page_find_range_empty (start, reserved_va, size, alignment); +if (start <= reserved_va) { + ret =page_find_range_empty (start, reserved_va, size, alignment); + }if (ret ==-1 && start >TARGET_PAGE_SIZE ) {/* Restart at the beginning of the address space. */ - ret =page_find_range_empty (TARGET_PAGE_SIZE , start -1 , + ret =page_find_range_empty (TARGET_PAGE_SIZE ,MIN (start -1 , reserved_va), size, alignment); }
PATCH 3/4 tests/tcg/multiarch/test-mmap: Print more details
In reply to Bingwu ZhangFrom: Bingwu Zhang <[email protected]>
Useful for debugging
--- tests/tcg/multiarch/test-mmap.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
tests/tcg/multiarch/test-mmap.c +9 -3
@@ -442,19 +442,19 @@ void check_invalid_mmaps(void)/* Attempt to map a zero length page. */ addr =mmap (NULL ,0 ,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); -fprintf (stdout,"%s addr=%p" , __func__, (void *)addr); +fprintf (stdout,"%s addr=%p errno=%d\n" , __func__, (void *)addr, errno);fail_unless (addr ==MAP_FAILED );fail_unless (errno ==EINVAL );/* Attempt to map a over length page. */ addr =mmap (NULL ,-4 ,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); -fprintf (stdout,"%s addr=%p" , __func__, (void *)addr); +fprintf (stdout,"%s addr=%p errno=%d\n" , __func__, (void *)addr, errno);fail_unless (addr ==MAP_FAILED );fail_unless (errno ==ENOMEM );/* Attempt to remap a region which exceeds the bounds of memory. */ addr =mremap ((void *)((uintptr_t )pagesize *10 ),SIZE_MAX & ~(size_t )pagemask, pagesize,0 ); -fprintf (stdout,"%s mremap addr=%p" , __func__, (void *)addr); +fprintf (stdout,"%s mremap addr=%p errno=%d\n" , __func__, (void *)addr, errno);fail_unless (addr ==MAP_FAILED );fail_unless (errno ==EFAULT );
@@ -465,8 +465,11 @@ void check_shrink_mmaps(void) {unsigned char *a, *b, *c; a =mmap (NULL , pagesize *2 ,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +fprintf (stdout,"%s addr=%p errno=%d\n" , __func__, (void *)a, errno); b =mmap (NULL , pagesize *2 ,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +fprintf (stdout,"%s addr=%p errno=%d\n" , __func__, (void *)b, errno); c =mmap (NULL , pagesize *2 ,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +fprintf (stdout,"%s addr=%p errno=%d\n" , __func__, (void *)c, errno);fail_unless (a !=MAP_FAILED );fail_unless (b !=MAP_FAILED );
@@ -479,6 +482,7 @@ void check_shrink_mmaps(void)/* Shrink the middle mapping in-place; the others should be unaffected */ b =mremap (b, pagesize *2 , pagesize,0 ); +fprintf (stdout,"%s mremap addr=%p errno=%d\n" , __func__, (void *)b, errno);fail_unless (b !=MAP_FAILED );/* Ensure we can still access all valid mappings */
@@ -489,6 +493,8 @@ void check_shrink_mmaps(void)munmap (a,2 * pagesize);munmap (b, pagesize);munmap (c,2 * pagesize); + +fprintf (stdout," passed\n" ); }int main (int argc,char **argv)
Signature · 1 line
2.52.0
PATCH 4/4 tests/tcg/multiarch/test-mmap: Check mmaps beyond reserved_va
In reply to Bingwu ZhangFrom: Bingwu Zhang <[email protected]>
Unfixed mmap calls where start > reserved_va or the max guest addr should have a valid result.
--- tests/tcg/multiarch/test-mmap.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
tests/tcg/multiarch/test-mmap.c +15 -0
@@ -497,6 +497,20 @@ void check_shrink_mmaps(void)fprintf (stdout," passed\n" ); } +void check_mmaps_beyond_addr_space (void ) +{ +unsigned char *addr; + addr =mmap ((void *)(-(unsigned long )pagesize *10 ), pagesize *2 , +PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +fprintf (stdout,"%s addr=%p errno=%d" , __func__, (void *)addr, errno); +fail_unless (addr !=MAP_FAILED ); + +memcpy (dummybuf, addr,2 * pagesize); +munmap (addr,2 * pagesize); + +fprintf (stdout," passed\n" ); +} +int main (int argc,char **argv) {char tempname[] ="/tmp/.cmmapXXXXXX" ;
@@ -540,6 +554,7 @@ int main(int argc, char **argv)check_file_unfixed_eof_mmaps ();check_invalid_mmaps ();check_shrink_mmaps (); +check_mmaps_beyond_addr_space ();/* Fails at the moment. */ /* check_aligned_anonymous_fixed_mmaps_collide_with_host(); */
Signature · 1 line
2.52.0
Quoted 10 lines · Bingwu Zhang
For
reserved_va& ~MAP_FIXED mmap, linux-user and bsd-user translates it into a fixed mmap within [mmap_min_addr,reserved_va]. However, the previous implementation didn't care about where the address hint provided by user is greater thanreserved_vaand will run into an assertion failure.This series fixed it for linux-user and bsd-user and added test coverage. For start >
reserved_va, skip [start,reserved_va] and just search within [min_addr,reserved_va].
Quoted 7 lines
I actually thought that I am adding too many CC's and causing unnecessary noise but b4 is asking me to run auto-to-cc (:
Signed-off-by Bingwu Zhang <[email protected]>---
Bingwu Zhang (4):
linux-user: Deal with mmap where start >reserved_va
Hi!
This smells like qemu-stable change.
Quoted 1 line
bsd-user: Deal with mmap where start >
reserved_va
I'm not sure this one should be picked up too, but since it's basically the same as for linux, I'd say it should, too.
Quoted 2 lines
tests/tcg/multiarch/test-mmap: Print more details tests/tcg/multiarch/test-mmap: Check mmaps beyond
reserved_va
And probably the tests, too.
Please let me know if I shouldn't pick these up.
Thanks,
/mjt