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 "gtest/gtest.h"
18 
19 #include <setjmp.h>
20 #include <signal.h>
21 #include <sys/ucontext.h>
22 
23 #include <cstdio>
24 
25 #include "berberis/ndk_program_tests/scoped_sigaction.h"
26 
27 namespace {
28 
29 sigjmp_buf g_recover_arm64;
30 
31 extern "C" char illegal_instruction_arm64;
32 
SigillSignalHandlerArm64(int,siginfo_t *,void * ctx)33 void SigillSignalHandlerArm64(int /* sig */, siginfo_t* /* info */, void* ctx) {
34   fprintf(stderr, "SIGILL caught\n");
35   // Warning: do not use ASSERT, so that we recover with longjump unconditionally.
36   // Otherwise we'll be looping executing illegal instruction.
37   EXPECT_EQ(static_cast<ucontext*>(ctx)->uc_mcontext.pc,
38             reinterpret_cast<unsigned long>(&illegal_instruction_arm64));
39   longjmp(g_recover_arm64, 1);
40 }
41 
TEST(Signal,SigillArm64)42 TEST(Signal, SigillArm64) {
43   struct sigaction sa;
44   sa.sa_flags = SA_SIGINFO;
45   sigemptyset(&sa.sa_mask);
46   sa.sa_sigaction = SigillSignalHandlerArm64;
47   ScopedSigaction scoped_sa(SIGILL, &sa);
48 
49   if (setjmp(g_recover_arm64) == 0) {
50     fprintf(stderr, "Executing invalid ARM instruction\n");
51     asm volatile(
52         ".align 8\n"
53         ".globl illegal_instruction_arm64\n"
54         "illegal_instruction_arm64:\n"
55         ".4byte 0x0\n");
56     fprintf(stderr, "Bug, recover from SIGILL shall come as longjump()\n");
57     EXPECT_TRUE(0);
58   } else {
59     fprintf(stderr, "Recovered, test passed\n");
60   }
61 }
62 
63 }  // namespace
64