From 86bcc53084fb06351353add8881ff4b789ca08cd Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 28 Feb 2022 14:59:25 +0100 Subject: [PATCH] DEBUG: implement 4 levels of choices between warn and crash. We used to have DEBUG_STRICT_NOCRASH to disable crashes on BUG_ON(). Now we have other levels (WARN_ON(), CHECK_IF()) so we need something finer-grained. This patch introduces DEBUG_STRICT_ACTION which takes an integer value. 0 disables crashes and is the equivalent of DEBUG_STRICT_NOCRASH. 1 is the default and only enables crashes on BUG_ON(). 2 also enables crashes on WARN_ON(), and 3 also enables warnings on CHECK_IF(), and is suited to developers and CI. --- Makefile | 2 +- include/haproxy/bug.h | 35 ++++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 28de8a39d5..964b7fbb56 100644 --- a/Makefile +++ b/Makefile @@ -255,7 +255,7 @@ SMALL_OPTS = # not use them at all. Some even more obscure ones might also be available # without appearing here. Currently defined DEBUG macros include DEBUG_FULL, # DEBUG_MEM_STATS, DEBUG_DONT_SHARE_POOLS, DEBUG_FD, DEBUG_POOL_INTEGRITY, -# DEBUG_NO_POOLS, DEBUG_FAIL_ALLOC, DEBUG_STRICT_NOCRASH, DEBUG_HPACK, +# DEBUG_NO_POOLS, DEBUG_FAIL_ALLOC, DEBUG_STRICT_ACTION=[0-3], DEBUG_HPACK, # DEBUG_AUTH, DEBUG_SPOE, DEBUG_UAF, DEBUG_THREAD, DEBUG_STRICT, DEBUG_DEV, # DEBUG_TASK, DEBUG_MEMORY_POOLS, DEBUG_POOL_TRACING. DEBUG = -DDEBUG_STRICT -DDEBUG_MEMORY_POOLS diff --git a/include/haproxy/bug.h b/include/haproxy/bug.h index cf4e728853..6dcbbd6a7e 100644 --- a/include/haproxy/bug.h +++ b/include/haproxy/bug.h @@ -106,18 +106,39 @@ __bug_cond; /* let's return the condition */ \ }) -/* BUG_ON: complains if is true when DEBUG_STRICT or DEBUG_STRICT_NOCRASH - * are set, does nothing otherwise. With DEBUG_STRICT in addition it immediately - * crashes using ABORT_NOW() above. +/* DEBUG_STRICT enables/disables runtime checks on condition + * DEBUG_STRICT_ACTION indicates the level of verification on the rules when + * is true: + * + * macro BUG_ON() WARN_ON() CHECK_IF() + * value 0 warn warn warn + * 1 CRASH warn warn + * 2 CRASH CRASH warn + * 3 CRASH CRASH CRASH */ + #if defined(DEBUG_STRICT) -# define BUG_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 3, "FATAL: bug ", "") -# define WARN_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 0, "WARNING: warn ", " (please report to developers)") -# define CHECK_IF(cond) _BUG_ON_ONCE(cond, __FILE__, __LINE__, 0, "WARNING: check ", " (please report to developers)") -#elif defined(DEBUG_STRICT_NOCRASH) +# if defined(DEBUG_STRICT_ACTION) && (DEBUG_STRICT_ACTION < 1) +/* Lowest level: BUG_ON() warns, WARN_ON() warns, CHECK_IF() warns */ # define BUG_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 2, "WARNING: bug ", " (not crashing but process is untrusted now, please report to developers)") # define WARN_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 0, "WARNING: warn ", " (please report to developers)") # define CHECK_IF(cond) _BUG_ON_ONCE(cond, __FILE__, __LINE__, 0, "WARNING: check ", " (please report to developers)") +# elif !defined(DEBUG_STRICT_ACTION) || (DEBUG_STRICT_ACTION == 1) +/* default level: BUG_ON() crashes, WARN_ON() warns, CHECK_IF() warns */ +# define BUG_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 3, "FATAL: bug ", "") +# define WARN_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 0, "WARNING: warn ", " (please report to developers)") +# define CHECK_IF(cond) _BUG_ON_ONCE(cond, __FILE__, __LINE__, 0, "WARNING: check ", " (please report to developers)") +# elif DEBUG_STRICT_ACTION == 2 +/* Stricter level: BUG_ON() crashes, WARN_ON() crashes, CHECK_IF() warns */ +# define BUG_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 3, "FATAL: bug ", "") +# define WARN_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 1, "FATAL: warn ", "") +# define CHECK_IF(cond) _BUG_ON_ONCE(cond, __FILE__, __LINE__, 0, "WARNING: check ", " (please report to developers)") +# elif DEBUG_STRICT_ACTION >= 3 +/* Developer/CI level: BUG_ON() crashes, WARN_ON() crashes, CHECK_IF() crashes */ +# define BUG_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 3, "FATAL: bug ", "") +# define WARN_ON(cond) _BUG_ON (cond, __FILE__, __LINE__, 1, "FATAL: warn ", "") +# define CHECK_IF(cond) _BUG_ON_ONCE(cond, __FILE__, __LINE__, 1, "FATAL: check ", "") +# endif #else # define BUG_ON(cond) # define WARN_ON(cond)