xref: /freebsd/lib/libthr/thread/thr_rtld.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006, David Xu <davidxu@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 unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
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
45 extern int errno;
46 
47 static int	_thr_rtld_clr_flag(int);
48 static void	*_thr_rtld_lock_create(void);
49 static void	_thr_rtld_lock_destroy(void *);
50 static void	_thr_rtld_lock_release(void *);
51 static void	_thr_rtld_rlock_acquire(void *);
52 static int	_thr_rtld_set_flag(int);
53 static void	_thr_rtld_wlock_acquire(void *);
54 
55 struct rtld_lock {
56 	struct	urwlock	lock;
57 	char		_pad[CACHE_LINE_SIZE - sizeof(struct urwlock)];
58 };
59 
60 static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE);
61 static int busy_places;
62 
63 static 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 
85 static 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 
111 static 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 
129 static 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 
146 static 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 (__predict_false(_thr_after_fork)) {
160 		/*
161 		 * After fork, only this thread is running, there is no
162 		 * waiters.  Keeping waiters recorded in rwlock breaks
163 		 * wake logic.
164 		 */
165 		atomic_clear_int(&l->lock.rw_state,
166 		    URWLOCK_WRITE_WAITERS | URWLOCK_READ_WAITERS);
167 		l->lock.rw_blocked_readers = 0;
168 		l->lock.rw_blocked_writers = 0;
169 	}
170 	if (_thr_rwlock_unlock(&l->lock) == 0) {
171 		if ((state & URWLOCK_WRITE_OWNER) == 0)
172 			curthread->rdlock_count--;
173 		THR_CRITICAL_LEAVE(curthread);
174 	}
175 	RESTORE_ERRNO();
176 }
177 
178 static int
179 _thr_rtld_set_flag(int mask __unused)
180 {
181 	/*
182 	 * The caller's code in rtld-elf is broken, it is not signal safe,
183 	 * just return zero to fool it.
184 	 */
185 	return (0);
186 }
187 
188 static int
189 _thr_rtld_clr_flag(int mask __unused)
190 {
191 	return (0);
192 }
193 
194 /*
195  * ABI bug workaround: This symbol must be present for rtld to accept
196  * RTLI_VERSION from RtldLockInfo
197  */
198 extern char _pli_rtli_version;
199 char _pli_rtli_version;
200 
201 static char *
202 _thr_dlerror_loc(void)
203 {
204 	struct pthread *curthread;
205 
206 	curthread = _get_curthread();
207 	return (curthread->dlerror_msg);
208 }
209 
210 static int *
211 _thr_dlerror_seen(void)
212 {
213 	struct pthread *curthread;
214 
215 	curthread = _get_curthread();
216 	return (&curthread->dlerror_seen);
217 }
218 
219 void
220 _thr_rtld_init(void)
221 {
222 	struct RtldLockInfo	li;
223 	struct pthread		*curthread;
224 	ucontext_t *uc;
225 	long dummy = -1;
226 	int uc_len;
227 
228 	curthread = _get_curthread();
229 
230 	/* force to resolve _umtx_op PLT */
231 	_umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
232 
233 	/* force to resolve errno() PLT */
234 	__error();
235 
236 	/* force to resolve memcpy PLT */
237 	memcpy(&dummy, &dummy, sizeof(dummy));
238 
239 	mprotect(NULL, 0, 0);
240 	_rtld_get_stack_prot();
241 	thr_wake(-1);
242 
243 	li.rtli_version = RTLI_VERSION;
244 	li.lock_create  = _thr_rtld_lock_create;
245 	li.lock_destroy = _thr_rtld_lock_destroy;
246 	li.rlock_acquire = _thr_rtld_rlock_acquire;
247 	li.wlock_acquire = _thr_rtld_wlock_acquire;
248 	li.lock_release  = _thr_rtld_lock_release;
249 	li.thread_set_flag = _thr_rtld_set_flag;
250 	li.thread_clr_flag = _thr_rtld_clr_flag;
251 	li.at_fork = NULL;
252 	li.dlerror_loc = _thr_dlerror_loc;
253 	li.dlerror_loc_sz = sizeof(curthread->dlerror_msg);
254 	li.dlerror_seen = _thr_dlerror_seen;
255 
256 	/*
257 	 * Preresolve the symbols needed for the fork interposer.  We
258 	 * call _rtld_atfork_pre() and _rtld_atfork_post() with NULL
259 	 * argument to indicate that no actual locking inside the
260 	 * functions should happen.  Neither rtld compat locks nor
261 	 * libthr rtld locks cannot work there:
262 	 * - compat locks do not handle the case of two locks taken
263 	 *   in write mode (the signal mask for the thread is corrupted);
264 	 * - libthr locks would work, but locked rtld_bind_lock prevents
265 	 *   symbol resolution for _rtld_atfork_post.
266 	 */
267 	_rtld_atfork_pre(NULL);
268 	_rtld_atfork_post(NULL);
269 	_malloc_prefork();
270 	_malloc_postfork();
271 	getpid();
272 	syscall(SYS_getpid);
273 
274 	/* mask signals, also force to resolve __sys_sigprocmask PLT */
275 	_thr_signal_block(curthread);
276 	_rtld_thread_init(&li);
277 	_thr_signal_unblock(curthread);
278 	_thr_signal_block_check_fast();
279 	_thr_signal_block_setup(curthread);
280 
281 	uc_len = __getcontextx_size();
282 	uc = alloca(uc_len);
283 	getcontext(uc);
284 	__fillcontextx2((char *)uc);
285 }
286