Skip to contentSkip to conversation

x86: mark BUG()s and assertion failures as terminal.

PATCH In review Tim Deegan 3 participants 8 messages Sep 19, 2013

lore
Share & tools
lore

Tim Deegan

author
#0 lore raw

PATCH 1/1 x86: mark BUG()s and assertion failures as terminal.

This helps avoid static analysis false-positives, and might lead to better code density as the compiler knows it doesn't have to restore spilled state &c.

---
 xen/include/asm-x86/bug.h  |   11 ++++++++---
 xen/include/xen/compiler.h |    6 ++++++
 2 files changed, 14 insertions(+), 3 deletions(-)
xen/include/asm-x86/bug.h +8 -3
index e5dd559..956bfd2 100644
@@ -46,12 +46,17 @@ struct bug_frame {
 
 
 #define WARN() BUG_FRAME(BUGFRAME_warn, __LINE__, __FILE__, 0, NULL)
-#define BUG()  BUG_FRAME(BUGFRAME_bug,  __LINE__, __FILE__, 0, NULL)
+#define BUG() do {                                              \
+    BUG_FRAME(BUGFRAME_bug,  __LINE__, __FILE__, 0, NULL);      \
+    unreachable();                                              \
+} while (0)
 
 #define run_in_exception_handler(fn) BUG_FRAME(BUGFRAME_run_fn, 0, fn, 0, NULL)
 
-#define assert_failed(msg)                                      \
-    BUG_FRAME(BUGFRAME_assert, __LINE__, __FILE__, 1, msg)
+#define assert_failed(msg) do {                                 \
+    BUG_FRAME(BUGFRAME_assert, __LINE__, __FILE__, 1, msg);     \
+    unreachable();                                              \
+} while (0)
 
 extern const struct bug_frame __start_bug_frames[],
                               __stop_bug_frames_0[],
xen/include/xen/compiler.h +6 -0
index 7009a09..7d6805c 100644
@@ -14,6 +14,12 @@
 #define always_inline __inline__ __attribute__ ((always_inline))
 #define noinline      __attribute__((noinline))
 
+#if (!defined(__clang__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 5))
+#define unreachable() do {} while (1)
+#else
+#define unreachable() __builtin_unreachable()
+#endif
+
 #ifdef __clang__
 /* Clang can replace some vars with new automatic ones that go in .data;
  * mark all explicit-segment vars 'used' to prevent that. */
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 41 lines · Tim Deegan

This helps avoid static analysis false-positives, and might lead to better code density as the compiler knows it doesn't have to restore spilled state &c.

---
 xen/include/asm-x86/bug.h  |   11 ++++++++---
 xen/include/xen/compiler.h |    6 ++++++
 2 files changed, 14 insertions(+), 3 deletions(-)
xen/include/asm-x86/bug.h +8 -3
index e5dd559..956bfd2 100644
@@ -46,12 +46,17 @@ struct bug_frame {
 
 
 #define WARN() BUG_FRAME(BUGFRAME_warn, __LINE__, __FILE__, 0, NULL)
-#define BUG()  BUG_FRAME(BUGFRAME_bug,  __LINE__, __FILE__, 0, NULL)
+#define BUG() do {                                              \
+    BUG_FRAME(BUGFRAME_bug,  __LINE__, __FILE__, 0, NULL);      \
+    unreachable();                                              \
+} while (0)
 
 #define run_in_exception_handler(fn) BUG_FRAME(BUGFRAME_run_fn, 0, fn, 0,
 NULL)
 
-#define assert_failed(msg)                                      \
-    BUG_FRAME(BUGFRAME_assert, __LINE__, __FILE__, 1, msg)
+#define assert_failed(msg) do {                                 \
+    BUG_FRAME(BUGFRAME_assert, __LINE__, __FILE__, 1, msg);     \
+    unreachable();                                              \
+} while (0)
 
 extern const struct bug_frame __start_bug_frames[],
                               __stop_bug_frames_0[],
xen/include/xen/compiler.h +0 -0
index 7009a09..7d6805c 100644

xen/include/xen/compiler.h line 17

Quoted 4 lines
xen/include/xen/compiler.h +1 -0
@@ -14,6 +14,12 @@
 #define always_inline __inline__ __attribute__ ((always_inline))
 #define noinline      __attribute__((noinline))
 
+#if (!defined(__clang__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 5))

Do you mean for gcc-3.4 to use __builtin_unreachable()? This might be clearer, correcter, and better match the prevailing compiler.h style, if it was switched round to handle the __builtin_unreachable() case first.

-- Keir

Quoted 8 lines
xen/include/xen/compiler.h +5 -0
+#define unreachable() do {} while (1)
+#else
+#define unreachable() __builtin_unreachable()
+#endif
+
 #ifdef __clang__
 /* Clang can replace some vars with new automatic ones that go in .data;
  * mark all explicit-segment vars 'used' to prevent that. */
author
#2 lore raw
Quoted 8 lines · Keir Fraser

xen/include/xen/compiler.h line 17

Quoted 5 lines · Tim Deegan
xen/include/xen/compiler.h +1 -0
@@ -14,6 +14,12 @@
 #define always_inline __inline__ __attribute__ ((always_inline))
 #define noinline      __attribute__((noinline))
 
+#if (!defined(__clang__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 5))

Do you mean for gcc-3.4 to use __builtin_unreachable()?

No, what I want is for clang and all GCCs >= 4.5 to use the builtin.

Quoted 3 lines

This might be
clearer, correcter, and better match the prevailing compiler.h style, if it was switched round to handle the __builtin_unreachable() case first.

Switched around, it looks like this:

#if (defined(__clang__) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ > 4)) #define unreachable() __builtin_unreachable()
#else
#define unreachable() do {} while (1)
#endif

Not sure that's any clearer or correcter, really.

Tim.

author
#3 lore raw
Quoted 11 lines · Tim Deegan
Quoted 8 lines · Keir Fraser

xen/include/xen/compiler.h line 17

Quoted 5 lines · Tim Deegan
xen/include/xen/compiler.h +1 -0
@@ -14,6 +14,12 @@
 #define always_inline __inline__ __attribute__ ((always_inline))
 #define noinline      __attribute__((noinline))
 
+#if (!defined(__clang__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 5))

Do you mean for gcc-3.4 to use __builtin_unreachable()?

No, what I want is for clang and all GCCs >= 4.5 to use the builtin.

Oh wait, I understand your question now: gcc 3.4 will already have failed the version check at the top of the file, so I don't need to check for it here.

Tim.

#4 lore raw
Quoted 11 lines · Tim Deegan
Quoted 10 lines · Tim Deegan
Quoted 8 lines · Keir Fraser

xen/include/xen/compiler.h line 17

Quoted 5 lines · Tim Deegan
xen/include/xen/compiler.h +1 -0
@@ -14,6 +14,12 @@
 #define always_inline __inline__ __attribute__ ((always_inline))
 #define noinline      __attribute__((noinline))
 
+#if (!defined(__clang__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 5))

Do you mean for gcc-3.4 to use __builtin_unreachable()?

Quoted 5 lines
Quoted 1 line

No, what I want is for clang and all GCCs >= 4.5 to use the builtin.

Oh wait, I understand your question now: gcc 3.4 will already have failed the version check at the top of the file, so I don't need to check for it here.

Argh, I was looking at a 4.1 branch. :)

Then my Ack applies to your original patch as is!

-- Keir

Quoted 1 line

Tim.

Acked-by
#5 lore raw
In reply to Tim Deegan
Quoted 5 lines · Tim Deegan
Quoted 3 lines
Quoted 1 line

+#if (!defined(__clang__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 5))

Do you mean for gcc-3.4 to use __builtin_unreachable()?

No, what I want is for clang and all GCCs >= 4.5 to use the builtin.

Then this original #if doesn't correctly handle __GNUC__ < 4.

Quoted 6 lines
Quoted 3 lines

This might be
clearer, correcter, and better match the prevailing compiler.h style, if it was switched round to handle the __builtin_unreachable() case first.

Switched around, it looks like this:

Quoted 8 lines

#if (defined(__clang__) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ > 4))
#define unreachable() __builtin_unreachable()
#else
#define unreachable() do {} while (1)
#endif

Not sure that's any clearer or correcter, really.

Well the GCC version check is correct in this one.

Looks good to me. Replace this hunk in the original patch and you can have my ack:

-- Keir

Reviewed-by on v1 1/1
#6 lore raw
In reply to Tim Deegan
Quoted 6 lines · Tim Deegan

This helps avoid static analysis false-positives, and might lead to better code density as the compiler knows it doesn't have to restore spilled state &c.

Out of interest, I tried looking at some numbers for this.

Using gcc 4.7.2 (Debian Wheezy), .text decreased by 656 bytes and .init.text decreased by 71 bytes. While those are expected, .rodata decreased by 1224 bytes. I am at a loss to explain the decrease in .rodata, but did double check my compiling, and find the decrease still present.

Either way, it looks to be a useful improvement.

author
#7 lore raw
Quoted 8 lines · Andrew Cooper
Quoted 6 lines · Tim Deegan

This helps avoid static analysis false-positives, and might lead to better code density as the compiler knows it doesn't have to restore spilled state &c.

Quoted 7 lines

Out of interest, I tried looking at some numbers for this.

Using gcc 4.7.2 (Debian Wheezy), .text decreased by 656 bytes and .init.text decreased by 71 bytes. While those are expected, .rodata decreased by 1224 bytes. I am at a loss to explain the decrease in .rodata, but did double check my compiling, and find the decrease still present.

.rodata contains the bug frames themselves -- it looks like 150 ASSERT()s are now unreachable. :) I'm sure that includes a lot of duplication from ASSERT()s in header files.

The remaining handful of bytes seem to be from GCC inlining one or two things it hadn't before, which makes the embedded copy of the symbol table a little smaller.

Tim.