signal_handler_libc.odin 4.2 KB

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