Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 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 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 17 | #include <jni.h> |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 18 | #include <signal.h> |
| 19 | #include <stdio.h> |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 21 | #include <string.h> |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 22 | #include <sys/ucontext.h> |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 25 | #include "base/macros.h" |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 26 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 27 | static int signal_count; |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 28 | static const int kMaxSignal = 1; |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 29 | |
Dave Allison | 49ddae7 | 2014-08-20 14:29:39 -0700 | [diff] [blame] | 30 | #if defined(__i386__) || defined(__x86_64__) |
| 31 | #if defined(__APPLE__) |
| 32 | #define ucontext __darwin_ucontext |
| 33 | |
| 34 | #if defined(__x86_64__) |
| 35 | // 64 bit mac build. |
| 36 | #define CTX_EIP uc_mcontext->__ss.__rip |
| 37 | #else |
| 38 | // 32 bit mac build. |
| 39 | #define CTX_EIP uc_mcontext->__ss.__eip |
| 40 | #endif |
| 41 | |
| 42 | #elif defined(__x86_64__) |
| 43 | // 64 bit linux build. |
| 44 | #define CTX_EIP uc_mcontext.gregs[REG_RIP] |
| 45 | #else |
| 46 | // 32 bit linux build. |
| 47 | #define CTX_EIP uc_mcontext.gregs[REG_EIP] |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 51 | #define BLOCKED_SIGNAL SIGUSR1 |
| 52 | #define UNBLOCKED_SIGNAL SIGUSR2 |
| 53 | |
| 54 | static void blocked_signal(int sig ATTRIBUTE_UNUSED) { |
| 55 | printf("blocked signal received\n"); |
| 56 | } |
| 57 | |
| 58 | static void unblocked_signal(int sig ATTRIBUTE_UNUSED) { |
| 59 | printf("unblocked signal received\n"); |
| 60 | } |
| 61 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 62 | static void signalhandler(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, |
| 63 | void* context) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 64 | printf("signal caught\n"); |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 65 | ++signal_count; |
| 66 | if (signal_count > kMaxSignal) { |
| 67 | abort(); |
| 68 | } |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 69 | |
| 70 | raise(UNBLOCKED_SIGNAL); |
| 71 | raise(BLOCKED_SIGNAL); |
| 72 | printf("unblocking blocked signal\n"); |
| 73 | |
| 74 | sigset_t mask; |
| 75 | sigemptyset(&mask); |
| 76 | sigaddset(&mask, BLOCKED_SIGNAL); |
| 77 | sigprocmask(SIG_UNBLOCK, &mask, nullptr); |
| 78 | |
Dave Allison | 49ddae7 | 2014-08-20 14:29:39 -0700 | [diff] [blame] | 79 | #if defined(__arm__) |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 80 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 81 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 82 | sc->arm_pc += 2; // Skip instruction causing segv. |
Dave Allison | 49ddae7 | 2014-08-20 14:29:39 -0700 | [diff] [blame] | 83 | #elif defined(__aarch64__) |
| 84 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 85 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 86 | sc->pc += 4; // Skip instruction causing segv. |
Andreas Gampe | fd28541 | 2015-08-26 14:24:26 -0700 | [diff] [blame] | 87 | #elif defined(__i386__) |
Dave Allison | 49ddae7 | 2014-08-20 14:29:39 -0700 | [diff] [blame] | 88 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 89 | uc->CTX_EIP += 3; |
Andreas Gampe | fd28541 | 2015-08-26 14:24:26 -0700 | [diff] [blame] | 90 | #elif defined(__x86_64__) |
| 91 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 92 | uc->CTX_EIP += 2; |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 93 | #else |
| 94 | UNUSED(context); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 95 | #endif |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 96 | |
| 97 | printf("signal handler done\n"); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static struct sigaction oldaction; |
| 101 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 102 | extern "C" JNIEXPORT void JNICALL Java_Main_initSignalTest(JNIEnv*, jclass) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 103 | struct sigaction action; |
| 104 | action.sa_sigaction = signalhandler; |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 105 | sigfillset(&action.sa_mask); |
| 106 | sigdelset(&action.sa_mask, UNBLOCKED_SIGNAL); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 107 | action.sa_flags = SA_SIGINFO | SA_ONSTACK; |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 108 | #if !defined(__APPLE__) && !defined(__mips__) |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 109 | action.sa_restorer = nullptr; |
| 110 | #endif |
| 111 | |
| 112 | sigaction(SIGSEGV, &action, &oldaction); |
Josh Gao | 9d631dd | 2017-03-23 20:04:58 -0700 | [diff] [blame] | 113 | struct sigaction check; |
| 114 | sigaction(SIGSEGV, nullptr, &check); |
| 115 | if (memcmp(&action, &check, sizeof(action)) != 0) { |
| 116 | printf("sigaction returned different value\n"); |
| 117 | } |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 118 | signal(BLOCKED_SIGNAL, blocked_signal); |
| 119 | signal(UNBLOCKED_SIGNAL, unblocked_signal); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 122 | extern "C" JNIEXPORT void JNICALL Java_Main_terminateSignalTest(JNIEnv*, jclass) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 123 | sigaction(SIGSEGV, &oldaction, nullptr); |
| 124 | } |
| 125 | |
| 126 | // Prevent the compiler being a smart-alec and optimizing out the assignment |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 127 | // to null. |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 128 | char *go_away_compiler = nullptr; |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 129 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 130 | extern "C" JNIEXPORT jint JNICALL Java_Main_testSignal(JNIEnv*, jclass) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 131 | // Unblock UNBLOCKED_SIGNAL. |
| 132 | sigset_t mask; |
| 133 | memset(&mask, 0, sizeof(mask)); |
| 134 | sigaddset(&mask, UNBLOCKED_SIGNAL); |
| 135 | sigprocmask(SIG_UNBLOCK, &mask, nullptr); |
| 136 | |
Agi Csaki | 3835acc | 2015-08-21 12:56:30 -0700 | [diff] [blame] | 137 | #if defined(__arm__) || defined(__i386__) || defined(__aarch64__) |
Dave Allison | 49ddae7 | 2014-08-20 14:29:39 -0700 | [diff] [blame] | 138 | // On supported architectures we cause a real SEGV. |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 139 | *go_away_compiler = 'a'; |
Agi Csaki | 3835acc | 2015-08-21 12:56:30 -0700 | [diff] [blame] | 140 | #elif defined(__x86_64__) |
Andreas Gampe | fd28541 | 2015-08-26 14:24:26 -0700 | [diff] [blame] | 141 | // Cause a SEGV using an instruction known to be 2 bytes long to account for hardcoded jump |
Agi Csaki | 3835acc | 2015-08-21 12:56:30 -0700 | [diff] [blame] | 142 | // in the signal handler |
Andreas Gampe | fd28541 | 2015-08-26 14:24:26 -0700 | [diff] [blame] | 143 | asm volatile("movl $0, %%eax;" "movb %%ah, (%%rax);" : : : "%eax"); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 144 | #else |
| 145 | // On other architectures we simulate SEGV. |
| 146 | kill(getpid(), SIGSEGV); |
| 147 | #endif |
| 148 | return 1234; |
| 149 | } |