]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/unwind.c
Use glibc_likely instead __builtin_expect.
[thirdparty/glibc.git] / nptl / unwind.c
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>
4 and Richard Henderson <rth@redhat.com>, 2003.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <setjmp.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include "pthreadP.h"
25 #include <jmpbuf-unwind.h>
26
27 #ifdef HAVE_FORCED_UNWIND
28
29 #ifdef _STACK_GROWS_DOWN
30 # define FRAME_LEFT(frame, other, adj) \
31 ((uintptr_t) frame - adj >= (uintptr_t) other - adj)
32 #elif _STACK_GROWS_UP
33 # define FRAME_LEFT(frame, other, adj) \
34 ((uintptr_t) frame - adj <= (uintptr_t) other - adj)
35 #else
36 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
37 #endif
38
39 static _Unwind_Reason_Code
40 unwind_stop (int version, _Unwind_Action actions,
41 _Unwind_Exception_Class exc_class,
42 struct _Unwind_Exception *exc_obj,
43 struct _Unwind_Context *context, void *stop_parameter)
44 {
45 struct pthread_unwind_buf *buf = stop_parameter;
46 struct pthread *self = THREAD_SELF;
47 struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
48 int do_longjump = 0;
49
50 /* Adjust all pointers used in comparisons, so that top of thread's
51 stack is at the top of address space. Without that, things break
52 if stack is allocated above the main stack. */
53 uintptr_t adj = (uintptr_t) self->stackblock + self->stackblock_size;
54
55 /* Do longjmp if we're at "end of stack", aka "end of unwind data".
56 We assume there are only C frame without unwind data in between
57 here and the jmp_buf target. Otherwise simply note that the CFA
58 of a function is NOT within it's stack frame; it's the SP of the
59 previous frame. */
60 if ((actions & _UA_END_OF_STACK)
61 || ! _JMPBUF_CFA_UNWINDS_ADJ (buf->cancel_jmp_buf[0].jmp_buf, context,
62 adj))
63 do_longjump = 1;
64
65 if (__glibc_unlikely (curp != NULL))
66 {
67 /* Handle the compatibility stuff. Execute all handlers
68 registered with the old method which would be unwound by this
69 step. */
70 struct _pthread_cleanup_buffer *oldp = buf->priv.data.cleanup;
71 void *cfa = (void *) (_Unwind_Ptr) _Unwind_GetCFA (context);
72
73 if (curp != oldp && (do_longjump || FRAME_LEFT (cfa, curp, adj)))
74 {
75 do
76 {
77 /* Pointer to the next element. */
78 struct _pthread_cleanup_buffer *nextp = curp->__prev;
79
80 /* Call the handler. */
81 curp->__routine (curp->__arg);
82
83 /* To the next. */
84 curp = nextp;
85 }
86 while (curp != oldp
87 && (do_longjump || FRAME_LEFT (cfa, curp, adj)));
88
89 /* Mark the current element as handled. */
90 THREAD_SETMEM (self, cleanup, curp);
91 }
92 }
93
94 if (do_longjump)
95 __libc_unwind_longjmp ((struct __jmp_buf_tag *) buf->cancel_jmp_buf, 1);
96
97 return _URC_NO_REASON;
98 }
99
100
101 static void
102 unwind_cleanup (_Unwind_Reason_Code reason, struct _Unwind_Exception *exc)
103 {
104 /* When we get here a C++ catch block didn't rethrow the object. We
105 cannot handle this case and therefore abort. */
106 # define STR_N_LEN(str) str, strlen (str)
107 INTERNAL_SYSCALL_DECL (err);
108 INTERNAL_SYSCALL (write, err, 3, STDERR_FILENO,
109 STR_N_LEN ("FATAL: exception not rethrown\n"));
110 abort ();
111 }
112
113 #endif /* have forced unwind */
114
115
116 void
117 __cleanup_fct_attribute __attribute ((noreturn))
118 __pthread_unwind (__pthread_unwind_buf_t *buf)
119 {
120 struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
121 struct pthread *self = THREAD_SELF;
122
123 #ifdef HAVE_FORCED_UNWIND
124 /* This is not a catchable exception, so don't provide any details about
125 the exception type. We do need to initialize the field though. */
126 THREAD_SETMEM (self, exc.exception_class, 0);
127 THREAD_SETMEM (self, exc.exception_cleanup, &unwind_cleanup);
128
129 _Unwind_ForcedUnwind (&self->exc, unwind_stop, ibuf);
130 #else
131 /* Handle the compatibility stuff first. Execute all handlers
132 registered with the old method. We don't execute them in order,
133 instead, they will run first. */
134 struct _pthread_cleanup_buffer *oldp = ibuf->priv.data.cleanup;
135 struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
136
137 if (curp != oldp)
138 {
139 do
140 {
141 /* Pointer to the next element. */
142 struct _pthread_cleanup_buffer *nextp = curp->__prev;
143
144 /* Call the handler. */
145 curp->__routine (curp->__arg);
146
147 /* To the next. */
148 curp = nextp;
149 }
150 while (curp != oldp);
151
152 /* Mark the current element as handled. */
153 THREAD_SETMEM (self, cleanup, curp);
154 }
155
156 /* We simply jump to the registered setjmp buffer. */
157 __libc_unwind_longjmp ((struct __jmp_buf_tag *) ibuf->cancel_jmp_buf, 1);
158 #endif
159 /* NOTREACHED */
160
161 /* We better do not get here. */
162 abort ();
163 }
164 hidden_def (__pthread_unwind)
165
166
167 void
168 __cleanup_fct_attribute __attribute ((noreturn))
169 __pthread_unwind_next (__pthread_unwind_buf_t *buf)
170 {
171 struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
172
173 __pthread_unwind ((__pthread_unwind_buf_t *) ibuf->priv.data.prev);
174 }
175 hidden_def (__pthread_unwind_next)