linux-user: Use correct alignment for long long on i386 guests
PATCH 1/1 linux-user: Use correct alignment for long long on i386 guests
For i386, the ABI specifies that 'long long' (8 byte values) need only be 4 aligned, but we were requiring them to be 8-aligned. This meant we were laying out the target_epoll_event structure wrongly. Add a suitable ifdef to abitypes.h to specify the i386-specific alignment requirement.
--- include/exec/user/abitypes.h | 4 ++++ 1 file changed, 4 insertions(+)
include/exec/user/abitypes.h +4 -0
@@ -15,6 +15,10 @@#define ABI_LLONG_ALIGNMENT 2#endif +#if defined(TARGET_I386 ) && !defined(TARGET_X86_64 ) +#define ABI_LLONG_ALIGNMENT 4 +#endif +#ifndef ABI_SHORT_ALIGNMENT #define ABI_SHORT_ALIGNMENT 2#endif
Signature · 1 line
1.9.1
Quoted 18 lines · Peter Maydell
For i386, the ABI specifies that 'long long' (8 byte values) need only be 4 aligned, but we were requiring them to be 8-aligned. This meant we were laying out the
target_epoll_eventstructure wrongly. Add a suitable ifdef toabitypes.hto specify the i386-specific alignment requirement.Reported-by Icenowy Zheng <[email protected]>Signed-off-by Peter Maydell <[email protected]>--- include/exec/user/abitypes.h | 4 ++++ 1 file changed, 4 insertions(+)include/exec/user/abitypes.h +0
-0
include/exec/user/abitypes.h line 24
Quoted 10 lines
include/exec/user/abitypes.h +4
-0@@ -15,6 +15,10 @@#define ABI_LLONG_ALIGNMENT 2#endif +#if defined(TARGET_I386 ) && !defined(TARGET_X86_64 ) +#define ABI_LLONG_ALIGNMENT 4 +#endif +#ifndef ABI_SHORT_ALIGNMENT #define ABI_SHORT_ALIGNMENT 2#endif
Why the following program from commit
c2e3dee linux-user: Define target alignment size
int main(void)
{
printf("alignof(short) %ld\n", __alignof__(short));
printf("alignof(int) %ld\n", __alignof__(int));
printf("alignof(long) %ld\n", __alignof__(long));
printf("alignof(long long) %ld\n", __alignof__(long long));
}gives me:
alignof(short) 2
alignof(int) 4
alignof(long) 4
alignof(long long) 8
?
I compile it on x86_64 in 32bit mode:
cc -m32 -o align align.c
file align
align: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=f5312f2334462b48fd9764e78a845879ff317b94, not stripped
Thanks,
Laurent
Quoted 13 lines · Laurent Vivier
Why the following program from commit
c2e3dee linux-user: Define target alignment size
int main(void)
{ printf("alignof(short) %ld\n", __alignof__(short)); printf("alignof(int) %ld\n", __alignof__(int)); printf("alignof(long) %ld\n", __alignof__(long)); printf("alignof(long long) %ld\n", __alignof__(long long)); }
Quoted 8 lines
gives me:
alignof(short) 2
alignof(int) 4
alignof(long) 4
alignof(long long) 8?
Because gcc __alignof__ gives you the maximum preferred alignment, not the minimum alignment. If you want to know what the alignment used for setting struct alignment is, use C99 alignof from stdalign.h and/or cross-check by using sizeof() on a struct with the type in it:
orth$ uname -a
Linux orth 3.16.0-4-686-pae #1 SMP Debian 3.16.7-ckt20-1+deb8u3 (2016-01-17) i686 GNU/Linux
orth$ cat /tmp/zz9.c
#include <stdio.h>
#include <stdalign.h>
struct epoll_event {
int events;
unsigned long long data;
};
#define PRINTALIGN(T) \
printf(#T ": sizeof %zd __alignof__ %zd _Alignof %zd alignof %zd\n", \
sizeof(T), __alignof__(T), _Alignof(T), alignof(T))int main(void) {
PRINTALIGN(unsigned long long);
PRINTALIGN(struct epoll_event);
return 0;
}orth$ gcc -g -Wall -o /tmp/zz9 /tmp/zz9.c
orth$ /tmp/zz9
unsigned long long: sizeof 8 __alignof__ 8 _Alignof 4 alignof 4 struct epoll_event: sizeof 12 __alignof__ 4 _Alignof 4 alignof 4
Also watch out for broken clang versions:
orth$ clang -g -Wall -o /tmp/zz9clang /tmp/zz9.c
orth$ /tmp/zz9clang
unsigned long long: sizeof 8 __alignof__ 8 _Alignof 8 alignof 8 struct epoll_event: sizeof 12 __alignof__ 4 _Alignof 4 alignof 4 orth$ clang --version
Debian clang version 3.5.0-10 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: i386-pc-linux-gnu
Thread model: posix
(that's fixed in later clang versions)
thanks
-- PMM
Quoted 5 lines · Peter Maydell
For i386, the ABI specifies that 'long long' (8 byte values) need only be 4 aligned, but we were requiring them to be 8-aligned. This meant we were laying out the
target_epoll_eventstructure wrongly. Add a suitable ifdef toabitypes.hto specify the i386-specific alignment requirement.
gdb qemu-i386
(gdb) p &(((struct target_epoll_event *)0)->data)
$1 = (target_epoll_data_t *) 0x8
whereas:
gdb qemu-x86_64
(gdb) p &(((struct target_epoll_event *)0)->data)
$1 = (target_epoll_data_t *) 0x4
I've checked on real systems x86_64/i386:
-----
#include <sys/epoll.h>
int main(void)
{
volatile struct epoll_event e;
e.events = 0;
}----
(gdb) p &(((struct epoll_event *)0)->data)
$1 = (epoll_data_t *) 0x4
but on ppc64, I have
(gdb) p &(((struct epoll_event *)0)->data)
$1 = (epoll_data_t *) 0x8
In fact, the structure should be packed in both cases, something like:
linux-user/syscall_defs.h +0 -0
@@ -2562,7 +2562,7 @@ struct target_mq_attr {#define FUTEX_CMD_MASK ~(FUTEX_PRIVATE_FLAG |
FUTEX_CLOCK_REALTIME)
#ifdef CONFIG_EPOLL
-#if defined(TARGET_X86_64)
+#if defined(TARGET_X86_64) || defined(TARGET_I386)
#define TARGET_EPOLL_PACKED QEMU_PACKED #else
#define TARGET_EPOLL_PACKED
on my Fedora systems x86_64/i386:
/usr/include/bits/epoll.h
#define __EPOLL_PACKED __attribute__ ((__packed__))
/usr/include/sys/epoll.h
struct epoll_event
{
uint32_t events; /* Epoll events */
epoll_data_t data; /* User data variable */
} __EPOLL_PACKED;
but I don't understand why in linux source tree we have
#ifdef __x86_64__
#define EPOLL_PACKED __attribute__((packed))
#else
#define EPOLL_PACKED
#endif
struct epoll_event {
__u32 events;
__u64 data;
} EPOLL_PACKED;Laurent
Quoted 9 lines · Laurent Vivier
Quoted 5 lines · Peter Maydell
For i386, the ABI specifies that 'long long' (8 byte values) need only be 4 aligned, but we were requiring them to be 8-aligned. This meant we were laying out the
target_epoll_eventstructure wrongly. Add a suitable ifdef toabitypes.hto specify the i386-specific alignment requirement.
Quoted 3 lines
gdb qemu-i386
(gdb) p &(((structtarget_epoll_event*)0)->data)
$1 = (target_epoll_data_t*)0x8
This is wrong; perhaps the debug info doesn't correctly include alignment requirements for types? (If you print sizes/offsets in QEMU's C code I believe you get different answers from what gdb is telling you here.)
Quoted 16 lines
whereas:
gdb qemu-x86_64
(gdb) p &(((structtarget_epoll_event*)0)->data)
$1 = (target_epoll_data_t*)0x4I've checked on real systems x86_64/i386:
-----
#include <sys/epoll.h>int main(void)
{ volatile struct epoll_event e; e.events = 0; }
Quoted 8 lines
----
(gdb) p &(((structepoll_event*)0)->data)
$1 = (epoll_data_t*)0x4but on ppc64, I have
(gdb) p &(((struct
epoll_event*)0)->data)
$1 = (epoll_data_t*)0x8
Yep; ppc64 doesn't pack the struct and it has an 8-align requirement for 64-bit types.
Quoted 13 lines
In fact, the structure should be packed in both cases, something like:
linux-user/syscall_defs.h +1
-1@@ -2562,7 +2562,7 @@ struct target_mq_attr {#define FUTEX_CMD_MASK ~(FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME)#ifdef CONFIG_EPOLL -#if defined(TARGET_X86_64 ) +#if defined(TARGET_X86_64 ) || defined(TARGET_I386 )#define TARGET_EPOLL_PACKED QEMU_PACKED#else
Quoted 1 line
#define
TARGET_EPOLL_PACKED
If we have the correctly defined alignment requirement on the target 64-bit type, then you don't need this. (See the kernel sources you quote below which only provide the packed attribute on x86-64, not on i386).
Quoted 6 lines
on my Fedora systems x86_64/i386:
/usr/include/bits/epoll.h#define
__EPOLL_PACKED__attribute__((__packed__))
Quoted 7 lines
/usr/include/sys/epoll.hstruct
epoll_event
{
uint32_tevents; /* Epoll events */
epoll_data_tdata; /* User data variable */
}__EPOLL_PACKED;
These are the libc data structures, which aren't defined the same way as the kernel ones.
Quoted 8 lines
but I don't understand why in linux source tree we have
#ifdef
__x86_64__
#defineEPOLL_PACKED__attribute__((packed))
#else
#defineEPOLL_PACKED
#endif
Quoted 4 lines
struct epoll_event {
__u32 events;
__u64 data;
} EPOLL_PACKED;This is the kernel structure. x86-64 is a weird outlier because they wanted to make the structure layout identical to i386 (to ease the compat-syscall implementation). Every other arch just lets the struct be naturally laid out for the types it contains.
thanks
-- PMM
Quoted 18 lines · Peter Maydell
For i386, the ABI specifies that 'long long' (8 byte values) need only be 4 aligned, but we were requiring them to be 8-aligned. This meant we were laying out the
target_epoll_eventstructure wrongly. Add a suitable ifdef toabitypes.hto specify the i386-specific alignment requirement.Reported-by Icenowy Zheng <[email protected]>Signed-off-by Peter Maydell <[email protected]>--- include/exec/user/abitypes.h | 4 ++++ 1 file changed, 4 insertions(+)include/exec/user/abitypes.h +0
-0
include/exec/user/abitypes.h line 24
Quoted 10 lines
include/exec/user/abitypes.h +4
-0@@ -15,6 +15,10 @@#define ABI_LLONG_ALIGNMENT 2#endif +#if defined(TARGET_I386 ) && !defined(TARGET_X86_64 ) +#define ABI_LLONG_ALIGNMENT 4 +#endif +#ifndef ABI_SHORT_ALIGNMENT #define ABI_SHORT_ALIGNMENT 2#endif
Riku Voipio
AppliedQuoted 5 lines · Peter Maydell
For i386, the ABI specifies that 'long long' (8 byte values) need only be 4 aligned, but we were requiring them to be 8-aligned. This meant we were laying out the
target_epoll_eventstructure wrongly. Add a suitable ifdef toabitypes.hto specify the i386-specific alignment requirement.
Thanks, applied all your patches upto this patch to:
https://git.linaro.org/people/riku.voipio/qemu.git/shortlog/refs/heads/for-next
I take none of the patches are important enough to warrant including in 2.7?
Quoted 24 lines
Reported-by Icenowy Zheng <[email protected]>Signed-off-by Peter Maydell <[email protected]>--- include/exec/user/abitypes.h | 4 ++++ 1 file changed, 4 insertions(+)include/exec/user/abitypes.h +4
-0@@ -15,6 +15,10 @@#define ABI_LLONG_ALIGNMENT 2#endif +#if defined(TARGET_I386 ) && !defined(TARGET_X86_64 ) +#define ABI_LLONG_ALIGNMENT 4 +#endif +#ifndef ABI_SHORT_ALIGNMENT #define ABI_SHORT_ALIGNMENT 2#endif Signature · 1 line
1.9.1
Quoted 7 lines · Riku Voipio
Quoted 5 lines · Peter Maydell
For i386, the ABI specifies that 'long long' (8 byte values) need only be 4 aligned, but we were requiring them to be 8-aligned. This meant we were laying out the
target_epoll_eventstructure wrongly. Add a suitable ifdef toabitypes.hto specify the i386-specific alignment requirement.
Quoted 6 lines
Thanks, applied all your patches upto this patch to:
https://git.linaro.org/people/riku.voipio/qemu.git/shortlog/refs/heads/for-next
I take none of the patches are important enough to warrant including in 2.7?
I think I would suggest at least these for 2.7:
linux-user: Use correct alignment for long long on i386 guests
(fixes a real user-reported bug)
linux-user: Fix memchr() argument in open_self_cmdline()
linux-user: Don't write off end of new_utsname buffer
(both buffer overruns that could plausibly happen)
linux-user: Fix target_semid_ds structure definition
(sysv semaphore completely broken on many guest archs)
linux-user: Handle brk() attempts with very large sizes
(because I'd like to be able to tell the gcc folks they
can just test with QEMU 2.7)
with perhaps the rest of the coverity-fixes on the maybe list.
thanks
-- PMM