signal_handler_libc.odin 5.0 KB

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