[lttng-dev] [PATCH lttng-modules] Fix: BUILD_BUG_ON with compile time constant on < v2.6.38

Michael Jeanson mjeanson at efficios.com
Thu Jun 7 12:24:28 EDT 2018


See upstream commits :

  commit 8c87df457cb58fe75b9b893007917cf8095660a0
  Author: Jan Beulich <JBeulich at novell.com>
  Date:   Tue Sep 22 16:43:52 2009 -0700

    BUILD_BUG_ON(): fix it and a couple of bogus uses of it

    gcc permitting variable length arrays makes the current construct used for
    BUILD_BUG_ON() useless, as that doesn't produce any diagnostic if the
    controlling expression isn't really constant.  Instead, this patch makes
    it so that a bit field gets used here.  Consequently, those uses where the
    condition isn't really constant now also need fixing.

    Note that in the gfp.h, kmemcheck.h, and virtio_config.h cases
    MAYBE_BUILD_BUG_ON() really just serves documentation purposes - even if
    the expression is compile time constant (__builtin_constant_p() yields
    true), the array is still deemed of variable length by gcc, and hence the
    whole expression doesn't have the intended effect.

  commit 7ef88ad561457c0346355dfd1f53e503ddfde719
  Author: Rusty Russell <rusty at rustcorp.com.au>
  Date:   Mon Jan 24 14:45:10 2011 -0600

    BUILD_BUG_ON: make it handle more cases

    BUILD_BUG_ON used to use the optimizer to do code elimination or fail
    at link time; it was changed to first the size of a negative array (a
    nicer compile time error), then (in
    8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.

    This forced us to change some non-constant cases to MAYBE_BUILD_BUG_ON();
    as Jan points out in that commit, it didn't work as intended anyway.

    bitfields: needs a literal constant at parse time, and can't be put under
            "if (__builtin_constant_p(x))" for example.
    negative array: can handle anything, but if the compiler can't tell it's
            a constant, silently has no effect.
    link time: breaks link if the compiler can't determine the value, but the
            linker output is not usually as informative as a compiler error.

    If we use the negative-array-size method *and* the link time trick,
    we get the ability to use BUILD_BUG_ON() under __builtin_constant_p()
    branches, and maximal ability for the compiler to detect errors at
    build time.

    We also document it thoroughly.

Signed-off-by: Michael Jeanson <mjeanson at efficios.com>
---
 lib/bug.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/bug.h b/lib/bug.h
index ecd4928..3de40ca 100644
--- a/lib/bug.h
+++ b/lib/bug.h
@@ -21,6 +21,14 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <linux/version.h>
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38))
+#define LTTNG_BUILD_BUG_ON(cond) BUILD_BUG_ON(cond)
+#else
+#define LTTNG_BUILD_BUG_ON(cond) MAYBE_BUILD_BUG_ON(cond)
+#endif
+
 /**
  * BUILD_RUNTIME_BUG_ON - check condition at build (if constant) or runtime
  * @condition: the condition which should be false.
@@ -33,7 +41,7 @@
 #define BUILD_RUNTIME_BUG_ON(condition)				\
 	do {							\
 		if (__builtin_constant_p(condition))		\
-			BUILD_BUG_ON(condition);		\
+			LTTNG_BUILD_BUG_ON(condition);		\
 		else						\
 			BUG_ON(condition);			\
 	} while (0)
-- 
2.17.1



More information about the lttng-dev mailing list