]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/unwind.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nptl / unwind.c
CommitLineData
b168057a 1/* Copyright (C) 2003-2015 Free Software Foundation, Inc.
09d65ff3
UD
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
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
09d65ff3
UD
19
20#include <setjmp.h>
7f0dfae0 21#include <stdio.h>
09d65ff3 22#include <stdlib.h>
d347a4ab 23#include <string.h>
6e66dc78 24#include <unistd.h>
09d65ff3 25#include "pthreadP.h"
8dea90aa 26#include <jmpbuf-unwind.h>
09d65ff3
UD
27
28#ifdef HAVE_FORCED_UNWIND
29
44e94149 30#ifdef _STACK_GROWS_DOWN
675620f7
UD
31# define FRAME_LEFT(frame, other, adj) \
32 ((uintptr_t) frame - adj >= (uintptr_t) other - adj)
44e94149 33#elif _STACK_GROWS_UP
675620f7
UD
34# define FRAME_LEFT(frame, other, adj) \
35 ((uintptr_t) frame - adj <= (uintptr_t) other - adj)
44e94149
UD
36#else
37# error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
38#endif
39
09d65ff3
UD
40static _Unwind_Reason_Code
41unwind_stop (int version, _Unwind_Action actions,
42 _Unwind_Exception_Class exc_class,
43 struct _Unwind_Exception *exc_obj,
44 struct _Unwind_Context *context, void *stop_parameter)
45{
46 struct pthread_unwind_buf *buf = stop_parameter;
44e94149
UD
47 struct pthread *self = THREAD_SELF;
48 struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
49 int do_longjump = 0;
f76c8499 50
675620f7
UD
51 /* Adjust all pointers used in comparisons, so that top of thread's
52 stack is at the top of address space. Without that, things break
53 if stack is allocated above the main stack. */
54 uintptr_t adj = (uintptr_t) self->stackblock + self->stackblock_size;
09d65ff3
UD
55
56 /* Do longjmp if we're at "end of stack", aka "end of unwind data".
57 We assume there are only C frame without unwind data in between
58 here and the jmp_buf target. Otherwise simply note that the CFA
59 of a function is NOT within it's stack frame; it's the SP of the
60 previous frame. */
61 if ((actions & _UA_END_OF_STACK)
675620f7
UD
62 || ! _JMPBUF_CFA_UNWINDS_ADJ (buf->cancel_jmp_buf[0].jmp_buf, context,
63 adj))
44e94149
UD
64 do_longjump = 1;
65
a1ffb40e 66 if (__glibc_unlikely (curp != NULL))
44e94149
UD
67 {
68 /* Handle the compatibility stuff. Execute all handlers
69 registered with the old method which would be unwound by this
70 step. */
71 struct _pthread_cleanup_buffer *oldp = buf->priv.data.cleanup;
ca7b8af5 72 void *cfa = (void *) (_Unwind_Ptr) _Unwind_GetCFA (context);
44e94149 73
675620f7 74 if (curp != oldp && (do_longjump || FRAME_LEFT (cfa, curp, adj)))
44e94149
UD
75 {
76 do
77 {
78 /* Pointer to the next element. */
79 struct _pthread_cleanup_buffer *nextp = curp->__prev;
80
81 /* Call the handler. */
82 curp->__routine (curp->__arg);
83
84 /* To the next. */
85 curp = nextp;
86 }
675620f7
UD
87 while (curp != oldp
88 && (do_longjump || FRAME_LEFT (cfa, curp, adj)));
44e94149
UD
89
90 /* Mark the current element as handled. */
91 THREAD_SETMEM (self, cleanup, curp);
92 }
93 }
94
95 if (do_longjump)
f76c8499 96 __libc_unwind_longjmp ((struct __jmp_buf_tag *) buf->cancel_jmp_buf, 1);
09d65ff3
UD
97
98 return _URC_NO_REASON;
99}
100
101
102static void
103unwind_cleanup (_Unwind_Reason_Code reason, struct _Unwind_Exception *exc)
104{
29b095a1
UD
105 /* When we get here a C++ catch block didn't rethrow the object. We
106 cannot handle this case and therefore abort. */
7f0dfae0 107 __libc_fatal ("FATAL: exception not rethrown\n");
09d65ff3
UD
108}
109
110#endif /* have forced unwind */
111
112
113void
114__cleanup_fct_attribute __attribute ((noreturn))
115__pthread_unwind (__pthread_unwind_buf_t *buf)
116{
117 struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
118 struct pthread *self = THREAD_SELF;
119
44e94149
UD
120#ifdef HAVE_FORCED_UNWIND
121 /* This is not a catchable exception, so don't provide any details about
122 the exception type. We do need to initialize the field though. */
123 THREAD_SETMEM (self, exc.exception_class, 0);
c515fb51 124 THREAD_SETMEM (self, exc.exception_cleanup, &unwind_cleanup);
44e94149
UD
125
126 _Unwind_ForcedUnwind (&self->exc, unwind_stop, ibuf);
127#else
09d65ff3
UD
128 /* Handle the compatibility stuff first. Execute all handlers
129 registered with the old method. We don't execute them in order,
130 instead, they will run first. */
131 struct _pthread_cleanup_buffer *oldp = ibuf->priv.data.cleanup;
132 struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
133
68107ec0 134 if (curp != oldp)
09d65ff3 135 {
68107ec0
UD
136 do
137 {
138 /* Pointer to the next element. */
139 struct _pthread_cleanup_buffer *nextp = curp->__prev;
09d65ff3 140
68107ec0
UD
141 /* Call the handler. */
142 curp->__routine (curp->__arg);
09d65ff3 143
68107ec0
UD
144 /* To the next. */
145 curp = nextp;
146 }
147 while (curp != oldp);
09d65ff3 148
68107ec0
UD
149 /* Mark the current element as handled. */
150 THREAD_SETMEM (self, cleanup, curp);
151 }
09d65ff3 152
09d65ff3 153 /* We simply jump to the registered setjmp buffer. */
f76c8499 154 __libc_unwind_longjmp ((struct __jmp_buf_tag *) ibuf->cancel_jmp_buf, 1);
09d65ff3
UD
155#endif
156 /* NOTREACHED */
157
158 /* We better do not get here. */
159 abort ();
160}
56421b23 161hidden_def (__pthread_unwind)
09d65ff3
UD
162
163
164void
165__cleanup_fct_attribute __attribute ((noreturn))
166__pthread_unwind_next (__pthread_unwind_buf_t *buf)
167{
168 struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
169
49b65043 170 __pthread_unwind ((__pthread_unwind_buf_t *) ibuf->priv.data.prev);
09d65ff3 171}
da0c02ee 172hidden_def (__pthread_unwind_next)