Home | History | Annotate | Download | only in 004-SignalTest
      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 <jni.h>
     18 #include <signal.h>
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <string.h>
     22 #include <sys/ucontext.h>
     23 #include <unistd.h>
     24 
     25 #include "base/macros.h"
     26 
     27 static int signal_count;
     28 static const int kMaxSignal = 1;
     29 
     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 
     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 
     62 static void signalhandler(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
     63                           void* context) {
     64   printf("signal caught\n");
     65   ++signal_count;
     66   if (signal_count > kMaxSignal) {
     67      abort();
     68   }
     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 
     79 #if defined(__arm__)
     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.
     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.
     87 #elif defined(__i386__)
     88   struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
     89   uc->CTX_EIP += 3;
     90 #elif defined(__x86_64__)
     91   struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
     92   uc->CTX_EIP += 2;
     93 #else
     94   UNUSED(context);
     95 #endif
     96 
     97   printf("signal handler done\n");
     98 }
     99 
    100 static struct sigaction oldaction;
    101 
    102 bool compare_sigaction(const struct sigaction* lhs, const struct sigaction* rhs) {
    103   // bionic's definition of `struct sigaction` has internal padding bytes, so we can't just do a
    104   // naive memcmp of the entire struct.
    105   return memcmp(&lhs->sa_mask, &rhs->sa_mask, sizeof(lhs->sa_mask)) == 0 &&
    106          lhs->sa_sigaction == rhs->sa_sigaction &&
    107          lhs->sa_flags == rhs->sa_flags &&
    108          lhs->sa_restorer == rhs->sa_restorer;
    109 }
    110 
    111 extern "C" JNIEXPORT void JNICALL Java_Main_initSignalTest(JNIEnv*, jclass) {
    112   struct sigaction action;
    113   action.sa_sigaction = signalhandler;
    114   sigfillset(&action.sa_mask);
    115   sigdelset(&action.sa_mask, UNBLOCKED_SIGNAL);
    116   action.sa_flags = SA_SIGINFO | SA_ONSTACK;
    117 #if !defined(__APPLE__) && !defined(__mips__)
    118   action.sa_restorer = nullptr;
    119 #endif
    120 
    121   sigaction(SIGSEGV, &action, &oldaction);
    122   struct sigaction check;
    123   sigaction(SIGSEGV, nullptr, &check);
    124   if (!compare_sigaction(&check, &action)) {
    125     printf("sigaction returned different value\n");
    126     printf("action.sa_mask = %p, check.sa_mask = %p\n",
    127            *reinterpret_cast<void**>(&action.sa_mask),
    128            *reinterpret_cast<void**>(&check.sa_mask));
    129     printf("action.sa_sigaction = %p, check.sa_sigaction = %p\n",
    130            action.sa_sigaction, check.sa_sigaction);
    131     printf("action.sa_flags = %x, check.sa_flags = %x\n",
    132            action.sa_flags, check.sa_flags);
    133   }
    134   signal(BLOCKED_SIGNAL, blocked_signal);
    135   signal(UNBLOCKED_SIGNAL, unblocked_signal);
    136 }
    137 
    138 extern "C" JNIEXPORT void JNICALL Java_Main_terminateSignalTest(JNIEnv*, jclass) {
    139   sigaction(SIGSEGV, &oldaction, nullptr);
    140 }
    141 
    142 // Prevent the compiler being a smart-alec and optimizing out the assignment
    143 // to null.
    144 char *go_away_compiler = nullptr;
    145 
    146 extern "C" JNIEXPORT jint JNICALL Java_Main_testSignal(JNIEnv*, jclass) {
    147   // Unblock UNBLOCKED_SIGNAL.
    148   sigset_t mask;
    149   memset(&mask, 0, sizeof(mask));
    150   sigaddset(&mask, UNBLOCKED_SIGNAL);
    151   sigprocmask(SIG_UNBLOCK, &mask, nullptr);
    152 
    153 #if defined(__arm__) || defined(__i386__) || defined(__aarch64__)
    154   // On supported architectures we cause a real SEGV.
    155   *go_away_compiler = 'a';
    156 #elif defined(__x86_64__)
    157   // Cause a SEGV using an instruction known to be 2 bytes long to account for hardcoded jump
    158   // in the signal handler
    159   asm volatile("movl $0, %%eax;" "movb %%ah, (%%rax);" : : : "%eax");
    160 #else
    161   // On other architectures we simulate SEGV.
    162   kill(getpid(), SIGSEGV);
    163 #endif
    164   return 1234;
    165 }
    166