1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "incfs_support/signal_handling.h"
18 
19 #if defined(__BIONIC__) && !INCFS_SUPPORT_DISABLED
20 
21 #include <errno.h>
22 
23 namespace incfs {
24 
enableSignal(int code)25 static void enableSignal(int code) {
26   sigset_t allowed;
27   sigemptyset(&allowed);
28   sigaddset(&allowed, code);
29   pthread_sigmask(SIG_UNBLOCK, &allowed, nullptr);
30 }
31 
~ScopedJmpBuf()32 ScopedJmpBuf::~ScopedJmpBuf() { SignalHandler::mJmpBuf = mPrev; }
33 
instance()34 SignalHandler& SignalHandler::instance() {
35   static SignalHandler self;
36   return self;
37 }
38 
SignalHandler()39 SignalHandler::SignalHandler() {
40   const struct sigaction action = {
41       .sa_sigaction = &handler,
42       .sa_flags = SA_SIGINFO,
43   };
44   if (sigaction(SIGBUS, &action, &mOldSigaction)) {
45     LOG_ALWAYS_FATAL("sigaction(SIGBUS) failed: %d", errno);
46     // not much can be done now
47     return;
48   }
49 
50   // ensure SIGBUS is unblocked, so the process won't get insta-killed
51   enableSignal(SIGBUS);
52 }
53 
handler(int sig,siginfo_t * info,void * ucontext)54 void SignalHandler::handler(int sig, siginfo_t* info, void* ucontext) {
55   if (sig != SIGBUS) {
56     LOG_FATAL("SIGBUS handler called for unexpected signal %d", sig);
57     return;
58   }
59 
60   if (!mJmpBuf.armed) {
61     // No error handler installed - run the previous one
62     if (mOldSigaction.sa_handler == SIG_DFL) {
63       // reset the action to default and re-raise the signal. It will kill the
64       // process
65       signal(sig, SIG_DFL);
66       raise(sig);
67       return;
68     }
69     if (mOldSigaction.sa_handler == SIG_IGN) {
70       // ignoring SIGBUS won't help us much, as we'll get back right here after
71       // retrying.
72       return;
73     }
74     if (mOldSigaction.sa_flags & SA_SIGINFO) {
75       mOldSigaction.sa_sigaction(sig, info, ucontext);
76     } else {
77       mOldSigaction.sa_handler(sig);
78     }
79     // Returning from a signal handler for SIGBUS is undefined, but if it
80     // happens better to at least forward that undefined thing further. Who are
81     // we to argue with the user?
82     return;
83   }
84 
85   // restore SIGBUS as signal handling blocks it before running the callback
86   enableSignal(SIGBUS);
87 
88   longjmp(mJmpBuf.buf, 1);
89 }
90 
91 }  // namespace incfs
92 
93 #endif
94