xref: /freebsd/lib/libthr/thread/thr_exit.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the author nor the names of any co-contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "namespace.h"
36 #include <errno.h>
37 #ifdef _PTHREAD_FORCED_UNWIND
38 #include <dlfcn.h>
39 #endif
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <pthread.h>
44 #include <sys/types.h>
45 #include <sys/signalvar.h>
46 #include "un-namespace.h"
47 
48 #include "libc_private.h"
49 #include "thr_private.h"
50 
51 static void	exit_thread(void) __dead2;
52 
53 __weak_reference(_pthread_exit, pthread_exit);
54 
55 #ifdef _PTHREAD_FORCED_UNWIND
56 static int message_printed;
57 
58 static void thread_unwind(void) __dead2;
59 #ifdef PIC
60 static void thread_uw_init(void);
61 static _Unwind_Reason_Code thread_unwind_stop(int version,
62 	_Unwind_Action actions,
63 	int64_t exc_class,
64 	struct _Unwind_Exception *exc_obj,
65 	struct _Unwind_Context *context, void *stop_parameter);
66 /* unwind library pointers */
67 static _Unwind_Reason_Code (*uwl_forcedunwind)(struct _Unwind_Exception *,
68 	_Unwind_Stop_Fn, void *);
69 static unsigned long (*uwl_getcfa)(struct _Unwind_Context *);
70 
71 static void
72 thread_uw_init(void)
73 {
74 	static int inited = 0;
75 	Dl_info dli;
76 	void *handle;
77 	void *forcedunwind, *getcfa;
78 
79 	if (inited)
80 	    return;
81 	handle = RTLD_DEFAULT;
82 	if ((forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind")) != NULL) {
83 	    if (dladdr(forcedunwind, &dli)) {
84 		/*
85 		 * Make sure the address is always valid by holding the library,
86 		 * also assume functions are in same library.
87 		 */
88 		if ((handle = dlopen(dli.dli_fname, RTLD_LAZY)) != NULL) {
89 		    forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind");
90 		    getcfa = dlsym(handle, "_Unwind_GetCFA");
91 		    if (forcedunwind != NULL && getcfa != NULL) {
92 			uwl_getcfa = getcfa;
93 			atomic_store_rel_ptr((volatile void *)&uwl_forcedunwind,
94 				(uintptr_t)forcedunwind);
95 		    } else {
96 			dlclose(handle);
97 		    }
98 		}
99 	    }
100 	}
101 	inited = 1;
102 }
103 
104 _Unwind_Reason_Code
105 _Unwind_ForcedUnwind(struct _Unwind_Exception *ex, _Unwind_Stop_Fn stop_func,
106 	void *stop_arg)
107 {
108 	return (*uwl_forcedunwind)(ex, stop_func, stop_arg);
109 }
110 
111 unsigned long
112 _Unwind_GetCFA(struct _Unwind_Context *context)
113 {
114 	return (*uwl_getcfa)(context);
115 }
116 #else
117 #pragma weak _Unwind_GetCFA
118 #pragma weak _Unwind_ForcedUnwind
119 #endif /* PIC */
120 
121 static void
122 thread_unwind_cleanup(_Unwind_Reason_Code code __unused,
123     struct _Unwind_Exception *e __unused)
124 {
125 	/*
126 	 * Specification said that _Unwind_Resume should not be used here,
127 	 * instead, user should rethrow the exception. For C++ user, they
128 	 * should put "throw" sentence in catch(...) block.
129 	 */
130 	PANIC("exception should be rethrown");
131 }
132 
133 static _Unwind_Reason_Code
134 thread_unwind_stop(int version __unused, _Unwind_Action actions,
135 	int64_t exc_class __unused,
136 	struct _Unwind_Exception *exc_obj __unused,
137 	struct _Unwind_Context *context, void *stop_parameter __unused)
138 {
139 	struct pthread *curthread = _get_curthread();
140 	struct pthread_cleanup *cur;
141 	uintptr_t cfa;
142 	int done = 0;
143 
144 	/* XXX assume stack grows down to lower address */
145 
146 	cfa = _Unwind_GetCFA(context);
147 	if (actions & _UA_END_OF_STACK ||
148 	    cfa >= (uintptr_t)curthread->unwind_stackend) {
149 		done = 1;
150 	}
151 
152 	while ((cur = curthread->cleanup) != NULL &&
153 	       (done || (uintptr_t)cur <= cfa)) {
154 		__pthread_cleanup_pop_imp(1);
155 	}
156 
157 	if (done) {
158 		/* Tell libc that it should call non-trivial TLS dtors. */
159 		__cxa_thread_call_dtors();
160 
161 		exit_thread(); /* Never return! */
162 	}
163 
164 	return (_URC_NO_REASON);
165 }
166 
167 static void
168 thread_unwind(void)
169 {
170 	struct pthread  *curthread = _get_curthread();
171 
172 	curthread->ex.exception_class = 0;
173 	curthread->ex.exception_cleanup = thread_unwind_cleanup;
174 	_Unwind_ForcedUnwind(&curthread->ex, thread_unwind_stop, NULL);
175 	PANIC("_Unwind_ForcedUnwind returned");
176 }
177 
178 #endif
179 
180 void
181 _thread_exitf(const char *fname, int lineno, const char *fmt, ...)
182 {
183 	va_list ap;
184 
185 	/* Write an error message to the standard error file descriptor: */
186 	_thread_printf(STDERR_FILENO, "Fatal error '");
187 
188 	va_start(ap, fmt);
189 	_thread_vprintf(STDERR_FILENO, fmt, ap);
190 	va_end(ap);
191 
192 	_thread_printf(STDERR_FILENO, "' at line %d in file %s (errno = %d)\n",
193 	    lineno, fname, errno);
194 
195 	abort();
196 }
197 
198 void
199 _thread_exit(const char *fname, int lineno, const char *msg)
200 {
201 
202 	_thread_exitf(fname, lineno, "%s", msg);
203 }
204 
205 void
206 _pthread_exit(void *status)
207 {
208 	_pthread_exit_mask(status, NULL);
209 }
210 
211 void
212 _pthread_exit_mask(void *status, sigset_t *mask)
213 {
214 	struct pthread *curthread = _get_curthread();
215 
216 	/* Check if this thread is already in the process of exiting: */
217 	if (curthread->cancelling)
218 		PANIC("Thread %p has called "
219 		    "pthread_exit() from a destructor. POSIX 1003.1 "
220 		    "1996 s16.2.5.2 does not allow this!", curthread);
221 
222 	/* Flag this thread as exiting. */
223 	curthread->cancelling = 1;
224 	curthread->no_cancel = 1;
225 	curthread->cancel_async = 0;
226 	curthread->cancel_point = 0;
227 	if (mask != NULL)
228 		__sys_sigprocmask(SIG_SETMASK, mask, NULL);
229 	if (curthread->unblock_sigcancel) {
230 		sigset_t set;
231 
232 		curthread->unblock_sigcancel = 0;
233 		SIGEMPTYSET(set);
234 		SIGADDSET(set, SIGCANCEL);
235 		__sys_sigprocmask(SIG_UNBLOCK, mask, NULL);
236 	}
237 
238 	/* Save the return value: */
239 	curthread->ret = status;
240 #ifdef _PTHREAD_FORCED_UNWIND
241 
242 #ifdef PIC
243 	thread_uw_init();
244 	if (uwl_forcedunwind != NULL) {
245 #else
246 	if (_Unwind_ForcedUnwind != NULL) {
247 #endif
248 		if (curthread->unwind_disabled) {
249 			if (message_printed == 0) {
250 				message_printed = 1;
251 				_thread_printf(2, "Warning: old _pthread_cleanup_push was called, "
252 				  	"stack unwinding is disabled.\n");
253 			}
254 			goto cleanup;
255 		}
256 		thread_unwind();
257 
258 	} else {
259 cleanup:
260 		while (curthread->cleanup != NULL) {
261 			__pthread_cleanup_pop_imp(1);
262 		}
263 		__cxa_thread_call_dtors();
264 
265 		exit_thread();
266 	}
267 
268 #else
269 	while (curthread->cleanup != NULL) {
270 		__pthread_cleanup_pop_imp(1);
271 	}
272 	__cxa_thread_call_dtors();
273 
274 	exit_thread();
275 #endif /* _PTHREAD_FORCED_UNWIND */
276 }
277 
278 static void
279 exit_thread(void)
280 {
281 	struct pthread *curthread = _get_curthread();
282 
283 	free(curthread->name);
284 	curthread->name = NULL;
285 
286 	/* Check if there is thread specific data: */
287 	if (curthread->specific != NULL) {
288 		/* Run the thread-specific data destructors: */
289 		_thread_cleanupspecific();
290 	}
291 
292 	if (!_thr_isthreaded())
293 		exit(0);
294 
295 	if (atomic_fetchadd_int(&_thread_active_threads, -1) == 1) {
296 		exit(0);
297 		/* Never reach! */
298 	}
299 
300 	/* Tell malloc that the thread is exiting. */
301 	_malloc_thread_cleanup();
302 
303 	THR_LOCK(curthread);
304 	curthread->state = PS_DEAD;
305 	if (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
306 		curthread->cycle++;
307 		_thr_umtx_wake(&curthread->cycle, INT_MAX, 0);
308 	}
309 	if (!curthread->force_exit && SHOULD_REPORT_EVENT(curthread, TD_DEATH))
310 		_thr_report_death(curthread);
311 	/*
312 	 * Thread was created with initial refcount 1, we drop the
313 	 * reference count to allow it to be garbage collected.
314 	 */
315 	curthread->refcount--;
316 	_thr_try_gc(curthread, curthread); /* thread lock released */
317 
318 #if defined(_PTHREADS_INVARIANTS)
319 	if (THR_IN_CRITICAL(curthread))
320 		PANIC("thread %p exits with resources held!", curthread);
321 #endif
322 	/*
323 	 * Kernel will do wakeup at the address, so joiner thread
324 	 * will be resumed if it is sleeping at the address.
325 	 */
326 	thr_exit(&curthread->tid);
327 	PANIC("thr_exit() returned");
328 	/* Never reach! */
329 }
330