signal_handler_libc.odin 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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:os"
  13. import "core:sync"
  14. import "core:terminal/ansi"
  15. @(private="file") stop_runner_flag: libc.sig_atomic_t
  16. @(private="file") stop_test_gate: sync.Mutex
  17. @(private="file") stop_test_index: libc.sig_atomic_t
  18. @(private="file") stop_test_signal: libc.sig_atomic_t
  19. @(private="file") stop_test_passed: libc.sig_atomic_t
  20. @(private="file") stop_test_alert: libc.sig_atomic_t
  21. when ODIN_ARCH == .i386 && ODIN_OS == .Windows {
  22. // Thread-local storage is problematic on Windows i386
  23. @(private="file")
  24. local_test_index: libc.sig_atomic_t
  25. @(private="file")
  26. local_test_index_set: bool
  27. } else {
  28. @(private="file", thread_local)
  29. local_test_index: libc.sig_atomic_t
  30. @(private="file", thread_local)
  31. local_test_index_set: bool
  32. }
  33. // Windows does not appear to have a SIGTRAP, so this is defined here, instead
  34. // of in the libc package, just so there's no confusion about it being
  35. // available there.
  36. SIGTRAP :: 5
  37. @(private="file")
  38. stop_runner_callback :: proc "c" (sig: libc.int) {
  39. prev := intrinsics.atomic_add(&stop_runner_flag, 1)
  40. // If the flag was already set (if this is the second signal sent for example),
  41. // consider this a forced (not graceful) exit.
  42. if prev > 0 {
  43. os.exit(int(sig))
  44. }
  45. }
  46. @(private)
  47. stop_test_callback :: proc "c" (sig: libc.int) {
  48. if !local_test_index_set {
  49. // We're a thread created by a test thread.
  50. //
  51. // There's nothing we can do to inform the test runner about who
  52. // signalled, so hopefully the test will handle their own sub-threads.
  53. return
  54. }
  55. if local_test_index == -1 {
  56. // We're the test runner, and we ourselves have caught a signal from
  57. // which there is no recovery.
  58. //
  59. // The most we can do now is make sure the user's cursor is visible,
  60. // nuke the entire processs, and hope a useful core dump survives.
  61. // NOTE(Feoramund): Using these write calls in a signal handler is
  62. // undefined behavior in C99 but possibly tolerated in POSIX 2008.
  63. // Either way, we may as well try to salvage what we can.
  64. if !global_ansi_disabled {
  65. show_cursor := ansi.CSI + ansi.DECTCEM_SHOW
  66. libc.fwrite(raw_data(show_cursor), size_of(byte), len(show_cursor), libc.stdout)
  67. libc.fflush(libc.stdout)
  68. }
  69. // This is an attempt at being compliant by avoiding printf.
  70. sigbuf: [8]byte
  71. sigstr: string
  72. {
  73. signum := cast(int)sig
  74. i := len(sigbuf) - 2
  75. for signum > 0 {
  76. m := signum % 10
  77. signum /= 10
  78. sigbuf[i] = cast(u8)('0' + m)
  79. i -= 1
  80. }
  81. sigstr = cast(string)sigbuf[1 + i:len(sigbuf) - 1]
  82. }
  83. advisory_a := `
  84. The test runner's main thread has caught an unrecoverable error (signal `
  85. advisory_b := `) and will now forcibly terminate.
  86. This is a dire bug and should be reported to the Odin developers.
  87. `
  88. libc.fwrite(raw_data(advisory_a), size_of(byte), len(advisory_a), libc.stderr)
  89. libc.fwrite(raw_data(sigstr), size_of(byte), len(sigstr), libc.stderr)
  90. libc.fwrite(raw_data(advisory_b), size_of(byte), len(advisory_b), libc.stderr)
  91. // Try to get a core dump.
  92. libc.abort()
  93. }
  94. if sync.mutex_guard(&stop_test_gate) {
  95. intrinsics.atomic_store(&stop_test_index, local_test_index)
  96. intrinsics.atomic_store(&stop_test_signal, cast(libc.sig_atomic_t)sig)
  97. passed: bool
  98. check_passing: {
  99. if location := local_test_assertion_raised.location; location != {} {
  100. for i in 0..<local_test_expected_failures.location_count {
  101. if local_test_expected_failures.locations[i] == location {
  102. passed = true
  103. break check_passing
  104. }
  105. }
  106. }
  107. if message := local_test_assertion_raised.message; message != "" {
  108. for i in 0..<local_test_expected_failures.message_count {
  109. if local_test_expected_failures.messages[i] == message {
  110. passed = true
  111. break check_passing
  112. }
  113. }
  114. }
  115. if signal := local_test_expected_failures.signal; signal == sig {
  116. passed = true
  117. }
  118. }
  119. intrinsics.atomic_store(&stop_test_passed, cast(libc.sig_atomic_t)passed)
  120. intrinsics.atomic_store(&stop_test_alert, 1)
  121. for {
  122. // Idle until this thread is terminated by the runner,
  123. // otherwise we may continue to generate signals.
  124. intrinsics.cpu_relax()
  125. _test_thread_cancel()
  126. }
  127. }
  128. }
  129. _setup_signal_handler :: proc() {
  130. local_test_index = -1
  131. local_test_index_set = true
  132. // Catch user interrupt / CTRL-C.
  133. libc.signal(libc.SIGINT, stop_runner_callback)
  134. // Catch polite termination request.
  135. libc.signal(libc.SIGTERM, stop_runner_callback)
  136. // For tests:
  137. // Catch asserts and panics.
  138. libc.signal(libc.SIGILL, stop_test_callback)
  139. // Catch arithmetic errors.
  140. libc.signal(libc.SIGFPE, stop_test_callback)
  141. // Catch segmentation faults (illegal memory access).
  142. libc.signal(libc.SIGSEGV, stop_test_callback)
  143. __setup_signal_handler()
  144. }
  145. _setup_task_signal_handler :: proc(test_index: int) {
  146. local_test_index = cast(libc.sig_atomic_t)test_index
  147. local_test_index_set = true
  148. }
  149. _should_stop_runner :: proc() -> bool {
  150. return intrinsics.atomic_load(&stop_runner_flag) == 1
  151. }
  152. @(private="file")
  153. unlock_stop_test_gate :: proc(_: int, _: Stop_Reason, ok: bool) {
  154. if ok {
  155. sync.mutex_unlock(&stop_test_gate)
  156. }
  157. }
  158. @(deferred_out=unlock_stop_test_gate)
  159. _should_stop_test :: proc() -> (test_index: int, reason: Stop_Reason, ok: bool) {
  160. if intrinsics.atomic_load(&stop_test_alert) == 1 {
  161. intrinsics.atomic_store(&stop_test_alert, 0)
  162. test_index = cast(int)intrinsics.atomic_load(&stop_test_index)
  163. if cast(bool)intrinsics.atomic_load(&stop_test_passed) {
  164. reason = .Successful_Stop
  165. } else {
  166. switch intrinsics.atomic_load(&stop_test_signal) {
  167. case libc.SIGFPE: reason = .Arithmetic_Error
  168. case libc.SIGILL: reason = .Illegal_Instruction
  169. case libc.SIGSEGV: reason = .Segmentation_Fault
  170. case SIGTRAP: reason = .Unhandled_Trap
  171. }
  172. }
  173. ok = true
  174. }
  175. return
  176. }