Unverified Commit aee966cb authored by teor's avatar teor
Browse files

Merge remote-tracking branch 'tor-github/pr/1277' into maint-0.3.5

parents 62cf12a8 f311d067
o Minor bugfixes (error handling):
- Report the tor version whenever an assertion fails. Previously, we only
reported the Tor version on some crashes, and some non-fatal assertions.
Fixes bug 31571; bugfix on 0.3.5.1-alpha.
- On abort, try harder to flush the output buffers of log messages. On
some platforms (macOS), log messages can be discarded when the process
terminates. Fixes bug 31571; bugfix on 0.3.5.1-alpha.
...@@ -68,10 +68,10 @@ ...@@ -68,10 +68,10 @@
// Redundant with util.h, but doing it here so we can avoid that dependency. // Redundant with util.h, but doing it here so we can avoid that dependency.
#define raw_free free #define raw_free free
#ifdef USE_BACKTRACE
/** Version of Tor to report in backtrace messages. */ /** Version of Tor to report in backtrace messages. */
static char bt_version[128] = ""; static char bt_version[128] = "";
#ifdef USE_BACKTRACE
/** Largest stack depth to try to dump. */ /** Largest stack depth to try to dump. */
#define MAX_DEPTH 256 #define MAX_DEPTH 256
/** Static allocation of stack to dump. This is static so we avoid stack /** Static allocation of stack to dump. This is static so we avoid stack
...@@ -127,7 +127,7 @@ log_backtrace_impl(int severity, int domain, const char *msg, ...@@ -127,7 +127,7 @@ log_backtrace_impl(int severity, int domain, const char *msg,
depth = backtrace(cb_buf, MAX_DEPTH); depth = backtrace(cb_buf, MAX_DEPTH);
symbols = backtrace_symbols(cb_buf, (int)depth); symbols = backtrace_symbols(cb_buf, (int)depth);
logger(severity, domain, "%s. Stack trace:", msg); logger(severity, domain, "%s: %s. Stack trace:", bt_version, msg);
if (!symbols) { if (!symbols) {
/* LCOV_EXCL_START -- we can't provoke this. */ /* LCOV_EXCL_START -- we can't provoke this. */
logger(severity, domain, " Unable to generate backtrace."); logger(severity, domain, " Unable to generate backtrace.");
...@@ -193,15 +193,12 @@ dump_stack_symbols_to_error_fds(void) ...@@ -193,15 +193,12 @@ dump_stack_symbols_to_error_fds(void)
/** Install signal handlers as needed so that when we crash, we produce a /** Install signal handlers as needed so that when we crash, we produce a
* useful stack trace. Return 0 on success, -errno on failure. */ * useful stack trace. Return 0 on success, -errno on failure. */
static int static int
install_bt_handler(const char *software) install_bt_handler(void)
{ {
int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS, int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS,
SIGIO, -1 }; SIGIO, -1 };
int i, rv=0; int i, rv=0;
strncpy(bt_version, software, sizeof(bt_version) - 1);
bt_version[sizeof(bt_version) - 1] = 0;
struct sigaction sa; struct sigaction sa;
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
...@@ -243,13 +240,13 @@ void ...@@ -243,13 +240,13 @@ void
log_backtrace_impl(int severity, int domain, const char *msg, log_backtrace_impl(int severity, int domain, const char *msg,
tor_log_fn logger) tor_log_fn logger)
{ {
logger(severity, domain, "%s. (Stack trace not available)", msg); logger(severity, domain, "%s: %s. (Stack trace not available)",
bt_version, msg);
} }
static int static int
install_bt_handler(const char *software) install_bt_handler(void)
{ {
(void) software;
return 0; return 0;
} }
...@@ -264,6 +261,14 @@ dump_stack_symbols_to_error_fds(void) ...@@ -264,6 +261,14 @@ dump_stack_symbols_to_error_fds(void)
} }
#endif /* defined(NO_BACKTRACE_IMPL) */ #endif /* defined(NO_BACKTRACE_IMPL) */
/** Return the tor version used for error messages on crashes.
* Signal-safe: returns a pointer to a static array. */
const char *
get_tor_backtrace_version(void)
{
return bt_version;
}
/** Set up code to handle generating error messages on crashes. */ /** Set up code to handle generating error messages on crashes. */
int int
configure_backtrace_handler(const char *tor_version) configure_backtrace_handler(const char *tor_version)
...@@ -271,10 +276,25 @@ configure_backtrace_handler(const char *tor_version) ...@@ -271,10 +276,25 @@ configure_backtrace_handler(const char *tor_version)
char version[128] = "Tor\0"; char version[128] = "Tor\0";
if (tor_version) { if (tor_version) {
snprintf(version, sizeof(version), "Tor %s", tor_version); int snp_rv = 0;
/* We can't use strlcat() here, because it is defined in
* string/compat_string.h on some platforms, and string uses torerr. */
snp_rv = snprintf(version, sizeof(version), "Tor %s", tor_version);
/* It's safe to call raw_assert() here, because raw_assert() does not
* call configure_backtrace_handler(). */
raw_assert(snp_rv < (int)sizeof(version));
raw_assert(snp_rv >= 0);
} }
return install_bt_handler(version); char *str_rv = NULL;
/* We can't use strlcpy() here, see the note about strlcat() above. */
str_rv = strncpy(bt_version, version, sizeof(bt_version) - 1);
/* We must terminate bt_version, then raw_assert(), because raw_assert()
* uses bt_version. */
bt_version[sizeof(bt_version) - 1] = 0;
raw_assert(str_rv == bt_version);
return install_bt_handler();
} }
/** Perform end-of-process cleanup for code that generates error messages on /** Perform end-of-process cleanup for code that generates error messages on
......
...@@ -21,6 +21,7 @@ void log_backtrace_impl(int severity, int domain, const char *msg, ...@@ -21,6 +21,7 @@ void log_backtrace_impl(int severity, int domain, const char *msg,
int configure_backtrace_handler(const char *tor_version); int configure_backtrace_handler(const char *tor_version);
void clean_up_backtrace_handler(void); void clean_up_backtrace_handler(void);
void dump_stack_symbols_to_error_fds(void); void dump_stack_symbols_to_error_fds(void);
const char *get_tor_backtrace_version(void);
#define log_backtrace(sev, dom, msg) \ #define log_backtrace(sev, dom, msg) \
log_backtrace_impl((sev), (dom), (msg), tor_log) log_backtrace_impl((sev), (dom), (msg), tor_log)
......
...@@ -144,14 +144,21 @@ tor_raw_assertion_failed_msg_(const char *file, int line, const char *expr, ...@@ -144,14 +144,21 @@ tor_raw_assertion_failed_msg_(const char *file, int line, const char *expr,
{ {
char linebuf[16]; char linebuf[16];
format_dec_number_sigsafe(line, linebuf, sizeof(linebuf)); format_dec_number_sigsafe(line, linebuf, sizeof(linebuf));
tor_log_err_sigsafe("INTERNAL ERROR: Raw assertion failed at ", tor_log_err_sigsafe("INTERNAL ERROR: Raw assertion failed in ",
file, ":", linebuf, ": ", expr, NULL); get_tor_backtrace_version(), " at ",
file, ":", linebuf, ": ", expr, "\n", NULL);
if (msg) { if (msg) {
tor_log_err_sigsafe_write(msg); tor_log_err_sigsafe_write(msg);
tor_log_err_sigsafe_write("\n"); tor_log_err_sigsafe_write("\n");
} }
dump_stack_symbols_to_error_fds(); dump_stack_symbols_to_error_fds();
/* Some platforms (macOS, maybe others?) can swallow the last write before an
* abort. This issue is probably caused by a race condition between write
* buffer cache flushing, and process termination. So we write an extra
* newline, to make sure that the message always gets through. */
tor_log_err_sigsafe_write("\n");
} }
/* As format_{hex,dex}_number_sigsafe, but takes a <b>radix</b> argument /* As format_{hex,dex}_number_sigsafe, but takes a <b>radix</b> argument
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment