signal_handler_libc.odin 4.8 KB

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