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