From: Michael Jeanson Date: Thu, 7 Jun 2018 16:24:28 +0000 (-0400) Subject: Fix: BUILD_BUG_ON with compile time constant on < v2.6.38 X-Git-Tag: v2.11.0-rc1~42 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=20cac8305d09ebb92964aae8917098535d741354;p=lttng-modules.git Fix: BUILD_BUG_ON with compile time constant on < v2.6.38 See upstream commits : commit 8c87df457cb58fe75b9b893007917cf8095660a0 Author: Jan Beulich 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 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 Signed-off-by: Mathieu Desnoyers --- diff --git a/lib/bug.h b/lib/bug.h index ecd49286..3de40ca9 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 + +#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)