thr_rtld.c revision 267200
1/*
2 * Copyright (c) 2006, David Xu <davidxu@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 unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/10/lib/libthr/thread/thr_rtld.c 267200 2014-06-07 02:45:24Z kib $
27 *
28 */
29
30 /*
31  * A lockless rwlock for rtld.
32  */
33#include <sys/cdefs.h>
34#include <sys/mman.h>
35#include <sys/syscall.h>
36#include <link.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include "libc_private.h"
41#include "rtld_lock.h"
42#include "thr_private.h"
43
44#undef errno
45extern int errno;
46
47static int	_thr_rtld_clr_flag(int);
48static void	*_thr_rtld_lock_create(void);
49static void	_thr_rtld_lock_destroy(void *);
50static void	_thr_rtld_lock_release(void *);
51static void	_thr_rtld_rlock_acquire(void *);
52static int	_thr_rtld_set_flag(int);
53static void	_thr_rtld_wlock_acquire(void *);
54
55struct rtld_lock {
56	struct	urwlock	lock;
57	char		_pad[CACHE_LINE_SIZE - sizeof(struct urwlock)];
58};
59
60static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE);
61static int busy_places;
62
63static void *
64_thr_rtld_lock_create(void)
65{
66	int locki;
67	struct rtld_lock *l;
68	static const char fail[] = "_thr_rtld_lock_create failed\n";
69
70	for (locki = 0; locki < MAX_RTLD_LOCKS; locki++) {
71		if ((busy_places & (1 << locki)) == 0)
72			break;
73	}
74	if (locki == MAX_RTLD_LOCKS) {
75		write(2, fail, sizeof(fail) - 1);
76		return (NULL);
77	}
78	busy_places |= (1 << locki);
79
80	l = &lock_place[locki];
81	l->lock.rw_flags = URWLOCK_PREFER_READER;
82	return (l);
83}
84
85static void
86_thr_rtld_lock_destroy(void *lock)
87{
88	int locki;
89	size_t i;
90
91	locki = (struct rtld_lock *)lock - &lock_place[0];
92	for (i = 0; i < sizeof(struct rtld_lock); ++i)
93		((char *)lock)[i] = 0;
94	busy_places &= ~(1 << locki);
95}
96
97#define SAVE_ERRNO()	{			\
98	if (curthread != _thr_initial)		\
99		errsave = curthread->error;	\
100	else					\
101		errsave = errno;		\
102}
103
104#define RESTORE_ERRNO()	{ 			\
105	if (curthread != _thr_initial)  	\
106		curthread->error = errsave;	\
107	else					\
108		errno = errsave;		\
109}
110
111static void
112_thr_rtld_rlock_acquire(void *lock)
113{
114	struct pthread		*curthread;
115	struct rtld_lock	*l;
116	int			errsave;
117
118	curthread = _get_curthread();
119	SAVE_ERRNO();
120	l = (struct rtld_lock *)lock;
121
122	THR_CRITICAL_ENTER(curthread);
123	while (_thr_rwlock_rdlock(&l->lock, 0, NULL) != 0)
124		;
125	curthread->rdlock_count++;
126	RESTORE_ERRNO();
127}
128
129static void
130_thr_rtld_wlock_acquire(void *lock)
131{
132	struct pthread		*curthread;
133	struct rtld_lock	*l;
134	int			errsave;
135
136	curthread = _get_curthread();
137	SAVE_ERRNO();
138	l = (struct rtld_lock *)lock;
139
140	THR_CRITICAL_ENTER(curthread);
141	while (_thr_rwlock_wrlock(&l->lock, NULL) != 0)
142		;
143	RESTORE_ERRNO();
144}
145
146static void
147_thr_rtld_lock_release(void *lock)
148{
149	struct pthread		*curthread;
150	struct rtld_lock	*l;
151	int32_t			state;
152	int			errsave;
153
154	curthread = _get_curthread();
155	SAVE_ERRNO();
156	l = (struct rtld_lock *)lock;
157
158	state = l->lock.rw_state;
159	if (_thr_rwlock_unlock(&l->lock) == 0) {
160		if ((state & URWLOCK_WRITE_OWNER) == 0)
161			curthread->rdlock_count--;
162		THR_CRITICAL_LEAVE(curthread);
163	}
164	RESTORE_ERRNO();
165}
166
167static int
168_thr_rtld_set_flag(int mask __unused)
169{
170	/*
171	 * The caller's code in rtld-elf is broken, it is not signal safe,
172	 * just return zero to fool it.
173	 */
174	return (0);
175}
176
177static int
178_thr_rtld_clr_flag(int mask __unused)
179{
180	return (0);
181}
182
183void
184_thr_rtld_init(void)
185{
186	struct RtldLockInfo	li;
187	struct pthread		*curthread;
188	long dummy = -1;
189
190	curthread = _get_curthread();
191
192	/* force to resolve _umtx_op PLT */
193	_umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
194
195	/* force to resolve errno() PLT */
196	__error();
197
198	/* force to resolve memcpy PLT */
199	memcpy(&dummy, &dummy, sizeof(dummy));
200
201	mprotect(NULL, 0, 0);
202	_rtld_get_stack_prot();
203
204	li.lock_create  = _thr_rtld_lock_create;
205	li.lock_destroy = _thr_rtld_lock_destroy;
206	li.rlock_acquire = _thr_rtld_rlock_acquire;
207	li.wlock_acquire = _thr_rtld_wlock_acquire;
208	li.lock_release  = _thr_rtld_lock_release;
209	li.thread_set_flag = _thr_rtld_set_flag;
210	li.thread_clr_flag = _thr_rtld_clr_flag;
211	li.at_fork = NULL;
212
213	/*
214	 * Preresolve the symbols needed for the fork interposer.  We
215	 * call _rtld_atfork_pre() and _rtld_atfork_post() with NULL
216	 * argument to indicate that no actual locking inside the
217	 * functions should happen.  Neither rtld compat locks nor
218	 * libthr rtld locks cannot work there:
219	 * - compat locks do not handle the case of two locks taken
220	 *   in write mode (the signal mask for the thread is corrupted);
221	 * - libthr locks would work, but locked rtld_bind_lock prevents
222	 *   symbol resolution for _rtld_atfork_post.
223	 */
224	_rtld_atfork_pre(NULL);
225	_rtld_atfork_post(NULL);
226	_malloc_prefork();
227	_malloc_postfork();
228	syscall(SYS_getpid);
229
230	/* mask signals, also force to resolve __sys_sigprocmask PLT */
231	_thr_signal_block(curthread);
232	_rtld_thread_init(&li);
233	_thr_signal_unblock(curthread);
234}
235