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 <signal.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <unistd.h>
     21 
     22 #include "jni.h"
     23 
     24 #include <sys/ucontext.h>
     25 
     26 static int signal_count;
     27 static const int kMaxSignal = 2;
     28 
     29 #if defined(__i386__) || defined(__x86_64__)
     30 #if defined(__APPLE__)
     31 #define ucontext __darwin_ucontext
     32 
     33 #if defined(__x86_64__)
     34 // 64 bit mac build.
     35 #define CTX_EIP uc_mcontext->__ss.__rip
     36 #else
     37 // 32 bit mac build.
     38 #define CTX_EIP uc_mcontext->__ss.__eip
     39 #endif
     40 
     41 #elif defined(__x86_64__)
     42 // 64 bit linux build.
     43 #define CTX_EIP uc_mcontext.gregs[REG_RIP]
     44 #else
     45 // 32 bit linux build.
     46 #define CTX_EIP uc_mcontext.gregs[REG_EIP]
     47 #endif
     48 #endif
     49 
     50 static void signalhandler(int sig, siginfo_t* info, void* context) {
     51   printf("signal caught\n");
     52   ++signal_count;
     53   if (signal_count > kMaxSignal) {
     54      abort();
     55   }
     56 #if defined(__arm__)
     57   struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
     58   struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
     59   sc->arm_pc += 2;          // Skip instruction causing segv.
     60 #elif defined(__aarch64__)
     61   struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
     62   struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
     63   sc->pc += 4;          // Skip instruction causing segv.
     64 #elif defined(__i386__) || defined(__x86_64__)
     65   struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
     66   uc->CTX_EIP += 3;
     67 #endif
     68 }
     69 
     70 static struct sigaction oldaction;
     71 
     72 extern "C" JNIEXPORT void JNICALL Java_Main_initSignalTest(JNIEnv*, jclass) {
     73   struct sigaction action;
     74   action.sa_sigaction = signalhandler;
     75   sigemptyset(&action.sa_mask);
     76   action.sa_flags = SA_SIGINFO | SA_ONSTACK;
     77 #if !defined(__APPLE__) && !defined(__mips__)
     78   action.sa_restorer = nullptr;
     79 #endif
     80 
     81   sigaction(SIGSEGV, &action, &oldaction);
     82 }
     83 
     84 extern "C" JNIEXPORT void JNICALL Java_Main_terminateSignalTest(JNIEnv*, jclass) {
     85   sigaction(SIGSEGV, &oldaction, nullptr);
     86 }
     87 
     88 // Prevent the compiler being a smart-alec and optimizing out the assignment
     89 // to nullptr.
     90 char *p = nullptr;
     91 
     92 extern "C" JNIEXPORT jint JNICALL Java_Main_testSignal(JNIEnv*, jclass) {
     93 #if defined(__arm__) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
     94   // On supported architectures we cause a real SEGV.
     95   *p = 'a';
     96 #else
     97   // On other architectures we simulate SEGV.
     98   kill(getpid(), SIGSEGV);
     99 #endif
    100   return 1234;
    101 }
    102 
    103