1 /*
2 * Copyright (C) 2023 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 <csignal>
20 #include <cstddef>
21 #include <cstdint>
22
23 #include "berberis/base/checks.h"
24
25 #include "../faulty_memory_accesses.h"
26
27 namespace berberis {
28
29 namespace {
30
31 #if defined(__i386__)
32 #define IP_ACCESSOR(ucontext) ucontext->uc_mcontext.gregs[REG_EIP]
33 #elif defined(__x86_64__)
34 #define IP_ACCESSOR(ucontext) ucontext->uc_mcontext.gregs[REG_RIP]
35 #elif defined(__aarch64__)
36 #define IP_ACCESSOR(ucontext) ucontext->uc_mcontext.pc
37 #else
38 #error "Unsupported arch"
39 #endif
40
FaultHandler(int,siginfo_t *,void * ctx)41 void FaultHandler(int /* sig */, siginfo_t* /* info */, void* ctx) {
42 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(ctx);
43 static_assert(sizeof(void*) == sizeof(greg_t), "Unsupported type sizes");
44 void* fault_addr = reinterpret_cast<void*>(IP_ACCESSOR(ucontext));
45 void* recovery_addr = FindFaultyMemoryAccessRecoveryAddrForTesting(fault_addr);
46 CHECK(recovery_addr);
47 IP_ACCESSOR(ucontext) = reinterpret_cast<greg_t>(recovery_addr);
48 }
49
50 class ScopedFaultySigaction {
51 public:
ScopedFaultySigaction()52 ScopedFaultySigaction() {
53 struct sigaction sa;
54 sa.sa_flags = SA_SIGINFO;
55 sigemptyset(&sa.sa_mask);
56 sa.sa_sigaction = FaultHandler;
57 CHECK_EQ(sigaction(SIGSEGV, &sa, &old_sa_), 0);
58 }
59
~ScopedFaultySigaction()60 ~ScopedFaultySigaction() { CHECK_EQ(sigaction(SIGSEGV, &old_sa_, nullptr), 0); }
61
62 private:
63 struct sigaction old_sa_;
64 };
65
TEST(FaultyMemoryAccessesTest,FaultyLoadSuccess)66 TEST(FaultyMemoryAccessesTest, FaultyLoadSuccess) {
67 ScopedFaultySigaction scoped_sa;
68 uint64_t data = 0xffff'eeee'cccc'bbaaULL;
69 FaultyLoadResult result;
70
71 result = FaultyLoad(&data, 1);
72 EXPECT_EQ(result.value, static_cast<uint8_t>(data));
73 EXPECT_FALSE(result.is_fault);
74
75 result = FaultyLoad(&data, 2);
76 EXPECT_EQ(result.value, static_cast<uint16_t>(data));
77 EXPECT_FALSE(result.is_fault);
78
79 result = FaultyLoad(&data, 4);
80 EXPECT_EQ(result.value, static_cast<uint32_t>(data));
81 EXPECT_FALSE(result.is_fault);
82
83 result = FaultyLoad(&data, 8);
84 EXPECT_EQ(result.value, data);
85 EXPECT_FALSE(result.is_fault);
86 }
87
TEST(FaultyMemoryAccessesTest,FaultyLoadFault)88 TEST(FaultyMemoryAccessesTest, FaultyLoadFault) {
89 ScopedFaultySigaction scoped_sa;
90 FaultyLoadResult result;
91
92 result = FaultyLoad(nullptr, 1);
93 EXPECT_TRUE(result.is_fault);
94 result = FaultyLoad(nullptr, 2);
95 EXPECT_TRUE(result.is_fault);
96 result = FaultyLoad(nullptr, 4);
97 EXPECT_TRUE(result.is_fault);
98 result = FaultyLoad(nullptr, 8);
99 EXPECT_TRUE(result.is_fault);
100 }
101
TEST(FaultyMemoryAccessesTest,FaultyStoreSuccess)102 TEST(FaultyMemoryAccessesTest, FaultyStoreSuccess) {
103 ScopedFaultySigaction scoped_sa;
104 uint64_t data = 0xffff'eeee'cccc'bbaaULL;
105 uint64_t storage = 0;
106
107 bool is_fault = FaultyStore(&storage, 1, data);
108 EXPECT_EQ(static_cast<uint8_t>(storage), static_cast<uint8_t>(data));
109 EXPECT_FALSE(is_fault);
110
111 is_fault = FaultyStore(&storage, 2, data);
112 EXPECT_EQ(static_cast<uint16_t>(storage), static_cast<uint16_t>(data));
113 EXPECT_FALSE(is_fault);
114
115 is_fault = FaultyStore(&storage, 4, data);
116 EXPECT_EQ(static_cast<uint32_t>(storage), static_cast<uint32_t>(data));
117 EXPECT_FALSE(is_fault);
118
119 is_fault = FaultyStore(&storage, 8, data);
120 EXPECT_EQ(storage, data);
121 EXPECT_FALSE(is_fault);
122 }
123
TEST(FaultyMemoryAccessesTest,FaultyStoreFault)124 TEST(FaultyMemoryAccessesTest, FaultyStoreFault) {
125 ScopedFaultySigaction scoped_sa;
126
127 bool is_fault = FaultyStore(nullptr, 1, 0);
128 EXPECT_TRUE(is_fault);
129 is_fault = FaultyStore(nullptr, 2, 0);
130 EXPECT_TRUE(is_fault);
131 is_fault = FaultyStore(nullptr, 4, 0);
132 EXPECT_TRUE(is_fault);
133 is_fault = FaultyStore(nullptr, 8, 0);
134 EXPECT_TRUE(is_fault);
135 }
136
137 } // namespace
138
139 } // namespace berberis
140