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         DFXLOG_ERROR("Failed to alloc memory for child stack.");
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     syscall(__NR_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     DFXLOG_INFO("DFX_SignalLocalHandler :: sig(%d), pid(%d), tid(%d).", 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         DFXLOG_ERROR("memcpy_s siginfo fail, ret=%d", ret);
112     }
113     ret = memcpy_s(&(g_request.context), sizeof(ucontext_t), context, sizeof(ucontext_t));
114     if (ret < 0) {
115         DFXLOG_ERROR("memcpy_s context fail, ret=%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         DFXLOG_ERROR("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     DFXLOG_INFO("child thread(%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             DFXLOG_ERROR("Failed to register signal(%d)", sig);
162         }
163     }
164     sigprocmask(SIG_UNBLOCK, &set, nullptr);
165 }
166