bsd-user: Handle short reads in mmap_h_gt_g

In particular, if an image has a large bss, we can hit EOF before reading
all bytes of the mapping.  Mirror the similar change to linux-user.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-Id: <20240820050848.165253-3-richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2024-08-20 09:41:34 +10:00
parent a4ad4a9d98
commit 5b73b248a1
1 changed files with 36 additions and 2 deletions

View File

@ -128,6 +128,40 @@ error:
return ret;
}
/*
* Perform a pread on behalf of target_mmap. We can reach EOF, we can be
* interrupted by signals, and in general there's no good error return path.
* If @zero, zero the rest of the block at EOF.
* Return true on success.
*/
static bool mmap_pread(int fd, void *p, size_t len, off_t offset, bool zero)
{
while (1) {
ssize_t r = pread(fd, p, len, offset);
if (likely(r == len)) {
/* Complete */
return true;
}
if (r == 0) {
/* EOF */
if (zero) {
memset(p, 0, len);
}
return true;
}
if (r > 0) {
/* Short read */
p += r;
len -= r;
offset += r;
} else if (errno != EINTR) {
/* Error */
return false;
}
}
}
/*
* map an incomplete host page
*
@ -190,7 +224,7 @@ static int mmap_frag(abi_ulong real_start,
mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
/* read the corresponding file data */
if (pread(fd, g2h_untagged(start), end - start, offset) == -1) {
if (!mmap_pread(fd, g2h_untagged(start), end - start, offset, true)) {
return -1;
}
@ -565,7 +599,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
-1, 0);
if (retaddr == -1)
goto fail;
if (pread(fd, g2h_untagged(start), len, offset) == -1) {
if (!mmap_pread(fd, g2h_untagged(start), len, offset, false)) {
goto fail;
}
if (!(prot & PROT_WRITE)) {