1 /*
2 * Copyright (c) 2021-2024 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 "dfx_signal_local_handler.h"
17
18 #include <securec.h>
19 #include <csignal>
20 #include <sigchain.h>
21 #include <cstdint>
22 #include <cstdio>
23 #include <unistd.h>
24 #include <pthread.h>
25 #include <sched.h>
26 #include <sys/syscall.h>
27 #include <sys/mman.h>
28 #include <sys/prctl.h>
29 #include <sys/wait.h>
30 #include <linux/futex.h>
31 #include "dfx_allocator.h"
32 #include "dfx_crash_local_handler.h"
33 #include "dfx_cutil.h"
34 #include "dfx_log.h"
35
36 #ifdef LOG_DOMAIN
37 #undef LOG_DOMAIN
38 #define LOG_DOMAIN 0xD002D11
39 #endif
40
41 #ifdef LOG_TAG
42 #undef LOG_TAG
43 #define LOG_TAG "DfxSignalLocalHandler"
44 #endif
45
46 #define LOCAL_HANDLER_STACK_SIZE (128 * 1024) // 128K
47 constexpr uint32_t FAULTLOGGERD_UID = 1202;
48
49 static CrashFdFunc g_crashFdFn = nullptr;
50 static void *g_reservedChildStack = nullptr;
51 static struct ProcessDumpRequest g_request;
52 static pthread_mutex_t g_signalHandlerMutex = PTHREAD_MUTEX_INITIALIZER;
53
54 static int g_platformSignals[] = {
55 SIGABRT, SIGBUS, SIGILL, SIGSEGV,
56 };
57
ReserveChildThreadSignalStack(void)58 static void ReserveChildThreadSignalStack(void)
59 {
60 // reserve stack for fork
61 g_reservedChildStack = mmap(nullptr, LOCAL_HANDLER_STACK_SIZE, \
62 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, 1, 0);
63 if (g_reservedChildStack == MAP_FAILED) {
64 DFXLOGE("[%{public}d]: Failed to alloc memory for child stack.", __LINE__);
65 return;
66 }
67 g_reservedChildStack = static_cast<void *>(static_cast<uint8_t *>(g_reservedChildStack) +
68 LOCAL_HANDLER_STACK_SIZE - 1);
69 }
70
FutexWait(volatile void * ftx,int value)71 AT_UNUSED static void FutexWait(volatile void* ftx, int value)
72 {
73 syscall(__NR_futex, ftx, FUTEX_WAIT, value, NULL, NULL, 0);
74 }
75
DoCrashHandler(void * arg)76 static int DoCrashHandler(void* arg)
77 {
78 (void)arg;
79 RegisterAllocator();
80 if (g_crashFdFn == nullptr) {
81 CrashLocalHandler(&g_request);
82 } else {
83 int fd = g_crashFdFn(&g_request);
84 CrashLocalHandlerFd(fd, &g_request);
85 if (fd >= 0) {
86 close(fd);
87 }
88 }
89 UnregisterAllocator();
90 pthread_mutex_unlock(&g_signalHandlerMutex);
91 _exit(0);
92 return 0;
93 }
94
DFX_SignalLocalHandler(int sig,siginfo_t * si,void * context)95 void DFX_SignalLocalHandler(int sig, siginfo_t *si, void *context)
96 {
97 pthread_mutex_lock(&g_signalHandlerMutex);
98 (void)memset_s(&g_request, sizeof(g_request), 0, sizeof(g_request));
99 g_request.type = static_cast<ProcessDumpType>(sig);
100 g_request.tid = gettid();
101 g_request.pid = getpid();
102 g_request.uid = FAULTLOGGERD_UID;
103 g_request.timeStamp = GetTimeMilliseconds();
104 DFXLOGI("DFX_SignalLocalHandler :: sig(%{public}d), pid(%{public}d), tid(%{public}d).",
105 sig, g_request.pid, g_request.tid);
106
107 GetThreadNameByTid(g_request.tid, g_request.threadName, sizeof(g_request.threadName));
108 GetProcessName(g_request.processName, sizeof(g_request.processName));
109
110 int ret = memcpy_s(&(g_request.siginfo), sizeof(siginfo_t), si, sizeof(siginfo_t));
111 if (ret < 0) {
112 DFXLOGE("memcpy_s siginfo fail, ret=%{public}d", ret);
113 }
114 ret = memcpy_s(&(g_request.context), sizeof(ucontext_t), context, sizeof(ucontext_t));
115 if (ret < 0) {
116 DFXLOGE("memcpy_s context fail, ret=%{public}d", ret);
117 }
118 #if defined(__aarch64__) || defined(__loongarch_lp64)
119 DoCrashHandler(NULL);
120 #else
121 int pseudothreadTid = -1;
122 pid_t childTid = clone(DoCrashHandler, g_reservedChildStack, \
123 CLONE_THREAD | CLONE_SIGHAND | CLONE_VM | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID, \
124 &pseudothreadTid, NULL, NULL, &pseudothreadTid);
125 if (childTid == -1) {
126 DFXLOGE("Failed to create thread for crash local handler");
127 pthread_mutex_unlock(&g_signalHandlerMutex);
128 return;
129 }
130
131 FutexWait(&pseudothreadTid, -1);
132 FutexWait(&pseudothreadTid, childTid);
133
134 DFXLOGI("child thread(%{public}d) exit.", childTid);
135 _exit(0);
136 #endif
137 }
138
DFX_GetCrashFdFunc(CrashFdFunc fn)139 void DFX_GetCrashFdFunc(CrashFdFunc fn)
140 {
141 g_crashFdFn = fn;
142 }
143
DFX_InstallLocalSignalHandler(void)144 void DFX_InstallLocalSignalHandler(void)
145 {
146 ReserveChildThreadSignalStack();
147
148 sigset_t set;
149 sigemptyset(&set);
150 struct sigaction action;
151 (void)memset_s(&action, sizeof(action), 0, sizeof(action));
152 sigfillset(&action.sa_mask);
153 action.sa_sigaction = DFX_SignalLocalHandler;
154 action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
155
156 for (size_t i = 0; i < sizeof(g_platformSignals) / sizeof(g_platformSignals[0]); i++) {
157 int32_t sig = g_platformSignals[i];
158 remove_all_special_handler(sig);
159
160 sigaddset(&set, sig);
161 if (sigaction(sig, &action, nullptr) != 0) {
162 DFXLOGE("Failed to register signal(%{public}d)", sig);
163 }
164 }
165 sigprocmask(SIG_UNBLOCK, &set, nullptr);
166 }
167