util_bug.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util_bug.h
  7. **/
  8. #ifndef TOR_UTIL_BUG_H
  9. #define TOR_UTIL_BUG_H
  10. #include "orconfig.h"
  11. #include "compat.h"
  12. #include "testsupport.h"
  13. /* Replace assert() with a variant that sends failures to the log before
  14. * calling assert() normally.
  15. */
  16. #ifdef NDEBUG
  17. /* Nobody should ever want to build with NDEBUG set. 99% of our asserts will
  18. * be outside the critical path anyway, so it's silly to disable bug-checking
  19. * throughout the entire program just because a few asserts are slowing you
  20. * down. Profile, optimize the critical path, and keep debugging on.
  21. *
  22. * And I'm not just saying that because some of our asserts check
  23. * security-critical properties.
  24. */
  25. #error "Sorry; we don't support building with NDEBUG."
  26. #endif
  27. /* Sometimes we don't want to use assertions during branch coverage tests; it
  28. * leads to tons of unreached branches which in reality are only assertions we
  29. * didn't hit. */
  30. #if defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  31. #define tor_assert(a) STMT_BEGIN \
  32. (void)(a); \
  33. STMT_END
  34. #else
  35. /** Like assert(3), but send assertion failures to the log as well as to
  36. * stderr. */
  37. #define tor_assert(expr) STMT_BEGIN \
  38. if (PREDICT_UNLIKELY(!(expr))) { \
  39. tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr); \
  40. abort(); \
  41. } STMT_END
  42. #endif
  43. #define tor_assert_unreached() tor_assert(0)
  44. /* Non-fatal bug assertions. The "unreached" variants mean "this line should
  45. * never be reached." The "once" variants mean "Don't log a warning more than
  46. * once".
  47. *
  48. * The 'BUG' macro checks a boolean condition and logs an error message if it
  49. * is true. Example usage:
  50. * if (BUG(x == NULL))
  51. * return -1;
  52. */
  53. #ifdef ALL_BUGS_ARE_FATAL
  54. #define tor_assert_nonfatal_unreached() tor_assert(0)
  55. #define tor_assert_nonfatal(cond) tor_assert((cond))
  56. #define tor_assert_nonfatal_unreached_once() tor_assert(0)
  57. #define tor_assert_nonfatal_once(cond) tor_assert((cond))
  58. #define BUG(cond) \
  59. (PREDICT_UNLIKELY(cond) ? \
  60. (tor_assertion_failed_(SHORT_FILE__,__LINE__,__func__,"!("#cond")"), \
  61. abort(), 1) \
  62. : 0)
  63. #elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  64. #define tor_assert_nonfatal_unreached() STMT_NIL
  65. #define tor_assert_nonfatal(cond) ((void)(cond))
  66. #define tor_assert_nonfatal_unreached_once() STMT_NIL
  67. #define tor_assert_nonfatal_once(cond) ((void)(cond))
  68. #define BUG(cond) (PREDICT_UNLIKELY(cond) ? 1 : 0)
  69. #else /* Normal case, !ALL_BUGS_ARE_FATAL, !DISABLE_ASSERTS_IN_UNIT_TESTS */
  70. #define tor_assert_nonfatal_unreached() STMT_BEGIN \
  71. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
  72. STMT_END
  73. #define tor_assert_nonfatal(cond) STMT_BEGIN \
  74. if (PREDICT_UNLIKELY(!(cond))) { \
  75. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0); \
  76. } \
  77. STMT_END
  78. #define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
  79. static int warning_logged__ = 0; \
  80. if (!warning_logged__) { \
  81. warning_logged__ = 1; \
  82. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1); \
  83. } \
  84. STMT_END
  85. #define tor_assert_nonfatal_once(cond) STMT_BEGIN \
  86. static int warning_logged__ = 0; \
  87. if (!warning_logged__ && PREDICT_UNLIKELY(!(cond))) { \
  88. warning_logged__ = 1; \
  89. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
  90. } \
  91. STMT_END
  92. #define BUG(cond) \
  93. (PREDICT_UNLIKELY(cond) ? \
  94. (tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,"!("#cond")",0), 1) \
  95. : 0)
  96. #endif
  97. #ifdef __GNUC__
  98. #define IF_BUG_ONCE__(cond,var) \
  99. if (( { \
  100. static int var = 0; \
  101. int bool_result = (cond); \
  102. if (PREDICT_UNLIKELY(bool_result) && !var) { \
  103. var = 1; \
  104. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  105. "!("#cond")", 1); \
  106. } \
  107. PREDICT_UNLIKELY(bool_result); } ))
  108. #else
  109. #define IF_BUG_ONCE__(cond,var) \
  110. static int var = 0; \
  111. if (PREDICT_UNLIKELY(cond) ? \
  112. (var ? 1 : \
  113. (var=1, \
  114. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
  115. "!("#cond")", 1), \
  116. 1)) \
  117. : 0)
  118. #endif
  119. #define IF_BUG_ONCE_VARNAME_(a) \
  120. warning_logged_on_ ## a ## __
  121. #define IF_BUG_ONCE_VARNAME__(a) \
  122. IF_BUG_ONCE_VARNAME_(a)
  123. /** This macro behaves as 'if (bug(x))', except that it only logs its
  124. * warning once, no matter how many times it triggers.
  125. */
  126. #define IF_BUG_ONCE(cond) \
  127. IF_BUG_ONCE__((cond), \
  128. IF_BUG_ONCE_VARNAME__(__LINE__))
  129. /** Define this if you want Tor to crash when any problem comes up,
  130. * so you can get a coredump and track things down. */
  131. // #define tor_fragile_assert() tor_assert_unreached(0)
  132. #define tor_fragile_assert() tor_assert_nonfatal_unreached_once()
  133. void tor_assertion_failed_(const char *fname, unsigned int line,
  134. const char *func, const char *expr);
  135. void tor_bug_occurred_(const char *fname, unsigned int line,
  136. const char *func, const char *expr,
  137. int once);
  138. #ifdef TOR_UNIT_TESTS
  139. void tor_capture_bugs_(int n);
  140. void tor_end_capture_bugs_(void);
  141. const struct smartlist_t *tor_get_captured_bug_log_(void);
  142. void tor_set_failed_assertion_callback(void (*fn)(void));
  143. #endif
  144. #endif