1 /*
2  * Copyright (C) 2014 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 <android-base/logging.h>
18 
19 #include <jni.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/ucontext.h>
25 #include <unistd.h>
26 
27 #include "base/macros.h"
28 
29 static int signal_count;
30 static const int kMaxSignal = 1;
31 
32 #if defined(__i386__) || defined(__x86_64__)
33 #if defined(__APPLE__)
34 #define ucontext __darwin_ucontext
35 
36 #if defined(__x86_64__)
37 // 64 bit mac build.
38 #define CTX_EIP uc_mcontext->__ss.__rip
39 #else
40 // 32 bit mac build.
41 #define CTX_EIP uc_mcontext->__ss.__eip
42 #endif
43 
44 #elif defined(__x86_64__)
45 // 64 bit linux build.
46 #define CTX_EIP uc_mcontext.gregs[REG_RIP]
47 #else
48 // 32 bit linux build.
49 #define CTX_EIP uc_mcontext.gregs[REG_EIP]
50 #endif
51 #endif
52 
53 #define BLOCKED_SIGNAL SIGUSR1
54 #define UNBLOCKED_SIGNAL SIGUSR2
55 
blocked_signal(int sig)56 static void blocked_signal([[maybe_unused]] int sig) {
57   printf("blocked signal received\n");
58 }
59 
unblocked_signal(int sig)60 static void unblocked_signal([[maybe_unused]] int sig) {
61   printf("unblocked signal received\n");
62 }
63 
signalhandler(int sig,siginfo_t * info,void * context)64 static void signalhandler([[maybe_unused]] int sig, [[maybe_unused]] siginfo_t* info,
65                           void* context) {
66   printf("signal caught\n");
67   ++signal_count;
68   if (signal_count > kMaxSignal) {
69      abort();
70   }
71 
72   raise(UNBLOCKED_SIGNAL);
73   raise(BLOCKED_SIGNAL);
74   printf("unblocking blocked signal\n");
75 
76   sigset_t mask;
77   sigemptyset(&mask);
78   sigaddset(&mask, BLOCKED_SIGNAL);
79   sigprocmask(SIG_UNBLOCK, &mask, nullptr);
80 
81 #if defined(__arm__)
82   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
83   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
84   mc->arm_pc += 2;  // Skip instruction causing segv.
85 #elif defined(__aarch64__)
86   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
87   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
88   mc->pc += 4;  // Skip instruction causing segv.
89 #elif defined(__riscv)
90   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
91   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
92   mc->__gregs[REG_PC] += 4;  // Skip instruction causing segv.
93 #elif defined(__i386__)
94   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
95   uc->CTX_EIP += 3;
96 #elif defined(__x86_64__)
97   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
98   uc->CTX_EIP += 2;
99 #else
100   UNUSED(context);
101   UNIMPLEMENTED(FATAL) << "Unsupported architecture";
102 #endif
103 
104   printf("signal handler done\n");
105 }
106 
107 static struct sigaction oldaction;
108 
compare_sigaction(const struct sigaction * lhs,const struct sigaction * rhs)109 bool compare_sigaction(const struct sigaction* lhs, const struct sigaction* rhs) {
110   // bionic's definition of `struct sigaction` has internal padding bytes, so we can't just do a
111   // naive memcmp of the entire struct.
112 #if defined(SA_RESTORER)
113   if (lhs->sa_restorer != rhs->sa_restorer) {
114     return false;
115   }
116 #endif
117   return memcmp(&lhs->sa_mask, &rhs->sa_mask, sizeof(lhs->sa_mask)) == 0 &&
118          lhs->sa_sigaction == rhs->sa_sigaction &&
119          lhs->sa_flags == rhs->sa_flags;
120 }
121 
Java_Main_initSignalTest(JNIEnv *,jclass)122 extern "C" JNIEXPORT void JNICALL Java_Main_initSignalTest(JNIEnv*, jclass) {
123   struct sigaction action;
124   action.sa_sigaction = signalhandler;
125   sigfillset(&action.sa_mask);
126   sigdelset(&action.sa_mask, UNBLOCKED_SIGNAL);
127   action.sa_flags = SA_SIGINFO | SA_ONSTACK;
128 #if !defined(__APPLE__)
129   action.sa_restorer = nullptr;
130 #endif
131 
132   sigaction(SIGSEGV, &action, &oldaction);
133   struct sigaction check;
134   sigaction(SIGSEGV, nullptr, &check);
135   if (!compare_sigaction(&check, &action)) {
136     printf("sigaction returned different value\n");
137     printf("action.sa_mask = %p, check.sa_mask = %p\n",
138            *reinterpret_cast<void**>(&action.sa_mask),
139            *reinterpret_cast<void**>(&check.sa_mask));
140     printf("action.sa_sigaction = %p, check.sa_sigaction = %p\n",
141            action.sa_sigaction, check.sa_sigaction);
142     printf("action.sa_flags = %x, check.sa_flags = %x\n",
143            action.sa_flags, check.sa_flags);
144   }
145   signal(BLOCKED_SIGNAL, blocked_signal);
146   signal(UNBLOCKED_SIGNAL, unblocked_signal);
147 }
148 
Java_Main_terminateSignalTest(JNIEnv *,jclass)149 extern "C" JNIEXPORT void JNICALL Java_Main_terminateSignalTest(JNIEnv*, jclass) {
150   sigaction(SIGSEGV, &oldaction, nullptr);
151 }
152 
153 // Prevent the compiler being a smart-alec and optimizing out the assignment
154 // to null.
155 char *go_away_compiler = nullptr;
156 
Java_Main_testSignal(JNIEnv *,jclass)157 extern "C" JNIEXPORT jint JNICALL Java_Main_testSignal(JNIEnv*, jclass) {
158   // Unblock UNBLOCKED_SIGNAL.
159   sigset_t mask;
160   memset(&mask, 0, sizeof(mask));
161   sigaddset(&mask, UNBLOCKED_SIGNAL);
162   sigprocmask(SIG_UNBLOCK, &mask, nullptr);
163 
164 #if defined(__arm__) || defined(__i386__) || defined(__aarch64__)
165   // On supported architectures we cause a real SEGV.
166   *go_away_compiler = 'a';
167 #elif defined(__riscv)
168   // Cause a SEGV using an instruction known to be 4 bytes long to account for hardcoded jump
169   // in the signal handler
170   asm volatile("ld zero, (zero);" : : :);
171 #elif defined(__x86_64__)
172   // Cause a SEGV using an instruction known to be 2 bytes long to account for hardcoded jump
173   // in the signal handler
174   asm volatile("movl $0, %%eax;" "movb %%ah, (%%rax);" : : : "%eax");
175 #else
176   // On other architectures we simulate SEGV.
177   kill(getpid(), SIGSEGV);
178 #endif
179   return 1234;
180 }
181