Skip to contentSkip to conversation

Fix ioctl code generation macros

PATCH In review Lionel Landwerlin 2 participants 2 messages Mar 15, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix ioctl code generation macros

On 64bits hosts, the current TARGET_IO* macros generate 64 bits signed integers that do not always match the 32bits signed integers from emulated program (when emulating a 32bits user space program).

This makes problems if you're comparing the ioctl command value from the emulated program directly with the result of a TARGET_IO* macros.

Here a little example of the problem :

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])

{
        int a = 3222335234;
        long b = 3222335234;

        printf ("a = %i/%x, b = %i/%x\n",
                a, a, b, b);

        if (a == b)
                printf ("Fine !\n");
        else
                printf ("Ohoh ??\n");

        return EXIT_FAILURE;
}

<<<<<<<<<

Of course, the problem is solved if you're saving the result of the TARGET_IO* macros in an unsigned integer.

So here is a little patch that forces TARGET_IO* macros to output abi_long sized integers.

---

linux-user/syscall_defs.h +4 -4
index 8abe08b..89926f9 100644
@@ -83,10 +83,10 @@
 #define TARGET_IOC_DIRSHIFT    (TARGET_IOC_SIZESHIFT+TARGET_IOC_SIZEBITS)
 
 #define TARGET_IOC(dir,type,nr,size) \
-       (((dir)  << TARGET_IOC_DIRSHIFT) | \
-        ((type) << TARGET_IOC_TYPESHIFT) | \
-        ((nr)   << TARGET_IOC_NRSHIFT) | \
-        ((size) << TARGET_IOC_SIZESHIFT))
+       ((abi_long) (((dir)  << TARGET_IOC_DIRSHIFT) |  \
+                     ((type) << TARGET_IOC_TYPESHIFT) | \
+                     ((nr)   << TARGET_IOC_NRSHIFT) |   \
+                     ((size) << TARGET_IOC_SIZESHIFT)))
 
 /* used to create numbers */
 #define TARGET_IO(type,nr)             TARGET_IOC(TARGET_IOC_NONE,(type),(nr),0)
#1 lore raw
Quoted 5 lines · Lionel Landwerlin

Of course, the problem is solved if you're saving the result of the TARGET_IO* macros in an unsigned integer.

So here is a little patch that forces TARGET_IO* macros to output abi_long sized integers.

Surely the value currently produced by TARGET_IOC() *is* an unsigned int (provided you use the direction macros)? I'm not sure if any arch will ever need more than 32 bits here, but even with the cast to abi_long, the high bits would be truncated before the cast.

target_cmd in IOCTLEntry is an unsigned int, but the command is also passed around as an abi_long is some places... I couldn't see any code that's currently affected by this, though -- did I miss something?

Cheers,

Signature · 1 line
Stuart Brady