bsd-user mmap fixes
PATCH 1/9 bsd-user: Apply e6deac9cf99 from linux-user (zero anonymous memory)
In reply to Warner LoshFrom: Mikaël Urankar <[email protected]>
linux-user/mmap.c: Always zero MAP_ANONYMOUS memory in mmap_frag()
When mapping MAP_ANONYMOUS memory fragments, still need notice about to set it zero, or it will cause issues.
--- bsd-user/mmap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
bsd-user/mmap.c +3 -1
@@ -180,10 +180,12 @@ static int mmap_frag(abi_ulong real_start,if (prot_new != (prot1 |PROT_WRITE ))mprotect (host_start, qemu_host_page_size, prot_new); }else { -/* just update the protection */ if (prot_new != prot1) {mprotect (host_start, qemu_host_page_size, prot_new); } +if (prot_new &PROT_WRITE ) { +memset (g2h_untagged (start),0 , end - start); + } }return 0 ; }
Signature · 1 line
2.32.0
cover bsd-user mmap fixes
This series synchronizes mmap.c with the bsd-user fork. This is a mix of old bug fixes pulled in from linux-user, as well as some newer fixes to adress bugs found in check-tcg and recent FreeBSD developments. There are also a couple of style commits.
Guy Yur (1):
bsd-user: Don't try to mmap fd when it is -1 independently from
MAP_ANONYMOUS flag
Kyle Evans (1):
bsd-user: Implement MAP_EXCL, required by jemalloc in head
Mikaël Urankar (2):
bsd-user: Apply e6deac9cf99 from linux-user (zero anonymous memory)
bsd-user: Apply fb7e378cf9c from linux-user (fix FORTIFY warnings)
Paolo Bonzini (1):
bsd-user: Apply 86abac06c14 from linux-user (target_mprotect can't
fail)
Warner Losh (4):
bsd-user: MAP_ symbols are defined, so no need for ifdefs
bsd-user: mmap return ENOMEM on overflow
bsd-user: mmap prefer MAP_ANON for BSD
bsd-user: mmap line wrap change
bsd-user/mmap.c | 69 +++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 34 deletions(-)
Signature · 1 line
2.32.0
PATCH 2/9 bsd-user: Apply fb7e378cf9c from linux-user (fix FORTIFY warnings)
From: Mikaël Urankar <[email protected]>
linux-user/mmap.c: fix warnings with _FORTIFY_SOURCE
CC i386-linux-user/mmap.o
cc1: warnings being treated as errors/usr/src/RPM/BUILD/qemu-0.11.92/linux-user/mmap.c: In function 'mmap_frag':/usr/src/RPM/BUILD/qemu-0.11.92/linux-user/mmap.c:253: error: ignoring return value of 'pread', declared with attribute warn_unused_result /usr/src/RPM/BUILD/qemu-0.11.92/linux-user/mmap.c: In function 'target_mmap':/usr/src/RPM/BUILD/qemu-0.11.92/linux-user/mmap.c:477: error: ignoring return value of 'pread', declared with attribute warn_unused_result make[1]: *** [mmap.o] Error 1
--- bsd-user/mmap.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
bsd-user/mmap.c +4 -2
@@ -174,7 +174,8 @@ static int mmap_frag(abi_ulong real_start,mprotect (host_start, qemu_host_page_size, prot1 |PROT_WRITE );/* read the corresponding file data */ -pread (fd,g2h_untagged (start), end - start, offset); +if (pread (fd,g2h_untagged (start), end - start, offset) ==-1 ) +return -1 ;/* put final protection */ if (prot_new != (prot1 |PROT_WRITE ))
@@ -593,7 +594,8 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, -1, 0);if (retaddr ==-1 ) goto fail; -pread (fd,g2h_untagged (start), len, offset); +if (pread (fd,g2h_untagged (start), len, offset) ==-1 ) + goto fail;if (!(prot &PROT_WRITE )) { ret =target_mprotect (start, len, prot);if (ret !=0 ) {
Signature · 1 line
2.32.0
PATCH 6/9 bsd-user: mmap line wrap change
In reply to Warner LoshKeep the shifted expression on one line. It's the same number of lines and easier to read like this.
--- bsd-user/mmap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
bsd-user/mmap.c +2 -2
@@ -399,8 +399,8 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, prot &PROT_WRITE ?'w' :'-' , prot & PROT_EXEC ? 'x' : '-');if (flags &MAP_ALIGNMENT_MASK ) { -printf ("MAP_ALIGNED(%u) " , (flags &MAP_ALIGNMENT_MASK ) - >>MAP_ALIGNMENT_SHIFT ); +printf ("MAP_ALIGNED(%u) " , + (flags &MAP_ALIGNMENT_MASK ) >>MAP_ALIGNMENT_SHIFT ); }if (flags &MAP_GUARD ) {printf ("MAP_GUARD " );
Signature · 1 line
2.32.0
PATCH 7/9 bsd-user: Don't try to mmap fd when it is -1 independently from MAP_ANONYMOUS flag
In reply to Warner LoshFrom: Guy Yur <[email protected]>
Switch checks for !(flags & MAP_ANONYMOUS) with checks for fd != -1. MAP_STACK and MAP_GUARD also force fd == -1 and they don't require mapping the fd either.
--- bsd-user/mmap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
bsd-user/mmap.c +3 -3
@@ -154,7 +154,7 @@ static int mmap_frag(abi_ulong real_start,if (prot1 ==0 ) {/* no page was there, so we allocate one */ void *p =mmap (host_start, qemu_host_page_size, prot, - flags |MAP_ANON ,-1 ,0 ); + flags | ((fd !=-1 ) ?MAP_ANON :0 ),-1 ,0 );if (p ==MAP_FAILED )return -1 ; prot1 = prot;
@@ -162,7 +162,7 @@ static int mmap_frag(abi_ulong real_start, prot1 &=PAGE_BITS ; prot_new = prot | prot1; -if (!(flags &MAP_ANON )) { +if (fd !=-1 ) {/* msync() won't work here, so we return an error if write is possible while it is a shared mapping */ if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
@@ -571,7 +571,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,* worst case: we cannot map the file because the offset is not * aligned, so we read it */ -if (!(flags &MAP_ANON ) && +if (fd !=-1 && (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {/* * msync() won't work here, so we return an error if write is
Signature · 1 line
2.32.0
PATCH 3/9 bsd-user: MAP_ symbols are defined, so no need for ifdefs
In reply to Warner LoshAll these MAP_ symbols are always defined on supported FreeBSD versions (12.2 and newer), so remove the #ifdefs since they aren't needed.
--- bsd-user/mmap.c | 14 -------------- 1 file changed, 14 deletions(-)
bsd-user/mmap.c +0 -14
@@ -285,13 +285,9 @@ static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size, wrapped = repeat =0 ; prev =0 ; flags =MAP_ANONYMOUS |MAP_PRIVATE ; -#ifdef MAP_ALIGNED if (alignment !=0 ) { flags |=MAP_ALIGNED (alignment); } -#else -/* XXX TODO */ -#endif for (;; prev = ptr) {/*
@@ -406,22 +402,18 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,printf ("MAP_ALIGNED(%u) " , (flags &MAP_ALIGNMENT_MASK ) >>MAP_ALIGNMENT_SHIFT ); } -#if MAP_GUARD if (flags &MAP_GUARD ) {printf ("MAP_GUARD " ); } -#endif if (flags &MAP_FIXED ) {printf ("MAP_FIXED " ); }if (flags &MAP_ANONYMOUS ) {printf ("MAP_ANON " ); } -#ifdef MAP_EXCL if (flags &MAP_EXCL ) {printf ("MAP_EXCL " ); } -#endif if (flags &MAP_PRIVATE ) {printf ("MAP_PRIVATE " ); }
@@ -431,11 +423,9 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,if (flags &MAP_NOCORE ) {printf ("MAP_NOCORE " ); } -#ifdef MAP_STACK if (flags &MAP_STACK ) {printf ("MAP_STACK " ); } -#endif printf("fd=%d offset=0x%llx\n", fd, offset); }#endif
@@ -444,7 +434,6 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, errno =EINVAL ; goto fail; } -#ifdef MAP_STACK if (flags &MAP_STACK ) {if ((fd !=-1 ) || ((prot & (PROT_READ |PROT_WRITE )) != (PROT_READ |PROT_WRITE ))) {
@@ -452,8 +441,6 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, goto fail; } } -#endif /* MAP_STACK */ -#ifdef MAP_GUARD if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 || offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |/* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
@@ -461,7 +448,6 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, errno =EINVAL ; goto fail; } -#endif if (offset & ~TARGET_PAGE_MASK ) { errno =EINVAL ;
Signature · 1 line
2.32.0
PATCH 5/9 bsd-user: mmap prefer MAP_ANON for BSD
In reply to Warner LoshMAP_ANON and MAP_ANONYMOUS are identical. Prefer MAP_ANON for BSD since the file is now a confusing mix of the two.
--- bsd-user/mmap.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
bsd-user/mmap.c +5 -6
@@ -284,7 +284,7 @@ static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size, addr = start; wrapped = repeat =0 ; prev =0 ; - flags =MAP_ANONYMOUS |MAP_PRIVATE ; + flags =MAP_ANON |MAP_PRIVATE ;if (alignment !=0 ) { flags |=MAP_ALIGNED (alignment); }
@@ -408,7 +408,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,if (flags &MAP_FIXED ) {printf ("MAP_FIXED " ); } -if (flags &MAP_ANONYMOUS ) { +if (flags &MAP_ANON ) {printf ("MAP_ANON " ); }if (flags &MAP_EXCL ) {
@@ -430,7 +430,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, }#endif -if ((flags &MAP_ANONYMOUS ) && fd !=-1 ) { +if ((flags &MAP_ANON ) && fd !=-1 ) { errno =EINVAL ; goto fail; }
@@ -532,7 +532,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,* qemu_real_host_page_size */ p =mmap (g2h_untagged (start), host_len, prot, - flags |MAP_FIXED | ((fd !=-1 ) ?MAP_ANONYMOUS :0 ),-1 ,0 ); + flags |MAP_FIXED | ((fd !=-1 ) ?MAP_ANON :0 ),-1 ,0 );if (p ==MAP_FAILED ) goto fail;/* update start so that it points to the file position at 'offset' */
@@ -694,8 +694,7 @@ static void mmap_reserve(abi_ulong start, abi_ulong size) }if (real_start != real_end) {mmap (g2h_untagged (real_start), real_end - real_start,PROT_NONE , -MAP_FIXED |MAP_ANONYMOUS |MAP_PRIVATE , --1 ,0 ); +MAP_FIXED |MAP_ANON |MAP_PRIVATE ,-1 ,0 ); } }
Signature · 1 line
2.32.0
PATCH 9/9 bsd-user: Apply 86abac06c14 from linux-user (target_mprotect can't fail)
In reply to Warner LoshFrom: Paolo Bonzini <[email protected]>
linux-user: assert that target_mprotect cannot fail
All error conditions that target_mprotect checks are also checked by target_mmap. EACCESS cannot happen because we are just removing PROT_WRITE. ENOMEM should not happen because we are modifying a whole VMA (and we have bigger problems anyway if it happens).
Fixes a Coverity false positive, where Coverity complains about target_mprotect's return value being passed to tb_invalidate_phys_range.
--- bsd-user/mmap.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
bsd-user/mmap.c +1 -4
@@ -591,10 +591,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, goto fail;if (!(prot &PROT_WRITE )) { ret =target_mprotect (start, len, prot); -if (ret !=0 ) { - start = ret; - goto the_end; - } +assert (ret ==0 ); } goto the_end; }
Signature · 1 line
2.32.0
PATCH 4/9 bsd-user: mmap return ENOMEM on overflow
In reply to Warner Loshmmap should return ENOMEM on len overflow rather than EINVAL. Return EINVAL when len == 0 and ENOMEM when the rounded to a page length is 0. Found by make check-tcg.
--- bsd-user/mmap.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
bsd-user/mmap.c +8 -1
@@ -454,11 +454,18 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, goto fail; } - len =TARGET_PAGE_ALIGN (len);if (len ==0 ) { errno =EINVAL ; goto fail; } + +/* Check for overflows */ + len =TARGET_PAGE_ALIGN (len); +if (len ==0 ) { + errno =ENOMEM ; + goto fail; + } + real_start = start & qemu_host_page_mask; host_offset = offset & qemu_host_page_mask;
Signature · 1 line
2.32.0
PATCH 8/9 bsd-user: Implement MAP_EXCL, required by jemalloc in head
In reply to Warner LoshFrom: Kyle Evans <[email protected]>
jemalloc requires a working MAP_EXCL. Emulate it by ensuring we don't double map anything.
--- bsd-user/mmap.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
bsd-user/mmap.c +9 -1
@@ -387,7 +387,7 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)abi_long target_mmap (abi_ulong start,abi_ulong len,int prot,int flags,int fd,off_t offset) { -abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; +abi_ulong addr, ret, end, real_start, real_end, retaddr, host_offset, host_len;mmap_lock ();#ifdef DEBUG_MMAP
@@ -599,6 +599,14 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, goto the_end; } +/* Reject the mapping if any page within the range is mapped */ +if (flags &MAP_EXCL ) { +for (addr = start; addr < end; addr++) { +if (page_get_flags (addr) !=0 ) + goto fail; + } + } +/* handle the start of the mapping */ if (start > real_start) {if (real_end == real_start + qemu_host_page_size) {
Signature · 1 line
2.32.0
Re: [PATCH 7/9] bsd-user: Don't try to mmap fd when it is -1 independently from MAP_ANONYMOUS flag
In reply to Warner LoshQuoted 2 lines · Warner Losh
From: Guy Yur <[email protected]>
I need to fix this email address in the next round or for the pull request. It's gmail.com, not ngmail.com.
Switch checks for !(flags & MAP_ANONYMOUS) with checks for fd != -1.
Quoted 47 lines
MAP_STACKandMAP_GUARDalso force fd == -1 and they don't require mapping the fd either.Signed-off-by Guy Yur <[email protected]>[ partially merged before, finishing the job and documenting origin]Signed-off-by Warner Losh <[email protected]>--- bsd-user/mmap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)bsd-user/mmap.c +3
-3@@ -154,7 +154,7 @@ static int mmap_frag(abi_ulong real_start,if (prot1 ==0 ) {/* no page was there, so we allocate one */ void *p =mmap (host_start, qemu_host_page_size, prot, - flags |MAP_ANON ,-1 ,0 ); + flags | ((fd !=-1 ) ?MAP_ANON :0 ),-1 ,0 );if (p ==MAP_FAILED )return -1 ; prot1 = prot;@@ -162,7 +162,7 @@ static int mmap_frag(abi_ulong real_start, prot1 &=PAGE_BITS ; prot_new = prot | prot1; -if (!(flags &MAP_ANON )) { +if (fd !=-1 ) {/* msync() won't work here, so we return an error if write is possible while it is a shared mapping */ if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&@@ -571,7 +571,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len,int prot, * worst case: we cannot map the file because the offset is not * aligned, so we read it */ -if (!(flags &MAP_ANON ) && +if (fd !=-1 && (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {/*
*msync()won't work here, so we return an error if write is
--
2.32.0
Re: [PATCH 1/9] bsd-user: Apply e6deac9cf99 from linux-user (zero anonymous memory)
In reply to Warner LoshQuoted 3 lines · Warner Losh
From: Mikaël Urankar <[email protected]>
linux-user/mmap.c: Always zeroMAP_ANONYMOUSmemory inmmap_frag()
Please use it as subject, "bsd-user/mmap: Always zero MAP_ANONYMOUS memory in mmap_frag()"
Then describe:
Similar to the equivalent linux-user commit e6deac9cf99, ...
Quoted 7 lines
When mapping
MAP_ANONYMOUSmemory fragments, still need notice about to set it zero, or it will cause issues.Signed-off-by Chen Gang <[email protected]>Reviewed-by Laurent Vivier <[email protected]>Signed-off-by Riku Voipio <[email protected]>
^ These tags were for another file, not this one, please remove them.
Quoted 26 lines
[ bsd-user merge by Mikaël Urankar, updated for untagged by Warner Losh ]Signed-off-by Mikaël Urankar <[email protected]>Signed-off-by Warner Losh <[email protected]>--- bsd-user/mmap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)bsd-user/mmap.c +3
-1@@ -180,10 +180,12 @@ static int mmap_frag(abi_ulong real_start,if (prot_new != (prot1 |PROT_WRITE ))mprotect (host_start, qemu_host_page_size, prot_new); }else { -/* just update the protection */ if (prot_new != prot1) {mprotect (host_start, qemu_host_page_size, prot_new); } +if (prot_new &PROT_WRITE ) { +memset (g2h_untagged (start),0 , end - start); + } }return 0 ; }
Re: [PATCH 3/9] bsd-user: MAP_ symbols are defined, so no need for ifdefs
In reply to Warner LoshQuoted 7 lines · Warner Losh
All these
MAP_symbols are always defined on supported FreeBSD versions (12.2 and newer), so remove the #ifdefs since they aren't needed.Signed-off-by Warner Losh <[email protected]>--- bsd-user/mmap.c | 14 -------------- 1 file changed, 14 deletions(-)
Re: [PATCH 5/9] bsd-user: mmap prefer MAP_ANON for BSD
In reply to Warner LoshQuoted 7 lines · Warner Losh
MAP_ANONandMAP_ANONYMOUSare identical. PreferMAP_ANONfor BSD since the file is now a confusing mix of the two.Signed-off-by Warner Losh <[email protected]>--- bsd-user/mmap.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
Re: [PATCH 1/9] bsd-user: Apply e6deac9cf99 from linux-user (zero anonymous memory)
In reply to Philippe Mathieu-DaudéQuoted 12 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Warner Losh
From: Mikaël Urankar <[email protected]>
linux-user/mmap.c: Always zeroMAP_ANONYMOUSmemory inmmap_frag()Please use it as subject, "bsd-user/mmap: Always zero
MAP_ANONYMOUSmemory inmmap_frag()"Then describe:
Similar to the equivalent linux-user commit
e6deac9cf99, ...
OK. I have three commits like this, so I'll go ahead and edit all three.
Quoted 11 lines
Quoted 7 lines
When mapping
MAP_ANONYMOUSmemory fragments, still need notice about to set it zero, or it will cause issues.Signed-off-by Chen Gang <[email protected]>Reviewed-by Laurent Vivier <[email protected]>Signed-off-by Riku Voipio <[email protected]>^ These tags were for another file, not this one, please remove them.
Gotcha. I wasn't completely sure what to do in this case since they describe that the work is able to be contributed so I could make a case either way.
I'll remove them.
Warner
Quoted 28 lines
Quoted 26 lines
[ bsd-user merge by Mikaël Urankar, updated for untagged by Warner Losh ]Signed-off-by Mikaël Urankar <[email protected]>Signed-off-by Warner Losh <[email protected]>--- bsd-user/mmap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)bsd-user/mmap.c +3
-1@@ -180,10 +180,12 @@ static int mmap_frag(abi_ulong real_start,if (prot_new != (prot1 |PROT_WRITE ))mprotect (host_start, qemu_host_page_size, prot_new); }else { -/* just update the protection */ if (prot_new != prot1) {mprotect (host_start, qemu_host_page_size, prot_new); } +if (prot_new &PROT_WRITE ) { +memset (g2h_untagged (start),0 , end - start); + } }return 0 ; }