Use setrlimit instead of ulimit -c in backtrace tests
[tor.git] / src / test / test_bt_cl.c
blob08b08ba4236edf1fbadbd5fdf033d447bc5e06ff
1 /* Copyright (c) 2012-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
5 #include <stdio.h>
6 #include <stdlib.h>
7 #ifdef HAVE_SYS_RESOURCE_H
8 #include <sys/resource.h>
9 #endif
11 /* To prevent 'assert' from going away. */
12 #undef TOR_COVERAGE
13 #include "core/or/or.h"
14 #include "lib/err/backtrace.h"
15 #include "lib/log/log.h"
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
21 /* -1: no crash.
22 * 0: crash with a segmentation fault.
23 * 1x: crash with an assertion failure. */
24 static int crashtype = 0;
26 #ifdef __GNUC__
27 #define NOINLINE __attribute__((noinline))
28 #endif
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)
37 #endif
38 int
39 crash(int x)
41 if (crashtype == 0) {
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. */
45 #else
46 *(volatile int *)0 = 0;
47 #endif /* defined(__clang_analyzer__) || defined(__COVERITY__) */
48 } else if (crashtype == 1) {
49 tor_assert(1 == 0);
50 } else if (crashtype == -1) {
54 crashtype *= x;
55 return crashtype;
57 #ifdef HAVE_CFLAG_WNULL_DEREFERENCE
58 ENABLE_GCC_WARNING(null-dereference)
59 #endif
61 int
62 oh_what(int x)
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);
71 int
72 a_tangled_web(int x)
74 return oh_what(x) * 99 + oh_what(x);
77 int
78 we_weave(int x)
80 return a_tangled_web(x) + a_tangled_web(x+1);
83 int
84 main(int argc, char **argv)
86 log_severity_list_t severity;
88 if (argc < 2) {
89 puts("I take an argument. It should be \"assert\" or \"crash\" or "
90 "\"backtraces\" or \"none\"");
91 return 1;
94 #ifdef HAVE_SYS_RESOURCE_H
95 struct rlimit rlim = { .rlim_cur = 0, .rlim_max = 0 };
96 setrlimit(RLIMIT_CORE, &rlim);
97 #endif
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");
102 return 77;
103 #endif
105 if (!strcmp(argv[1], "assert")) {
106 crashtype = 1;
107 } else if (!strcmp(argv[1], "crash")) {
108 crashtype = 0;
109 } else if (!strcmp(argv[1], "none")) {
110 crashtype = -1;
111 } else if (!strcmp(argv[1], "backtraces")) {
112 return 0;
113 } else {
114 puts("Argument should be \"assert\" or \"crash\" or \"none\"");
115 return 1;
118 init_logging(1);
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();
128 logs_free_all();
130 return 0;