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