aboutsummaryrefslogtreecommitdiff
path: root/src/lib/err/backtrace.c
blob: afb6b9503f7ef5b91729250eedd40e81bdd56413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* Copyright (c) 2013-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file backtrace.c
 *
 * \brief Functions to produce backtraces on bugs, crashes, or assertion
 * failures.
 *
 * Currently, we've only got an implementation here using the backtrace()
 * family of functions, which are sometimes provided by libc and sometimes
 * provided by libexecinfo.  We tie into the sigaction() backend in order to
 * detect crashes.
 *
 * This is one of the lowest-level modules, since nearly everything needs to
 * be able to log an error.  As such, it doesn't call the log module or any
 * other higher-level modules directly.
 */

#include "orconfig.h"
#include "lib/err/torerr.h"

#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#ifdef HAVE_CYGWIN_SIGNAL_H
#include <cygwin/signal.h>
#elif defined(HAVE_SYS_UCONTEXT_H)
#include <sys/ucontext.h>
#elif defined(HAVE_UCONTEXT_H)
#include <ucontext.h>
#endif /* defined(HAVE_CYGWIN_SIGNAL_H) || ... */

#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif

#include "lib/cc/ctassert.h"

#define BACKTRACE_PRIVATE
#include "lib/err/backtrace.h"

#if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
  defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION) && \
  defined(HAVE_PTHREAD_H)
#define USE_BACKTRACE
#endif

#if !defined(USE_BACKTRACE)
#define NO_BACKTRACE_IMPL
#endif

// Redundant with util.h, but doing it here so we can avoid that dependency.
#define raw_free free

/** Version of Tor to report in backtrace messages. */
static char bt_version[128] = "";

#ifdef USE_BACKTRACE

/** Largest stack depth to try to dump. */
#define MAX_DEPTH 256
/** The size of the callback buffer, so we can clear it in unlock_cb_buf(). */
#define SIZEOF_CB_BUF (MAX_DEPTH * sizeof(void *))
/** Protects cb_buf from concurrent access. Pthreads, since this code
 * is Unix-only, and since this code needs to be lowest-level. */
static pthread_mutex_t cb_buf_mutex = PTHREAD_MUTEX_INITIALIZER;

/** Lock and return a static stack pointer buffer that can hold up to
 *  MAX_DEPTH function pointers. */
static void **
lock_cb_buf(void)
{
  /* Lock the mutex first, before even declaring the buffer. */
  pthread_mutex_lock(&cb_buf_mutex);

  /** Static allocation of stack to dump. This is static so we avoid stack
   * pressure. */
  static void *cb_buf[MAX_DEPTH];
  CTASSERT(SIZEOF_CB_BUF == sizeof(cb_buf));
  memset(cb_buf, 0, SIZEOF_CB_BUF);

  return cb_buf;
}

/** Unlock the static stack pointer buffer. */
static void
unlock_cb_buf(void **cb_buf)
{
  memset(cb_buf, 0, SIZEOF_CB_BUF);
  pthread_mutex_unlock(&cb_buf_mutex);
}

/** Change a stacktrace in <b>stack</b> of depth <b>depth</b> so that it will
 * log the correct function from which a signal was received with context
 * <b>ctx</b>.  (When we get a signal, the current function will not have
 * called any other function, and will therefore have not pushed its address
 * onto the stack.  Fortunately, we usually have the program counter in the
 * ucontext_t structure.
 */
void
clean_backtrace(void **stack, size_t depth, const ucontext_t *ctx)
{
#ifdef PC_FROM_UCONTEXT
#if defined(__linux__)
  const size_t n = 1;
#elif defined(__darwin__) || defined(__APPLE__) || defined(OpenBSD) \
  || defined(__FreeBSD__)
  const size_t n = 2;
#else
  const size_t n = 1;
#endif /* defined(__linux__) || ... */
  if (depth <= n)
    return;

  stack[n] = (void*) ctx->PC_FROM_UCONTEXT;
#else /* !defined(PC_FROM_UCONTEXT) */
  (void) depth;
  (void) ctx;
  (void) stack;
#endif /* defined(PC_FROM_UCONTEXT) */
}

/** Log a message <b>msg</b> at <b>severity</b> in <b>domain</b>, and follow
 * that with a backtrace log.  Send messages via the tor_log function at
 * logger". */
void
log_backtrace_impl(int severity, log_domain_mask_t domain, const char *msg,
                   tor_log_fn logger)
{
  size_t depth;
  char **symbols;
  size_t i;

  void **cb_buf = lock_cb_buf();

  depth = backtrace(cb_buf, MAX_DEPTH);
  symbols = backtrace_symbols(cb_buf, (int)depth);

  logger(severity, domain, "%s: %s. Stack trace:", bt_version, msg);
  if (!symbols) {
    /* LCOV_EXCL_START -- we can't provoke this. */
    logger(severity, domain, "    Unable to generate backtrace.");
    goto done;
    /* LCOV_EXCL_STOP */
  }
  for (i=0; i < depth; ++i) {
    logger(severity, domain, "    %s", symbols[i]);
  }
  raw_free(symbols);

 done:
  unlock_cb_buf(cb_buf);
}

static void crash_handler(int sig, siginfo_t *si, void *ctx_)
  __attribute__((noreturn));

/** Signal handler: write a crash message with a stack trace, and die. */
static void
crash_handler(int sig, siginfo_t *si, void *ctx_)
{
  char buf[40];
  size_t depth;
  ucontext_t *ctx = (ucontext_t *) ctx_;
  int n_fds, i;
  const int *fds = NULL;

  void **cb_buf = lock_cb_buf();

  (void) si;

  depth = backtrace(cb_buf, MAX_DEPTH);
  /* Clean up the top stack frame so we get the real function
   * name for the most recently failing function. */
  clean_backtrace(cb_buf, depth, ctx);

  format_dec_number_sigsafe((unsigned)sig, buf, sizeof(buf));

  tor_log_err_sigsafe(bt_version, " died: Caught signal ", buf, "\n",
                      NULL);

  n_fds = tor_log_get_sigsafe_err_fds(&fds);
  for (i=0; i < n_fds; ++i)
    backtrace_symbols_fd(cb_buf, (int)depth, fds[i]);

  unlock_cb_buf(cb_buf);

  tor_raw_abort_();
}

/** Write a backtrace to all of the emergency-error fds. */
void
dump_stack_symbols_to_error_fds(void)
{
  int n_fds, i;
  const int *fds = NULL;
  size_t depth;

  void **cb_buf = lock_cb_buf();

  depth = backtrace(cb_buf, MAX_DEPTH);

  n_fds = tor_log_get_sigsafe_err_fds(&fds);
  for (i=0; i < n_fds; ++i)
    backtrace_symbols_fd(cb_buf, (int)depth, fds[i]);

  unlock_cb_buf(cb_buf);
}

/* The signals that we want our backtrace handler to trap */
static int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS,
  SIGIO, -1 };

/** Install signal handlers as needed so that when we crash, we produce a
 * useful stack trace. Return 0 on success, -errno on failure. */
static int
install_bt_handler(void)
{
  int i, rv=0;

  struct sigaction sa;

  memset(&sa, 0, sizeof(sa));
  sa.sa_sigaction = crash_handler;
  sa.sa_flags = SA_SIGINFO;
  sigfillset(&sa.sa_mask);

  for (i = 0; trap_signals[i] >= 0; ++i) {
    if (sigaction(trap_signals[i], &sa, NULL) == -1) {
      /* LCOV_EXCL_START */
      rv = -errno;
      /* LCOV_EXCL_STOP */
    }
  }

  {
    /* Now, generate (but do not log) a backtrace.  This ensures that
     * libc has pre-loaded the symbols we need to dump things, so that later
     * reads won't be denied by the sandbox code */
    char **symbols;
    void **cb_buf = lock_cb_buf();
    size_t depth = backtrace(cb_buf, MAX_DEPTH);
    symbols = backtrace_symbols(cb_buf, (int) depth);
    if (symbols)
      raw_free(symbols);
    unlock_cb_buf(cb_buf);
  }

  return rv;
}

/** Uninstall crash handlers. */
static void
remove_bt_handler(void)
{
  int i;

  struct sigaction sa;

  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = SIG_DFL;
  sigfillset(&sa.sa_mask);

  for (i = 0; trap_signals[i] >= 0; ++i) {
    /* remove_bt_handler() is called on shutdown, from low-level code.
     * It's not a fatal error, so we just ignore it. */
    (void)sigaction(trap_signals[i], &sa, NULL);
  }

  /* cb_buf_mutex is statically initialised, so we can not destroy it.
   * If we destroy it, and then re-initialise tor, all our backtraces will
   * fail. */
}
#endif /* defined(USE_BACKTRACE) */

#ifdef NO_BACKTRACE_IMPL
void
log_backtrace_impl(int severity, log_domain_mask_t domain, const char *msg,
                   tor_log_fn logger)
{
  logger(severity, domain, "%s: %s. (Stack trace not available)",
         bt_version, msg);
}

static int
install_bt_handler(void)
{
  return 0;
}

static void
remove_bt_handler(void)
{
}

void
dump_stack_symbols_to_error_fds(void)
{
}
#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. */
int
configure_backtrace_handler(const char *tor_version)
{
  char version[128] = "Tor\0";

  if (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);
  }

  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
 * crashes.  */
void
clean_up_backtrace_handler(void)
{
  remove_bt_handler();
}