test_bt_cl.c 2.7 KB

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