test_bt_cl.c 2.8 KB

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