diff mbox

linux-user: Fix large seeks by 32 bit guest on 64 bit host

Message ID 1298379746-18484-1-git-send-email-peter.maydell@linaro.org
State Accepted
Commit 0c1592d93585ddce572a71a77534ca765c5f5d34
Headers show

Commit Message

Peter Maydell Feb. 22, 2011, 1:02 p.m. UTC
When emulating a 32 bit Linux user-mode program on a 64 bit target
we implement the llseek syscall in terms of lseek. Correct a bug
which meant we were silently casting the result of host lseek()
to a 32 bit integer as it passed through get_errno() and thus
throwing away the top half.

We also don't try to store the result back to userspace unless
the seek succeeded; this matches the kernel behaviour.

Thanks to Eoghan Sherry for identifying the problem and suggesting
a solution.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 linux-user/syscall.c |   16 ++++++++++------
 1 files changed, 10 insertions(+), 6 deletions(-)

Comments

Aurelien Jarno March 6, 2011, 6:07 p.m. UTC | #1
On Tue, Feb 22, 2011 at 01:02:26PM +0000, Peter Maydell wrote:
> When emulating a 32 bit Linux user-mode program on a 64 bit target
> we implement the llseek syscall in terms of lseek. Correct a bug
> which meant we were silently casting the result of host lseek()
> to a 32 bit integer as it passed through get_errno() and thus
> throwing away the top half.
> 
> We also don't try to store the result back to userspace unless
> the seek succeeded; this matches the kernel behaviour.
> 
> Thanks to Eoghan Sherry for identifying the problem and suggesting
> a solution.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>  linux-user/syscall.c |   16 ++++++++++------
>  1 files changed, 10 insertions(+), 6 deletions(-)

Thanks, applied.

> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index cf8a4c3..23d7a63 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -6127,16 +6127,20 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
>  #ifdef TARGET_NR__llseek /* Not on alpha */
>      case TARGET_NR__llseek:
>          {
> +            int64_t res;
>  #if !defined(__NR_llseek)
> -            ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
> -            if (put_user_s64(ret, arg4))
> -                goto efault;
> +            res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
> +            if (res == -1) {
> +                ret = get_errno(res);
> +            } else {
> +                ret = 0;
> +            }
>  #else
> -            int64_t res;
>              ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
> -            if (put_user_s64(res, arg4))
> -                goto efault;
>  #endif
> +            if ((ret == 0) && put_user_s64(res, arg4)) {
> +                goto efault;
> +            }
>          }
>          break;
>  #endif
> -- 
> 1.7.1
> 
> 
>
diff mbox

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index cf8a4c3..23d7a63 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -6127,16 +6127,20 @@  abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #ifdef TARGET_NR__llseek /* Not on alpha */
     case TARGET_NR__llseek:
         {
+            int64_t res;
 #if !defined(__NR_llseek)
-            ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
-            if (put_user_s64(ret, arg4))
-                goto efault;
+            res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
+            if (res == -1) {
+                ret = get_errno(res);
+            } else {
+                ret = 0;
+            }
 #else
-            int64_t res;
             ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
-            if (put_user_s64(res, arg4))
-                goto efault;
 #endif
+            if ((ret == 0) && put_user_s64(res, arg4)) {
+                goto efault;
+            }
         }
         break;
 #endif