bug.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_ZERO(e) (0)
  14. #define BUILD_BUG_ON_NULL(e) ((void*)0)
  15. #define BUILD_BUG_ON_INVALID(e) (0)
  16. #define BUILD_BUG_ON(condition) (0)
  17. #define BUILD_BUG() (0)
  18. #else /* __CHECKER__ */
  19. /* Force a compilation error if a constant expression is not a power of 2 */
  20. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  21. BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
  22. /* Force a compilation error if condition is true, but also produce a
  23. result (of value 0 and type size_t), so the expression can be used
  24. e.g. in a structure initializer (or where-ever else comma expressions
  25. aren't permitted). */
  26. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  27. #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
  28. /*
  29. * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
  30. * expression but avoids the generation of any code, even if that expression
  31. * has side-effects.
  32. */
  33. #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
  34. /**
  35. * BUILD_BUG_ON - break compile if a condition is true.
  36. * @condition: the condition which the compiler should know is false.
  37. *
  38. * If you have some code which relies on certain constants being equal, or
  39. * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
  40. * detect if someone changes it.
  41. *
  42. * The implementation uses gcc's reluctance to create a negative array, but gcc
  43. * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
  44. * inline functions). Luckily, in 4.3 they added the "error" function
  45. * attribute just for this type of case. Thus, we use a negative sized array
  46. * (should always create an error on gcc versions older than 4.4) and then call
  47. * an undefined function with the error attribute (should always create an
  48. * error on gcc 4.3 and later). If for some reason, neither creates a
  49. * compile-time error, we'll still have a link-time error, which is harder to
  50. * track down.
  51. */
  52. #ifndef __OPTIMIZE__
  53. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  54. #else
  55. #define BUILD_BUG_ON(condition) \
  56. do { \
  57. bool __cond = !!(condition); \
  58. extern void __build_bug_on_failed(void) \
  59. __compiletime_error("BUILD_BUG_ON failed"); \
  60. if (__cond) \
  61. __build_bug_on_failed(); \
  62. __compiletime_error_fallback(__cond); \
  63. } while (0)
  64. #endif
  65. /**
  66. * BUILD_BUG - break compile if used.
  67. *
  68. * If you have some code that you expect the compiler to eliminate at
  69. * build time, you should use BUILD_BUG to detect if it is
  70. * unexpectedly used.
  71. */
  72. #define BUILD_BUG() \
  73. do { \
  74. extern void __build_bug_failed(void) \
  75. __compiletime_error("BUILD_BUG failed");\
  76. __build_bug_failed(); \
  77. } while (0)
  78. #endif /* __CHECKER__ */
  79. #ifdef CONFIG_GENERIC_BUG
  80. #include <asm-generic/bug.h>
  81. static inline int is_warning_bug(const struct bug_entry *bug)
  82. {
  83. return bug->flags & BUGFLAG_WARNING;
  84. }
  85. const struct bug_entry *find_bug(unsigned long bugaddr);
  86. enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
  87. /* These are defined by the architecture */
  88. int is_valid_bugaddr(unsigned long addr);
  89. #else /* !CONFIG_GENERIC_BUG */
  90. static inline enum bug_trap_type report_bug(unsigned long bug_addr,
  91. struct pt_regs *regs)
  92. {
  93. return BUG_TRAP_TYPE_BUG;
  94. }
  95. #endif /* CONFIG_GENERIC_BUG */
  96. #endif /* _LINUX_BUG_H */