On Thu, 6 Oct 2022 at 19:05, Soichiro Isshiki
<[email protected]> wrote:
>
> From: sisshiki1969 <[email protected]>
>
> For now, qemu-x86_64 returns ENOMEM when mprotect() was called with an 
> argument
> len is 0 from a guest process.
> This behavior is incompatible with the current Linux implementation,
> which mprotect() with len = 0 does nothing and returns 0,
> although it does not appear to be explicitly described in man.
>
> This is due to the following function which always returns false if len = 0.
>
> ```C
> static inline bool guest_range_valid_untagged(abi_ulong start, abi_ulong len)
> {
>     return len - 1 <= GUEST_ADDR_MAX && start <= GUEST_ADDR_MAX - len + 1;
> }
>
> ```
>
> This patch fix this incompatibility problem.
>
> Signed-off-by: sisshiki1969 <[email protected]>
> ---
>  linux-user/mmap.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/linux-user/mmap.c b/linux-user/mmap.c
> index 28f3bc85ed..1ed79459ea 100644
> --- a/linux-user/mmap.c
> +++ b/linux-user/mmap.c
> @@ -130,12 +130,12 @@ int target_mprotect(abi_ulong start, abi_ulong len, int 
> target_prot)
>      }
>      len = TARGET_PAGE_ALIGN(len);
>      end = start + len;
> -    if (!guest_range_valid_untagged(start, len)) {
> -        return -TARGET_ENOMEM;
> -    }
>      if (len == 0) {
>          return 0;
>      }
> +    if (!guest_range_valid_untagged(start, len)) {
> +        return -TARGET_ENOMEM;
> +    }
>
>      mmap_lock();
>      host_start = start & qemu_host_page_mask;

Cc'ing Richard -- is this the right fix, or would it be better instead
to make guest_range_valid_untagged() correctly handle a zero-length
range ?

thanks
-- PMM

Reply via email to