test_bt_cl.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright (c) 2012-2015, 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. int
  26. crash(int x)
  27. {
  28. if (crashtype == 0) {
  29. #if defined(__clang_analyzer__) || defined(__COVERITY__)
  30. tor_assert(1 == 0); /* Avert your eyes, clangalyzer and coverity! You
  31. * don't need to see us dereference NULL. */
  32. #else
  33. *(volatile int *)0 = 0;
  34. #endif
  35. } else if (crashtype == 1) {
  36. tor_assert(1 == 0);
  37. } else if (crashtype == -1) {
  38. ;
  39. }
  40. crashtype *= x;
  41. return crashtype;
  42. }
  43. int
  44. oh_what(int x)
  45. {
  46. /* We call crash() twice here, so that the compiler won't try to do a
  47. * tail-call optimization. Only the first call will actually happen, but
  48. * telling the compiler to maybe do the second call will prevent it from
  49. * replacing the first call with a jump. */
  50. return crash(x) + crash(x*2);
  51. }
  52. int
  53. a_tangled_web(int x)
  54. {
  55. return oh_what(x) * 99 + oh_what(x);
  56. }
  57. int
  58. we_weave(int x)
  59. {
  60. return a_tangled_web(x) + a_tangled_web(x+1);
  61. }
  62. static void
  63. abort_handler(int s)
  64. {
  65. (void)s;
  66. exit(0);
  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. signal(SIGABRT, abort_handler);
  100. printf("%d\n", we_weave(2));
  101. clean_up_backtrace_handler();
  102. return 0;
  103. }