1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2001 Daniel Eischen <deischen@FreeBSD.org>.
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 *
16 * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS''
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <signal.h>
33#include <pthread.h>
34#include <stdlib.h>
35#include <errno.h>
36
37#include "libc_private.h"
38
39/*
40 * Weak symbols: All libc internal usage of these functions should
41 * use the weak symbol versions (_pthread_XXX).  If libpthread is
42 * linked, it will override these functions with (non-weak) routines.
43 * The _pthread_XXX functions are provided solely for internal libc
44 * usage to avoid unwanted cancellation points and to differentiate
45 * between application locks and libc locks (threads holding the
46 * latter can't be allowed to exit/terminate).
47 */
48
49/* Define a null pthread structure just to satisfy _pthread_self. */
50struct pthread {
51};
52
53static struct pthread	main_thread;
54
55static int		stub_main(void);
56static void 		*stub_null(void);
57static struct pthread	*stub_self(void);
58static int		stub_zero(void);
59static int		stub_fail(void);
60static int		stub_true(void);
61static void		stub_exit(void);
62static int		stub_esrch(void);
63
64#define	PJT_DUAL_ENTRY(entry)	\
65	(pthread_func_t)entry, (pthread_func_t)entry
66
67pthread_func_entry_t __thr_jtable[PJT_MAX] = {
68	[PJT_ATFORK] =			{PJT_DUAL_ENTRY(stub_zero)},
69	[PJT_ATTR_DESTROY] =		{PJT_DUAL_ENTRY(stub_zero)},
70	[PJT_ATTR_GETDETACHSTATE] =	{PJT_DUAL_ENTRY(stub_zero)},
71	[PJT_ATTR_GETGUARDSIZE] =	{PJT_DUAL_ENTRY(stub_zero)},
72	[PJT_ATTR_GETINHERITSCHED] =	{PJT_DUAL_ENTRY(stub_zero)},
73	[PJT_ATTR_GETSCHEDPARAM] =	{PJT_DUAL_ENTRY(stub_zero)},
74	[PJT_ATTR_GETSCHEDPOLICY] =	{PJT_DUAL_ENTRY(stub_zero)},
75	[PJT_ATTR_GETSCOPE] =		{PJT_DUAL_ENTRY(stub_zero)},
76	[PJT_ATTR_GETSTACKADDR] =	{PJT_DUAL_ENTRY(stub_zero)},
77	[PJT_ATTR_GETSTACKSIZE] =	{PJT_DUAL_ENTRY(stub_zero)},
78	[PJT_ATTR_INIT] =		{PJT_DUAL_ENTRY(stub_zero)},
79	[PJT_ATTR_SETDETACHSTATE] =	{PJT_DUAL_ENTRY(stub_zero)},
80	[PJT_ATTR_SETGUARDSIZE] =	{PJT_DUAL_ENTRY(stub_zero)},
81	[PJT_ATTR_SETINHERITSCHED] =	{PJT_DUAL_ENTRY(stub_zero)},
82	[PJT_ATTR_SETSCHEDPARAM] =	{PJT_DUAL_ENTRY(stub_zero)},
83	[PJT_ATTR_SETSCHEDPOLICY] =	{PJT_DUAL_ENTRY(stub_zero)},
84	[PJT_ATTR_SETSCOPE] =		{PJT_DUAL_ENTRY(stub_zero)},
85	[PJT_ATTR_SETSTACKADDR] =	{PJT_DUAL_ENTRY(stub_zero)},
86	[PJT_ATTR_SETSTACKSIZE] =	{PJT_DUAL_ENTRY(stub_zero)},
87	[PJT_CANCEL] =			{PJT_DUAL_ENTRY(stub_zero)},
88	[PJT_CLEANUP_POP] =		{PJT_DUAL_ENTRY(stub_zero)},
89	[PJT_CLEANUP_PUSH] =		{PJT_DUAL_ENTRY(stub_zero)},
90	[PJT_COND_BROADCAST] =		{PJT_DUAL_ENTRY(stub_zero)},
91	[PJT_COND_DESTROY] =		{PJT_DUAL_ENTRY(stub_zero)},
92	[PJT_COND_INIT] =		{PJT_DUAL_ENTRY(stub_zero)},
93	[PJT_COND_SIGNAL] =		{PJT_DUAL_ENTRY(stub_zero)},
94	[PJT_COND_TIMEDWAIT] =		{PJT_DUAL_ENTRY(stub_zero)},
95	[PJT_COND_WAIT] =		{PJT_DUAL_ENTRY(stub_zero)},
96	[PJT_DETACH] =			{PJT_DUAL_ENTRY(stub_zero)},
97	[PJT_EQUAL] =			{PJT_DUAL_ENTRY(stub_true)},
98	[PJT_EXIT] =			{PJT_DUAL_ENTRY(stub_exit)},
99	[PJT_GETSPECIFIC] =		{PJT_DUAL_ENTRY(stub_null)},
100	[PJT_JOIN] =			{PJT_DUAL_ENTRY(stub_zero)},
101	[PJT_KEY_CREATE] =		{PJT_DUAL_ENTRY(stub_fail)},
102	[PJT_KEY_DELETE] =		{PJT_DUAL_ENTRY(stub_zero)},
103	[PJT_KILL] =			{PJT_DUAL_ENTRY(stub_zero)},
104	[PJT_MAIN_NP] =			{PJT_DUAL_ENTRY(stub_main)},
105	[PJT_MUTEXATTR_DESTROY] =	{PJT_DUAL_ENTRY(stub_zero)},
106	[PJT_MUTEXATTR_INIT] =		{PJT_DUAL_ENTRY(stub_zero)},
107	[PJT_MUTEXATTR_SETTYPE] =	{PJT_DUAL_ENTRY(stub_zero)},
108	[PJT_MUTEX_DESTROY] =		{PJT_DUAL_ENTRY(stub_zero)},
109	[PJT_MUTEX_INIT] =		{PJT_DUAL_ENTRY(stub_zero)},
110	[PJT_MUTEX_LOCK] =		{PJT_DUAL_ENTRY(stub_zero)},
111	[PJT_MUTEX_TRYLOCK] =		{PJT_DUAL_ENTRY(stub_zero)},
112	[PJT_MUTEX_UNLOCK] =		{PJT_DUAL_ENTRY(stub_zero)},
113	[PJT_ONCE] =			{PJT_DUAL_ENTRY(stub_fail)},
114	[PJT_RWLOCK_DESTROY] =		{PJT_DUAL_ENTRY(stub_zero)},
115	[PJT_RWLOCK_INIT] =		{PJT_DUAL_ENTRY(stub_zero)},
116	[PJT_RWLOCK_RDLOCK] =		{PJT_DUAL_ENTRY(stub_zero)},
117	[PJT_RWLOCK_TRYRDLOCK] =	{PJT_DUAL_ENTRY(stub_zero)},
118	[PJT_RWLOCK_TRYWRLOCK] =	{PJT_DUAL_ENTRY(stub_zero)},
119	[PJT_RWLOCK_UNLOCK] =		{PJT_DUAL_ENTRY(stub_zero)},
120	[PJT_RWLOCK_WRLOCK] =		{PJT_DUAL_ENTRY(stub_zero)},
121	[PJT_SELF] =			{PJT_DUAL_ENTRY(stub_self)},
122	[PJT_SETCANCELSTATE] =		{PJT_DUAL_ENTRY(stub_zero)},
123	[PJT_SETCANCELTYPE] =		{PJT_DUAL_ENTRY(stub_zero)},
124	[PJT_SETSPECIFIC] =		{PJT_DUAL_ENTRY(stub_zero)},
125	[PJT_SIGMASK] =			{PJT_DUAL_ENTRY(stub_zero)},
126	[PJT_TESTCANCEL] =		{PJT_DUAL_ENTRY(stub_zero)},
127	[PJT_CLEANUP_POP_IMP] =		{PJT_DUAL_ENTRY(stub_zero)},
128	[PJT_CLEANUP_PUSH_IMP] =	{PJT_DUAL_ENTRY(stub_zero)},
129	[PJT_CANCEL_ENTER] =		{PJT_DUAL_ENTRY(stub_zero)},
130	[PJT_CANCEL_LEAVE] =		{PJT_DUAL_ENTRY(stub_zero)},
131	[PJT_MUTEX_CONSISTENT] =	{PJT_DUAL_ENTRY(stub_zero)},
132	[PJT_MUTEXATTR_GETROBUST] =	{PJT_DUAL_ENTRY(stub_zero)},
133	[PJT_MUTEXATTR_SETROBUST] =	{PJT_DUAL_ENTRY(stub_zero)},
134	[PJT_GETTHREADID_NP] =		{PJT_DUAL_ENTRY(stub_zero)},
135	[PJT_ATTR_GET_NP] =		{PJT_DUAL_ENTRY(stub_esrch)},
136};
137
138/*
139 * Weak aliases for exported (pthread_*) and internal (_pthread_*) routines.
140 */
141#define	WEAK_REF(sym, alias)	__weak_reference(sym, alias)
142
143#define	FUNC_TYPE(name)		__CONCAT(name, _func_t)
144#define	FUNC_INT(name)		__CONCAT(name, _int)
145#define	FUNC_EXP(name)		__CONCAT(name, _exp)
146
147#define	STUB_FUNC(name, idx, ret)				\
148	static ret FUNC_EXP(name)(void) __used;			\
149	static ret FUNC_INT(name)(void) __used;			\
150	WEAK_REF(FUNC_EXP(name), name);				\
151	WEAK_REF(FUNC_INT(name), __CONCAT(_, name));		\
152	typedef ret (*FUNC_TYPE(name))(void);			\
153	static ret FUNC_EXP(name)(void)				\
154	{							\
155		FUNC_TYPE(name) func;				\
156		func = (FUNC_TYPE(name))__thr_jtable[idx][0];	\
157		return (func());				\
158	}							\
159	static ret FUNC_INT(name)(void)				\
160	{							\
161		FUNC_TYPE(name) func;				\
162		func = (FUNC_TYPE(name))__thr_jtable[idx][1];	\
163		return (func());				\
164	}
165
166#define	STUB_FUNC1(name, idx, ret, p0_type)			\
167	static ret FUNC_EXP(name)(p0_type) __used;		\
168	static ret FUNC_INT(name)(p0_type) __used;		\
169	WEAK_REF(FUNC_EXP(name), name);				\
170	WEAK_REF(FUNC_INT(name), __CONCAT(_, name));		\
171	typedef ret (*FUNC_TYPE(name))(p0_type);		\
172	static ret FUNC_EXP(name)(p0_type p0)			\
173	{							\
174		FUNC_TYPE(name) func;				\
175		func = (FUNC_TYPE(name))__thr_jtable[idx][0];	\
176		return (func(p0));				\
177	}							\
178	static ret FUNC_INT(name)(p0_type p0)			\
179	{							\
180		FUNC_TYPE(name) func;				\
181		func = (FUNC_TYPE(name))__thr_jtable[idx][1];	\
182		return (func(p0));				\
183	}
184
185#define	STUB_FUNC2(name, idx, ret, p0_type, p1_type)		\
186	static ret FUNC_EXP(name)(p0_type, p1_type) __used;	\
187	static ret FUNC_INT(name)(p0_type, p1_type) __used;	\
188	WEAK_REF(FUNC_EXP(name), name);				\
189	WEAK_REF(FUNC_INT(name), __CONCAT(_, name));		\
190	typedef ret (*FUNC_TYPE(name))(p0_type, p1_type);	\
191	static ret FUNC_EXP(name)(p0_type p0, p1_type p1)	\
192	{							\
193		FUNC_TYPE(name) func;				\
194		func = (FUNC_TYPE(name))__thr_jtable[idx][0];	\
195		return (func(p0, p1));				\
196	}							\
197	static ret FUNC_INT(name)(p0_type p0, p1_type p1)	\
198	{							\
199		FUNC_TYPE(name) func;				\
200		func = (FUNC_TYPE(name))__thr_jtable[idx][1];	\
201		return (func(p0, p1));				\
202	}
203
204#define	STUB_FUNC3(name, idx, ret, p0_type, p1_type, p2_type)	\
205	static ret FUNC_EXP(name)(p0_type, p1_type, p2_type) __used; \
206	static ret FUNC_INT(name)(p0_type, p1_type, p2_type) __used; \
207	WEAK_REF(FUNC_EXP(name), name);				\
208	WEAK_REF(FUNC_INT(name), __CONCAT(_, name));		\
209	typedef ret (*FUNC_TYPE(name))(p0_type, p1_type, p2_type); \
210	static ret FUNC_EXP(name)(p0_type p0, p1_type p1, p2_type p2) \
211	{							\
212		FUNC_TYPE(name) func;				\
213		func = (FUNC_TYPE(name))__thr_jtable[idx][0];	\
214		return (func(p0, p1, p2));			\
215	}							\
216	static ret FUNC_INT(name)(p0_type p0, p1_type p1, p2_type p2) \
217	{							\
218		FUNC_TYPE(name) func;				\
219		func = (FUNC_TYPE(name))__thr_jtable[idx][1];	\
220		return (func(p0, p1, p2));			\
221	}
222
223STUB_FUNC1(pthread_cond_broadcast, PJT_COND_BROADCAST, int, void *)
224STUB_FUNC1(pthread_cond_destroy, PJT_COND_DESTROY, int, void *)
225STUB_FUNC2(pthread_cond_init,	PJT_COND_INIT, int, void *, void *)
226STUB_FUNC1(pthread_cond_signal,	PJT_COND_SIGNAL, int, void *)
227STUB_FUNC2(pthread_cond_wait,	PJT_COND_WAIT, int, void *, void *)
228STUB_FUNC1(pthread_getspecific,	PJT_GETSPECIFIC, void *, pthread_key_t)
229STUB_FUNC2(pthread_key_create,	PJT_KEY_CREATE, int, void *, void *)
230STUB_FUNC1(pthread_key_delete,	PJT_KEY_DELETE, int, pthread_key_t)
231STUB_FUNC(pthread_main_np,	PJT_MAIN_NP, int)
232STUB_FUNC1(pthread_mutex_destroy, PJT_MUTEX_DESTROY, int, void *)
233STUB_FUNC2(pthread_mutex_init,	PJT_MUTEX_INIT, int, void *, void *)
234STUB_FUNC1(pthread_mutex_lock,	PJT_MUTEX_LOCK, int, void *)
235STUB_FUNC1(pthread_mutex_trylock, PJT_MUTEX_TRYLOCK, int, void *)
236STUB_FUNC1(pthread_mutex_unlock, PJT_MUTEX_UNLOCK, int, void *)
237STUB_FUNC1(pthread_mutex_consistent, PJT_MUTEX_CONSISTENT, int, void *)
238STUB_FUNC1(pthread_mutexattr_destroy, PJT_MUTEXATTR_DESTROY, int, void *)
239STUB_FUNC1(pthread_mutexattr_init, PJT_MUTEXATTR_INIT, int, void *)
240STUB_FUNC2(pthread_mutexattr_settype, PJT_MUTEXATTR_SETTYPE, int, void *, int)
241STUB_FUNC2(pthread_mutexattr_getrobust, PJT_MUTEXATTR_GETROBUST, int, void *,
242    int *)
243STUB_FUNC2(pthread_mutexattr_setrobust, PJT_MUTEXATTR_SETROBUST, int, void *,
244    int)
245STUB_FUNC2(pthread_once, 	PJT_ONCE, int, void *, void *)
246STUB_FUNC1(pthread_rwlock_destroy, PJT_RWLOCK_DESTROY, int, void *)
247STUB_FUNC2(pthread_rwlock_init,	PJT_RWLOCK_INIT, int, void *, void *)
248STUB_FUNC1(pthread_rwlock_rdlock, PJT_RWLOCK_RDLOCK, int, void *)
249STUB_FUNC1(pthread_rwlock_tryrdlock, PJT_RWLOCK_TRYRDLOCK, int, void *)
250STUB_FUNC1(pthread_rwlock_trywrlock, PJT_RWLOCK_TRYWRLOCK, int, void *)
251STUB_FUNC1(pthread_rwlock_unlock, PJT_RWLOCK_UNLOCK, int, void *)
252STUB_FUNC1(pthread_rwlock_wrlock, PJT_RWLOCK_WRLOCK, int, void *)
253STUB_FUNC(pthread_self,		PJT_SELF, pthread_t)
254STUB_FUNC(pthread_getthreadid_np, PJT_GETTHREADID_NP, int)
255STUB_FUNC2(pthread_setspecific, PJT_SETSPECIFIC, int, pthread_key_t, void *)
256STUB_FUNC3(pthread_sigmask, PJT_SIGMASK, int, int, void *, void *)
257STUB_FUNC3(pthread_atfork, PJT_ATFORK, int, void *, void *, void*)
258STUB_FUNC1(pthread_attr_destroy, PJT_ATTR_DESTROY, int, void *);
259STUB_FUNC2(pthread_attr_getdetachstate, PJT_ATTR_GETDETACHSTATE, int, void *, void *)
260STUB_FUNC2(pthread_attr_getguardsize, PJT_ATTR_GETGUARDSIZE, int, void *, void *)
261STUB_FUNC2(pthread_attr_getstackaddr, PJT_ATTR_GETSTACKADDR, int, void *, void *)
262STUB_FUNC2(pthread_attr_getstacksize, PJT_ATTR_GETSTACKSIZE, int, void *, void *)
263STUB_FUNC2(pthread_attr_getinheritsched, PJT_ATTR_GETINHERITSCHED, int, void *, void *)
264STUB_FUNC2(pthread_attr_getschedparam, PJT_ATTR_GETSCHEDPARAM, int, void *, void *)
265STUB_FUNC2(pthread_attr_getschedpolicy, PJT_ATTR_GETSCHEDPOLICY, int, void *, void *)
266STUB_FUNC2(pthread_attr_getscope, PJT_ATTR_GETSCOPE, int, void *, void *)
267STUB_FUNC1(pthread_attr_init, PJT_ATTR_INIT, int, void *)
268STUB_FUNC2(pthread_attr_setdetachstate, PJT_ATTR_SETDETACHSTATE, int, void *, int)
269STUB_FUNC2(pthread_attr_setguardsize, PJT_ATTR_SETGUARDSIZE, int, void *, size_t)
270STUB_FUNC2(pthread_attr_setstackaddr, PJT_ATTR_SETSTACKADDR, int, void *, void *)
271STUB_FUNC2(pthread_attr_setstacksize, PJT_ATTR_SETSTACKSIZE, int, void *, size_t)
272STUB_FUNC2(pthread_attr_setinheritsched, PJT_ATTR_SETINHERITSCHED, int, void *, int)
273STUB_FUNC2(pthread_attr_setschedparam, PJT_ATTR_SETSCHEDPARAM, int, void *, void *)
274STUB_FUNC2(pthread_attr_setschedpolicy, PJT_ATTR_SETSCHEDPOLICY, int, void *, int)
275STUB_FUNC2(pthread_attr_setscope, PJT_ATTR_SETSCOPE, int, void *, int)
276STUB_FUNC1(pthread_cancel, PJT_CANCEL, int, void *)
277STUB_FUNC1(pthread_cleanup_pop, PJT_CLEANUP_POP, int, int)
278STUB_FUNC2(pthread_cleanup_push, PJT_CLEANUP_PUSH, void, void *, void *)
279STUB_FUNC3(pthread_cond_timedwait, PJT_COND_TIMEDWAIT, int, void *, void *, void *)
280STUB_FUNC1(pthread_detach, PJT_DETACH, int, void *)
281STUB_FUNC2(pthread_equal, PJT_EQUAL, int, void *, void *)
282STUB_FUNC1(pthread_exit, PJT_EXIT, void, void *)
283STUB_FUNC2(pthread_join, PJT_JOIN, int, void *, void *)
284STUB_FUNC2(pthread_kill, PJT_KILL, int, void *, int)
285STUB_FUNC2(pthread_setcancelstate, PJT_SETCANCELSTATE, int, int, void *)
286STUB_FUNC2(pthread_setcanceltype, PJT_SETCANCELTYPE, int, int, void *)
287STUB_FUNC(pthread_testcancel, PJT_TESTCANCEL, void)
288STUB_FUNC1(__pthread_cleanup_pop_imp, PJT_CLEANUP_POP_IMP, void, int)
289STUB_FUNC3(__pthread_cleanup_push_imp, PJT_CLEANUP_PUSH_IMP, void, void *,
290    void *, void *)
291STUB_FUNC1(_pthread_cancel_enter, PJT_CANCEL_ENTER, void, int)
292STUB_FUNC1(_pthread_cancel_leave, PJT_CANCEL_LEAVE, void, int)
293STUB_FUNC2(pthread_attr_get_np, PJT_ATTR_GET_NP, int, pthread_t, pthread_attr_t *)
294
295static int
296stub_zero(void)
297{
298	return (0);
299}
300
301static void *
302stub_null(void)
303{
304	return (NULL);
305}
306
307static struct pthread *
308stub_self(void)
309{
310	return (&main_thread);
311}
312
313static int
314stub_fail(void)
315{
316	return (ENOSYS);
317}
318
319static int
320stub_main(void)
321{
322	return (-1);
323}
324
325static int
326stub_true(void)
327{
328	return (1);
329}
330
331static void
332stub_exit(void)
333{
334	exit(0);
335}
336
337static int
338stub_esrch(void)
339{
340	return (ESRCH);
341}
342