tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

test_bt_cl.c (3067B)


      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 crash(int x)
     40 {
     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_assertf(1 == 0, "%d != %d", 1, 0);
     50  } else if (crashtype == -1) {
     51    ;
     52  }
     53 
     54  crashtype *= x;
     55  return crashtype;
     56 }
     57 #ifdef HAVE_CFLAG_WNULL_DEREFERENCE
     58 ENABLE_GCC_WARNING("-Wnull-dereference")
     59 #endif
     60 
     61 int
     62 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  return crash(x) + crash(x*2);
     69 }
     70 
     71 int
     72 a_tangled_web(int x)
     73 {
     74  return oh_what(x) * 99 + oh_what(x);
     75 }
     76 
     77 int
     78 we_weave(int x)
     79 {
     80  return a_tangled_web(x) + a_tangled_web(x+1);
     81 }
     82 
     83 int
     84 main(int argc, char **argv)
     85 {
     86  log_severity_list_t severity;
     87 
     88  if (argc < 2) {
     89    puts("I take an argument. It should be \"assert\" or \"crash\" or "
     90         "\"backtraces\" or \"none\"");
     91    return 1;
     92  }
     93 
     94 #ifdef HAVE_SYS_RESOURCE_H
     95  struct rlimit rlim = { .rlim_cur = 0, .rlim_max = 0 };
     96  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  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;
    116  }
    117 
    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();
    122 
    123  configure_backtrace_handler(NULL);
    124 
    125  printf("%d\n", we_weave(2));
    126 
    127  clean_up_backtrace_handler();
    128  logs_free_all();
    129 
    130  return 0;
    131 }