test_bt_cl.c 2.9 KB

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