signal_handler_libc.odin 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //+private
  2. //+build windows, linux, darwin, freebsd, openbsd, netbsd, haiku
  3. package testing
  4. import "base:intrinsics"
  5. import "core:c/libc"
  6. import "core:encoding/ansi"
  7. import "core:sync"
  8. import "core:os"
  9. @require import "core:sys/unix"
  10. @(private="file") stop_runner_flag: libc.sig_atomic_t
  11. @(private="file") stop_test_gate: sync.Mutex
  12. @(private="file") stop_test_index: libc.sig_atomic_t
  13. @(private="file") stop_test_reason: libc.sig_atomic_t
  14. @(private="file") stop_test_alert: libc.sig_atomic_t
  15. @(private="file", thread_local)
  16. local_test_index: libc.sig_atomic_t
  17. // Windows does not appear to have a SIGTRAP, so this is defined here, instead
  18. // of in the libc package, just so there's no confusion about it being
  19. // available there.
  20. SIGTRAP :: 5
  21. @(private="file")
  22. stop_runner_callback :: proc "c" (sig: libc.int) {
  23. prev := intrinsics.atomic_add(&stop_runner_flag, 1)
  24. // If the flag was already set (if this is the second signal sent for example),
  25. // consider this a forced (not graceful) exit.
  26. if prev > 0 {
  27. os.exit(int(sig))
  28. }
  29. }
  30. @(private="file")
  31. stop_test_callback :: proc "c" (sig: libc.int) {
  32. if local_test_index == -1 {
  33. // We're the test runner, and we ourselves have caught a signal from
  34. // which there is no recovery.
  35. //
  36. // The most we can do now is make sure the user's cursor is visible,
  37. // nuke the entire processs, and hope a useful core dump survives.
  38. // NOTE(Feoramund): Using these write calls in a signal handler is
  39. // undefined behavior in C99 but possibly tolerated in POSIX 2008.
  40. // Either way, we may as well try to salvage what we can.
  41. show_cursor := ansi.CSI + ansi.DECTCEM_SHOW
  42. libc.fwrite(raw_data(show_cursor), size_of(byte), len(show_cursor), libc.stdout)
  43. libc.fflush(libc.stdout)
  44. // This is an attempt at being compliant by avoiding printf.
  45. sigbuf: [8]byte
  46. sigstr: string
  47. {
  48. signum := cast(int)sig
  49. i := len(sigbuf) - 2
  50. for signum > 0 {
  51. m := signum % 10
  52. signum /= 10
  53. sigbuf[i] = cast(u8)('0' + m)
  54. i -= 1
  55. }
  56. sigstr = cast(string)sigbuf[1 + i:len(sigbuf) - 1]
  57. }
  58. advisory_a := `
  59. The test runner's main thread has caught an unrecoverable error (signal `
  60. advisory_b := `) and will now forcibly terminate.
  61. This is a dire bug and should be reported to the Odin developers.
  62. `
  63. libc.fwrite(raw_data(advisory_a), size_of(byte), len(advisory_a), libc.stderr)
  64. libc.fwrite(raw_data(sigstr), size_of(byte), len(sigstr), libc.stderr)
  65. libc.fwrite(raw_data(advisory_b), size_of(byte), len(advisory_b), libc.stderr)
  66. // Try to get a core dump.
  67. libc.abort()
  68. }
  69. if sync.mutex_guard(&stop_test_gate) {
  70. intrinsics.atomic_store(&stop_test_index, local_test_index)
  71. intrinsics.atomic_store(&stop_test_reason, cast(libc.sig_atomic_t)sig)
  72. intrinsics.atomic_store(&stop_test_alert, 1)
  73. for {
  74. // Idle until this thread is terminated by the runner,
  75. // otherwise we may continue to generate signals.
  76. intrinsics.cpu_relax()
  77. when ODIN_OS != .Windows {
  78. // NOTE(Feoramund): Some UNIX-like platforms may require this.
  79. //
  80. // During testing, I found that NetBSD 10.0 refused to
  81. // terminate a task thread, even when its thread had been
  82. // properly set to PTHREAD_CANCEL_ASYNCHRONOUS.
  83. //
  84. // The runner would stall after returning from `pthread_cancel`.
  85. unix.pthread_testcancel()
  86. }
  87. }
  88. }
  89. }
  90. _setup_signal_handler :: proc() {
  91. local_test_index = -1
  92. // Catch user interrupt / CTRL-C.
  93. libc.signal(libc.SIGINT, stop_runner_callback)
  94. // Catch polite termination request.
  95. libc.signal(libc.SIGTERM, stop_runner_callback)
  96. // For tests:
  97. // Catch asserts and panics.
  98. libc.signal(libc.SIGILL, stop_test_callback)
  99. when ODIN_OS == .Linux || ODIN_OS == .FreeBSD || ODIN_OS == .Haiku || ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD || ODIN_OS == .Darwin {
  100. // Catch panics on Darwin and unhandled calls to `debug_trap`.
  101. libc.signal(SIGTRAP, stop_test_callback)
  102. }
  103. // Catch arithmetic errors.
  104. libc.signal(libc.SIGFPE, stop_test_callback)
  105. // Catch segmentation faults (illegal memory access).
  106. libc.signal(libc.SIGSEGV, stop_test_callback)
  107. }
  108. _setup_task_signal_handler :: proc(test_index: int) {
  109. local_test_index = cast(libc.sig_atomic_t)test_index
  110. }
  111. _should_stop_runner :: proc() -> bool {
  112. return intrinsics.atomic_load(&stop_runner_flag) == 1
  113. }
  114. @(private="file")
  115. unlock_stop_test_gate :: proc(_: int, _: Stop_Reason, ok: bool) {
  116. if ok {
  117. sync.mutex_unlock(&stop_test_gate)
  118. }
  119. }
  120. @(deferred_out=unlock_stop_test_gate)
  121. _should_stop_test :: proc() -> (test_index: int, reason: Stop_Reason, ok: bool) {
  122. if intrinsics.atomic_load(&stop_test_alert) == 1 {
  123. intrinsics.atomic_store(&stop_test_alert, 0)
  124. test_index = cast(int)intrinsics.atomic_load(&stop_test_index)
  125. switch intrinsics.atomic_load(&stop_test_reason) {
  126. case libc.SIGFPE: reason = .Arithmetic_Error
  127. case libc.SIGILL: reason = .Illegal_Instruction
  128. case libc.SIGSEGV: reason = .Segmentation_Fault
  129. case SIGTRAP: reason = .Unhandled_Trap
  130. }
  131. ok = true
  132. }
  133. return
  134. }