Skip to contentSkip to conversation

fix fcntl support in linux-user.

PATCH In review Arnaud Patard (Rtp) 3 participants 7 messages Apr 19, 2009 – Apr 22, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix fcntl support in linux-user.

The current code in do_fcntl is passing the target command as a host command. This is introducing 2 problems:

- When building with "-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE", we may end up passing commands like F_GETFL instead of F_GETFL64. This is likely to return an error.

- The F_*64 constants doesn't always have the same value between host and target.

Without this patch locking is not working (My test-case was pwck with arm as target and mips as host).

---

linux-user/syscall.c +5 -5
index 2d51d6b..2d876c1 100644
@@ -3126,7 +3141,7 @@ static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
         fl.l_len = tswapl(target_fl->l_len);
         fl.l_pid = tswapl(target_fl->l_pid);
         unlock_user_struct(target_fl, arg, 0);
-        ret = get_errno(fcntl(fd, cmd, &fl));
+        ret = get_errno(fcntl(fd, F_GETLK, &fl));
         if (ret == 0) {
             if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
                 return -TARGET_EFAULT;
@@ -3149,7 +3164,7 @@ static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
         fl.l_len = tswapl(target_fl->l_len);
         fl.l_pid = tswapl(target_fl->l_pid);
         unlock_user_struct(target_fl, arg, 0);
-        ret = get_errno(fcntl(fd, cmd, &fl));
+        ret = get_errno(fcntl(fd, F_SETLK+(cmd-TARGET_F_SETLK), &fl));
         break;
 
     case TARGET_F_GETLK64:
@@ -3161,7 +3176,7 @@ static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
         fl64.l_len = tswapl(target_fl64->l_len);
         fl64.l_pid = tswap16(target_fl64->l_pid);
         unlock_user_struct(target_fl64, arg, 0);
-        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
+        ret = get_errno(fcntl(fd, F_GETLK64, &fl64));
         if (ret == 0) {
             if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
                 return -TARGET_EFAULT;
@@ -3183,7 +3198,7 @@ static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
         fl64.l_len = tswapl(target_fl64->l_len);
         fl64.l_pid = tswap16(target_fl64->l_pid);
         unlock_user_struct(target_fl64, arg, 0);
-        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
+        ret = get_errno(fcntl(fd, F_SETLK64+(cmd-TARGET_F_SETLK64), &fl64));
         break;
 
     case F_GETFL:
@@ -6022,7 +6039,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             ret = get_errno(fcntl(arg1, cmd, &fl));
 	    break;
         default:
-            ret = do_fcntl(arg1, cmd, arg3);
+            ret = do_fcntl(arg1, arg2, arg3);
             break;
         }
 	break;
#1 lore raw
Quoted 10 lines · Arnaud Patard

The current code in do_fcntl is passing the target command as a host command. This is introducing 2 problems:

- When building with "-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE", we may end up passing commands like F_GETFL instead of F_GETFL64. This is likely to return an error.

- The F_*64 constants doesn't always have the same value between host and target.

This appears to be the case for some other fcntl constants as well. Howabout implementing this as a target_to_host_fcntl_cmd() mapping function instead?

Quoted 2 lines

Without this patch locking is not working (My test-case was pwck with arm as target and mips as host).

Could you also add the MIPS target definition for F_*64 so that mips on arm would get fixed too?

Quoted 52 lines

---

linux-user/syscall.c +5 -5
index 2d51d6b..2d876c1 100644
@@ -3126,7 +3141,7 @@ static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
         fl.l_len = tswapl(target_fl->l_len);
         fl.l_pid = tswapl(target_fl->l_pid);
         unlock_user_struct(target_fl, arg, 0);
-        ret = get_errno(fcntl(fd, cmd, &fl));
+        ret = get_errno(fcntl(fd, F_GETLK, &fl));
         if (ret == 0) {
             if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
                 return -TARGET_EFAULT;
@@ -3149,7 +3164,7 @@ static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
         fl.l_len = tswapl(target_fl->l_len);
         fl.l_pid = tswapl(target_fl->l_pid);
         unlock_user_struct(target_fl, arg, 0);
-        ret = get_errno(fcntl(fd, cmd, &fl));
+        ret = get_errno(fcntl(fd, F_SETLK+(cmd-TARGET_F_SETLK), &fl));
         break;
 
     case TARGET_F_GETLK64:
@@ -3161,7 +3176,7 @@ static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
         fl64.l_len = tswapl(target_fl64->l_len);
         fl64.l_pid = tswap16(target_fl64->l_pid);
         unlock_user_struct(target_fl64, arg, 0);
-        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
+        ret = get_errno(fcntl(fd, F_GETLK64, &fl64));
         if (ret == 0) {
             if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
                 return -TARGET_EFAULT;
@@ -3183,7 +3198,7 @@ static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
         fl64.l_len = tswapl(target_fl64->l_len);
         fl64.l_pid = tswap16(target_fl64->l_pid);
         unlock_user_struct(target_fl64, arg, 0);
-        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
+        ret = get_errno(fcntl(fd, F_SETLK64+(cmd-TARGET_F_SETLK64), &fl64));
         break;
 
     case F_GETFL:
@@ -6022,7 +6039,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             ret = get_errno(fcntl(arg1, cmd, &fl));
 	    break;
         default:
-            ret = do_fcntl(arg1, cmd, arg3);
+            ret = do_fcntl(arg1, arg2, arg3);
             break;
         }
 	break;
#2 lore raw

Riku Voipio <[email protected]> writes:

Hi,

Quoted 9 lines · Arnaud Patard
Quoted 8 lines

The current code in do_fcntl is passing the target command as a host command. This is introducing 2 problems:

- When building with "-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE", we may end up passing commands like F_GETFL instead of F_GETFL64. This is likely to return an error.

Quoted 5 lines
Quoted 2 lines

- The F_*64 constants doesn't always have the same value between host and target.

This appears to be the case for some other fcntl constants as well. Howabout implementing this as a target_to_host_fcntl_cmd() mapping function instead?

Ok, done. New patch attached. Please comment :)

Quoted 6 lines
Quoted 3 lines

Without this patch locking is not working (My test-case was pwck with arm as target and mips as host).

Could you also add the MIPS target definition for F_*64 so that mips on arm would get fixed too?

oops. Forgot to include that in my patch

linux-user/syscall.c +30 -23
Index: qemu/linux-user/syscall.c
===================================================================
@@ -253,6 +253,18 @@ static bitmask_transtbl fcntl_flags_tbl[
   { 0, 0, 0, 0 }
 };
 
+static char target_to_host_fcntl_cmd[] = {
+  [ TARGET_F_GETLK    ] = F_GETLK,
+  [ TARGET_F_SETLK    ] = F_SETLK,
+  [ TARGET_F_SETLKW   ] = F_SETLKW,
+  [ TARGET_F_GETLK64  ] = F_GETLK64,
+  [ TARGET_F_SETLK64  ] = F_SETLK64,
+  [ TARGET_F_SETLKW64 ] = F_SETLKW64,
+  [ TARGET_F_SETOWN   ] = F_SETOWN,
+  [ TARGET_F_GETOWN   ] = F_GETOWN,
+  [ TARGET_F_SETSIG   ] = F_SETSIG,
+  [ TARGET_F_GETSIG   ] = F_GETSIG,
+};
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
       /* __NEW_UTS_LEN doesn't include terminating null */ \
@@ -3365,6 +3377,7 @@ static abi_long do_fcntl(int fd, int cmd
     struct flock64 fl64;
     struct target_flock64 *target_fl64;
     abi_long ret;
+    int cmd2 = target_to_host_fcntl_cmd[cmd];
 
     switch(cmd) {
     case TARGET_F_GETLK:
@@ -3376,7 +3389,7 @@ static abi_long do_fcntl(int fd, int cmd
         fl.l_len = tswapl(target_fl->l_len);
         fl.l_pid = tswapl(target_fl->l_pid);
         unlock_user_struct(target_fl, arg, 0);
-        ret = get_errno(fcntl(fd, cmd, &fl));
+        ret = get_errno(fcntl(fd, cmd2, &fl));
         if (ret == 0) {
             if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
                 return -TARGET_EFAULT;
@@ -3399,7 +3412,7 @@ static abi_long do_fcntl(int fd, int cmd
         fl.l_len = tswapl(target_fl->l_len);
         fl.l_pid = tswapl(target_fl->l_pid);
         unlock_user_struct(target_fl, arg, 0);
-        ret = get_errno(fcntl(fd, cmd, &fl));
+        ret = get_errno(fcntl(fd, cmd2, &fl));
         break;
 
     case TARGET_F_GETLK64:
@@ -3411,7 +3424,7 @@ static abi_long do_fcntl(int fd, int cmd
         fl64.l_len = tswapl(target_fl64->l_len);
         fl64.l_pid = tswap16(target_fl64->l_pid);
         unlock_user_struct(target_fl64, arg, 0);
-        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
+        ret = get_errno(fcntl(fd, cmd2, &fl64));
         if (ret == 0) {
             if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
                 return -TARGET_EFAULT;
@@ -3433,18 +3446,25 @@ static abi_long do_fcntl(int fd, int cmd
         fl64.l_len = tswapl(target_fl64->l_len);
         fl64.l_pid = tswap16(target_fl64->l_pid);
         unlock_user_struct(target_fl64, arg, 0);
-        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
+        ret = get_errno(fcntl(fd, cmd2, &fl64));
         break;
 
-    case F_GETFL:
-        ret = get_errno(fcntl(fd, cmd, arg));
+    case TARGET_F_GETFL:
+        ret = get_errno(fcntl(fd, cmd2, arg));
         if (ret >= 0) {
             ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
         }
         break;
 
-    case F_SETFL:
-        ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
+    case TARGET_F_SETFL:
+        ret = get_errno(fcntl(fd, cmd2, target_to_host_bitmask(arg, fcntl_flags_tbl)));
+        break;
+
+    case TARGET_F_SETOWN:
+    case TARGET_F_GETOWN:
+    case TARGET_F_SETSIG:
+    case TARGET_F_GETSIG:
+        ret = get_errno(fcntl(fd, cmd2, arg));
         break;
 
     default:
@@ -6222,20 +6242,7 @@ abi_long do_syscall(void *cpu_env, int n
 	struct target_eabi_flock64 *target_efl;
 #endif
 
-        switch(arg2){
-        case TARGET_F_GETLK64:
-            cmd = F_GETLK64;
-            break;
-        case TARGET_F_SETLK64:
-            cmd = F_SETLK64;
-            break;
-        case TARGET_F_SETLKW64:
-            cmd = F_SETLK64;
-            break;
-        default:
-            cmd = arg2;
-            break;
-        }
+	cmd = target_to_host_fcntl_cmd[arg2];
 
         switch(arg2) {
         case TARGET_F_GETLK64:
@@ -6315,7 +6322,7 @@ abi_long do_syscall(void *cpu_env, int n
             ret = get_errno(fcntl(arg1, cmd, &fl));
 	    break;
         default:
-            ret = do_fcntl(arg1, cmd, arg3);
+            ret = do_fcntl(arg1, arg2, arg3);
             break;
         }
 	break;
linux-user/syscall_defs.h +12 -1
Index: qemu/linux-user/syscall_defs.h
===================================================================
@@ -1662,6 +1662,12 @@ struct target_statfs64 {
 #define TARGET_F_SETLKW        9
 #define TARGET_F_SETOWN        5       /*  for sockets. */
 #define TARGET_F_GETOWN        6       /*  for sockets. */
+#elif defined(TARGET_MIPS)
+#define TARGET_F_GETLK         14
+#define TARGET_F_SETLK         6
+#define TARGET_F_SETLKW        7
+#define TARGET_F_SETOWN        24       /*  for sockets. */
+#define TARGET_F_GETOWN        25       /*  for sockets. */
 #else
 #define TARGET_F_GETLK         5
 #define TARGET_F_SETLK         6
@@ -1673,10 +1679,15 @@ struct target_statfs64 {
 #define TARGET_F_SETSIG        10      /*  for sockets. */
 #define TARGET_F_GETSIG        11      /*  for sockets. */
 
+#if defined(TARGET_MIPS)
+#define TARGET_F_GETLK64       33      /*  using 'struct flock64' */
+#define TARGET_F_SETLK64       34
+#define TARGET_F_SETLKW64      35
+#else
 #define TARGET_F_GETLK64       12      /*  using 'struct flock64' */
 #define TARGET_F_SETLK64       13
 #define TARGET_F_SETLKW64      14
-
+#endif
 #if defined (TARGET_ARM)
 #define TARGET_O_ACCMODE          0003
 #define TARGET_O_RDONLY             00
#3 lore raw
Quoted 1 line · Arnaud Patard

Ok, done. New patch attached. Please comment :)

thanks, this is already better. just two comments.

Quoted 10 lines

Index: qemu/linux-user/syscall.c
===================================================================

linux-user/syscall.c +2 -0
@@ -253,6 +253,18 @@ static bitmask_transtbl fcntl_flags_tbl[
   { 0, 0, 0, 0 }
 };
 
+static char target_to_host_fcntl_cmd[] = {
+  [ TARGET_F_GETLK    ] = F_GETLK,

linux-user/syscall.c static bitmask_transtbl fcntl_flags_tbl[ · line 267

Quoted 10 lines
linux-user/syscall.c +10 -0
+  [ TARGET_F_SETLK    ] = F_SETLK,
+  [ TARGET_F_SETLKW   ] = F_SETLKW,
+  [ TARGET_F_GETLK64  ] = F_GETLK64,
+  [ TARGET_F_SETLK64  ] = F_SETLK64,
+  [ TARGET_F_SETLKW64 ] = F_SETLKW64,
+  [ TARGET_F_SETOWN   ] = F_SETOWN,
+  [ TARGET_F_GETOWN   ] = F_GETOWN,
+  [ TARGET_F_SETSIG   ] = F_SETSIG,
+  [ TARGET_F_GETSIG   ] = F_GETSIG,
+};

explict size for table and check for not overflowing?

linux-user/syscall.c static abi_long do_fcntl(int fd, int cmd · line 3380

Quoted 8 lines
linux-user/syscall.c +1 -0
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
       /* __NEW_UTS_LEN doesn't include terminating null */ \
@@ -3365,6 +3377,7 @@ static abi_long do_fcntl(int fd, int cmd
     struct flock64 fl64;
     struct target_flock64 *target_fl64;
     abi_long ret;
+    int cmd2 = target_to_host_fcntl_cmd[cmd];

cmd2 is not a very good variable name, for example host_cmd would describe it better.

Quoted 125 lines
linux-user/syscall.c +17 -23
     switch(cmd) {
     case TARGET_F_GETLK:
@@ -3376,7 +3389,7 @@ static abi_long do_fcntl(int fd, int cmd
         fl.l_len = tswapl(target_fl->l_len);
         fl.l_pid = tswapl(target_fl->l_pid);
         unlock_user_struct(target_fl, arg, 0);
-        ret = get_errno(fcntl(fd, cmd, &fl));
+        ret = get_errno(fcntl(fd, cmd2, &fl));
         if (ret == 0) {
             if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
                 return -TARGET_EFAULT;
@@ -3399,7 +3412,7 @@ static abi_long do_fcntl(int fd, int cmd
         fl.l_len = tswapl(target_fl->l_len);
         fl.l_pid = tswapl(target_fl->l_pid);
         unlock_user_struct(target_fl, arg, 0);
-        ret = get_errno(fcntl(fd, cmd, &fl));
+        ret = get_errno(fcntl(fd, cmd2, &fl));
         break;
 
     case TARGET_F_GETLK64:
@@ -3411,7 +3424,7 @@ static abi_long do_fcntl(int fd, int cmd
         fl64.l_len = tswapl(target_fl64->l_len);
         fl64.l_pid = tswap16(target_fl64->l_pid);
         unlock_user_struct(target_fl64, arg, 0);
-        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
+        ret = get_errno(fcntl(fd, cmd2, &fl64));
         if (ret == 0) {
             if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
                 return -TARGET_EFAULT;
@@ -3433,18 +3446,25 @@ static abi_long do_fcntl(int fd, int cmd
         fl64.l_len = tswapl(target_fl64->l_len);
         fl64.l_pid = tswap16(target_fl64->l_pid);
         unlock_user_struct(target_fl64, arg, 0);
-        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
+        ret = get_errno(fcntl(fd, cmd2, &fl64));
         break;
 
-    case F_GETFL:
-        ret = get_errno(fcntl(fd, cmd, arg));
+    case TARGET_F_GETFL:
+        ret = get_errno(fcntl(fd, cmd2, arg));
         if (ret >= 0) {
             ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
         }
         break;
 
-    case F_SETFL:
-        ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
+    case TARGET_F_SETFL:
+        ret = get_errno(fcntl(fd, cmd2, target_to_host_bitmask(arg, fcntl_flags_tbl)));
+        break;
+
+    case TARGET_F_SETOWN:
+    case TARGET_F_GETOWN:
+    case TARGET_F_SETSIG:
+    case TARGET_F_GETSIG:
+        ret = get_errno(fcntl(fd, cmd2, arg));
         break;
 
     default:
@@ -6222,20 +6242,7 @@ abi_long do_syscall(void *cpu_env, int n
 	struct target_eabi_flock64 *target_efl;
 #endif
 
-        switch(arg2){
-        case TARGET_F_GETLK64:
-            cmd = F_GETLK64;
-            break;
-        case TARGET_F_SETLK64:
-            cmd = F_SETLK64;
-            break;
-        case TARGET_F_SETLKW64:
-            cmd = F_SETLK64;
-            break;
-        default:
-            cmd = arg2;
-            break;
-        }
+	cmd = target_to_host_fcntl_cmd[arg2];
 
         switch(arg2) {
         case TARGET_F_GETLK64:
@@ -6315,7 +6322,7 @@ abi_long do_syscall(void *cpu_env, int n
             ret = get_errno(fcntl(arg1, cmd, &fl));
 	    break;
         default:
-            ret = do_fcntl(arg1, cmd, arg3);
+            ret = do_fcntl(arg1, arg2, arg3);
             break;
         }
 	break;

Index: qemu/linux-user/syscall_defs.h
===================================================================

linux-user/syscall_defs.h +12 -1
@@ -1662,6 +1662,12 @@ struct target_statfs64 {
 #define TARGET_F_SETLKW        9
 #define TARGET_F_SETOWN        5       /*  for sockets. */
 #define TARGET_F_GETOWN        6       /*  for sockets. */
+#elif defined(TARGET_MIPS)
+#define TARGET_F_GETLK         14
+#define TARGET_F_SETLK         6
+#define TARGET_F_SETLKW        7
+#define TARGET_F_SETOWN        24       /*  for sockets. */
+#define TARGET_F_GETOWN        25       /*  for sockets. */
 #else
 #define TARGET_F_GETLK         5
 #define TARGET_F_SETLK         6
@@ -1673,10 +1679,15 @@ struct target_statfs64 {
 #define TARGET_F_SETSIG        10      /*  for sockets. */
 #define TARGET_F_GETSIG        11      /*  for sockets. */
 
+#if defined(TARGET_MIPS)
+#define TARGET_F_GETLK64       33      /*  using 'struct flock64' */
+#define TARGET_F_SETLK64       34
+#define TARGET_F_SETLKW64      35
+#else
 #define TARGET_F_GETLK64       12      /*  using 'struct flock64' */
 #define TARGET_F_SETLK64       13
 #define TARGET_F_SETLKW64      14
-
+#endif
 #if defined (TARGET_ARM)
 #define TARGET_O_ACCMODE          0003
 #define TARGET_O_RDONLY             00
#4 lore raw
Quoted 5 lines · Riku Voipio
Quoted 1 line · Arnaud Patard

Ok, done. New patch attached. Please comment :)

thanks, this is already better. just two comments.

Quoted 14 lines

linux-user/syscall.c static bitmask_transtbl fcntl_flags_tbl[ · line 267

Quoted 12 lines
linux-user/syscall.c +12 -0
+static char target_to_host_fcntl_cmd[] = {
+  [ TARGET_F_GETLK    ] = F_GETLK,
+  [ TARGET_F_SETLK    ] = F_SETLK,
+  [ TARGET_F_SETLKW   ] = F_SETLKW,
+  [ TARGET_F_GETLK64  ] = F_GETLK64,
+  [ TARGET_F_SETLK64  ] = F_SETLK64,
+  [ TARGET_F_SETLKW64 ] = F_SETLKW64,
+  [ TARGET_F_SETOWN   ] = F_SETOWN,
+  [ TARGET_F_GETOWN   ] = F_GETOWN,
+  [ TARGET_F_SETSIG   ] = F_SETSIG,
+  [ TARGET_F_GETSIG   ] = F_GETSIG,
+};

explict size for table and check for not overflowing?

And check for unset entries. The new code doesn't return EINVAL for unknown commands as it should. (But it calls the host with a zero command _if_ the target command is smaller than the table... which results in EINVAL). The old code wasn't perfect, passing junk command values to the host that it didn't recognise.

Also, are TARGET_F_GETLK64 and TARGET_F_GETLK distinct values on 64-bit hosts - or even exist at all?

Quoted 2 lines

cmd2 is not a very good variable name, for example host_cmd would describe it better.

Agree.

Quoted 7 lines
Quoted 7 lines
linux-user/syscall.c +0 -7
-        switch(arg2){
-        case TARGET_F_GETLK64:
-            cmd = F_GETLK64;
-            break;
-        case TARGET_F_SETLK64:
-            cmd = F_SETLK64;
-            break;
Quoted 8 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int n · line 6245

Quoted 8 lines
linux-user/syscall.c +1 -7
-        case TARGET_F_SETLKW64:
-            cmd = F_SETLK64;
-            break;
-        default:
-            cmd = arg2;
-            break;
-        }
+	cmd = target_to_host_fcntl_cmd[arg2];

The new code behaves differently for unknown arg2 values. The old code passed junk to the host kernel; the new code passes zero if arg2 < the table size, and reads outside the array otherwise. Both are surely wrong? Simply return EINVAL if arg2 isn't recognised.

I'm inclined to keeping the switch, adding the other cases (TARGET_F_GETLK etc.), #ifdef around the ..64 ones, and making the default case return EINVAL explicitly. A table lookup wouldn't save anything once you've checked its bounds and for the no-entry case. room.

-- Jamie

#5 lore raw
Quoted 10 lines · Jamie Lokier
Quoted 1 line

explict size for table and check for not overflowing?

And check for unset entries. The new code doesn't return EINVAL for unknown commands as it should. (But it calls the host with a zero command _if_ the target command is smaller than the table... which results in EINVAL). The old code wasn't perfect, passing junk command values to the host that it didn't recognise.

Also, are TARGET_F_GETLK64 and TARGET_F_GETLK distinct values on 64-bit hosts - or even exist at all?

The old code appears to be a slightly incoherant mix of fcntl, fcntl64, GETLK64, GETLK, ... and in a need of major cleanup, too.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
linux-user/syscall.c +0 -12
-        switch(arg2){
-        case TARGET_F_GETLK64:
-            cmd = F_GETLK64;
-            break;
-        case TARGET_F_SETLK64:
-            cmd = F_SETLK64;
-            break;
-        case TARGET_F_SETLKW64:
-            cmd = F_SETLK64;
-            break;
-        default:
-            cmd = arg2;
Quoted 8 lines
Quoted 3 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int n · line 6245

Quoted 3 lines
linux-user/syscall.c +1 -2
-            break;
-        }
+	cmd = target_to_host_fcntl_cmd[arg2];

The new code behaves differently for unknown arg2 values. The old code passed junk to the host kernel; the new code passes zero if arg2 < the table size, and reads outside the array otherwise. Both are surely wrong? Simply return EINVAL if arg2 isn't recognised.

note that that was just the fcntl64 syscall, the other place was fcntl.

Quoted 5 lines

I'm inclined to keeping the switch, adding the other cases (TARGET_F_GETLK etc.), #ifdef around the ..64 ones, and making the default case return EINVAL explicitly. A table lookup wouldn't save anything once you've checked its bounds and for the no-entry case. room.

My original idea was that a mapping function would make it clearer what is being done, and the main save being able to use it from two places (fcntl and fcntl64).

#6 lore raw
Quoted 9 lines · Riku Voipio
Quoted 7 lines · Jamie Lokier
Quoted 1 line

explict size for table and check for not overflowing?

And check for unset entries. The new code doesn't return EINVAL for unknown commands as it should. (But it calls the host with a zero command _if_ the target command is smaller than the table... which results in EINVAL). The old code wasn't perfect, passing junk command values to the host that it didn't recognise.

Quoted 5 lines
Quoted 2 lines

Also, are TARGET_F_GETLK64 and TARGET_F_GETLK distinct values on 64-bit hosts - or even exist at all?

The old code appears to be a slightly incoherant mix of fcntl, fcntl64, GETLK64, GETLK, ... and in a need of major cleanup, too.

I would rather say that it's doing what the kernel is doing :) Look at fs/fcntl.c and you'll see a very similar code. Having similar code between qemu and kernel for the linux-user syscall stuff is a good idea as it makes sure the emulated syscall has a very similar behaviour as the real one.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
linux-user/syscall.c +0 -12
-        switch(arg2){
-        case TARGET_F_GETLK64:
-            cmd = F_GETLK64;
-            break;
-        case TARGET_F_SETLK64:
-            cmd = F_SETLK64;
-            break;
-        case TARGET_F_SETLKW64:
-            cmd = F_SETLK64;
-            break;
-        default:
-            cmd = arg2;
Quoted 8 lines
Quoted 3 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int n · line 6245

Quoted 3 lines
linux-user/syscall.c +1 -2
-            break;
-        }
+	cmd = target_to_host_fcntl_cmd[arg2];

The new code behaves differently for unknown arg2 values. The old code passed junk to the host kernel; the new code passes zero if arg2 < the table size, and reads outside the array otherwise. Both are surely wrong? Simply return EINVAL if arg2 isn't recognised.

Quoted 7 lines

note that that was just the fcntl64 syscall, the other place was fcntl.

Quoted 5 lines

I'm inclined to keeping the switch, adding the other cases (TARGET_F_GETLK etc.), #ifdef around the ..64 ones, and making the default case return EINVAL explicitly. A table lookup wouldn't save anything once you've checked its bounds and for the no-entry case. room.

keeping the first fcntl64 switch is making things harder to understand. Also, the do_fcntl call should be kept to avoid duplicating code imho.

Quoted 4 lines

My original idea was that a mapping function would make it clearer what is being done, and the main save being able to use it from two places (fcntl and fcntl64).

I though it was better to use an array instead of a function with something like a switch() but looks like I can't avoid making one. Will wait for new comments (if there are some) and come with a new patch tomorrow.

Thanks,
Arnaud