signal_handler_libc.odin 5.4 KB

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