util_bug.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, 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. #ifdef ALL_BUGS_ARE_FATAL
  49. #define tor_assert_nonfatal_unreached() tor_assert(0)
  50. #define tor_assert_nonfatal(cond) tor_assert((cond))
  51. #define tor_assert_nonfatal_unreached_once() tor_assert(0)
  52. #define tor_assert_nonfatal_once(cond) tor_assert((cond))
  53. #elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
  54. #define tor_assert_nonfatal_unreached() STMT_NIL
  55. #define tor_assert_nonfatal(cond) ((void)(cond))
  56. #define tor_assert_nonfatal_unreached_once() STMT_NIL
  57. #define tor_assert_nonfatal_once(cond) ((void)(cond))
  58. #else /* Normal case, !ALL_BUGS_ARE_FATAL, !DISABLE_ASSERTS_IN_UNIT_TESTS */
  59. #define tor_assert_nonfatal_unreached() STMT_BEGIN \
  60. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
  61. STMT_END
  62. #define tor_assert_nonfatal(cond) STMT_BEGIN \
  63. if (PREDICT_UNLIKELY(!(cond))) { \
  64. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0); \
  65. } \
  66. STMT_END
  67. #define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
  68. static int warning_logged__ = 0; \
  69. if (!warning_logged__) { \
  70. warning_logged__ = 1; \
  71. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1); \
  72. } \
  73. STMT_END
  74. #define tor_assert_nonfatal_once(cond) STMT_BEGIN \
  75. static int warning_logged__ = 0; \
  76. if (!warning_logged__ && PREDICT_UNLIKELY(!(cond))) { \
  77. warning_logged__ = 1; \
  78. tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
  79. } \
  80. STMT_END
  81. #endif
  82. /** Define this if you want Tor to crash when any problem comes up,
  83. * so you can get a coredump and track things down. */
  84. // #define tor_fragile_assert() tor_assert_unreached(0)
  85. #define tor_fragile_assert() tor_assert_nonfatal_unreached_once()
  86. void tor_assertion_failed_(const char *fname, unsigned int line,
  87. const char *func, const char *expr);
  88. void tor_bug_occurred_(const char *fname, unsigned int line,
  89. const char *func, const char *expr,
  90. int once);
  91. #endif