aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Santos <daniel.santos@pobox.com>2013-02-21 19:41:52 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-02-21 20:22:16 -0500
commita3ccc497cd17147713363a4bf975f1a269fadb6d (patch)
tree6c5e14d9b9f8687edba0ee5a604277433d4db7d2
parent1d6a0d19c85587581a364850b77f30446810a560 (diff)
bug.h: make BUILD_BUG_ON generate compile-time error
Negative sized arrays wont create a compile-time error in some cases starting with gcc 4.4 (e.g., inlined functions), but gcc 4.3 introduced the error function attribute that will. This patch modifies BUILD_BUG_ON to behave like BUILD_BUG already does, using the error function attribute so that you don't have to build the entire kernel to discover that you have a problem, and then enjoy trying to track it down from a link-time error. Also, we are only including asm/bug.h and then expecting that linux/compiler.h will eventually be included to define __linktime_error (used in BUILD_BUG_ON). This patch includes it directly for clarity and to avoid the possibility of changes in <arch>/*/include/asm/bug.h being changed or not including linux/compiler.h for some reason. Signed-off-by: Daniel Santos <daniel.santos@pobox.com> Acked-by: Borislav Petkov <bp@alien8.de> Cc: Andi Kleen <ak@linux.intel.com> Cc: David Rientjes <rientjes@google.com> Cc: Joe Perches <joe@perches.com> Cc: Josh Triplett <josh@joshtriplett.org> Cc: Paul Gortmaker <paul.gortmaker@windriver.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--include/linux/bug.h32
1 files changed, 19 insertions, 13 deletions
diff --git a/include/linux/bug.h b/include/linux/bug.h
index 89fb91d0c929..73af37ca472c 100644
--- a/include/linux/bug.h
+++ b/include/linux/bug.h
@@ -2,6 +2,7 @@
2#define _LINUX_BUG_H 2#define _LINUX_BUG_H
3 3
4#include <asm/bug.h> 4#include <asm/bug.h>
5#include <linux/compiler.h>
5 6
6enum bug_trap_type { 7enum bug_trap_type {
7 BUG_TRAP_TYPE_NONE = 0, 8 BUG_TRAP_TYPE_NONE = 0,
@@ -43,25 +44,30 @@ struct pt_regs;
43 * @condition: the condition which the compiler should know is false. 44 * @condition: the condition which the compiler should know is false.
44 * 45 *
45 * If you have some code which relies on certain constants being equal, or 46 * If you have some code which relies on certain constants being equal, or
46 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to 47 * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
47 * detect if someone changes it. 48 * detect if someone changes it.
48 * 49 *
49 * The implementation uses gcc's reluctance to create a negative array, but 50 * The implementation uses gcc's reluctance to create a negative array, but gcc
50 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments 51 * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
51 * to inline functions). So as a fallback we use the optimizer; if it can't 52 * inline functions). Luckily, in 4.3 they added the "error" function
52 * prove the condition is false, it will cause a link error on the undefined 53 * attribute just for this type of case. Thus, we use a negative sized array
53 * "__build_bug_on_failed". This error message can be harder to track down 54 * (should always create an error on gcc versions older than 4.4) and then call
54 * though, hence the two different methods. 55 * an undefined function with the error attribute (should always create an
56 * error on gcc 4.3 and later). If for some reason, neither creates a
57 * compile-time error, we'll still have a link-time error, which is harder to
58 * track down.
55 */ 59 */
56#ifndef __OPTIMIZE__ 60#ifndef __OPTIMIZE__
57#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 61#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
58#else 62#else
59extern int __build_bug_on_failed; 63#define BUILD_BUG_ON(condition) \
60#define BUILD_BUG_ON(condition) \ 64 do { \
61 do { \ 65 bool __cond = !!(condition); \
62 bool __cond = !!(condition); \ 66 extern void __build_bug_on_failed(void) \
63 ((void)sizeof(char[1 - 2 * __cond])); \ 67 __compiletime_error("BUILD_BUG_ON failed"); \
64 if (__cond) __build_bug_on_failed = 1; \ 68 if (__cond) \
69 __build_bug_on_failed(); \
70 ((void)sizeof(char[1 - 2 * __cond])); \
65 } while (0) 71 } while (0)
66#endif 72#endif
67 73