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 
48 static CrashFdFunc g_crashFdFn = nullptr;
49 static void *g_reservedChildStack = nullptr;
50 static struct ProcessDumpRequest g_request;
51 static pthread_mutex_t g_signalHandlerMutex = PTHREAD_MUTEX_INITIALIZER;
52 
53 static int g_platformSignals[] = {
54     SIGABRT, SIGBUS, SIGILL, SIGSEGV,
55 };
56 
ReserveChildThreadSignalStack(void)57 static void ReserveChildThreadSignalStack(void)
58 {
59     // reserve stack for fork
60     g_reservedChildStack = mmap(nullptr, LOCAL_HANDLER_STACK_SIZE, \
61         PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, 1, 0);
62     if (g_reservedChildStack == MAP_FAILED) {
63         DFXLOGE("[%{public}d]: Failed to alloc memory for child stack.", __LINE__);
64         return;
65     }
66     g_reservedChildStack = static_cast<void *>(static_cast<uint8_t *>(g_reservedChildStack) +
67         LOCAL_HANDLER_STACK_SIZE - 1);
68 }
69 
FutexWait(volatile void* ftx, int value)70 AT_UNUSED static void FutexWait(volatile void* ftx, int value)
71 {
72     syscall(__NR_futex, ftx, FUTEX_WAIT, value, NULL, NULL, 0);
73 }
74 
DoCrashHandler(void* arg)75 static int DoCrashHandler(void* arg)
76 {
77     (void)arg;
78     RegisterAllocator();
79     if (g_crashFdFn == nullptr) {
80         CrashLocalHandler(&g_request);
81     } else {
82         int fd = g_crashFdFn();
83         CrashLocalHandlerFd(fd, &g_request);
84         if (fd >= 0) {
85             close(fd);
86         }
87     }
88     UnregisterAllocator();
89     pthread_mutex_unlock(&g_signalHandlerMutex);
90     syscall(__NR_exit, 0);
91     return 0;
92 }
93 
DFX_SignalLocalHandler(int sig, siginfo_t *si, void *context)94 void DFX_SignalLocalHandler(int sig, siginfo_t *si, void *context)
95 {
96     pthread_mutex_lock(&g_signalHandlerMutex);
97     (void)memset_s(&g_request, sizeof(g_request), 0, sizeof(g_request));
98     g_request.type = static_cast<ProcessDumpType>(sig);
99     g_request.tid = gettid();
100     g_request.pid = getpid();
101     g_request.uid = getuid();
102     g_request.timeStamp = GetTimeMilliseconds();
103     DFXLOGI("DFX_SignalLocalHandler :: sig(%{public}d), pid(%{public}d), tid(%{public}d).",
104         sig, g_request.pid, g_request.tid);
105 
106     GetThreadNameByTid(g_request.tid, g_request.threadName, sizeof(g_request.threadName));
107     GetProcessName(g_request.processName, sizeof(g_request.processName));
108 
109     int ret = memcpy_s(&(g_request.siginfo), sizeof(siginfo_t), si, sizeof(siginfo_t));
110     if (ret < 0) {
111         DFXLOGE("memcpy_s siginfo fail, ret=%{public}d", ret);
112     }
113     ret = memcpy_s(&(g_request.context), sizeof(ucontext_t), context, sizeof(ucontext_t));
114     if (ret < 0) {
115         DFXLOGE("memcpy_s context fail, ret=%{public}d", ret);
116     }
117 #ifdef __aarch64__
118     DoCrashHandler(NULL);
119 #else
120     int pseudothreadTid = -1;
121     pid_t childTid = clone(DoCrashHandler, g_reservedChildStack, \
122         CLONE_THREAD | CLONE_SIGHAND | CLONE_VM | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID, \
123         &pseudothreadTid, NULL, NULL, &pseudothreadTid);
124     if (childTid == -1) {
125         DFXLOGE("Failed to create thread for crash local handler");
126         pthread_mutex_unlock(&g_signalHandlerMutex);
127         return;
128     }
129 
130     FutexWait(&pseudothreadTid, -1);
131     FutexWait(&pseudothreadTid, childTid);
132 
133     DFXLOGI("child thread(%{public}d) exit.", childTid);
134     syscall(__NR_exit, 0);
135 #endif
136 }
137 
DFX_GetCrashFdFunc(CrashFdFunc fn)138 void DFX_GetCrashFdFunc(CrashFdFunc fn)
139 {
140     g_crashFdFn = fn;
141 }
142 
DFX_InstallLocalSignalHandler(void)143 void DFX_InstallLocalSignalHandler(void)
144 {
145     ReserveChildThreadSignalStack();
146 
147     sigset_t set;
148     sigemptyset(&set);
149     struct sigaction action;
150     (void)memset_s(&action, sizeof(action), 0, sizeof(action));
151     sigfillset(&action.sa_mask);
152     action.sa_sigaction = DFX_SignalLocalHandler;
153     action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
154 
155     for (size_t i = 0; i < sizeof(g_platformSignals) / sizeof(g_platformSignals[0]); i++) {
156         int32_t sig = g_platformSignals[i];
157         remove_all_special_handler(sig);
158 
159         sigaddset(&set, sig);
160         if (sigaction(sig, &action, nullptr) != 0) {
161             DFXLOGE("Failed to register signal(%{public}d)", sig);
162         }
163     }
164     sigprocmask(SIG_UNBLOCK, &set, nullptr);
165 }
166