Skip to contentSkip to conversation

Fix: BUILD_BUG_ON with compile time constant on < v2.6.38

PATCH Mathieu Desnoyers 1 participant 1 message Jun 7, 2018 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

merged thanks!

Mathieu

Quoted 83 lines · ----- On Jun 7, 2018, at 12:24 PM, Michael Jeanson [email protected] wrote:

See upstream commits :

commit 8c87df457cb58fe75b9b893007917cf8095660a0
Author: Jan Beulich <[email protected]>
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 <[email protected]>
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.

---
lib/bug.h | 10 +++++++++-

1 file changed, 9 insertions(+), 1 deletion(-)
lib/bug.h +9 -2
index ecd4928..3de40ca 100644
@@ -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

Signature · 7 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev