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