1 /* Copyright (c) 2012-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
7 #ifdef HAVE_SYS_RESOURCE_H
8 #include <sys/resource.h>
11 /* To prevent 'assert' from going away. */
13 #include "core/or/or.h"
14 #include "lib/err/backtrace.h"
15 #include "lib/log/log.h"
22 * 0: crash with a segmentation fault.
23 * 1x: crash with an assertion failure. */
24 static int crashtype
= 0;
27 #define NOINLINE __attribute__((noinline))
30 int crash(int x
) NOINLINE
;
31 int oh_what(int x
) NOINLINE
;
32 int a_tangled_web(int x
) NOINLINE
;
33 int we_weave(int x
) NOINLINE
;
35 #ifdef HAVE_CFLAG_WNULL_DEREFERENCE
36 DISABLE_GCC_WARNING(null
-dereference
)
42 #if defined(__clang_analyzer__) || defined(__COVERITY__)
43 tor_assert(1 == 0); /* Avert your eyes, clangalyzer and coverity! You
44 * don't need to see us dereference NULL. */
46 *(volatile int *)0 = 0;
47 #endif /* defined(__clang_analyzer__) || defined(__COVERITY__) */
48 } else if (crashtype
== 1) {
49 tor_assertf(1 == 0, "%d != %d", 1, 0);
50 } else if (crashtype
== -1) {
57 #ifdef HAVE_CFLAG_WNULL_DEREFERENCE
58 ENABLE_GCC_WARNING(null
-dereference
)
64 /* We call crash() twice here, so that the compiler won't try to do a
65 * tail-call optimization. Only the first call will actually happen, but
66 * telling the compiler to maybe do the second call will prevent it from
67 * replacing the first call with a jump. */
68 return crash(x
) + crash(x
*2);
74 return oh_what(x
) * 99 + oh_what(x
);
80 return a_tangled_web(x
) + a_tangled_web(x
+1);
84 main(int argc
, char **argv
)
86 log_severity_list_t severity
;
89 puts("I take an argument. It should be \"assert\" or \"crash\" or "
90 "\"backtraces\" or \"none\"");
94 #ifdef HAVE_SYS_RESOURCE_H
95 struct rlimit rlim
= { .rlim_cur
= 0, .rlim_max
= 0 };
96 setrlimit(RLIMIT_CORE
, &rlim
);
99 #if !(defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
100 defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION))
101 puts("Backtrace reporting is not supported on this platform");
105 if (!strcmp(argv
[1], "assert")) {
107 } else if (!strcmp(argv
[1], "crash")) {
109 } else if (!strcmp(argv
[1], "none")) {
111 } else if (!strcmp(argv
[1], "backtraces")) {
114 puts("Argument should be \"assert\" or \"crash\" or \"none\"");
119 set_log_severity_config(LOG_WARN
, LOG_ERR
, &severity
);
120 add_stream_log(&severity
, "stdout", STDOUT_FILENO
);
121 tor_log_update_sigsafe_err_fds();
123 configure_backtrace_handler(NULL
);
125 printf("%d\n", we_weave(2));
127 clean_up_backtrace_handler();