Skip to contentSkip to conversation

configure: Always compile with -fwrapv

PATCH In review Peter Maydell 3 participants 5 messages Sep 12, 2016 – Sep 13, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 configure: Always compile with -fwrapv

QEMU's code relies on left shifts of signed integers always being defined behaviour with the obvious 2s-complement semantics. The only way to tell the compiler (and any associated undefined-behaviour sanitizer) that we require a C dialect with these semantics is to use the -fwrapv option. This is a bit of a heavy hammer for the job as it also gives us guaranteed semantics on integer arithmetic overflow which in theory we don't require.

In an ideal world this would allow us to drop the warning flag -Wno-shift-negative-value, but we must retain this to avoid spurious warnings on clang versions predating the fix to https://llvm.org/bugs/show_bug.cgi?id=25552.

---
We agreed before 2.7 release that this was the best long term approach to our shift issues, since it's now clear that both clang and gcc do agree that -fwrapv provides the semantics we want.

 configure | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
configure +5 -1
index 331c36f..14efce3 100755
@@ -389,7 +389,11 @@ sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
 ARFLAGS="${ARFLAGS-rv}"
 
 # default flags for all hosts
-QEMU_CFLAGS="-fno-strict-aliasing -fno-common $QEMU_CFLAGS"
+# We use -fwrapv to tell the compiler that we require a C dialect where
+# left shift of signed integers is well defined and has the expected
+# 2s-complement style results. (Both clang and gcc agree that it
+# provides these semantics.)
+QEMU_CFLAGS="-fno-strict-aliasing -fno-common -fwrapv $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wall -Wundef -Wwrite-strings -Wmissing-prototypes $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS"
 QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 31 lines · Peter Maydell

QEMU's code relies on left shifts of signed integers always being defined behaviour with the obvious 2s-complement semantics. The only way to tell the compiler (and any associated undefined-behaviour sanitizer) that we require a C dialect with these semantics is to use the -fwrapv option. This is a bit of a heavy hammer for the job as it also gives us guaranteed semantics on integer arithmetic overflow which in theory we don't require.

In an ideal world this would allow us to drop the warning flag -Wno-shift-negative-value, but we must retain this to avoid spurious warnings on clang versions predating the fix to https://llvm.org/bugs/show_bug.cgi?id=25552.

---
We agreed before 2.7 release that this was the best long term approach to our shift issues, since it's now clear that both clang and gcc do agree that -fwrapv provides the semantics we want.

 configure | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
configure +0 -0
index 331c36f..14efce3 100755
@@ -389,7 +389,11 @@ sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
 ARFLAGS="${ARFLAGS-rv}"
 

configure sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}" · line 399

Quoted 10 lines
configure +5 -1
 # default flags for all hosts
-QEMU_CFLAGS="-fno-strict-aliasing -fno-common $QEMU_CFLAGS"
+# We use -fwrapv to tell the compiler that we require a C dialect where
+# left shift of signed integers is well defined and has the expected
+# 2s-complement style results. (Both clang and gcc agree that it
+# provides these semantics.)
+QEMU_CFLAGS="-fno-strict-aliasing -fno-common -fwrapv $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wall -Wundef -Wwrite-strings -Wmissing-prototypes $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS"
 QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"

If I remember correctly, we discussed -fno-strict-overflow (which the kernel uses), but in the end opted for the more stringent -fwrapv.

#2 lore raw
Quoted 2 lines · Markus Armbruster

If I remember correctly, we discussed -fno-strict-overflow (which the kernel uses), but in the end opted for the more stringent -fwrapv.

Yep. -fno-strict-overflow just says "don't do bad things on integer overflow (but it's still a bug in the program if it happens)", so overflows are still program bugs and the compiler/sanitizer will still complain about them. You need -fwrapv to say "overflows aren't bugs at all".

thanks
-- PMM

Acked-by on v1 1/1
#3 lore raw
In reply to Peter Maydell
Quoted 32 lines · Peter Maydell

QEMU's code relies on left shifts of signed integers always being defined behaviour with the obvious 2s-complement semantics. The only way to tell the compiler (and any associated undefined-behaviour sanitizer) that we require a C dialect with these semantics is to use the -fwrapv option. This is a bit of a heavy hammer for the job as it also gives us guaranteed semantics on integer arithmetic overflow which in theory we don't require.

In an ideal world this would allow us to drop the warning flag -Wno-shift-negative-value, but we must retain this to avoid spurious warnings on clang versions predating the fix to https://llvm.org/bugs/show_bug.cgi?id=25552.

---
We agreed before 2.7 release that this was the best long term approach to our shift issues, since it's now clear that both clang and gcc do agree that -fwrapv provides the semantics we want.

 configure | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
configure +0 -0
index 331c36f..14efce3 100755
@@ -389,7 +389,11 @@ sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
 ARFLAGS="${ARFLAGS-rv}"
 

configure sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}" · line 399

Quoted 10 lines
configure +5 -1
 # default flags for all hosts
-QEMU_CFLAGS="-fno-strict-aliasing -fno-common $QEMU_CFLAGS"
+# We use -fwrapv to tell the compiler that we require a C dialect where
+# left shift of signed integers is well defined and has the expected
+# 2s-complement style results. (Both clang and gcc agree that it
+# provides these semantics.)
+QEMU_CFLAGS="-fno-strict-aliasing -fno-common -fwrapv $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wall -Wundef -Wwrite-strings -Wmissing-prototypes $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS"
 QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"
author Applied
#4 lore raw
Quoted 34 lines · Paolo Bonzini
Quoted 31 lines · Peter Maydell

QEMU's code relies on left shifts of signed integers always being defined behaviour with the obvious 2s-complement semantics. The only way to tell the compiler (and any associated undefined-behaviour sanitizer) that we require a C dialect with these semantics is to use the -fwrapv option. This is a bit of a heavy hammer for the job as it also gives us guaranteed semantics on integer arithmetic overflow which in theory we don't require.

In an ideal world this would allow us to drop the warning flag -Wno-shift-negative-value, but we must retain this to avoid spurious warnings on clang versions predating the fix to https://llvm.org/bugs/show_bug.cgi?id=25552.

---
We agreed before 2.7 release that this was the best long term approach to our shift issues, since it's now clear that both clang and gcc do agree that -fwrapv provides the semantics we want.

 configure | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
configure +0 -0
index 331c36f..14efce3 100755
@@ -389,7 +389,11 @@ sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
 ARFLAGS="${ARFLAGS-rv}"
 
Quoted 13 lines

configure sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}" · line 399

Quoted 11 lines
configure +5 -1
 # default flags for all hosts
-QEMU_CFLAGS="-fno-strict-aliasing -fno-common $QEMU_CFLAGS"
+# We use -fwrapv to tell the compiler that we require a C dialect where
+# left shift of signed integers is well defined and has the expected
+# 2s-complement style results. (Both clang and gcc agree that it
+# provides these semantics.)
+QEMU_CFLAGS="-fno-strict-aliasing -fno-common -fwrapv $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wall -Wundef -Wwrite-strings -Wmissing-prototypes $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS"
 QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"

Applied to master, thanks.

-- PMM