2016-04-03 22:48:44 +02:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 21:13:17 +01:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2016-04-03 22:48:44 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file util_bug.h
|
|
|
|
**/
|
|
|
|
|
|
|
|
#ifndef TOR_UTIL_BUG_H
|
|
|
|
#define TOR_UTIL_BUG_H
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "testsupport.h"
|
|
|
|
|
|
|
|
/* Replace assert() with a variant that sends failures to the log before
|
|
|
|
* calling assert() normally.
|
|
|
|
*/
|
|
|
|
#ifdef NDEBUG
|
|
|
|
/* Nobody should ever want to build with NDEBUG set. 99% of our asserts will
|
|
|
|
* be outside the critical path anyway, so it's silly to disable bug-checking
|
|
|
|
* throughout the entire program just because a few asserts are slowing you
|
|
|
|
* down. Profile, optimize the critical path, and keep debugging on.
|
|
|
|
*
|
|
|
|
* And I'm not just saying that because some of our asserts check
|
|
|
|
* security-critical properties.
|
|
|
|
*/
|
|
|
|
#error "Sorry; we don't support building with NDEBUG."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Sometimes we don't want to use assertions during branch coverage tests; it
|
|
|
|
* leads to tons of unreached branches which in reality are only assertions we
|
|
|
|
* didn't hit. */
|
|
|
|
#if defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
|
|
|
|
#define tor_assert(a) STMT_BEGIN \
|
|
|
|
(void)(a); \
|
|
|
|
STMT_END
|
|
|
|
#else
|
|
|
|
/** Like assert(3), but send assertion failures to the log as well as to
|
|
|
|
* stderr. */
|
|
|
|
#define tor_assert(expr) STMT_BEGIN \
|
|
|
|
if (PREDICT_UNLIKELY(!(expr))) { \
|
|
|
|
tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr); \
|
|
|
|
abort(); \
|
|
|
|
} STMT_END
|
|
|
|
#endif
|
|
|
|
|
2016-04-05 15:40:51 +02:00
|
|
|
#define tor_assert_unreached() tor_assert(0)
|
|
|
|
|
|
|
|
/* Non-fatal bug assertions. The "unreached" variants mean "this line should
|
|
|
|
* never be reached." The "once" variants mean "Don't log a warning more than
|
|
|
|
* once".
|
2016-04-12 16:10:44 +02:00
|
|
|
*
|
|
|
|
* The 'BUG' macro checks a boolean condition and logs an error message if it
|
|
|
|
* is true. Example usage:
|
|
|
|
* if (BUG(x == NULL))
|
|
|
|
* return -1;
|
2016-04-05 15:40:51 +02:00
|
|
|
*/
|
|
|
|
|
2017-07-25 16:14:24 +02:00
|
|
|
#ifdef __COVERITY__
|
2017-07-28 16:02:38 +02:00
|
|
|
extern int bug_macro_deadcode_dummy__;
|
2017-07-25 16:14:24 +02:00
|
|
|
#undef BUG
|
|
|
|
// Coverity defines this in global headers; let's override it. This is a
|
|
|
|
// magic coverity-only preprocessor thing.
|
2017-07-28 16:02:38 +02:00
|
|
|
// We use this "deadcode_dummy__" trick to prevent coverity from
|
|
|
|
// complaining about unreachable bug cases.
|
|
|
|
#nodef BUG(x) ((x)?(__coverity_panic__(),1):(0+bug_macro_deadcode_dummy__))
|
2017-07-25 16:14:24 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__COVERITY__) || defined(__clang_analyzer__)
|
|
|
|
// We're running with a static analysis tool: let's treat even nonfatal
|
|
|
|
// assertion failures as something that we need to avoid.
|
|
|
|
#define ALL_BUGS_ARE_FATAL
|
|
|
|
#endif
|
|
|
|
|
2016-04-05 15:40:51 +02:00
|
|
|
#ifdef ALL_BUGS_ARE_FATAL
|
|
|
|
#define tor_assert_nonfatal_unreached() tor_assert(0)
|
|
|
|
#define tor_assert_nonfatal(cond) tor_assert((cond))
|
|
|
|
#define tor_assert_nonfatal_unreached_once() tor_assert(0)
|
|
|
|
#define tor_assert_nonfatal_once(cond) tor_assert((cond))
|
2016-04-12 16:10:44 +02:00
|
|
|
#define BUG(cond) \
|
2016-04-15 15:12:03 +02:00
|
|
|
(PREDICT_UNLIKELY(cond) ? \
|
2016-09-07 02:59:07 +02:00
|
|
|
(tor_assertion_failed_(SHORT_FILE__,__LINE__,__func__,"!("#cond")"), \
|
|
|
|
abort(), 1) \
|
2016-04-12 16:10:44 +02:00
|
|
|
: 0)
|
2016-04-05 15:40:51 +02:00
|
|
|
#elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
|
|
|
|
#define tor_assert_nonfatal_unreached() STMT_NIL
|
|
|
|
#define tor_assert_nonfatal(cond) ((void)(cond))
|
|
|
|
#define tor_assert_nonfatal_unreached_once() STMT_NIL
|
|
|
|
#define tor_assert_nonfatal_once(cond) ((void)(cond))
|
2016-04-15 15:12:03 +02:00
|
|
|
#define BUG(cond) (PREDICT_UNLIKELY(cond) ? 1 : 0)
|
2016-04-05 15:40:51 +02:00
|
|
|
#else /* Normal case, !ALL_BUGS_ARE_FATAL, !DISABLE_ASSERTS_IN_UNIT_TESTS */
|
|
|
|
#define tor_assert_nonfatal_unreached() STMT_BEGIN \
|
|
|
|
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
|
|
|
|
STMT_END
|
|
|
|
#define tor_assert_nonfatal(cond) STMT_BEGIN \
|
|
|
|
if (PREDICT_UNLIKELY(!(cond))) { \
|
|
|
|
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0); \
|
|
|
|
} \
|
|
|
|
STMT_END
|
|
|
|
#define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
|
|
|
|
static int warning_logged__ = 0; \
|
|
|
|
if (!warning_logged__) { \
|
|
|
|
warning_logged__ = 1; \
|
|
|
|
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1); \
|
|
|
|
} \
|
|
|
|
STMT_END
|
|
|
|
#define tor_assert_nonfatal_once(cond) STMT_BEGIN \
|
|
|
|
static int warning_logged__ = 0; \
|
|
|
|
if (!warning_logged__ && PREDICT_UNLIKELY(!(cond))) { \
|
|
|
|
warning_logged__ = 1; \
|
|
|
|
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
|
|
|
|
} \
|
|
|
|
STMT_END
|
2016-04-12 16:10:44 +02:00
|
|
|
#define BUG(cond) \
|
2016-04-15 15:12:03 +02:00
|
|
|
(PREDICT_UNLIKELY(cond) ? \
|
2016-09-07 02:59:07 +02:00
|
|
|
(tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,"!("#cond")",0), 1) \
|
2016-04-12 16:10:44 +02:00
|
|
|
: 0)
|
2016-04-05 15:40:51 +02:00
|
|
|
#endif
|
|
|
|
|
2016-04-12 16:35:16 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define IF_BUG_ONCE__(cond,var) \
|
2016-09-07 02:59:07 +02:00
|
|
|
if (( { \
|
2016-04-12 16:35:16 +02:00
|
|
|
static int var = 0; \
|
|
|
|
int bool_result = (cond); \
|
2016-04-15 15:12:03 +02:00
|
|
|
if (PREDICT_UNLIKELY(bool_result) && !var) { \
|
2016-04-12 16:35:16 +02:00
|
|
|
var = 1; \
|
2016-09-07 02:59:07 +02:00
|
|
|
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
|
|
|
|
"!("#cond")", 1); \
|
2016-04-12 16:35:16 +02:00
|
|
|
} \
|
2016-06-09 13:50:25 +02:00
|
|
|
PREDICT_UNLIKELY(bool_result); } ))
|
2016-04-12 16:35:16 +02:00
|
|
|
#else
|
|
|
|
#define IF_BUG_ONCE__(cond,var) \
|
|
|
|
static int var = 0; \
|
2016-09-14 16:53:49 +02:00
|
|
|
if (PREDICT_UNLIKELY(cond) ? \
|
2016-04-12 16:35:16 +02:00
|
|
|
(var ? 1 : \
|
|
|
|
(var=1, \
|
2016-09-07 02:59:07 +02:00
|
|
|
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
|
|
|
|
"!("#cond")", 1), \
|
2016-04-12 16:35:16 +02:00
|
|
|
1)) \
|
|
|
|
: 0)
|
|
|
|
#endif
|
|
|
|
#define IF_BUG_ONCE_VARNAME_(a) \
|
|
|
|
warning_logged_on_ ## a ## __
|
|
|
|
#define IF_BUG_ONCE_VARNAME__(a) \
|
|
|
|
IF_BUG_ONCE_VARNAME_(a)
|
|
|
|
|
|
|
|
/** This macro behaves as 'if (bug(x))', except that it only logs its
|
|
|
|
* warning once, no matter how many times it triggers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define IF_BUG_ONCE(cond) \
|
|
|
|
IF_BUG_ONCE__((cond), \
|
|
|
|
IF_BUG_ONCE_VARNAME__(__LINE__))
|
|
|
|
|
2016-04-03 22:48:44 +02:00
|
|
|
/** Define this if you want Tor to crash when any problem comes up,
|
|
|
|
* so you can get a coredump and track things down. */
|
2016-04-05 15:40:51 +02:00
|
|
|
// #define tor_fragile_assert() tor_assert_unreached(0)
|
|
|
|
#define tor_fragile_assert() tor_assert_nonfatal_unreached_once()
|
2016-04-03 22:48:44 +02:00
|
|
|
|
|
|
|
void tor_assertion_failed_(const char *fname, unsigned int line,
|
|
|
|
const char *func, const char *expr);
|
2016-04-05 15:40:51 +02:00
|
|
|
void tor_bug_occurred_(const char *fname, unsigned int line,
|
|
|
|
const char *func, const char *expr,
|
|
|
|
int once);
|
2016-04-03 22:48:44 +02:00
|
|
|
|
2016-09-07 03:01:17 +02:00
|
|
|
#ifdef TOR_UNIT_TESTS
|
|
|
|
void tor_capture_bugs_(int n);
|
|
|
|
void tor_end_capture_bugs_(void);
|
|
|
|
const struct smartlist_t *tor_get_captured_bug_log_(void);
|
2016-09-08 19:27:30 +02:00
|
|
|
void tor_set_failed_assertion_callback(void (*fn)(void));
|
2016-09-07 03:01:17 +02:00
|
|
|
#endif
|
|
|
|
|
2016-04-03 22:48:44 +02:00
|
|
|
#endif
|
|
|
|
|