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$
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
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 *
_thr_rtld_lock_create(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
_thr_rtld_lock_destroy(void * lock)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
_thr_rtld_rlock_acquire(void * lock)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
_thr_rtld_wlock_acquire(void * lock)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
_thr_rtld_lock_release(void * lock)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
167 static int
_thr_rtld_set_flag(int mask __unused)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
177 static int
_thr_rtld_clr_flag(int mask __unused)178 _thr_rtld_clr_flag(int mask __unused)
179 {
180 return (0);
181 }
182
183 void
_thr_rtld_init(void)184 _thr_rtld_init(void)
185 {
186 struct RtldLockInfo li;
187 struct pthread *curthread;
188 ucontext_t *uc;
189 long dummy = -1;
190 int uc_len;
191
192 curthread = _get_curthread();
193
194 /* force to resolve _umtx_op PLT */
195 _umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
196
197 /* force to resolve errno() PLT */
198 __error();
199
200 /* force to resolve memcpy PLT */
201 memcpy(&dummy, &dummy, sizeof(dummy));
202
203 mprotect(NULL, 0, 0);
204 _rtld_get_stack_prot();
205
206 li.lock_create = _thr_rtld_lock_create;
207 li.lock_destroy = _thr_rtld_lock_destroy;
208 li.rlock_acquire = _thr_rtld_rlock_acquire;
209 li.wlock_acquire = _thr_rtld_wlock_acquire;
210 li.lock_release = _thr_rtld_lock_release;
211 li.thread_set_flag = _thr_rtld_set_flag;
212 li.thread_clr_flag = _thr_rtld_clr_flag;
213 li.at_fork = NULL;
214
215 /*
216 * Preresolve the symbols needed for the fork interposer. We
217 * call _rtld_atfork_pre() and _rtld_atfork_post() with NULL
218 * argument to indicate that no actual locking inside the
219 * functions should happen. Neither rtld compat locks nor
220 * libthr rtld locks cannot work there:
221 * - compat locks do not handle the case of two locks taken
222 * in write mode (the signal mask for the thread is corrupted);
223 * - libthr locks would work, but locked rtld_bind_lock prevents
224 * symbol resolution for _rtld_atfork_post.
225 */
226 _rtld_atfork_pre(NULL);
227 _rtld_atfork_post(NULL);
228 _malloc_prefork();
229 _malloc_postfork();
230 syscall(SYS_getpid);
231
232 /* mask signals, also force to resolve __sys_sigprocmask PLT */
233 _thr_signal_block(curthread);
234 _rtld_thread_init(&li);
235 _thr_signal_unblock(curthread);
236
237 uc_len = __getcontextx_size();
238 uc = alloca(uc_len);
239 getcontext(uc);
240 __fillcontextx2((char *)uc);
241 }
242