Skip to contentSkip to conversation

xen: let ASSERT_UNREACHABLE() WARN() in non-debug builds

PATCH In review Juergen Gross 5 participants 8 messages Aug 24, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: let ASSERT_UNREACHABLE() WARN() in non-debug builds

Hitting an ASSERT_UNREACHABLE() is always wrong, so even in production builds a warning seems to be appropriate when hitting one.

In order not to flood the console in reproducible cases, introduce WARN_ONCE() to be used in this case.

---
Notice for the release manager: this patch isn't really urgent for the 4.17 release, OTOH it is adding probably useful debug information for the unlikely case of hitting an ASSERT_UNREACHABLE(). The risk for taking the patch should be rather low, but you have the last saying, of course.

---
 xen/include/xen/lib.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
xen/include/xen/lib.h +11 -1
index 05ee1e18af..b8472d753c 100644
@@ -40,6 +40,16 @@
     unlikely(ret_warn_on_);             \
 })
 
+#define WARN_ONCE() do {                \
+    static bool warned = false;         \
+                                        \
+    if ( !warned )                      \
+    {                                   \
+        warned = true;                  \
+        WARN();                         \
+    }                                   \
+} while (0)
+
 /* All clang versions supported by Xen have _Static_assert. */
 #if defined(__clang__) || \
     (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
@@ -63,7 +73,7 @@
 #define ASSERT_UNREACHABLE() assert_failed("unreachable")
 #else
 #define ASSERT(p) do { if ( 0 && (p) ) {} } while (0)
-#define ASSERT_UNREACHABLE() do { } while (0)
+#define ASSERT_UNREACHABLE() WARN_ONCE()
 #endif
 
 #define ABS(_x) ({                              \
Signature · 1 line
2.35.3
Acked-by on v1 1/1
#1 lore raw

Hi Juergen,

Thanks for sending the patch quickly :).

Quoted 7 lines · Juergen Gross

Hitting an ASSERT_UNREACHABLE() is always wrong, so even in production builds a warning seems to be appropriate when hitting one.

In order not to flood the console in reproducible cases, introduce WARN_ONCE() to be used in this case.

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
In reply to Juergen Gross
Quoted 2 lines · Juergen Gross

Hitting an ASSERT_UNREACHABLE() is always wrong, so even in production builds a warning seems to be appropriate when hitting one.

I disagree, for two reasons: This violates the implication of NDEBUG meaning ASSERT() and friends expand to no actual code. Plus if doing so for ASSERT_UNREACHABLE(), why would we not do the same for ASSERT()? There's a reason we have ASSERT() and friends and, independently, WARN_ON() / BUG_ON() et al.

Jan

#3 lore raw

Hi,

Quoted 11 lines · Jan Beulich
Quoted 2 lines · Juergen Gross

Hitting an ASSERT_UNREACHABLE() is always wrong, so even in production builds a warning seems to be appropriate when hitting one.

I disagree, for two reasons: This violates the implication of NDEBUG meaning ASSERT() and friends expand to no actual code. Plus if doing so for ASSERT_UNREACHABLE(), why would we not do the same for ASSERT()? There's a reason we have ASSERT() and friends and, independently, WARN_ON() / BUG_ON() et al.

I agree with Jan here, this is introducing code in ASSERT which is not the intention and will end up with dead code in production mode.

In NDEBUG those should appear.

If something is needed or we think there could be a situation where this is reachable, then the code should be modified to use something else then ASSERT[_UNREACHABLE]().

Bertrand

Quoted 3 lines

Jan

#4 lore raw
In reply to Jan Beulich
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Juergen Gross

Hitting an ASSERT_UNREACHABLE() is always wrong, so even in production builds a warning seems to be appropriate when hitting one.

I disagree, for two reasons: This violates the implication of NDEBUG meaning ASSERT() and friends expand to no actual code. Plus if doing so

This is something we can change IMHO.

Quoted 1 line

for ASSERT_UNREACHABLE(), why would we not do the same for ASSERT()?

There are multiple reasons to have ASSERT()s. Some serve as a kind of documentation (e.g. to document that the programmer thought of a special case not being possible), or they are meant to catch hard to diagnose bugs rather early instead of letting them hit later in a situation where it wouldn't be clear what caused them. Adding a WARN() for all of these cases isn't really appropriate, especially as this might impact performance due to added tests, which isn't the case for theoretically unreachable code.

Quoted 2 lines

There's a reason we have ASSERT() and friends and, independently, WARN_ON() / BUG_ON() et al.

We might want to introduce something like ASSERT_OR_WARN(). I'm sure this could be useful in some cases.

Juergen

#5 lore raw
Quoted 21 lines · Juergen Gross
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Juergen Gross

Hitting an ASSERT_UNREACHABLE() is always wrong, so even in production builds a warning seems to be appropriate when hitting one.

I disagree, for two reasons: This violates the implication of NDEBUG meaning ASSERT() and friends expand to no actual code. Plus if doing so

This is something we can change IMHO.

Quoted 1 line

for ASSERT_UNREACHABLE(), why would we not do the same for ASSERT()?

There are multiple reasons to have ASSERT()s. Some serve as a kind of documentation (e.g. to document that the programmer thought of a special case not being possible), or they are meant to catch hard to diagnose bugs rather early instead of letting them hit later in a situation where it wouldn't be clear what caused them. Adding a WARN() for all of these cases isn't really appropriate, especially as this might impact performance due to added tests, which isn't the case for theoretically unreachable code.

Quoted 5 lines
Quoted 2 lines

There's a reason we have ASSERT() and friends and, independently, WARN_ON() / BUG_ON() et al.

We might want to introduce something like ASSERT_OR_WARN(). I'm sure this could be useful in some cases.

I'm curious why in such cases it can't just be WARN_ON().

Jan

#6 lore raw
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Juergen Gross
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Juergen Gross

Hitting an ASSERT_UNREACHABLE() is always wrong, so even in production builds a warning seems to be appropriate when hitting one.

I disagree, for two reasons: This violates the implication of NDEBUG meaning ASSERT() and friends expand to no actual code. Plus if doing so

This is something we can change IMHO.

Quoted 1 line

for ASSERT_UNREACHABLE(), why would we not do the same for ASSERT()?

There are multiple reasons to have ASSERT()s. Some serve as a kind of documentation (e.g. to document that the programmer thought of a special case not being possible), or they are meant to catch hard to diagnose bugs rather early instead of letting them hit later in a situation where it wouldn't be clear what caused them. Adding a WARN() for all of these cases isn't really appropriate, especially as this might impact performance due to added tests, which isn't the case for theoretically unreachable code.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

There's a reason we have ASSERT() and friends and, independently, WARN_ON() / BUG_ON() et al.

We might want to introduce something like ASSERT_OR_WARN(). I'm sure this could be useful in some cases.

I'm curious why in such cases it can't just be WARN_ON().

It won't result in test failure of debug builds.

In the end I'm not feeling really strong here, so I'm fine to drop this patch.

Juergen

#7 lore raw
In reply to Jan Beulich
Quoted 5 lines · Jan Beulich
Quoted 2 lines · Juergen Gross

Hitting an ASSERT_UNREACHABLE() is always wrong, so even in production builds a warning seems to be appropriate when hitting one.

I disagree, for two reasons: This violates the implication of NDEBUG meaning ASSERT() and friends expand to no actual code.

I agree.  ASSERT() and friends should no code in !DEBUG builds.

Furthermore, if an ASSERT_UNREACHALBE() is proving to be problematic even at runtime, then it's not the correct construct in the first place.

~Andrew