test_bt_cl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Copyright (c) 2012-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #ifdef HAVE_SYS_RESOURCE_H
  7. #include <sys/resource.h>
  8. #endif
  9. /* To prevent 'assert' from going away. */
  10. #undef TOR_COVERAGE
  11. #include "core/or/or.h"
  12. #include "lib/err/backtrace.h"
  13. #include "lib/log/log.h"
  14. #ifdef HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. /* -1: no crash.
  18. * 0: crash with a segmentation fault.
  19. * 1x: crash with an assertion failure. */
  20. static int crashtype = 0;
  21. #ifdef __GNUC__
  22. #define NOINLINE __attribute__((noinline))
  23. #endif
  24. int crash(int x) NOINLINE;
  25. int oh_what(int x) NOINLINE;
  26. int a_tangled_web(int x) NOINLINE;
  27. int we_weave(int x) NOINLINE;
  28. #ifdef HAVE_CFLAG_WNULL_DEREFERENCE
  29. DISABLE_GCC_WARNING(null-dereference)
  30. #endif
  31. int
  32. crash(int x)
  33. {
  34. if (crashtype == 0) {
  35. #if defined(__clang_analyzer__) || defined(__COVERITY__)
  36. tor_assert(1 == 0); /* Avert your eyes, clangalyzer and coverity! You
  37. * don't need to see us dereference NULL. */
  38. #else
  39. *(volatile int *)0 = 0;
  40. #endif /* defined(__clang_analyzer__) || defined(__COVERITY__) */
  41. } else if (crashtype == 1) {
  42. tor_assertf(1 == 0, "%d != %d", 1, 0);
  43. } else if (crashtype == -1) {
  44. ;
  45. }
  46. crashtype *= x;
  47. return crashtype;
  48. }
  49. #ifdef HAVE_CFLAG_WNULL_DEREFERENCE
  50. ENABLE_GCC_WARNING(null-dereference)
  51. #endif
  52. int
  53. oh_what(int x)
  54. {
  55. /* We call crash() twice here, so that the compiler won't try to do a
  56. * tail-call optimization. Only the first call will actually happen, but
  57. * telling the compiler to maybe do the second call will prevent it from
  58. * replacing the first call with a jump. */
  59. return crash(x) + crash(x*2);
  60. }
  61. int
  62. a_tangled_web(int x)
  63. {
  64. return oh_what(x) * 99 + oh_what(x);
  65. }
  66. int
  67. we_weave(int x)
  68. {
  69. return a_tangled_web(x) + a_tangled_web(x+1);
  70. }
  71. int
  72. main(int argc, char **argv)
  73. {
  74. log_severity_list_t severity;
  75. if (argc < 2) {
  76. puts("I take an argument. It should be \"assert\" or \"crash\" or "
  77. "\"backtraces\" or \"none\"");
  78. return 1;
  79. }
  80. #ifdef HAVE_SYS_RESOURCE_H
  81. struct rlimit rlim = { .rlim_cur = 0, .rlim_max = 0 };
  82. setrlimit(RLIMIT_CORE, &rlim);
  83. #endif
  84. #if !(defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
  85. defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION))
  86. puts("Backtrace reporting is not supported on this platform");
  87. return 77;
  88. #endif
  89. if (!strcmp(argv[1], "assert")) {
  90. crashtype = 1;
  91. } else if (!strcmp(argv[1], "crash")) {
  92. crashtype = 0;
  93. } else if (!strcmp(argv[1], "none")) {
  94. crashtype = -1;
  95. } else if (!strcmp(argv[1], "backtraces")) {
  96. return 0;
  97. } else {
  98. puts("Argument should be \"assert\" or \"crash\" or \"none\"");
  99. return 1;
  100. }
  101. init_logging(1);
  102. set_log_severity_config(LOG_WARN, LOG_ERR, &severity);
  103. add_stream_log(&severity, "stdout", STDOUT_FILENO);
  104. tor_log_update_sigsafe_err_fds();
  105. configure_backtrace_handler(NULL);
  106. printf("%d\n", we_weave(2));
  107. clean_up_backtrace_handler();
  108. logs_free_all();
  109. return 0;
  110. }