2020-01-09 00:39:17 +01:00
|
|
|
/* Copyright (c) 2012-2020, The Tor Project, Inc. */
|
2013-07-30 03:56:31 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-02-13 14:50:09 +01:00
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2013-07-30 03:56:31 +02:00
|
|
|
|
2015-04-23 16:25:16 +02:00
|
|
|
/* To prevent 'assert' from going away. */
|
|
|
|
#undef TOR_COVERAGE
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/or.h"
|
2018-06-21 15:54:04 +02:00
|
|
|
#include "lib/err/backtrace.h"
|
2018-07-10 21:20:28 +02:00
|
|
|
#include "lib/log/log.h"
|
2013-07-30 03:56:31 +02:00
|
|
|
|
2018-07-01 21:02:01 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 03:56:31 +02:00
|
|
|
/* -1: no crash.
|
|
|
|
* 0: crash with a segmentation fault.
|
|
|
|
* 1x: crash with an assertion failure. */
|
|
|
|
static int crashtype = 0;
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define NOINLINE __attribute__((noinline))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int crash(int x) NOINLINE;
|
|
|
|
int oh_what(int x) NOINLINE;
|
|
|
|
int a_tangled_web(int x) NOINLINE;
|
|
|
|
int we_weave(int x) NOINLINE;
|
|
|
|
|
2016-06-01 19:38:36 +02:00
|
|
|
#ifdef HAVE_CFLAG_WNULL_DEREFERENCE
|
2019-12-02 23:04:17 +01:00
|
|
|
DISABLE_GCC_WARNING("-Wnull-dereference")
|
2016-05-30 17:12:58 +02:00
|
|
|
#endif
|
2013-07-30 03:56:31 +02:00
|
|
|
int
|
|
|
|
crash(int x)
|
|
|
|
{
|
|
|
|
if (crashtype == 0) {
|
2014-09-02 19:56:31 +02:00
|
|
|
#if defined(__clang_analyzer__) || defined(__COVERITY__)
|
|
|
|
tor_assert(1 == 0); /* Avert your eyes, clangalyzer and coverity! You
|
|
|
|
* don't need to see us dereference NULL. */
|
|
|
|
#else
|
2013-07-30 03:56:31 +02:00
|
|
|
*(volatile int *)0 = 0;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(__clang_analyzer__) || defined(__COVERITY__) */
|
2013-07-30 03:56:31 +02:00
|
|
|
} else if (crashtype == 1) {
|
2019-03-05 16:58:31 +01:00
|
|
|
tor_assertf(1 == 0, "%d != %d", 1, 0);
|
2013-07-30 03:56:31 +02:00
|
|
|
} else if (crashtype == -1) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
crashtype *= x;
|
|
|
|
return crashtype;
|
|
|
|
}
|
2016-06-01 19:38:36 +02:00
|
|
|
#ifdef HAVE_CFLAG_WNULL_DEREFERENCE
|
2019-12-02 23:04:17 +01:00
|
|
|
ENABLE_GCC_WARNING("-Wnull-dereference")
|
2016-05-30 17:12:58 +02:00
|
|
|
#endif
|
2013-07-30 03:56:31 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
oh_what(int x)
|
|
|
|
{
|
|
|
|
/* We call crash() twice here, so that the compiler won't try to do a
|
|
|
|
* tail-call optimization. Only the first call will actually happen, but
|
|
|
|
* telling the compiler to maybe do the second call will prevent it from
|
|
|
|
* replacing the first call with a jump. */
|
|
|
|
return crash(x) + crash(x*2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
a_tangled_web(int x)
|
|
|
|
{
|
|
|
|
return oh_what(x) * 99 + oh_what(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
we_weave(int x)
|
|
|
|
{
|
|
|
|
return a_tangled_web(x) + a_tangled_web(x+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
log_severity_list_t severity;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
puts("I take an argument. It should be \"assert\" or \"crash\" or "
|
2015-09-25 00:32:51 +02:00
|
|
|
"\"backtraces\" or \"none\"");
|
2013-07-30 03:56:31 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2015-09-25 00:32:51 +02:00
|
|
|
|
2019-02-13 14:50:09 +01:00
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
struct rlimit rlim = { .rlim_cur = 0, .rlim_max = 0 };
|
|
|
|
setrlimit(RLIMIT_CORE, &rlim);
|
|
|
|
#endif
|
|
|
|
|
2015-09-25 00:32:51 +02:00
|
|
|
#if !(defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
|
|
|
|
defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION))
|
|
|
|
puts("Backtrace reporting is not supported on this platform");
|
|
|
|
return 77;
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 03:56:31 +02:00
|
|
|
if (!strcmp(argv[1], "assert")) {
|
|
|
|
crashtype = 1;
|
|
|
|
} else if (!strcmp(argv[1], "crash")) {
|
|
|
|
crashtype = 0;
|
|
|
|
} else if (!strcmp(argv[1], "none")) {
|
|
|
|
crashtype = -1;
|
2015-09-25 00:32:51 +02:00
|
|
|
} else if (!strcmp(argv[1], "backtraces")) {
|
|
|
|
return 0;
|
2013-07-30 03:56:31 +02:00
|
|
|
} else {
|
|
|
|
puts("Argument should be \"assert\" or \"crash\" or \"none\"");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-09-11 05:30:37 +02:00
|
|
|
init_logging(1);
|
2013-07-30 03:56:31 +02:00
|
|
|
set_log_severity_config(LOG_WARN, LOG_ERR, &severity);
|
|
|
|
add_stream_log(&severity, "stdout", STDOUT_FILENO);
|
|
|
|
tor_log_update_sigsafe_err_fds();
|
|
|
|
|
|
|
|
configure_backtrace_handler(NULL);
|
|
|
|
|
|
|
|
printf("%d\n", we_weave(2));
|
|
|
|
|
|
|
|
clean_up_backtrace_handler();
|
2015-12-18 12:31:14 +01:00
|
|
|
logs_free_all();
|
2013-07-30 03:56:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|