Skip to contentSkip to conversation

linux-user: Suppress address-of-packed-member warnings in __get/put_user_e

PATCH Laurent Vivier 3 participants 9 messages Oct 4, 2018 – Oct 9, 2018 1 message not in the archive

lore
Share & tools
lore

Laurent Vivier

author Applied
#1 lore raw
In reply to a message not in the archive

Le 28/09/2018 à 16:25, Peter Maydell a écrit :

Quoted 6 lines

Our __get_user_e() and __put_user_e() macros cause newer versions of clang to generate false-positive -Waddress-of-packed-member warnings if they are passed the address of a member of a packed struct (see https://bugs.llvm.org/show_bug.cgi?id=39113).
Suppress these using the _Pragma() operator.

Quoted 9 lines

To put in the pragmas we need to convert the macros from expressions to statements, but all the callsites effectively treat them as statements already so this is OK.

---
 linux-user/qemu.h | 57 +++++++++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 19 deletions(-)

Applied to my linux-user branch.

Thanks,
Laurent

#2 lore raw
Quoted 11 lines · Laurent Vivier

Le 28/09/2018 à 16:25, Peter Maydell a écrit :

Quoted 10 lines

Our __get_user_e() and __put_user_e() macros cause newer versions of clang to generate false-positive -Waddress-of-packed-member warnings if they are passed the address of a member of a packed struct (see https://bugs.llvm.org/show_bug.cgi?id=39113).
Suppress these using the _Pragma() operator.

To put in the pragmas we need to convert the macros from expressions to statements, but all the callsites effectively treat them as statements already so this is OK.

Quoted 7 lines
Quoted 5 lines
---
 linux-user/qemu.h | 57 +++++++++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 19 deletions(-)

Applied to my linux-user branch.

I have the following error when building on Fedora 28 and gcc (GCC) 8.1.1 20180712 (Red Hat 8.1.1-5)

CC aarch64_be-linux-user/target/arm/arm-semi.o
.../target/arm/arm-semi.c: In function ‘do_arm_semihosting’:
.../target/arm/arm-semi.c:270:1: error: unknown option after ‘#pragma GCC diagnostic’ kind [-Werror=pragmas]

Perhaps you should use a "#if defined(__clang__)" to apply your fix only to clang?

Thanks,
Laurent

#3 lore raw
Quoted 10 lines · Laurent Vivier

I have the following error when building on Fedora 28 and gcc (GCC) 8.1.1 20180712 (Red Hat 8.1.1-5)

CC aarch64_be-linux-user/target/arm/arm-semi.o
.../target/arm/arm-semi.c: In function ‘do_arm_semihosting’:
.../target/arm/arm-semi.c:270:1: error: unknown option after ‘#pragma GCC diagnostic’ kind [-Werror=pragmas]

Perhaps you should use a "#if defined(__clang__)" to apply your fix only to clang?

I did test on gcc, but not that version. The point of the
_Pragma("GCC diagnostic ignored \"-Wpragmas\"");
line is to suppress that error (which it does on my gcc 5) so I don't know why your gcc is complaining :-(

If you add an extra diagnostic push, so:

+        _Pragma("GCC diagnostic push");                                     \
+        _Pragma("GCC diagnostic ignored \"-Wpragmas\"");                    \
+        _Pragma("GCC diagnostic push");                                     \
+        _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"");   \

and then a balancing extra

+ _Pragma("GCC diagnostic pop"); \

at the end, I don't suppose that helps?

(I generally prefer to avoid marking things as clang- or gcc- specific where I can.)

thanks
-- PMM

#4 lore raw
Quoted 9 lines · Peter Maydell
Quoted 8 lines · Laurent Vivier

I have the following error when building on Fedora 28 and gcc (GCC) 8.1.1 20180712 (Red Hat 8.1.1-5)

CC aarch64_be-linux-user/target/arm/arm-semi.o
.../target/arm/arm-semi.c: In function ‘do_arm_semihosting’:
.../target/arm/arm-semi.c:270:1: error: unknown option after ‘#pragma GCC diagnostic’ kind [-Werror=pragmas]

Quoted 7 lines
Quoted 2 lines

Perhaps you should use a "#if defined(__clang__)" to apply your fix only to clang?

I did test on gcc, but not that version. The point of the
_Pragma("GCC diagnostic ignored \"-Wpragmas\"");
line is to suppress that error (which it does on my gcc 5) so I don't know why your gcc is complaining :-(

You should add Fedora 28 to you collection of virtual machines :)

Quoted 8 lines

If you add an extra diagnostic push, so:

(quoted diff) +4 -0
+        _Pragma("GCC diagnostic push");                                     \
+        _Pragma("GCC diagnostic ignored \"-Wpragmas\"");                    \
+        _Pragma("GCC diagnostic push");                                     \
+        _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"");   \
Quoted 5 lines

and then a balancing extra

+ _Pragma("GCC diagnostic pop"); \

at the end, I don't suppose that helps?

No, it doesn't help

Quoted 2 lines

(I generally prefer to avoid marking things as clang- or gcc- specific where I can.)

I understand.
But it's a clang bug, it seems reasonable to have specific code to workaround it.

Thanks,
Laurent

#5 lore raw
In reply to Peter Maydell
Quoted 9 lines · Peter Maydell
Quoted 8 lines · Laurent Vivier

I have the following error when building on Fedora 28 and gcc (GCC) 8.1.1 20180712 (Red Hat 8.1.1-5)

CC aarch64_be-linux-user/target/arm/arm-semi.o
.../target/arm/arm-semi.c: In function ‘do_arm_semihosting’:
.../target/arm/arm-semi.c:270:1: error: unknown option after ‘#pragma GCC diagnostic’ kind [-Werror=pragmas]

Quoted 7 lines
Quoted 2 lines

Perhaps you should use a "#if defined(__clang__)" to apply your fix only to clang?

I did test on gcc, but not that version. The point of the
_Pragma("GCC diagnostic ignored \"-Wpragmas\"");
line is to suppress that error (which it does on my gcc 5) so I don't know why your gcc is complaining :-(

I suppose you could try -Wunknown-pragmas.
But from reading the manual it shouldn't make a difference. OTOH, what you wrote should have worked...

r~

#6 lore raw
Quoted 13 lines · Richard Henderson
Quoted 12 lines · Peter Maydell
Quoted 10 lines · Laurent Vivier

I have the following error when building on Fedora 28 and gcc (GCC) 8.1.1 20180712 (Red Hat 8.1.1-5)

CC aarch64_be-linux-user/target/arm/arm-semi.o
.../target/arm/arm-semi.c: In function ‘do_arm_semihosting’:
.../target/arm/arm-semi.c:270:1: error: unknown option after ‘#pragma GCC diagnostic’ kind [-Werror=pragmas]

Perhaps you should use a "#if defined(__clang__)" to apply your fix only to clang?

Quoted 8 lines
Quoted 4 lines

I did test on gcc, but not that version. The point of the
_Pragma("GCC diagnostic ignored \"-Wpragmas\"");
line is to suppress that error (which it does on my gcc 5) so I don't know why your gcc is complaining :-(

I suppose you could try -Wunknown-pragmas.
But from reading the manual it shouldn't make a difference. OTOH, what you wrote should have worked...

Could it be a bug in _Pragma()?

If I use "#pragma" for the file, it works:

linux-user/qemu.h +1 -2
@@ -474,10 +474,10 @@ static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
  *   an unknown warning type from older compilers that don't know about
  *   -Waddress-of-packed-member.
  */
+#pragma GCC diagnostic ignored "-Wpragmas"
 #define __put_user_e(x, hptr, e)                                            \
     do {                                                                    \
         _Pragma("GCC diagnostic push");                                     \
-        _Pragma("GCC diagnostic ignored \"-Wpragmas\"");                    \
         _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"");   \
         (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,                 \
         __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,            \
@@ -490,7 +490,6 @@ static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
 #define __get_user_e(x, hptr, e)                                            \
     do {                                                                    \
         _Pragma("GCC diagnostic push");                                     \
-        _Pragma("GCC diagnostic ignored \"-Wpragmas\"");                    \
         _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"");   \
         ((x) = (typeof(*hptr))(                                             \
         __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,                 \
#7 lore raw
Quoted 19 lines · Laurent Vivier
Quoted 18 lines · Richard Henderson
Quoted 16 lines · Peter Maydell
Quoted 10 lines · Laurent Vivier

I have the following error when building on Fedora 28 and gcc (GCC) 8.1.1 20180712 (Red Hat 8.1.1-5)

CC aarch64_be-linux-user/target/arm/arm-semi.o
.../target/arm/arm-semi.c: In function ‘do_arm_semihosting’:
.../target/arm/arm-semi.c:270:1: error: unknown option after ‘#pragma GCC diagnostic’ kind [-Werror=pragmas]

Perhaps you should use a "#if defined(__clang__)" to apply your fix only to clang?

I did test on gcc, but not that version. The point of the
_Pragma("GCC diagnostic ignored \"-Wpragmas\"");
line is to suppress that error (which it does on my gcc 5) so I don't know why your gcc is complaining :-(

Quoted 5 lines
Quoted 3 lines

I suppose you could try -Wunknown-pragmas.
But from reading the manual it shouldn't make a difference. OTOH, what you wrote should have worked...

Could it be a bug in _Pragma()?

I got an f28 VM and can repro this. It looks like the problem only happens when the get_user/put_user stuff is used via some other macro, for some reason -- so the GET_ARG and SET_ARG macros in arm-semi.c and the NEW_AUX_ENT macro in elfload.c cause problems, but less indirect use does not. I'll try to trim this down to a smaller test case somehow...

thanks
-- PMM

#8 lore raw
Quoted 9 lines · Peter Maydell
Quoted 1 line · Laurent Vivier

Could it be a bug in _Pragma()?

I got an f28 VM and can repro this. It looks like the problem only happens when the get_user/put_user stuff is used via some other macro, for some reason -- so the GET_ARG and SET_ARG macros in arm-semi.c and the NEW_AUX_ENT macro in elfload.c cause problems, but less indirect use does not. I'll try to trim this down to a smaller test case somehow...

I think it is a bug in _Pragma(). I boiled it down to a small test case, and tested using the compilers on godbolt.org. The test fails with gcc 5, 6, 7 and 8 but works on 4 and on gcc trunk.

Trying to use _Pragma() inside a macro to disable warnings seems to be pretty broken. The GCC bugzilla has:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85153
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69558
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82335
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66099
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69543

which are various bug reports in this area, with test cases which variously are fixed in gcc 8, or fixed not in 8 but in trunk, or still broken in trunk. So there isn't a single bug here but a cluster of similar ones.

I think the underlying difficulty is that the compiler decides whether to suppress the warning by looking at the source location for the warning, and at the source locations of the various pragmas, and especially when macro expansion is involved the "source location" can be harder to correctly identify.

I'm not sure if there is some "safe" subset of _Pragma()-in-macro use here that will work regardless of compiler version, or if we should just give up on it as non-feasible and try to silence the warnings some other way.

Looks like clang is not flawless in this area either:
https://bugs.llvm.org/show_bug.cgi?id=31999
https://bugs.llvm.org/show_bug.cgi?id=15129
https://bugs.llvm.org/show_bug.cgi?id=35154

thanks
-- PMM

#9 lore raw

Le 09/10/2018 à 16:52, Peter Maydell a écrit :

Quoted 36 lines · Peter Maydell
Quoted 9 lines · Laurent Vivier
Quoted 1 line

Could it be a bug in _Pragma()?

I got an f28 VM and can repro this. It looks like the problem only happens when the get_user/put_user stuff is used via some other macro, for some reason -- so the GET_ARG and SET_ARG macros in arm-semi.c and the NEW_AUX_ENT macro in elfload.c cause problems, but less indirect use does not. I'll try to trim this down to a smaller test case somehow...

I think it is a bug in _Pragma(). I boiled it down to a small test case, and tested using the compilers on godbolt.org. The test fails with gcc 5, 6, 7 and 8 but works on 4 and on gcc trunk.

Trying to use _Pragma() inside a macro to disable warnings seems to be pretty broken. The GCC bugzilla has:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85153
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69558
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82335
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66099
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69543

which are various bug reports in this area, with test cases which variously are fixed in gcc 8, or fixed not in 8 but in trunk, or still broken in trunk. So there isn't a single bug here but a cluster of similar ones.

I think the underlying difficulty is that the compiler decides whether to suppress the warning by looking at the source location for the warning, and at the source locations of the various pragmas, and especially when macro expansion is involved the "source location" can be harder to correctly identify.

Quoted 4 lines

I'm not sure if there is some "safe" subset of _Pragma()-in-macro use here that will work regardless of compiler version, or if we should just give up on it as non-feasible and try to silence the warnings some other way.

To keep it simple, and as I have suggested before, perhaps you can use only the _Pragma() we need to workaround clang bug with clang (#ifde __clang__)?

Thanks,
Laurent