1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <sigchain.h>
17 #include <locale.h>
18 #include <pthread.h>
19 #include <errno.h>
20 #include <threads.h>
21 #include <musl_log.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <pthread_impl.h>
25 #include "syscall.h"
26
27 extern int __libc_sigaction(int sig, const struct sigaction *restrict sa,
28 struct sigaction *restrict old);
29
30 #define SIG_CHAIN_KEY_VALUE_1 1
31 #define SIGNAL_CHAIN_SPECIAL_ACTION_MAX 3
32
33 #define SIGCHAIN_LOG_TAG "MUSL-SIGCHAIN"
34
35 #if (defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG))
36 #define SIGCHAIN_PRINT_ERROR(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_ERROR, \
37 MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
38 #define SIGCHAIN_PRINT_INFO(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_INFO, \
39 MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
40 #define SIGCHAIN_PRINT_DEBUG(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_DEBUG, \
41 MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
42 #define SIGCHAIN_LOG_FATAL(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_FATAL, \
43 MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
44 #else
45 #define SIGCHAIN_PRINT_ERROR(...)
46 #define SIGCHAIN_PRINT_INFO(...)
47 #define SIGCHAIN_PRINT_DEBUG(...)
48 #define SIGCHAIN_LOG_FATAL(...)
49 #endif
50
51 #define FREEZE_SIGNAL_35 (35)
52
53 #define SIGCHAIN_PRINT_FATAL(...) do { \
54 SIGCHAIN_LOG_FATAL(__VA_ARGS__); \
55 abort(); \
56 } while (0)
57
58 struct sc_signal_chain {
59 bool marked;
60 struct sigaction sig_action;
61 struct signal_chain_action sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX];
62 };
63
64 /* Signal chain set, from 0 to 63. */
65 static struct sc_signal_chain sig_chains[_NSIG - 1];
66 /* static thread Keyword */
67 static pthread_key_t g_sigchain_key;
68 /* This is once flag! */
69 static once_flag g_flag = ONCE_FLAG_INIT;
70
71 /**
72 * @brief Create the thread key
73 * @retval void
74 */
create_pthread_key(void)75 static void create_pthread_key(void)
76 {
77 SIGCHAIN_PRINT_INFO("%{public}s create the thread key!", __func__);
78 int rc = pthread_key_create(&g_sigchain_key, NULL);
79 if (rc != 0) {
80 SIGCHAIN_PRINT_FATAL("%{public}s failed to create sigchain pthread key, rc:%{public}d errno:%{public}d",
81 __func__, rc, errno);
82 }
83 }
84
85 /**
86 * @brief Get the key of the signal thread.
87 * @retval int32_t, the value of the sigchain key.
88 */
get_handling_signal_key()89 static pthread_key_t get_handling_signal_key()
90 {
91 return g_sigchain_key;
92 }
93
94 /**
95 * @brief Get the value of the sigchain key
96 * @retval bool, true if set the value of the key,or false.
97 */
get_handling_signal()98 static bool get_handling_signal()
99 {
100 void *result = pthread_getspecific(get_handling_signal_key());
101 return result == NULL ? false : true;
102 }
103
104 /**
105 * @brief Set the value of the sigchain key
106 * @param[in] value, the value of the sigchain key
107 * @retval void.
108 */
set_handling_signal(bool value)109 static void set_handling_signal(bool value)
110 {
111 pthread_setspecific(get_handling_signal_key(),
112 (void *)((uintptr_t)(value)));
113 }
114
115 /**
116 * @brief Set the mask of the system. Its prototype comes from pthread_sigmask.
117 * @param[in] how, the value of the mask operation .
118 * @param[in] set, the new value of the sigset.
119 * @param[in] old, the old value of the sigset.
120 */
sigchain_sigmask(int how,const sigset_t * restrict set,sigset_t * restrict old)121 static int sigchain_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
122 {
123 int ret;
124 if (set && (unsigned)how - SIG_BLOCK > 2U) return EINVAL;
125 ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG/8);
126 if (!ret && old) {
127 if (sizeof old->__bits[0] == 8) {
128 old->__bits[0] &= ~0x380000000ULL;
129 } else {
130 old->__bits[0] &= ~0x80000000UL;
131 old->__bits[1] &= ~0x3UL;
132 }
133 }
134 return ret;
135 }
136
137 /**
138 * @brief Judge whether the signal is marked
139 * @param[in] signo, the value of the signal.
140 * @retval true if the signal is marked, or false.
141 */
ismarked(int signo)142 static bool ismarked(int signo)
143 {
144 return sig_chains[signo - 1].marked;
145 }
146
147 /**
148 * @brief This is a callback function, which is registered to the kernel
149 * @param[in] signo, the value of the signal.
150 * @param[in] siginfo, the information of the signal.
151 * @param[in] ucontext_raw, the context of the signal.
152 * @retval void
153 */
signal_chain_handler(int signo,siginfo_t * siginfo,void * ucontext_raw)154 static void signal_chain_handler(int signo, siginfo_t* siginfo, void* ucontext_raw)
155 {
156 SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
157 /* First call special handler. */
158 /* If a process crashes, the sigchain'll call the corresponding handler */
159 if (!get_handling_signal()) {
160 int idx, validSigaction = 0;
161 for (idx = 0; idx < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; idx++) {
162 if (sig_chains[signo - 1].sca_special_actions[idx].sca_sigaction == NULL) {
163 continue;
164 }
165 validSigaction = 1;
166 /* The special handler might not return. */
167 bool noreturn = (sig_chains[signo - 1].sca_special_actions[idx].sca_flags &
168 SIGCHAIN_ALLOW_NORETURN);
169 sigset_t previous_mask;
170 sigchain_sigmask(SIG_SETMASK, &sig_chains[signo - 1].sca_special_actions[idx].sca_mask,
171 &previous_mask);
172
173 bool previous_value = get_handling_signal();
174 if (!noreturn) {
175 set_handling_signal(true);
176 }
177 int thread_list_lock_status = -1;
178 if (signo == FREEZE_SIGNAL_35) {
179 #ifdef a_ll
180 thread_list_lock_status = a_ll(&__thread_list_lock);
181 #else
182 thread_list_lock_status = __thread_list_lock;
183 #endif
184 }
185 /**
186 * modify style: move `thread_list_lock_status` from `if`
187 * internal branch to outer branch.
188 * void performance degradation
189 */
190 struct call_tl_lock *call_tl_lock_dump;
191 struct call_tl_lock call_tl_lock_stub = { -1 };
192 if (get_tl_lock_caller_count()) {
193 call_tl_lock_dump = get_tl_lock_caller_count();
194 } else {
195 call_tl_lock_dump = &call_tl_lock_stub;
196 }
197 SIGCHAIN_PRINT_ERROR("%{public}s call %{public}d rd sigchain action for signal: %{public}d"
198 " sca_sigaction=%{public}llx noreturn=%{public}d "
199 "FREEZE_signo_%{public}d thread_list_lock_status:%{public}d "
200 "tl_lock_count=%{public}d tl_lock_waiters=%{public}d "
201 "tl_lock_tid_fail=%{public}d tl_lock_count_tid=%{public}d "
202 "tl_lock_count_fail=%{public}d tl_lock_count_tid_sub=%{public}d "
203 "thread_list_lock_after_lock=%{public}d thread_list_lock_pre_unlock=%{public}d "
204 "thread_list_lock_pthread_exit=%{public}d thread_list_lock_tid_overlimit=%{public}d "
205 "tl_lock_unlock_count=%{public}d "
206 "__pthread_gettid_np_tl_lock=%{public}d "
207 "__pthread_exit_tl_lock=%{public}d "
208 "__pthread_create_tl_lock=%{public}d "
209 "__pthread_key_delete_tl_lock=%{public}d "
210 "__synccall_tl_lock=%{public}d "
211 "__membarrier_tl_lock=%{public}d "
212 "install_new_tls_tl_lock=%{public}d "
213 "set_syscall_hooks_tl_lock=%{public}d "
214 "set_syscall_hooks_linux_tl_lock=%{public}d "
215 "fork_tl_lock=%{public}d ",
216 __func__, idx, signo, (unsigned long long)sig_chains[signo - 1].sca_special_actions[idx].sca_sigaction,
217 noreturn, signo, thread_list_lock_status,
218 get_tl_lock_count(), get_tl_lock_waiters(), get_tl_lock_tid_fail(), get_tl_lock_count_tid(),
219 get_tl_lock_count_fail(), get_tl_lock_count_tid_sub(),
220 get_thread_list_lock_after_lock(), get_thread_list_lock_pre_unlock(),
221 get_thread_list_lock_pthread_exit(), get_thread_list_lock_tid_overlimit(),
222 call_tl_lock_dump->tl_lock_unlock_count,
223 call_tl_lock_dump->__pthread_gettid_np_tl_lock,
224 call_tl_lock_dump->__pthread_exit_tl_lock,
225 call_tl_lock_dump->__pthread_create_tl_lock,
226 call_tl_lock_dump->__pthread_key_delete_tl_lock,
227 call_tl_lock_dump->__synccall_tl_lock,
228 call_tl_lock_dump->__membarrier_tl_lock,
229 call_tl_lock_dump->install_new_tls_tl_lock,
230 call_tl_lock_dump->set_syscall_hooks_tl_lock,
231 call_tl_lock_dump->set_syscall_hooks_linux_tl_lock,
232 call_tl_lock_dump->fork_tl_lock);
233 if (sig_chains[signo - 1].sca_special_actions[idx].sca_sigaction(signo,
234 siginfo, ucontext_raw)) {
235 set_handling_signal(previous_value);
236 SIGCHAIN_PRINT_ERROR("%{public}s call %{public}d rd sigchain action for signal: %{public}d directly return", __func__, idx, signo);
237 return;
238 }
239
240 sigchain_sigmask(SIG_SETMASK, &previous_mask, NULL);
241 set_handling_signal(previous_value);
242 }
243 if (idx == SIGNAL_CHAIN_SPECIAL_ACTION_MAX && !validSigaction) {
244 SIGCHAIN_PRINT_ERROR("signal_chain_handler sca_sigaction all null for signal: %{public}d", signo);
245 }
246 }
247 /* Then Call the user's signal handler */
248 int sa_flags = sig_chains[signo - 1].sig_action.sa_flags;
249 ucontext_t* ucontext = (ucontext_t*)(ucontext_raw);
250
251 sigset_t mask;
252 sigorset(&mask, &ucontext->uc_sigmask, &sig_chains[signo - 1].sig_action.sa_mask);
253
254 if (!(sa_flags & SA_NODEFER)) {
255 sigaddset(&mask, signo);
256 }
257
258 sigchain_sigmask(SIG_SETMASK, &mask, NULL);
259
260 if ((sa_flags & SA_SIGINFO)) {
261 SIGCHAIN_PRINT_ERROR("%{public}s call usr sigaction for signal: %{public}d sig_action.sa_sigaction=%{public}llx",
262 __func__, signo, (unsigned long long)sig_chains[signo - 1].sig_action.sa_sigaction);
263 sig_chains[signo - 1].sig_action.sa_sigaction(signo, siginfo, ucontext_raw);
264 } else {
265 if (sig_chains[signo - 1].sig_action.sa_handler == SIG_IGN) {
266 SIGCHAIN_PRINT_ERROR("%{public}s SIG_IGN handler for signal: %{public}d", __func__, signo);
267 return;
268 } else if (sig_chains[signo - 1].sig_action.sa_handler == SIG_DFL) {
269 SIGCHAIN_PRINT_ERROR("%{public}s SIG_DFL handler for signal: %{public}d", __func__, signo);
270 remove_all_special_handler(signo);
271 if (__syscall(SYS_rt_tgsigqueueinfo, __syscall(SYS_getpid), __syscall(SYS_gettid), signo, siginfo) != 0) {
272 SIGCHAIN_PRINT_ERROR("Failed to rethrow sig(%{public}d), errno(%{public}d).", signo, errno);
273 } else {
274 SIGCHAIN_PRINT_ERROR("pid(%{public}d) rethrow sig(%{public}d) success.", __syscall(SYS_getpid), signo);
275 }
276 } else {
277 SIGCHAIN_PRINT_ERROR("%{public}s call usr sa_handler: %{public}llx for signal: %{public}d",
278 __func__, (unsigned long long)sig_chains[signo - 1].sig_action.sa_handler, signo);
279 sig_chains[signo - 1].sig_action.sa_handler(signo);
280 }
281 }
282
283 return;
284 }
285
286 /**
287 * @brief Register the signal chain with the kernel if needed
288 * @param[in] signo, the value of the signal.
289 * @retval void
290 */
sigchain_register(int signo)291 static void sigchain_register(int signo)
292 {
293 SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
294 struct sigaction signal_action = {};
295 sigfillset(&signal_action.sa_mask);
296 call_once(&g_flag, create_pthread_key);
297
298 signal_action.sa_sigaction = signal_chain_handler;
299 signal_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
300 __libc_sigaction(signo, &signal_action, &sig_chains[signo - 1].sig_action);
301 }
302
303 /**
304 * @brief Unregister the signal from sigchain, register the signal's user handler with the kernel if needed
305 * @param[in] signo, the value of the signal.
306 * @retval void
307 */
unregister_sigchain(int signo)308 static void unregister_sigchain(int signo)
309 {
310 SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
311 __libc_sigaction(signo, &sig_chains[signo - 1].sig_action, NULL);
312 sig_chains[signo - 1].marked = false;
313 }
314
315 /**
316 * @brief Mark the signal to the sigchain.
317 * @param[in] signo, the value of the signal.
318 * @retval void
319 */
mark_signal_to_sigchain(int signo)320 static void mark_signal_to_sigchain(int signo)
321 {
322 SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
323 if (!sig_chains[signo - 1].marked) {
324 sigchain_register(signo);
325 sig_chains[signo - 1].marked = true;
326 }
327 }
328
329 /**
330 * @brief Set the action of the signal.
331 * @param[in] signo, the value of the signal.
332 * @param[in] new_sa, the new action of the signal.
333 * @retval void
334 */
setaction(int signo,const struct sigaction * restrict new_sa)335 static void setaction(int signo, const struct sigaction *restrict new_sa)
336 {
337 SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
338 sig_chains[signo - 1].sig_action = *new_sa;
339 }
340
341 /**
342 * @brief Get the action of the signal.
343 * @param[in] signo, the value of the signal.
344 * @retval The current action of the signal
345 */
getaction(int signo)346 static struct sigaction getaction(int signo)
347 {
348 SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
349 return sig_chains[signo - 1].sig_action;
350 }
351
352 /**
353 * @brief Add the special handler to the sigchain.
354 * @param[in] signo, the value of the signal.
355 * @param[in] sa, the action with special handler.
356 * @retval void
357 */
add_special_handler(int signo,struct signal_chain_action * sa)358 static void add_special_handler(int signo, struct signal_chain_action* sa)
359 {
360 SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
361 for (int i = 0; i < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; i++) {
362 if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction == NULL) {
363 sig_chains[signo - 1].sca_special_actions[i] = *sa;
364 SIGCHAIN_PRINT_INFO("%{public}s signo %{public}d is registered with special handler!", __func__, signo);
365 return;
366 }
367 }
368
369 SIGCHAIN_PRINT_FATAL("Add too many the special handlers!");
370 }
371
372 /**
373 * @brief Remove the special handler from the sigchain.
374 * @param[in] signo, the value of the signal.
375 * @param[in] fn, the special handler of the signal.
376 * @retval void
377 */
rm_special_handler(int signo,bool (* fn)(int,siginfo_t *,void *))378 static void rm_special_handler(int signo, bool (*fn)(int, siginfo_t*, void*))
379 {
380 SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
381 int len = SIGNAL_CHAIN_SPECIAL_ACTION_MAX;
382 for (int i = 0; i < len; i++) {
383 if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction == fn) {
384 sig_chains[signo - 1].sca_special_actions[i].sca_sigaction = NULL;
385 int count = 0;
386 for (int k = 0; k < len; k++) {
387 if (sig_chains[signo - 1].sca_special_actions[k].sca_sigaction == NULL) {
388 count++;
389 }
390 }
391 if (count == len) {
392 unregister_sigchain(signo);
393 }
394 return;
395 }
396 }
397
398 SIGCHAIN_PRINT_FATAL("%{public}s failed to remove the special handler!. signo: %{public}d",
399 __func__, signo);
400 }
401
402 /**
403 * @brief This is an external interface,
404 * Mark the signal to sigchain ,add the special handler to the sigchain.
405 * @param[in] signo, the value of the signal.
406 * @param[in] sa, the action with special handler.
407 * @retval void
408 */
add_special_signal_handler(int signo,struct signal_chain_action * sa)409 void add_special_signal_handler(int signo, struct signal_chain_action* sa)
410 {
411 SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
412 if (signo <= 0 || signo >= _NSIG) {
413 SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
414 return;
415 }
416
417 // Add the special hander to the sigchain
418 add_special_handler(signo, sa);
419 mark_signal_to_sigchain(signo);
420 }
421
422 /**
423 * @brief This is an external interface, remove the special handler from the sigchain.
424 * @param[in] signo, the value of the signal.
425 * @param[in] fn, the special handler of the signal.
426 * @retval void
427 */
remove_special_signal_handler(int signo,bool (* fn)(int,siginfo_t *,void *))428 void remove_special_signal_handler(int signo, bool (*fn)(int, siginfo_t*, void*))
429 {
430 SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
431 if (signo <= 0 || signo >= _NSIG) {
432 SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
433 return;
434 }
435
436 if (ismarked(signo)) {
437 // remove the special handler from the sigchain.
438 rm_special_handler(signo, fn);
439 }
440 }
441
442 /**
443 * @brief This is an external interface, remove all special handler from the sigchain.
444 * @param[in] signo, the value of the signal.
445 * @retval void
446 */
remove_all_special_handler(int signo)447 void remove_all_special_handler(int signo)
448 {
449 SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
450 if (signo <= 0 || signo >= _NSIG) {
451 SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
452 return;
453 }
454
455 if (ismarked(signo)) {
456 // remove all special handler from the sigchain.
457 for (int i = 0; i < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; i++) {
458 sig_chains[signo - 1].sca_special_actions[i].sca_sigaction = NULL;
459 }
460 unregister_sigchain(signo);
461 }
462 }
463
464 /**
465 * @brief This is an external interface, add the special handler at the last of sigchain chains.
466 * @param[in] signo, the value of the signal.
467 * @param[in] sa, the action with special handler.
468 * @retval void
469 */
add_special_handler_at_last(int signo,struct signal_chain_action * sa)470 void add_special_handler_at_last(int signo, struct signal_chain_action* sa)
471 {
472 SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
473 if (signo <= 0 || signo >= _NSIG) {
474 SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
475 return;
476 }
477
478 if (sig_chains[signo - 1].sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX - 1].sca_sigaction == NULL) {
479 sig_chains[signo - 1].sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX - 1] = *sa;
480 mark_signal_to_sigchain(signo);
481 return;
482 }
483
484 SIGCHAIN_PRINT_FATAL("Add too many the special handlers at last!");
485 }
486
487 /**
488 * @brief Intercept the signal and sigaction.
489 * @param[in] signo, the value of the signal.
490 * @param[in] sa, the new action with the signal handler.
491 * @param[out] old, the old action with the signal handler.
492 * @retval true if the signal if intercepted, or false.
493 */
intercept_sigaction(int signo,const struct sigaction * restrict sa,struct sigaction * restrict old)494 bool intercept_sigaction(int signo, const struct sigaction *restrict sa,
495 struct sigaction *restrict old)
496 {
497 SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
498 if (signo <= 0 || signo >= _NSIG) {
499 SIGCHAIN_PRINT_ERROR("%{public}s Invalid signal %{public}d", __func__, signo);
500 return false;
501 }
502
503 if (ismarked(signo)) {
504 struct sigaction saved_action = getaction(signo);
505
506 if (sa != NULL) {
507 setaction(signo, sa);
508 }
509 if (old != NULL) {
510 *old = saved_action;
511 }
512 return true;
513 }
514
515 return false;
516 }
517
518 /**
519 * @brief Intercept the pthread_sigmask.
520 * @param[in] how, the value of the mask operation .
521 * @param[out] set, the value of the sigset.
522 * @retval void.
523 */
intercept_pthread_sigmask(int how,sigset_t * restrict set)524 void intercept_pthread_sigmask(int how, sigset_t *restrict set)
525 {
526 SIGCHAIN_PRINT_DEBUG("%{public}s how: %{public}d", __func__, how);
527 // Forward directly to the system mask When this sigchain is handling a signal.
528 if (get_handling_signal()) {
529 return;
530 }
531
532 sigset_t tmpset;
533 if (set != NULL) {
534 tmpset = *set;
535 if (how == SIG_BLOCK || how == SIG_SETMASK) {
536 for (int i = 1; i < _NSIG; ++i) {
537 if (ismarked(i) && sigismember(&tmpset, i)) {
538 sigdelset(&tmpset, i);
539 }
540 }
541 }
542 *set = tmpset;
543 }
544
545 return;
546 }
547