Line data Source code
1 : /* Copyright (c) 2012-2021, The Tor Project, Inc. */ 2 : /* See LICENSE for licensing information */ 3 : 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 10 : 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" 16 : 17 : #ifdef HAVE_UNISTD_H 18 : #include <unistd.h> 19 : #endif 20 : 21 : /* -1: no crash. 22 : * 0: crash with a segmentation fault. 23 : * 1x: crash with an assertion failure. */ 24 : static int crashtype = 0; 25 : 26 : #ifdef __GNUC__ 27 : #define NOINLINE __attribute__((noinline)) 28 : #endif 29 : 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; 34 : 35 : #ifdef HAVE_CFLAG_WNULL_DEREFERENCE 36 : DISABLE_GCC_WARNING("-Wnull-dereference") 37 : #endif 38 : int 39 8 : crash(int x) 40 : { 41 8 : 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 0 : *(volatile int *)0 = 0; 47 : #endif /* defined(__clang_analyzer__) || defined(__COVERITY__) */ 48 8 : } else if (crashtype == 1) { 49 0 : tor_assertf(1 == 0, "%d != %d", 1, 0); 50 : } else if (crashtype == -1) { 51 8 : ; 52 : } 53 : 54 8 : crashtype *= x; 55 8 : return crashtype; 56 : } 57 : #ifdef HAVE_CFLAG_WNULL_DEREFERENCE 58 : ENABLE_GCC_WARNING("-Wnull-dereference") 59 : #endif 60 : 61 : int 62 4 : oh_what(int x) 63 : { 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 4 : return crash(x) + crash(x*2); 69 : } 70 : 71 : int 72 2 : a_tangled_web(int x) 73 : { 74 2 : return oh_what(x) * 99 + oh_what(x); 75 : } 76 : 77 : int 78 1 : we_weave(int x) 79 : { 80 1 : return a_tangled_web(x) + a_tangled_web(x+1); 81 : } 82 : 83 : int 84 2 : main(int argc, char **argv) 85 : { 86 2 : log_severity_list_t severity; 87 : 88 2 : if (argc < 2) { 89 0 : puts("I take an argument. It should be \"assert\" or \"crash\" or " 90 : "\"backtraces\" or \"none\""); 91 0 : return 1; 92 : } 93 : 94 : #ifdef HAVE_SYS_RESOURCE_H 95 2 : struct rlimit rlim = { .rlim_cur = 0, .rlim_max = 0 }; 96 2 : setrlimit(RLIMIT_CORE, &rlim); 97 : #endif 98 : 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 104 : 105 2 : if (!strcmp(argv[1], "assert")) { 106 0 : crashtype = 1; 107 2 : } else if (!strcmp(argv[1], "crash")) { 108 0 : crashtype = 0; 109 2 : } else if (!strcmp(argv[1], "none")) { 110 1 : crashtype = -1; 111 1 : } else if (!strcmp(argv[1], "backtraces")) { 112 : return 0; 113 : } else { 114 0 : puts("Argument should be \"assert\" or \"crash\" or \"none\""); 115 0 : return 1; 116 : } 117 : 118 1 : init_logging(1); 119 1 : set_log_severity_config(LOG_WARN, LOG_ERR, &severity); 120 1 : add_stream_log(&severity, "stdout", STDOUT_FILENO); 121 1 : tor_log_update_sigsafe_err_fds(); 122 : 123 1 : configure_backtrace_handler(NULL); 124 : 125 1 : printf("%d\n", we_weave(2)); 126 : 127 1 : clean_up_backtrace_handler(); 128 1 : logs_free_all(); 129 : 130 1 : return 0; 131 : }