bug.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef _LINUX_BUG_H
  2. #define _LINUX_BUG_H
  3. #include <asm/bug.h>
  4. #include <linux/compiler.h>
  5. enum bug_trap_type {
  6. BUG_TRAP_TYPE_NONE = 0,
  7. BUG_TRAP_TYPE_WARN = 1,
  8. BUG_TRAP_TYPE_BUG = 2,
  9. };
  10. struct pt_regs;
  11. #ifdef __CHECKER__
  12. #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
  13. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
  14. #define BUILD_BUG_ON_ZERO(e) (0)
  15. #define BUILD_BUG_ON_NULL(e) ((void *)0)
  16. #define BUILD_BUG_ON_INVALID(e) (0)
  17. #define BUILD_BUG_ON_MSG(cond, msg) (0)
  18. #define BUILD_BUG_ON(condition) (0)
  19. #define BUILD_BUG() (0)
  20. #define MAYBE_BUILD_BUG_ON(cond) (0)
  21. #else /* __CHECKER__ */
  22. /* Force a compilation error if a constant expression is not a power of 2 */
  23. #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  24. BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
  25. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  26. BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
  27. /*
  28. * Force a compilation error if condition is true, but also produce a
  29. * result (of value 0 and type size_t), so the expression can be used
  30. * e.g. in a structure initializer (or where-ever else comma expressions
  31. * aren't permitted).
  32. */
  33. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  34. #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
  35. /*
  36. * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
  37. * expression but avoids the generation of any code, even if that expression
  38. * has side-effects.
  39. */
  40. #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
  41. /**
  42. * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
  43. * error message.
  44. * @condition: the condition which the compiler should know is false.
  45. *
  46. * See BUILD_BUG_ON for description.
  47. */
  48. #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
  49. /**
  50. * BUILD_BUG_ON - break compile if a condition is true.
  51. * @condition: the condition which the compiler should know is false.
  52. *
  53. * If you have some code which relies on certain constants being equal, or
  54. * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
  55. * detect if someone changes it.
  56. *
  57. * The implementation uses gcc's reluctance to create a negative array, but gcc
  58. * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
  59. * inline functions). Luckily, in 4.3 they added the "error" function
  60. * attribute just for this type of case. Thus, we use a negative sized array
  61. * (should always create an error on gcc versions older than 4.4) and then call
  62. * an undefined function with the error attribute (should always create an
  63. * error on gcc 4.3 and later). If for some reason, neither creates a
  64. * compile-time error, we'll still have a link-time error, which is harder to
  65. * track down.
  66. */
  67. #ifndef __OPTIMIZE__
  68. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  69. #else
  70. #define BUILD_BUG_ON(condition) \
  71. BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
  72. #endif
  73. /**
  74. * BUILD_BUG - break compile if used.
  75. *
  76. * If you have some code that you expect the compiler to eliminate at
  77. * build time, you should use BUILD_BUG to detect if it is
  78. * unexpectedly used.
  79. */
  80. #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
  81. #define MAYBE_BUILD_BUG_ON(cond) \
  82. do { \
  83. if (__builtin_constant_p((cond))) \
  84. BUILD_BUG_ON(cond); \
  85. else \
  86. BUG_ON(cond); \
  87. } while (0)
  88. #endif /* __CHECKER__ */
  89. #ifdef CONFIG_GENERIC_BUG
  90. #include <asm-generic/bug.h>
  91. static inline int is_warning_bug(const struct bug_entry *bug)
  92. {
  93. return bug->flags & BUGFLAG_WARNING;
  94. }
  95. struct bug_entry *find_bug(unsigned long bugaddr);
  96. enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
  97. /* These are defined by the architecture */
  98. int is_valid_bugaddr(unsigned long addr);
  99. #else /* !CONFIG_GENERIC_BUG */
  100. static inline enum bug_trap_type report_bug(unsigned long bug_addr,
  101. struct pt_regs *regs)
  102. {
  103. return BUG_TRAP_TYPE_BUG;
  104. }
  105. #endif /* CONFIG_GENERIC_BUG */
  106. /*
  107. * Since detected data corruption should stop operation on the affected
  108. * structures. Return value must be checked and sanely acted on by caller.
  109. */
  110. static inline __must_check bool check_data_corruption(bool v) { return v; }
  111. #define CHECK_DATA_CORRUPTION(condition, fmt, ...) \
  112. check_data_corruption(({ \
  113. bool corruption = unlikely(condition); \
  114. if (corruption) { \
  115. if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
  116. pr_err(fmt, ##__VA_ARGS__); \
  117. BUG(); \
  118. } else \
  119. WARN(1, fmt, ##__VA_ARGS__); \
  120. } \
  121. corruption; \
  122. }))
  123. #endif /* _LINUX_BUG_H */