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