blob: 6dd63551fd1de3efeb24542ce13d1456b8d1340f [file] [log] [blame]
Dave Allisonf4b80bc2014-05-14 15:41:25 -07001/*
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 Rogers6a3c1fc2014-10-31 00:33:20 -070017#include <jni.h>
Dave Allisonf4b80bc2014-05-14 15:41:25 -070018#include <signal.h>
19#include <stdio.h>
Dave Allison648d7112014-07-25 16:15:27 -070020#include <stdlib.h>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070021#include <sys/ucontext.h>
Dave Allisonf4b80bc2014-05-14 15:41:25 -070022#include <unistd.h>
23
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070024#include "base/macros.h"
Dave Allisonf4b80bc2014-05-14 15:41:25 -070025
Dave Allison648d7112014-07-25 16:15:27 -070026static int signal_count;
27static const int kMaxSignal = 2;
28
Dave Allison49ddae72014-08-20 14:29:39 -070029#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
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070050static void signalhandler(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
51 void* context) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070052 printf("signal caught\n");
Dave Allison648d7112014-07-25 16:15:27 -070053 ++signal_count;
54 if (signal_count > kMaxSignal) {
55 abort();
56 }
Dave Allison49ddae72014-08-20 14:29:39 -070057#if defined(__arm__)
Dave Allisonf4b80bc2014-05-14 15:41:25 -070058 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
59 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
60 sc->arm_pc += 2; // Skip instruction causing segv.
Dave Allison49ddae72014-08-20 14:29:39 -070061#elif defined(__aarch64__)
62 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
63 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
64 sc->pc += 4; // Skip instruction causing segv.
Andreas Gampefd285412015-08-26 14:24:26 -070065#elif defined(__i386__)
Dave Allison49ddae72014-08-20 14:29:39 -070066 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
67 uc->CTX_EIP += 3;
Andreas Gampefd285412015-08-26 14:24:26 -070068#elif defined(__x86_64__)
69 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
70 uc->CTX_EIP += 2;
Andreas Gampe7c3952f2015-02-19 18:21:24 -080071#else
72 UNUSED(context);
Dave Allisonf4b80bc2014-05-14 15:41:25 -070073#endif
74}
75
76static struct sigaction oldaction;
77
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070078extern "C" JNIEXPORT void JNICALL Java_Main_initSignalTest(JNIEnv*, jclass) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070079 struct sigaction action;
80 action.sa_sigaction = signalhandler;
81 sigemptyset(&action.sa_mask);
82 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
Ian Rogersc5f17732014-06-05 20:48:42 -070083#if !defined(__APPLE__) && !defined(__mips__)
Dave Allisonf4b80bc2014-05-14 15:41:25 -070084 action.sa_restorer = nullptr;
85#endif
86
87 sigaction(SIGSEGV, &action, &oldaction);
88}
89
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070090extern "C" JNIEXPORT void JNICALL Java_Main_terminateSignalTest(JNIEnv*, jclass) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070091 sigaction(SIGSEGV, &oldaction, nullptr);
92}
93
94// Prevent the compiler being a smart-alec and optimizing out the assignment
Mathieu Chartier2cebb242015-04-21 16:50:40 -070095// to null.
Dave Allison91a83662014-08-28 16:12:40 -070096char *go_away_compiler = nullptr;
Dave Allisonf4b80bc2014-05-14 15:41:25 -070097
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070098extern "C" JNIEXPORT jint JNICALL Java_Main_testSignal(JNIEnv*, jclass) {
Agi Csaki3835acc2015-08-21 12:56:30 -070099#if defined(__arm__) || defined(__i386__) || defined(__aarch64__)
Dave Allison49ddae72014-08-20 14:29:39 -0700100 // On supported architectures we cause a real SEGV.
Dave Allison91a83662014-08-28 16:12:40 -0700101 *go_away_compiler = 'a';
Agi Csaki3835acc2015-08-21 12:56:30 -0700102#elif defined(__x86_64__)
Andreas Gampefd285412015-08-26 14:24:26 -0700103 // Cause a SEGV using an instruction known to be 2 bytes long to account for hardcoded jump
Agi Csaki3835acc2015-08-21 12:56:30 -0700104 // in the signal handler
Andreas Gampefd285412015-08-26 14:24:26 -0700105 asm volatile("movl $0, %%eax;" "movb %%ah, (%%rax);" : : : "%eax");
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700106#else
107 // On other architectures we simulate SEGV.
108 kill(getpid(), SIGSEGV);
109#endif
110 return 1234;
111}