sigunwind.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* longjmp cleanup function for unwinding past signal handlers.
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <hurd.h>
  16. #include <thread_state.h>
  17. #include <jmpbuf-unwind.h>
  18. #include <assert.h>
  19. #include <stdint.h>
  20. #include <pointer_guard.h>
  21. /* _hurd_setup_sighandler puts a link on the `active resources' chain so that
  22. _longjmp_unwind will call this function with the `struct sigcontext *'
  23. describing the context interrupted by the signal, when `longjmp' is jumping
  24. to an environment that unwinds past the interrupted frame. */
  25. void
  26. _hurdsig_longjmp_from_handler (void *data, jmp_buf env, int val)
  27. {
  28. struct sigcontext *scp = data;
  29. struct hurd_sigstate *ss = _hurd_self_sigstate ();
  30. int onstack;
  31. inline void cleanup (void)
  32. {
  33. /* Destroy the MiG reply port used by the signal handler, and restore
  34. the reply port in use by the thread when interrupted. */
  35. mach_port_t reply_port = THREAD_GETMEM (THREAD_SELF, reply_port);
  36. /* Assigning MACH_PORT_DEAD here tells libc's mig_get_reply_port not to
  37. get another reply port, but avoids mig_dealloc_reply_port trying to
  38. deallocate it after the receive fails (which it will, because the
  39. reply port will be bogus, regardless). */
  40. THREAD_SETMEM (THREAD_SELF, reply_port, MACH_PORT_DEAD);
  41. if (MACH_PORT_VALID (reply_port))
  42. __mach_port_mod_refs (__mach_task_self (), reply_port,
  43. MACH_PORT_RIGHT_RECEIVE, -1);
  44. if (scp->sc_reply_port)
  45. __mach_port_mod_refs (__mach_task_self (), scp->sc_reply_port,
  46. MACH_PORT_RIGHT_RECEIVE, -1);
  47. }
  48. __spin_lock (&ss->lock);
  49. /* We should only ever be called from _longjmp_unwind (in jmp-unwind.c),
  50. which calls us inside a critical section. */
  51. assert (__spin_lock_locked (&ss->critical_section_lock));
  52. /* Are we on the alternate signal stack now? */
  53. onstack = (ss->sigaltstack.ss_flags & SS_ONSTACK);
  54. __spin_unlock (&ss->lock);
  55. if (onstack && ! scp->sc_onstack)
  56. {
  57. /* We are unwinding off the signal stack. We must use sigreturn to
  58. do it robustly. Mutate the sigcontext so that when sigreturn
  59. resumes from that context, it will be as if `__longjmp (ENV, VAL)'
  60. were done. */
  61. struct hurd_userlink *link;
  62. inline uintptr_t demangle_ptr (uintptr_t x)
  63. {
  64. PTR_DEMANGLE (x);
  65. return x;
  66. }
  67. /* Continue _longjmp_unwind's job of running the unwind
  68. forms for frames being unwound, since we will not
  69. return to its loop like this one, which called us. */
  70. for (link = ss->active_resources;
  71. link && _JMPBUF_UNWINDS (env[0].__jmpbuf, link, demangle_ptr);
  72. link = link->thread.next)
  73. if (_hurd_userlink_unlink (link))
  74. {
  75. if (link->cleanup == &_hurdsig_longjmp_from_handler)
  76. {
  77. /* We are unwinding past another signal handler invocation.
  78. Just finish the cleanup for this (inner) one, and then
  79. swap SCP to restore to the outer context. */
  80. cleanup ();
  81. scp = link->cleanup_data;
  82. }
  83. else
  84. (*link->cleanup) (link->cleanup_data, env, val);
  85. }
  86. #define sc_machine_thread_state paste(sc_,machine_thread_state)
  87. #define paste(a,b) paste1(a,b)
  88. #define paste1(a,b) a##b
  89. /* There are no more unwind forms to be run!
  90. Now we can just have the sigreturn do the longjmp for us. */
  91. _hurd_longjmp_thread_state
  92. ((struct machine_thread_state *) &scp->sc_machine_thread_state,
  93. env, val);
  94. /* Restore to the same current signal mask. If sigsetjmp saved the
  95. mask, longjmp has already restored it as desired; if not, we
  96. should leave it as it is. */
  97. scp->sc_mask = ss->blocked;
  98. /* sigreturn expects the link added by _hurd_setup_sighandler
  99. to still be there, but _longjmp_unwind removed it just before
  100. calling us. Put it back now so sigreturn can find it. */
  101. link = (void *) &scp[1];
  102. assert (! link->resource.next && ! link->resource.prevp);
  103. assert (link->thread.next == ss->active_resources);
  104. assert (link->thread.prevp == &ss->active_resources);
  105. if (link->thread.next)
  106. link->thread.next->thread.prevp = &link->thread.next;
  107. ss->active_resources = link;
  108. /* We must momentarily exit the critical section so that sigreturn
  109. does not get upset with us. But we don't want signal handlers
  110. running right now, because we are presently in the bogus state of
  111. having run all the unwind forms back to ENV's frame, but our SP is
  112. still inside those unwound frames. */
  113. __spin_lock (&ss->lock);
  114. __spin_unlock (&ss->critical_section_lock);
  115. ss->blocked = ~(sigset_t) 0 & ~_SIG_CANT_MASK;
  116. __spin_unlock (&ss->lock);
  117. /* Restore to the modified signal context that now
  118. performs `longjmp (ENV, VAL)'. */
  119. __sigreturn (scp);
  120. assert (! "sigreturn returned!");
  121. }
  122. /* We are not unwinding off the alternate signal stack. So nothing
  123. really funny is going on here. We can just clean up this handler
  124. frame and let _longjmp_unwind continue unwinding. */
  125. cleanup ();
  126. ss->intr_port = scp->sc_intr_port;
  127. }