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_arm;
30
SigillSignalHandlerArm(int,siginfo_t *,void * ctx)31 void SigillSignalHandlerArm(int /* sig */, siginfo_t* /* info */, void* ctx) {
32 #pragma clang diagnostic push
33 #pragma clang diagnostic ignored "-Wundefined-internal"
34 extern const uint8_t illegal_instruction_arm[] asm(".L.arm_code.illegal_instruction_arm")
35 __attribute__((visibility("hidden")));
36 #pragma clang diagnostic pop
37 fprintf(stderr, "SIGILL caught\n");
38 ASSERT_EQ(static_cast<ucontext*>(ctx)->uc_mcontext.arm_pc,
39 reinterpret_cast<unsigned long>(illegal_instruction_arm));
40 longjmp(g_recover_arm, 1);
41 }
42
TEST(Signal,SigillArm)43 TEST(Signal, SigillArm) {
44 struct sigaction sa;
45 sa.sa_flags = SA_SIGINFO;
46 sigemptyset(&sa.sa_mask);
47 sa.sa_sigaction = SigillSignalHandlerArm;
48 ScopedSigaction scoped_sa(SIGILL, &sa);
49
50 if (setjmp(g_recover_arm) == 0) {
51 fprintf(stderr, "Executing invalid ARM instruction\n");
52 #ifdef __THUMBEL__
53 extern const uint8_t illegal_instruction_arm[] asm(".L.arm_code.illegal_instruction_arm")
54 __attribute__((visibility("hidden")));
55 asm volatile(
56 "bx %[illegal_instruction_addr]\n"
57 ".p2align 2\n"
58 ".code 32\n"
59 ".L.arm_code.illegal_instruction_arm:\n"
60 ".4byte 0xe7fedeff\n"
61 ".code 16\n"
62 :
63 : [illegal_instruction_addr] "r"(illegal_instruction_arm));
64 #else
65 asm volatile(
66 ".L.arm_code.illegal_instruction_arm:\n"
67 ".4byte 0xe7fedeff");
68 #endif
69 fprintf(stderr, "Bug, recover from SIGILL shall come as longjump()\n");
70 EXPECT_TRUE(0);
71 } else {
72 fprintf(stderr, "Recovered, test passed\n");
73 }
74 }
75
76 sigjmp_buf g_recover_thumb;
77
SigillSignalHandlerThumb(int,siginfo_t *,void * ctx)78 void SigillSignalHandlerThumb(int /* sig */, siginfo_t* /* info */, void* ctx) {
79 #pragma clang diagnostic push
80 #pragma clang diagnostic ignored "-Wundefined-internal"
81 extern const uint8_t illegal_instruction_thumb[] asm(".L.arm_code.illegal_instruction_thumb")
82 __attribute__((visibility("hidden")));
83 #pragma clang diagnostic pop
84 fprintf(stderr, "SIGILL caught\n");
85 ASSERT_EQ(static_cast<ucontext*>(ctx)->uc_mcontext.arm_pc,
86 reinterpret_cast<unsigned long>(illegal_instruction_thumb));
87 longjmp(g_recover_thumb, 1);
88 }
89
TEST(Signal,SigillThumb)90 TEST(Signal, SigillThumb) {
91 struct sigaction sa;
92 sa.sa_flags = SA_SIGINFO;
93 sigemptyset(&sa.sa_mask);
94 sa.sa_sigaction = SigillSignalHandlerThumb;
95 ScopedSigaction scoped_sa(SIGILL, &sa);
96
97 if (setjmp(g_recover_thumb) == 0) {
98 fprintf(stderr, "Executing invalid Thumb instruction\n");
99 #ifdef __THUMBEL__
100 asm volatile(
101 ".L.arm_code.illegal_instruction_thumb:\n"
102 ".2byte 0xdeef");
103 #else
104 extern const uint8_t illegal_instruction_thumb[] asm(".L.arm_code.illegal_instruction_thumb")
105 __attribute__((visibility("hidden")));
106 asm volatile(
107 "bx %[illegal_instruction_addr]\n"
108 ".code 16\n"
109 ".L.arm_code.illegal_instruction_thumb:\n"
110 ".2byte 0xdeef\n"
111 ".p2align 2\n"
112 ".code 32\n"
113 :
114 : [illegal_instruction_addr] "r"(illegal_instruction_thumb + 1));
115 #endif
116 fprintf(stderr, "Bug, recover from SIGILL shall come as longjump()\n");
117 EXPECT_TRUE(0);
118 } else {
119 fprintf(stderr, "Recovered, test passed\n");
120 }
121 }
122
123 } // namespace
124