test_bt_cl.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. *(volatile int *)0 = 0;
  28. } else if (crashtype == 1) {
  29. tor_assert(1 == 0);
  30. } else if (crashtype == -1) {
  31. ;
  32. }
  33. crashtype *= x;
  34. return crashtype;
  35. }
  36. int
  37. oh_what(int x)
  38. {
  39. /* We call crash() twice here, so that the compiler won't try to do a
  40. * tail-call optimization. Only the first call will actually happen, but
  41. * telling the compiler to maybe do the second call will prevent it from
  42. * replacing the first call with a jump. */
  43. return crash(x) + crash(x*2);
  44. }
  45. int
  46. a_tangled_web(int x)
  47. {
  48. return oh_what(x) * 99 + oh_what(x);
  49. }
  50. int
  51. we_weave(int x)
  52. {
  53. return a_tangled_web(x) + a_tangled_web(x+1);
  54. }
  55. static void
  56. abort_handler(int s)
  57. {
  58. (void)s;
  59. exit(0);
  60. }
  61. int
  62. main(int argc, char **argv)
  63. {
  64. log_severity_list_t severity;
  65. if (argc < 2) {
  66. puts("I take an argument. It should be \"assert\" or \"crash\" or "
  67. "\"none\"");
  68. return 1;
  69. }
  70. if (!strcmp(argv[1], "assert")) {
  71. crashtype = 1;
  72. } else if (!strcmp(argv[1], "crash")) {
  73. crashtype = 0;
  74. } else if (!strcmp(argv[1], "none")) {
  75. crashtype = -1;
  76. } else {
  77. puts("Argument should be \"assert\" or \"crash\" or \"none\"");
  78. return 1;
  79. }
  80. init_logging();
  81. set_log_severity_config(LOG_WARN, LOG_ERR, &severity);
  82. add_stream_log(&severity, "stdout", STDOUT_FILENO);
  83. tor_log_update_sigsafe_err_fds();
  84. configure_backtrace_handler(NULL);
  85. signal(SIGABRT, abort_handler);
  86. printf("%d\n", we_weave(2));
  87. clean_up_backtrace_handler();
  88. return 0;
  89. }