blob: 483207cb99fb82b839346aebef4fce8f05969bfa [file] [log] [blame]
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08001#ifndef _LINUX_BUG_H
2#define _LINUX_BUG_H
3
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08004#include <asm/bug.h>
Daniel Santosa3ccc492013-02-21 16:41:52 -08005#include <linux/compiler.h>
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08006
7enum bug_trap_type {
8 BUG_TRAP_TYPE_NONE = 0,
9 BUG_TRAP_TYPE_WARN = 1,
10 BUG_TRAP_TYPE_BUG = 2,
11};
12
Heiko Carstens608e2612007-07-15 23:41:39 -070013struct pt_regs;
14
Paul Gortmaker35edd912011-11-16 23:51:05 -050015#ifdef __CHECKER__
Jakub Kicinski3e9b3112016-08-31 12:46:44 +010016#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
Daniel Santosca623c92013-02-21 16:41:44 -080017#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050018#define BUILD_BUG_ON_ZERO(e) (0)
Ian Abbott8cdd7cc2017-07-10 15:51:01 -070019#define BUILD_BUG_ON_NULL(e) ((void *)0)
Tushar Beherac5782e92012-11-26 16:29:38 -080020#define BUILD_BUG_ON_INVALID(e) (0)
Daniel Santos9a8ab1c2013-02-21 16:41:55 -080021#define BUILD_BUG_ON_MSG(cond, msg) (0)
Daniel Santosca623c92013-02-21 16:41:44 -080022#define BUILD_BUG_ON(condition) (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050023#define BUILD_BUG() (0)
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053024#define MAYBE_BUILD_BUG_ON(cond) (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050025#else /* __CHECKER__ */
26
27/* Force a compilation error if a constant expression is not a power of 2 */
Jakub Kicinski3e9b3112016-08-31 12:46:44 +010028#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
29 BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050030#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
31 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
32
Ian Abbotte9d5a482017-07-10 15:50:58 -070033/*
34 * Force a compilation error if condition is true, but also produce a
35 * result (of value 0 and type size_t), so the expression can be used
36 * e.g. in a structure initializer (or where-ever else comma expressions
37 * aren't permitted).
38 */
Ian Abbott47e81e52017-07-10 15:51:04 -070039#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); }))
40#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); }))
Paul Gortmaker35edd912011-11-16 23:51:05 -050041
Konstantin Khlebnikovbaf05aa2012-05-29 15:06:27 -070042/*
43 * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
44 * expression but avoids the generation of any code, even if that expression
45 * has side-effects.
46 */
47#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
48
Paul Gortmaker35edd912011-11-16 23:51:05 -050049/**
Daniel Santos9a8ab1c2013-02-21 16:41:55 -080050 * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
51 * error message.
52 * @condition: the condition which the compiler should know is false.
53 *
54 * See BUILD_BUG_ON for description.
55 */
56#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
57
58/**
Paul Gortmaker35edd912011-11-16 23:51:05 -050059 * BUILD_BUG_ON - break compile if a condition is true.
60 * @condition: the condition which the compiler should know is false.
61 *
62 * If you have some code which relies on certain constants being equal, or
Daniel Santosa3ccc492013-02-21 16:41:52 -080063 * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
Paul Gortmaker35edd912011-11-16 23:51:05 -050064 * detect if someone changes it.
65 *
Daniel Santosa3ccc492013-02-21 16:41:52 -080066 * The implementation uses gcc's reluctance to create a negative array, but gcc
67 * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
68 * inline functions). Luckily, in 4.3 they added the "error" function
69 * attribute just for this type of case. Thus, we use a negative sized array
70 * (should always create an error on gcc versions older than 4.4) and then call
71 * an undefined function with the error attribute (should always create an
72 * error on gcc 4.3 and later). If for some reason, neither creates a
73 * compile-time error, we'll still have a link-time error, which is harder to
74 * track down.
Paul Gortmaker35edd912011-11-16 23:51:05 -050075 */
76#ifndef __OPTIMIZE__
77#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
78#else
Daniel Santos9a8ab1c2013-02-21 16:41:55 -080079#define BUILD_BUG_ON(condition) \
80 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
Paul Gortmaker35edd912011-11-16 23:51:05 -050081#endif
82
83/**
84 * BUILD_BUG - break compile if used.
85 *
86 * If you have some code that you expect the compiler to eliminate at
87 * build time, you should use BUILD_BUG to detect if it is
88 * unexpectedly used.
89 */
Daniel Santos9a8ab1c2013-02-21 16:41:55 -080090#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
Paul Gortmaker35edd912011-11-16 23:51:05 -050091
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053092#define MAYBE_BUILD_BUG_ON(cond) \
93 do { \
94 if (__builtin_constant_p((cond))) \
95 BUILD_BUG_ON(cond); \
96 else \
97 BUG_ON(cond); \
98 } while (0)
99
Paul Gortmaker35edd912011-11-16 23:51:05 -0500100#endif /* __CHECKER__ */
101
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800102#ifdef CONFIG_GENERIC_BUG
103#include <asm-generic/bug.h>
104
105static inline int is_warning_bug(const struct bug_entry *bug)
106{
107 return bug->flags & BUGFLAG_WARNING;
108}
109
Peter Zijlstra19d43622017-02-25 08:56:53 +0100110struct bug_entry *find_bug(unsigned long bugaddr);
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800111
Heiko Carstens608e2612007-07-15 23:41:39 -0700112enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800113
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800114/* These are defined by the architecture */
115int is_valid_bugaddr(unsigned long addr);
116
117#else /* !CONFIG_GENERIC_BUG */
118
Heiko Carstens608e2612007-07-15 23:41:39 -0700119static inline enum bug_trap_type report_bug(unsigned long bug_addr,
120 struct pt_regs *regs)
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800121{
122 return BUG_TRAP_TYPE_BUG;
123}
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800124
125#endif /* CONFIG_GENERIC_BUG */
Kees Cookde54ebbe2016-08-17 14:42:11 -0700126
127/*
128 * Since detected data corruption should stop operation on the affected
Kees Cook85caa952017-02-24 15:00:38 -0800129 * structures. Return value must be checked and sanely acted on by caller.
Kees Cookde54ebbe2016-08-17 14:42:11 -0700130 */
Kees Cook85caa952017-02-24 15:00:38 -0800131static inline __must_check bool check_data_corruption(bool v) { return v; }
Kees Cookde54ebbe2016-08-17 14:42:11 -0700132#define CHECK_DATA_CORRUPTION(condition, fmt, ...) \
Kees Cook85caa952017-02-24 15:00:38 -0800133 check_data_corruption(({ \
134 bool corruption = unlikely(condition); \
135 if (corruption) { \
Kees Cookde54ebbe2016-08-17 14:42:11 -0700136 if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
137 pr_err(fmt, ##__VA_ARGS__); \
138 BUG(); \
139 } else \
140 WARN(1, fmt, ##__VA_ARGS__); \
Kees Cookde54ebbe2016-08-17 14:42:11 -0700141 } \
Kees Cook85caa952017-02-24 15:00:38 -0800142 corruption; \
143 }))
Kees Cookde54ebbe2016-08-17 14:42:11 -0700144
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800145#endif /* _LINUX_BUG_H */