1 /*
2  * Copyright (C) 2017 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 <stdint.h>
18 
19 #include <gtest/gtest.h>
20 
21 #include "Elf.h"
22 #include "ElfInterface.h"
23 #include "MapInfo.h"
24 #include "Regs.h"
25 
26 #include "MemoryFake.h"
27 
28 class ElfFake : public Elf {
29  public:
ElfFake(Memory * memory)30   ElfFake(Memory* memory) : Elf(memory) { valid_ = true; }
31   virtual ~ElfFake() = default;
32 
set_elf_interface(ElfInterface * interface)33   void set_elf_interface(ElfInterface* interface) { interface_.reset(interface); }
34 };
35 
36 class ElfInterfaceFake : public ElfInterface {
37  public:
ElfInterfaceFake(Memory * memory)38   ElfInterfaceFake(Memory* memory) : ElfInterface(memory) {}
39   virtual ~ElfInterfaceFake() = default;
40 
set_load_bias(uint64_t load_bias)41   void set_load_bias(uint64_t load_bias) { load_bias_ = load_bias; }
42 
Init()43   bool Init() override { return false; }
InitHeaders()44   void InitHeaders() override {}
GetSoname(std::string *)45   bool GetSoname(std::string*) override { return false; }
GetFunctionName(uint64_t,std::string *,uint64_t *)46   bool GetFunctionName(uint64_t, std::string*, uint64_t*) override { return false; }
Step(uint64_t,Regs *,Memory *)47   bool Step(uint64_t, Regs*, Memory*) override { return false; }
48 };
49 
50 template <typename TypeParam>
51 class RegsTestTmpl : public RegsTmpl<TypeParam> {
52  public:
RegsTestTmpl(uint16_t total_regs,uint16_t regs_sp)53   RegsTestTmpl(uint16_t total_regs, uint16_t regs_sp)
54       : RegsTmpl<TypeParam>(total_regs, regs_sp, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
RegsTestTmpl(uint16_t total_regs,uint16_t regs_sp,Regs::Location return_loc)55   RegsTestTmpl(uint16_t total_regs, uint16_t regs_sp, Regs::Location return_loc)
56       : RegsTmpl<TypeParam>(total_regs, regs_sp, return_loc) {}
57   virtual ~RegsTestTmpl() = default;
58 
GetAdjustedPc(uint64_t,Elf *)59   uint64_t GetAdjustedPc(uint64_t, Elf*) { return 0; }
60 };
61 
62 class RegsTest : public ::testing::Test {
63  protected:
SetUp()64   void SetUp() override {
65     memory_ = new MemoryFake;
66     elf_.reset(new ElfFake(memory_));
67     elf_interface_ = new ElfInterfaceFake(elf_->memory());
68     elf_->set_elf_interface(elf_interface_);
69   }
70 
71   template <typename AddressType>
72   void regs_rel_pc();
73 
74   template <typename AddressType>
75   void regs_return_address_register();
76 
77   ElfInterfaceFake* elf_interface_;
78   MemoryFake* memory_;
79   std::unique_ptr<ElfFake> elf_;
80 };
81 
TEST_F(RegsTest,regs32)82 TEST_F(RegsTest, regs32) {
83   RegsTestTmpl<uint32_t> regs32(50, 10);
84   ASSERT_EQ(50U, regs32.total_regs());
85   ASSERT_EQ(10U, regs32.sp_reg());
86 
87   uint32_t* raw = reinterpret_cast<uint32_t*>(regs32.RawData());
88   for (size_t i = 0; i < 50; i++) {
89     raw[i] = 0xf0000000 + i;
90   }
91   regs32.set_pc(0xf0120340);
92   regs32.set_sp(0xa0ab0cd0);
93 
94   for (size_t i = 0; i < 50; i++) {
95     ASSERT_EQ(0xf0000000U + i, regs32[i]) << "Failed comparing register " << i;
96   }
97 
98   ASSERT_EQ(0xf0120340U, regs32.pc());
99   ASSERT_EQ(0xa0ab0cd0U, regs32.sp());
100 
101   regs32[32] = 10;
102   ASSERT_EQ(10U, regs32[32]);
103 }
104 
TEST_F(RegsTest,regs64)105 TEST_F(RegsTest, regs64) {
106   RegsTestTmpl<uint64_t> regs64(30, 12);
107   ASSERT_EQ(30U, regs64.total_regs());
108   ASSERT_EQ(12U, regs64.sp_reg());
109 
110   uint64_t* raw = reinterpret_cast<uint64_t*>(regs64.RawData());
111   for (size_t i = 0; i < 30; i++) {
112     raw[i] = 0xf123456780000000UL + i;
113   }
114   regs64.set_pc(0xf123456780102030UL);
115   regs64.set_sp(0xa123456780a0b0c0UL);
116 
117   for (size_t i = 0; i < 30; i++) {
118     ASSERT_EQ(0xf123456780000000U + i, regs64[i]) << "Failed reading register " << i;
119   }
120 
121   ASSERT_EQ(0xf123456780102030UL, regs64.pc());
122   ASSERT_EQ(0xa123456780a0b0c0UL, regs64.sp());
123 
124   regs64[8] = 10;
125   ASSERT_EQ(10U, regs64[8]);
126 }
127 
128 template <typename AddressType>
regs_rel_pc()129 void RegsTest::regs_rel_pc() {
130   RegsTestTmpl<AddressType> regs(30, 12);
131 
132   elf_interface_->set_load_bias(0);
133   MapInfo map_info{.start = 0x1000, .end = 0x2000};
134   regs.set_pc(0x1101);
135   ASSERT_EQ(0x101U, regs.GetRelPc(elf_.get(), &map_info));
136   elf_interface_->set_load_bias(0x3000);
137   ASSERT_EQ(0x3101U, regs.GetRelPc(elf_.get(), &map_info));
138 }
139 
TEST_F(RegsTest,regs32_rel_pc)140 TEST_F(RegsTest, regs32_rel_pc) {
141   regs_rel_pc<uint32_t>();
142 }
143 
TEST_F(RegsTest,regs64_rel_pc)144 TEST_F(RegsTest, regs64_rel_pc) {
145   regs_rel_pc<uint64_t>();
146 }
147 
148 template <typename AddressType>
regs_return_address_register()149 void RegsTest::regs_return_address_register() {
150   RegsTestTmpl<AddressType> regs(20, 10, Regs::Location(Regs::LOCATION_REGISTER, 5));
151 
152   regs[5] = 0x12345;
153   uint64_t value;
154   ASSERT_TRUE(regs.GetReturnAddressFromDefault(memory_, &value));
155   ASSERT_EQ(0x12345U, value);
156 }
157 
TEST_F(RegsTest,regs32_return_address_register)158 TEST_F(RegsTest, regs32_return_address_register) {
159   regs_return_address_register<uint32_t>();
160 }
161 
TEST_F(RegsTest,regs64_return_address_register)162 TEST_F(RegsTest, regs64_return_address_register) {
163   regs_return_address_register<uint64_t>();
164 }
165 
TEST_F(RegsTest,regs32_return_address_sp_offset)166 TEST_F(RegsTest, regs32_return_address_sp_offset) {
167   RegsTestTmpl<uint32_t> regs(20, 10, Regs::Location(Regs::LOCATION_SP_OFFSET, -2));
168 
169   regs.set_sp(0x2002);
170   memory_->SetData32(0x2000, 0x12345678);
171   uint64_t value;
172   ASSERT_TRUE(regs.GetReturnAddressFromDefault(memory_, &value));
173   ASSERT_EQ(0x12345678U, value);
174 }
175 
TEST_F(RegsTest,regs64_return_address_sp_offset)176 TEST_F(RegsTest, regs64_return_address_sp_offset) {
177   RegsTestTmpl<uint64_t> regs(20, 10, Regs::Location(Regs::LOCATION_SP_OFFSET, -8));
178 
179   regs.set_sp(0x2008);
180   memory_->SetData64(0x2000, 0x12345678aabbccddULL);
181   uint64_t value;
182   ASSERT_TRUE(regs.GetReturnAddressFromDefault(memory_, &value));
183   ASSERT_EQ(0x12345678aabbccddULL, value);
184 }
185 
TEST_F(RegsTest,rel_pc)186 TEST_F(RegsTest, rel_pc) {
187   RegsArm64 arm64;
188   ASSERT_EQ(0xcU, arm64.GetAdjustedPc(0x10, elf_.get()));
189   ASSERT_EQ(0x0U, arm64.GetAdjustedPc(0x4, elf_.get()));
190   ASSERT_EQ(0x3U, arm64.GetAdjustedPc(0x3, elf_.get()));
191   ASSERT_EQ(0x2U, arm64.GetAdjustedPc(0x2, elf_.get()));
192   ASSERT_EQ(0x1U, arm64.GetAdjustedPc(0x1, elf_.get()));
193   ASSERT_EQ(0x0U, arm64.GetAdjustedPc(0x0, elf_.get()));
194 
195   RegsX86 x86;
196   ASSERT_EQ(0xffU,  x86.GetAdjustedPc(0x100, elf_.get()));
197   ASSERT_EQ(0x1U,  x86.GetAdjustedPc(0x2, elf_.get()));
198   ASSERT_EQ(0x0U,  x86.GetAdjustedPc(0x1, elf_.get()));
199   ASSERT_EQ(0x0U,  x86.GetAdjustedPc(0x0, elf_.get()));
200 
201   RegsX86_64 x86_64;
202   ASSERT_EQ(0xffU,  x86_64.GetAdjustedPc(0x100, elf_.get()));
203   ASSERT_EQ(0x1U,  x86_64.GetAdjustedPc(0x2, elf_.get()));
204   ASSERT_EQ(0x0U,  x86_64.GetAdjustedPc(0x1, elf_.get()));
205   ASSERT_EQ(0x0U,  x86_64.GetAdjustedPc(0x0, elf_.get()));
206 }
207 
TEST_F(RegsTest,rel_pc_arm)208 TEST_F(RegsTest, rel_pc_arm) {
209   RegsArm arm;
210 
211   // Check fence posts.
212   elf_interface_->set_load_bias(0);
213   ASSERT_EQ(3U,  arm.GetAdjustedPc(0x5, elf_.get()));
214   ASSERT_EQ(4U,  arm.GetAdjustedPc(0x4, elf_.get()));
215   ASSERT_EQ(3U,  arm.GetAdjustedPc(0x3, elf_.get()));
216   ASSERT_EQ(2U,  arm.GetAdjustedPc(0x2, elf_.get()));
217   ASSERT_EQ(1U,  arm.GetAdjustedPc(0x1, elf_.get()));
218   ASSERT_EQ(0U,  arm.GetAdjustedPc(0x0, elf_.get()));
219 
220   elf_interface_->set_load_bias(0x100);
221   ASSERT_EQ(0xffU,  arm.GetAdjustedPc(0xff, elf_.get()));
222   ASSERT_EQ(0x103U,  arm.GetAdjustedPc(0x105, elf_.get()));
223   ASSERT_EQ(0x104U,  arm.GetAdjustedPc(0x104, elf_.get()));
224   ASSERT_EQ(0x103U,  arm.GetAdjustedPc(0x103, elf_.get()));
225   ASSERT_EQ(0x102U,  arm.GetAdjustedPc(0x102, elf_.get()));
226   ASSERT_EQ(0x101U,  arm.GetAdjustedPc(0x101, elf_.get()));
227   ASSERT_EQ(0x100U,  arm.GetAdjustedPc(0x100, elf_.get()));
228 
229   // Check thumb instructions handling.
230   elf_interface_->set_load_bias(0);
231   memory_->SetData32(0x2000, 0);
232   ASSERT_EQ(0x2003U,  arm.GetAdjustedPc(0x2005, elf_.get()));
233   memory_->SetData32(0x2000, 0xe000f000);
234   ASSERT_EQ(0x2001U,  arm.GetAdjustedPc(0x2005, elf_.get()));
235 
236   elf_interface_->set_load_bias(0x400);
237   memory_->SetData32(0x2100, 0);
238   ASSERT_EQ(0x2503U,  arm.GetAdjustedPc(0x2505, elf_.get()));
239   memory_->SetData32(0x2100, 0xf111f111);
240   ASSERT_EQ(0x2501U,  arm.GetAdjustedPc(0x2505, elf_.get()));
241 }
242 
TEST_F(RegsTest,elf_invalid)243 TEST_F(RegsTest, elf_invalid) {
244   Elf invalid_elf(new MemoryFake);
245   RegsArm regs_arm;
246   RegsArm64 regs_arm64;
247   RegsX86 regs_x86;
248   RegsX86_64 regs_x86_64;
249   MapInfo map_info{.start = 0x1000, .end = 0x2000};
250 
251   regs_arm.set_pc(0x1500);
252   ASSERT_EQ(0x500U, regs_arm.GetRelPc(&invalid_elf, &map_info));
253   ASSERT_EQ(0x500U, regs_arm.GetAdjustedPc(0x500U, &invalid_elf));
254 
255   regs_arm64.set_pc(0x1600);
256   ASSERT_EQ(0x600U, regs_arm64.GetRelPc(&invalid_elf, &map_info));
257   ASSERT_EQ(0x600U, regs_arm64.GetAdjustedPc(0x600U, &invalid_elf));
258 
259   regs_x86.set_pc(0x1700);
260   ASSERT_EQ(0x700U, regs_x86.GetRelPc(&invalid_elf, &map_info));
261   ASSERT_EQ(0x700U, regs_x86.GetAdjustedPc(0x700U, &invalid_elf));
262 
263   regs_x86_64.set_pc(0x1800);
264   ASSERT_EQ(0x800U, regs_x86_64.GetRelPc(&invalid_elf, &map_info));
265   ASSERT_EQ(0x800U, regs_x86_64.GetAdjustedPc(0x800U, &invalid_elf));
266 }
267