tor  0.4.1.0-alpha-dev
Macros | Functions
util_bug.h File Reference
#include "orconfig.h"
#include "lib/cc/compat_compiler.h"
#include "lib/log/log.h"
#include "lib/testsupport/testsupport.h"

Go to the source code of this file.

Macros

#define ASSERT_PREDICT_UNLIKELY_(e)   PREDICT_UNLIKELY(e)
 
#define ASSERT_PREDICT_LIKELY_(e)   PREDICT_LIKELY(e)
 
#define tor_assert(expr)
 
#define tor_assert_unreached()
 
#define tor_assert_nonfatal_unreached()
 
#define tor_assert_nonfatal(cond)
 
#define tor_assert_nonfatal_unreached_once()
 
#define tor_assert_nonfatal_once(cond)
 
#define BUG(cond)
 
#define IF_BUG_ONCE__(cond, var)
 
#define IF_BUG_ONCE_VARNAME_(a)   warning_logged_on_ ## a ## __
 
#define IF_BUG_ONCE_VARNAME__(a)   IF_BUG_ONCE_VARNAME_(a)
 
#define IF_BUG_ONCE(cond)
 
#define tor_fragile_assert()   tor_assert_nonfatal_unreached_once()
 
#define SHORT_FILE__   (__FILE__)
 
#define tor_fix_source_file(s)   (s)
 

Functions

void tor_assertion_failed_ (const char *fname, unsigned int line, const char *func, const char *expr)
 
void tor_bug_occurred_ (const char *fname, unsigned int line, const char *func, const char *expr, int once)
 

Detailed Description

Macros to manage assertions, fatal and non-fatal.

Guidelines: All the different kinds of assertion in this file are for bug-checking only. Don't write code that can assert based on bad inputs.

We provide two kinds of assertion here: "fatal" and "nonfatal". Use nonfatal assertions for any bug you can reasonably recover from – and please, try to recover! Many severe bugs in Tor have been caused by using a regular assertion when a nonfatal assertion would have been better.

If you need to check a condition with a nonfatal assertion, AND recover from that same condition, consider using the BUG() macro inside a conditional. For example:

// wrong – use tor_assert_nonfatal() if you just want an assertion. BUG(ptr == NULL);

// okay, but needlessly verbose tor_assert_nonfatal(ptr != NULL); if (ptr == NULL) { ... }

// this is how we do it: if (BUG(ptr == NULL)) { ... }

Definition in file util_bug.h.

Macro Definition Documentation

◆ BUG

#define BUG (   cond)
Value:
(ASSERT_PREDICT_UNLIKELY_(cond) ? \
(tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,"!("#cond")",0), 1) \
: 0)
void tor_bug_occurred_(const char *fname, unsigned int line, const char *func, const char *expr, int once)
Definition: util_bug.c:86

Definition at line 177 of file util_bug.h.

◆ IF_BUG_ONCE

#define IF_BUG_ONCE (   cond)
Value:
IF_BUG_ONCE__(ASSERT_PREDICT_UNLIKELY_(cond), \
IF_BUG_ONCE_VARNAME__(__LINE__))

This macro behaves as 'if (bug(x))', except that it only logs its warning once, no matter how many times it triggers.

Definition at line 214 of file util_bug.h.

Referenced by geoip_add_entry(), MOCK_IMPL(), and scheduler_channel_wants_writes().

◆ IF_BUG_ONCE__

#define IF_BUG_ONCE__ (   cond,
  var 
)
Value:
static int var = 0; \
if ((cond) ? \
(var ? 1 : \
(var=1, \
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
"!("#cond")", 1), \
1)) \
: 0)
void tor_bug_occurred_(const char *fname, unsigned int line, const char *func, const char *expr, int once)
Definition: util_bug.c:86

Definition at line 195 of file util_bug.h.

◆ tor_assert

#define tor_assert (   expr)
Value:
STMT_BEGIN \
if (ASSERT_PREDICT_LIKELY_(expr)) { \
} else { \
tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr); \
abort(); \
} STMT_END

Like assert(3), but send assertion failures to the log as well as to stderr.

Definition at line 98 of file util_bug.h.

◆ tor_assert_nonfatal

#define tor_assert_nonfatal (   cond)
Value:
STMT_BEGIN \
if (ASSERT_PREDICT_LIKELY_(cond)) { \
} else { \
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 0); \
} \
STMT_END

Definition at line 156 of file util_bug.h.

◆ tor_assert_nonfatal_once

#define tor_assert_nonfatal_once (   cond)
Value:
STMT_BEGIN \
static int warning_logged__ = 0; \
if (ASSERT_PREDICT_LIKELY_(cond)) { \
} else if (!warning_logged__) { \
warning_logged__ = 1; \
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
} \
STMT_END

Definition at line 169 of file util_bug.h.

◆ tor_assert_nonfatal_unreached

#define tor_assert_nonfatal_unreached ( )
Value:
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
STMT_END
void tor_bug_occurred_(const char *fname, unsigned int line, const char *func, const char *expr, int once)
Definition: util_bug.c:86

Definition at line 153 of file util_bug.h.

◆ tor_assert_nonfatal_unreached_once

#define tor_assert_nonfatal_unreached_once ( )
Value:
STMT_BEGIN \
static int warning_logged__ = 0; \
if (!warning_logged__) { \
warning_logged__ = 1; \
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1); \
} \
STMT_END

Definition at line 162 of file util_bug.h.

◆ tor_assert_unreached

#define tor_assert_unreached ( )
Value:
STMT_BEGIN { \
tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, \
"line should be unreached"); \
abort(); \
} STMT_END

Definition at line 106 of file util_bug.h.

◆ tor_fragile_assert

#define tor_fragile_assert ( )    tor_assert_nonfatal_unreached_once()

Define this if you want Tor to crash when any problem comes up, so you can get a coredump and track things down.

Definition at line 221 of file util_bug.h.

Referenced by circpad_send_padding_callback(), circuit_purpose_to_controller_hs_state_string(), connection_handle_read_impl(), node_exit_policy_is_exact(), tor_addr_copy_tight(), tor_addr_hash(), tor_addr_is_loopback(), and tor_addr_keyed_hash().

Function Documentation

◆ tor_assertion_failed_()

void tor_assertion_failed_ ( const char *  fname,
unsigned int  line,
const char *  func,
const char *  expr 
)

Helper for tor_assert: report the assertion failure.

Definition at line 72 of file util_bug.c.

◆ tor_bug_occurred_()

void tor_bug_occurred_ ( const char *  fname,
unsigned int  line,
const char *  func,
const char *  expr,
int  once 
)

Helper for tor_assert_nonfatal: report the assertion failure.

Definition at line 86 of file util_bug.c.