]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEBUG: add BUG_ON_STRESS(): a BUG_ON() implemented only when DEBUG_STRESS > 0
authorWilly Tarreau <w@1wt.eu>
Fri, 14 Nov 2025 15:25:27 +0000 (16:25 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 14 Nov 2025 15:42:53 +0000 (16:42 +0100)
The purpose of this new BUG_ON is beyond BUG_ON_HOT(). While BUG_ON_HOT()
is meant to be light but placed on very hot code paths, BUG_ON_STRESS()
might be heavy and only used under stress-testing, to try to detect early
that something bad is starting to happen. This one is not even type-checked
when not defined because we don't want to risk the compiler emitting the
slightest piece of code there in production mode, so as to give enough
freedom to the developers.

include/haproxy/bug.h

index 8b110711f85b375fdea4f8e12f996c4f1c82ac0e..f3ec08dfbe62a44b6e01e6b8ae8c16af4b1194e4 100644 (file)
@@ -424,6 +424,20 @@ extern __attribute__((__weak__)) struct debug_count __stop_dbg_cnt  HA_SECTION_S
 #  define COUNT_IF_HOT(cond, ...) DISGUISE(cond)
 #endif
 
+/* turn BUG_ON_STRESS() into a real statement when DEBUG_STRESS is set,
+ * otherwise simply ignore it, at the risk of failing to notice if the
+ * condition would build at all. We don't really care if BUG_ON_STRESS
+ * doesn't always build, because it's meant to be used only in certain
+ * scenarios, possibly requiring certain combinations of options. We
+ * just want to be certain that the condition is not implemented at all
+ * when not used, so as to encourage developers to put a lot of them at
+ * zero cost.
+ */
+#if DEBUG_STRESS > 0
+# define BUG_ON_STRESS(cond, ...)  BUG_ON(cond, __VA_ARGS__)
+#else
+# define BUG_ON_STRESS(cond, ...)  do { } while (0)
+#endif
 
 /* When not optimizing, clang won't remove that code, so only compile it in when optimizing */
 #if defined(__GNUC__) && defined(__OPTIMIZE__)