xref: /linux/include/linux/bug.h (revision 3e9b3112ec74f192eaab976c3889e34255cae940)
17664c5a1SJeremy Fitzhardinge #ifndef _LINUX_BUG_H
27664c5a1SJeremy Fitzhardinge #define _LINUX_BUG_H
37664c5a1SJeremy Fitzhardinge 
47664c5a1SJeremy Fitzhardinge #include <asm/bug.h>
5a3ccc497SDaniel Santos #include <linux/compiler.h>
67664c5a1SJeremy Fitzhardinge 
77664c5a1SJeremy Fitzhardinge enum bug_trap_type {
87664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_NONE = 0,
97664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_WARN = 1,
107664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_BUG = 2,
117664c5a1SJeremy Fitzhardinge };
127664c5a1SJeremy Fitzhardinge 
13608e2619SHeiko Carstens struct pt_regs;
14608e2619SHeiko Carstens 
1535edd910SPaul Gortmaker #ifdef __CHECKER__
16*3e9b3112SJakub Kicinski #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
17ca623c91SDaniel Santos #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
1835edd910SPaul Gortmaker #define BUILD_BUG_ON_ZERO(e) (0)
1935edd910SPaul Gortmaker #define BUILD_BUG_ON_NULL(e) ((void*)0)
20c5782e9fSTushar Behera #define BUILD_BUG_ON_INVALID(e) (0)
219a8ab1c3SDaniel Santos #define BUILD_BUG_ON_MSG(cond, msg) (0)
22ca623c91SDaniel Santos #define BUILD_BUG_ON(condition) (0)
2335edd910SPaul Gortmaker #define BUILD_BUG() (0)
24ff20c2e0SKirill A. Shutemov #define MAYBE_BUILD_BUG_ON(cond) (0)
2535edd910SPaul Gortmaker #else /* __CHECKER__ */
2635edd910SPaul Gortmaker 
2735edd910SPaul Gortmaker /* Force a compilation error if a constant expression is not a power of 2 */
28*3e9b3112SJakub Kicinski #define __BUILD_BUG_ON_NOT_POWER_OF_2(n)	\
29*3e9b3112SJakub Kicinski 	BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
3035edd910SPaul Gortmaker #define BUILD_BUG_ON_NOT_POWER_OF_2(n)			\
3135edd910SPaul Gortmaker 	BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
3235edd910SPaul Gortmaker 
3335edd910SPaul Gortmaker /* Force a compilation error if condition is true, but also produce a
3435edd910SPaul Gortmaker    result (of value 0 and type size_t), so the expression can be used
3535edd910SPaul Gortmaker    e.g. in a structure initializer (or where-ever else comma expressions
3635edd910SPaul Gortmaker    aren't permitted). */
3735edd910SPaul Gortmaker #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
3835edd910SPaul Gortmaker #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
3935edd910SPaul Gortmaker 
40baf05aa9SKonstantin Khlebnikov /*
41baf05aa9SKonstantin Khlebnikov  * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
42baf05aa9SKonstantin Khlebnikov  * expression but avoids the generation of any code, even if that expression
43baf05aa9SKonstantin Khlebnikov  * has side-effects.
44baf05aa9SKonstantin Khlebnikov  */
45baf05aa9SKonstantin Khlebnikov #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
46baf05aa9SKonstantin Khlebnikov 
4735edd910SPaul Gortmaker /**
489a8ab1c3SDaniel Santos  * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
499a8ab1c3SDaniel Santos  *		      error message.
509a8ab1c3SDaniel Santos  * @condition: the condition which the compiler should know is false.
519a8ab1c3SDaniel Santos  *
529a8ab1c3SDaniel Santos  * See BUILD_BUG_ON for description.
539a8ab1c3SDaniel Santos  */
549a8ab1c3SDaniel Santos #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
559a8ab1c3SDaniel Santos 
569a8ab1c3SDaniel Santos /**
5735edd910SPaul Gortmaker  * BUILD_BUG_ON - break compile if a condition is true.
5835edd910SPaul Gortmaker  * @condition: the condition which the compiler should know is false.
5935edd910SPaul Gortmaker  *
6035edd910SPaul Gortmaker  * If you have some code which relies on certain constants being equal, or
61a3ccc497SDaniel Santos  * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
6235edd910SPaul Gortmaker  * detect if someone changes it.
6335edd910SPaul Gortmaker  *
64a3ccc497SDaniel Santos  * The implementation uses gcc's reluctance to create a negative array, but gcc
65a3ccc497SDaniel Santos  * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
66a3ccc497SDaniel Santos  * inline functions).  Luckily, in 4.3 they added the "error" function
67a3ccc497SDaniel Santos  * attribute just for this type of case.  Thus, we use a negative sized array
68a3ccc497SDaniel Santos  * (should always create an error on gcc versions older than 4.4) and then call
69a3ccc497SDaniel Santos  * an undefined function with the error attribute (should always create an
70a3ccc497SDaniel Santos  * error on gcc 4.3 and later).  If for some reason, neither creates a
71a3ccc497SDaniel Santos  * compile-time error, we'll still have a link-time error, which is harder to
72a3ccc497SDaniel Santos  * track down.
7335edd910SPaul Gortmaker  */
7435edd910SPaul Gortmaker #ifndef __OPTIMIZE__
7535edd910SPaul Gortmaker #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
7635edd910SPaul Gortmaker #else
7735edd910SPaul Gortmaker #define BUILD_BUG_ON(condition) \
789a8ab1c3SDaniel Santos 	BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
7935edd910SPaul Gortmaker #endif
8035edd910SPaul Gortmaker 
8135edd910SPaul Gortmaker /**
8235edd910SPaul Gortmaker  * BUILD_BUG - break compile if used.
8335edd910SPaul Gortmaker  *
8435edd910SPaul Gortmaker  * If you have some code that you expect the compiler to eliminate at
8535edd910SPaul Gortmaker  * build time, you should use BUILD_BUG to detect if it is
8635edd910SPaul Gortmaker  * unexpectedly used.
8735edd910SPaul Gortmaker  */
889a8ab1c3SDaniel Santos #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
8935edd910SPaul Gortmaker 
90ff20c2e0SKirill A. Shutemov #define MAYBE_BUILD_BUG_ON(cond)			\
91ff20c2e0SKirill A. Shutemov 	do {						\
92ff20c2e0SKirill A. Shutemov 		if (__builtin_constant_p((cond)))       \
93ff20c2e0SKirill A. Shutemov 			BUILD_BUG_ON(cond);             \
94ff20c2e0SKirill A. Shutemov 		else                                    \
95ff20c2e0SKirill A. Shutemov 			BUG_ON(cond);                   \
96ff20c2e0SKirill A. Shutemov 	} while (0)
97ff20c2e0SKirill A. Shutemov 
9835edd910SPaul Gortmaker #endif	/* __CHECKER__ */
9935edd910SPaul Gortmaker 
1007664c5a1SJeremy Fitzhardinge #ifdef CONFIG_GENERIC_BUG
1017664c5a1SJeremy Fitzhardinge #include <asm-generic/bug.h>
1027664c5a1SJeremy Fitzhardinge 
1037664c5a1SJeremy Fitzhardinge static inline int is_warning_bug(const struct bug_entry *bug)
1047664c5a1SJeremy Fitzhardinge {
1057664c5a1SJeremy Fitzhardinge 	return bug->flags & BUGFLAG_WARNING;
1067664c5a1SJeremy Fitzhardinge }
1077664c5a1SJeremy Fitzhardinge 
1087664c5a1SJeremy Fitzhardinge const struct bug_entry *find_bug(unsigned long bugaddr);
1097664c5a1SJeremy Fitzhardinge 
110608e2619SHeiko Carstens enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
1117664c5a1SJeremy Fitzhardinge 
1127664c5a1SJeremy Fitzhardinge /* These are defined by the architecture */
1137664c5a1SJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr);
1147664c5a1SJeremy Fitzhardinge 
1157664c5a1SJeremy Fitzhardinge #else	/* !CONFIG_GENERIC_BUG */
1167664c5a1SJeremy Fitzhardinge 
117608e2619SHeiko Carstens static inline enum bug_trap_type report_bug(unsigned long bug_addr,
118608e2619SHeiko Carstens 					    struct pt_regs *regs)
1197664c5a1SJeremy Fitzhardinge {
1207664c5a1SJeremy Fitzhardinge 	return BUG_TRAP_TYPE_BUG;
1217664c5a1SJeremy Fitzhardinge }
1227664c5a1SJeremy Fitzhardinge 
1237664c5a1SJeremy Fitzhardinge #endif	/* CONFIG_GENERIC_BUG */
1247664c5a1SJeremy Fitzhardinge #endif	/* _LINUX_BUG_H */
125