test_bt_cl.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (c) 2012-2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "or.h"
  7. #include "util.h"
  8. #include "backtrace.h"
  9. #include "torlog.h"
  10. /* -1: no crash.
  11. * 0: crash with a segmentation fault.
  12. * 1x: crash with an assertion failure. */
  13. static int crashtype = 0;
  14. #ifdef __GNUC__
  15. #define NOINLINE __attribute__((noinline))
  16. #define NORETURN __attribute__((noreturn))
  17. #endif
  18. int crash(int x) NOINLINE;
  19. int oh_what(int x) NOINLINE;
  20. int a_tangled_web(int x) NOINLINE;
  21. int we_weave(int x) NOINLINE;
  22. static void abort_handler(int s) NORETURN;
  23. int
  24. crash(int x)
  25. {
  26. if (crashtype == 0) {
  27. #if defined(__clang_analyzer__) || defined(__COVERITY__)
  28. tor_assert(1 == 0); /* Avert your eyes, clangalyzer and coverity! You
  29. * don't need to see us dereference NULL. */
  30. #else
  31. *(volatile int *)0 = 0;
  32. #endif
  33. } else if (crashtype == 1) {
  34. tor_assert(1 == 0);
  35. } else if (crashtype == -1) {
  36. ;
  37. }
  38. crashtype *= x;
  39. return crashtype;
  40. }
  41. int
  42. oh_what(int x)
  43. {
  44. /* We call crash() twice here, so that the compiler won't try to do a
  45. * tail-call optimization. Only the first call will actually happen, but
  46. * telling the compiler to maybe do the second call will prevent it from
  47. * replacing the first call with a jump. */
  48. return crash(x) + crash(x*2);
  49. }
  50. int
  51. a_tangled_web(int x)
  52. {
  53. return oh_what(x) * 99 + oh_what(x);
  54. }
  55. int
  56. we_weave(int x)
  57. {
  58. return a_tangled_web(x) + a_tangled_web(x+1);
  59. }
  60. static void
  61. abort_handler(int s)
  62. {
  63. (void)s;
  64. exit(0);
  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. "\"none\"");
  73. return 1;
  74. }
  75. if (!strcmp(argv[1], "assert")) {
  76. crashtype = 1;
  77. } else if (!strcmp(argv[1], "crash")) {
  78. crashtype = 0;
  79. } else if (!strcmp(argv[1], "none")) {
  80. crashtype = -1;
  81. } else {
  82. puts("Argument should be \"assert\" or \"crash\" or \"none\"");
  83. return 1;
  84. }
  85. init_logging(1);
  86. set_log_severity_config(LOG_WARN, LOG_ERR, &severity);
  87. add_stream_log(&severity, "stdout", STDOUT_FILENO);
  88. tor_log_update_sigsafe_err_fds();
  89. configure_backtrace_handler(NULL);
  90. signal(SIGABRT, abort_handler);
  91. printf("%d\n", we_weave(2));
  92. clean_up_backtrace_handler();
  93. return 0;
  94. }