Fix: BUILD_BUG_ON with compile time constant on < v2.6.38
authorMichael Jeanson <mjeanson@efficios.com>
Thu, 7 Jun 2018 16:24:28 +0000 (12:24 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 7 Jun 2018 16:31:15 +0000 (12:31 -0400)
See upstream commits :

  commit 8c87df457cb58fe75b9b893007917cf8095660a0
  Author: Jan Beulich <JBeulich@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@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@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
lib/bug.h

index ecd492866a92c15668f416ca47c06ad8db7cd9aa..3de40ca906cc56ab7954395ad363ba5a0e1c4e25 100644 (file)
--- a/lib/bug.h
+++ b/lib/bug.h
  * 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)
This page took 0.026349 seconds and 4 git commands to generate.