test_bt_cl.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /* To prevent 'assert' from going away. */
  7. #undef TOR_COVERAGE
  8. #include "core/or/or.h"
  9. #include "lib/err/backtrace.h"
  10. #include "lib/log/log.h"
  11. #ifdef HAVE_UNISTD_H
  12. #include <unistd.h>
  13. #endif
  14. /* -1: no crash.
  15. * 0: crash with a segmentation fault.
  16. * 1x: crash with an assertion failure. */
  17. static int crashtype = 0;
  18. #ifdef __GNUC__
  19. #define NOINLINE __attribute__((noinline))
  20. #endif
  21. int crash(int x) NOINLINE;
  22. int oh_what(int x) NOINLINE;
  23. int a_tangled_web(int x) NOINLINE;
  24. int we_weave(int x) NOINLINE;
  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 /* defined(__clang_analyzer__) || defined(__COVERITY__) */
  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. int
  69. main(int argc, char **argv)
  70. {
  71. log_severity_list_t severity;
  72. if (argc < 2) {
  73. puts("I take an argument. It should be \"assert\" or \"crash\" or "
  74. "\"backtraces\" or \"none\"");
  75. return 1;
  76. }
  77. #if !(defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
  78. defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION))
  79. puts("Backtrace reporting is not supported on this platform");
  80. return 77;
  81. #endif
  82. if (!strcmp(argv[1], "assert")) {
  83. crashtype = 1;
  84. } else if (!strcmp(argv[1], "crash")) {
  85. crashtype = 0;
  86. } else if (!strcmp(argv[1], "none")) {
  87. crashtype = -1;
  88. } else if (!strcmp(argv[1], "backtraces")) {
  89. return 0;
  90. } else {
  91. puts("Argument should be \"assert\" or \"crash\" or \"none\"");
  92. return 1;
  93. }
  94. init_logging(1);
  95. set_log_severity_config(LOG_WARN, LOG_ERR, &severity);
  96. add_stream_log(&severity, "stdout", STDOUT_FILENO);
  97. tor_log_update_sigsafe_err_fds();
  98. configure_backtrace_handler(NULL);
  99. printf("%d\n", we_weave(2));
  100. clean_up_backtrace_handler();
  101. logs_free_all();
  102. return 0;
  103. }