• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 <elf.h>
18 #include <unistd.h>
19 
20 #include <android-base/file.h>
21 
22 #include <gtest/gtest.h>
23 
24 #include <unwindstack/Elf.h>
25 #include <unwindstack/MapInfo.h>
26 
27 #include "ElfTestUtils.h"
28 #include "MemoryFake.h"
29 
30 namespace unwindstack {
31 
32 class ElfCacheTest : public ::testing::Test {
33  protected:
SetUpTestSuite()34   static void SetUpTestSuite() { memory_.reset(new MemoryFake); }
35 
SetUp()36   void SetUp() override { Elf::SetCachingEnabled(true); }
37 
TearDown()38   void TearDown() override { Elf::SetCachingEnabled(false); }
39 
WriteElfFile(uint64_t offset,TemporaryFile * tf,uint32_t type)40   void WriteElfFile(uint64_t offset, TemporaryFile* tf, uint32_t type) {
41     ASSERT_TRUE(type == EM_ARM || type == EM_386 || type == EM_X86_64);
42     size_t ehdr_size;
43     Elf32_Ehdr ehdr32;
44     Elf64_Ehdr ehdr64;
45     void* ptr;
46     if (type == EM_ARM || type == EM_386) {
47       ehdr_size = sizeof(ehdr32);
48       ptr = &ehdr32;
49       TestInitEhdr(&ehdr32, ELFCLASS32, type);
50     } else {
51       ehdr_size = sizeof(ehdr64);
52       ptr = &ehdr64;
53       TestInitEhdr(&ehdr64, ELFCLASS64, type);
54     }
55 
56     ASSERT_EQ(offset, static_cast<uint64_t>(lseek(tf->fd, offset, SEEK_SET)));
57     ASSERT_TRUE(android::base::WriteFully(tf->fd, ptr, ehdr_size));
58   }
59 
60   void VerifyWithinSameMap(bool cache_enabled);
61   void VerifySameMap(bool cache_enabled);
62   void VerifyWithinSameMapNeverReadAtZero(bool cache_enabled);
63 
64   static std::shared_ptr<Memory> memory_;
65 };
66 
67 std::shared_ptr<Memory> ElfCacheTest::memory_;
68 
VerifySameMap(bool cache_enabled)69 void ElfCacheTest::VerifySameMap(bool cache_enabled) {
70   if (!cache_enabled) {
71     Elf::SetCachingEnabled(false);
72   }
73 
74   TemporaryFile tf;
75   ASSERT_TRUE(tf.fd != -1);
76   WriteElfFile(0, &tf, EM_ARM);
77   close(tf.fd);
78 
79   uint64_t start = 0x1000;
80   uint64_t end = 0x20000;
81   MapInfo info1(nullptr, nullptr, start, end, 0, 0x5, tf.path);
82   MapInfo info2(nullptr, nullptr, start, end, 0, 0x5, tf.path);
83 
84   Elf* elf1 = info1.GetElf(memory_, ARCH_ARM);
85   ASSERT_TRUE(elf1->valid());
86   Elf* elf2 = info2.GetElf(memory_, ARCH_ARM);
87   ASSERT_TRUE(elf2->valid());
88 
89   if (cache_enabled) {
90     EXPECT_EQ(elf1, elf2);
91   } else {
92     EXPECT_NE(elf1, elf2);
93   }
94 }
95 
TEST_F(ElfCacheTest,no_caching)96 TEST_F(ElfCacheTest, no_caching) {
97   VerifySameMap(false);
98 }
99 
TEST_F(ElfCacheTest,caching_invalid_elf)100 TEST_F(ElfCacheTest, caching_invalid_elf) {
101   VerifySameMap(true);
102 }
103 
VerifyWithinSameMap(bool cache_enabled)104 void ElfCacheTest::VerifyWithinSameMap(bool cache_enabled) {
105   if (!cache_enabled) {
106     Elf::SetCachingEnabled(false);
107   }
108 
109   TemporaryFile tf;
110   ASSERT_TRUE(tf.fd != -1);
111   WriteElfFile(0, &tf, EM_ARM);
112   WriteElfFile(0x100, &tf, EM_386);
113   WriteElfFile(0x200, &tf, EM_X86_64);
114   lseek(tf.fd, 0x500, SEEK_SET);
115   uint8_t value = 0;
116   write(tf.fd, &value, 1);
117   close(tf.fd);
118 
119   uint64_t start = 0x1000;
120   uint64_t end = 0x20000;
121   // Will have an elf at offset 0 in file.
122   MapInfo info0_1(nullptr, nullptr, start, end, 0, 0x5, tf.path);
123   MapInfo info0_2(nullptr, nullptr, start, end, 0, 0x5, tf.path);
124   // Will have an elf at offset 0x100 in file.
125   MapInfo info100_1(nullptr, nullptr, start, end, 0x100, 0x5, tf.path);
126   MapInfo info100_2(nullptr, nullptr, start, end, 0x100, 0x5, tf.path);
127   // Will have an elf at offset 0x200 in file.
128   MapInfo info200_1(nullptr, nullptr, start, end, 0x200, 0x5, tf.path);
129   MapInfo info200_2(nullptr, nullptr, start, end, 0x200, 0x5, tf.path);
130   // Will have an elf at offset 0 in file.
131   MapInfo info300_1(nullptr, nullptr, start, end, 0x300, 0x5, tf.path);
132   MapInfo info300_2(nullptr, nullptr, start, end, 0x300, 0x5, tf.path);
133 
134   Elf* elf0_1 = info0_1.GetElf(memory_, ARCH_ARM);
135   ASSERT_TRUE(elf0_1->valid());
136   EXPECT_EQ(ARCH_ARM, elf0_1->arch());
137   Elf* elf0_2 = info0_2.GetElf(memory_, ARCH_ARM);
138   ASSERT_TRUE(elf0_2->valid());
139   EXPECT_EQ(ARCH_ARM, elf0_2->arch());
140   EXPECT_EQ(0U, info0_1.elf_offset());
141   EXPECT_EQ(0U, info0_2.elf_offset());
142   if (cache_enabled) {
143     EXPECT_EQ(elf0_1, elf0_2);
144   } else {
145     EXPECT_NE(elf0_1, elf0_2);
146   }
147 
148   Elf* elf100_1 = info100_1.GetElf(memory_, ARCH_X86);
149   ASSERT_TRUE(elf100_1->valid());
150   EXPECT_EQ(ARCH_X86, elf100_1->arch());
151   Elf* elf100_2 = info100_2.GetElf(memory_, ARCH_X86);
152   ASSERT_TRUE(elf100_2->valid());
153   EXPECT_EQ(ARCH_X86, elf100_2->arch());
154   EXPECT_EQ(0U, info100_1.elf_offset());
155   EXPECT_EQ(0U, info100_2.elf_offset());
156   if (cache_enabled) {
157     EXPECT_EQ(elf100_1, elf100_2);
158   } else {
159     EXPECT_NE(elf100_1, elf100_2);
160   }
161 
162   Elf* elf200_1 = info200_1.GetElf(memory_, ARCH_X86_64);
163   ASSERT_TRUE(elf200_1->valid());
164   EXPECT_EQ(ARCH_X86_64, elf200_1->arch());
165   Elf* elf200_2 = info200_2.GetElf(memory_, ARCH_X86_64);
166   ASSERT_TRUE(elf200_2->valid());
167   EXPECT_EQ(ARCH_X86_64, elf200_2->arch());
168   EXPECT_EQ(0U, info200_1.elf_offset());
169   EXPECT_EQ(0U, info200_2.elf_offset());
170   if (cache_enabled) {
171     EXPECT_EQ(elf200_1, elf200_2);
172   } else {
173     EXPECT_NE(elf200_1, elf200_2);
174   }
175 
176   Elf* elf300_1 = info300_1.GetElf(memory_, ARCH_ARM);
177   ASSERT_TRUE(elf300_1->valid());
178   EXPECT_EQ(ARCH_ARM, elf300_1->arch());
179   Elf* elf300_2 = info300_2.GetElf(memory_, ARCH_ARM);
180   ASSERT_TRUE(elf300_2->valid());
181   EXPECT_EQ(ARCH_ARM, elf300_2->arch());
182   EXPECT_EQ(0x300U, info300_1.elf_offset());
183   EXPECT_EQ(0x300U, info300_2.elf_offset());
184   if (cache_enabled) {
185     EXPECT_EQ(elf300_1, elf300_2);
186     EXPECT_EQ(elf0_1, elf300_1);
187   } else {
188     EXPECT_NE(elf300_1, elf300_2);
189     EXPECT_NE(elf0_1, elf300_1);
190   }
191 }
192 
TEST_F(ElfCacheTest,no_caching_valid_elf_offset_non_zero)193 TEST_F(ElfCacheTest, no_caching_valid_elf_offset_non_zero) {
194   VerifyWithinSameMap(false);
195 }
196 
TEST_F(ElfCacheTest,caching_valid_elf_offset_non_zero)197 TEST_F(ElfCacheTest, caching_valid_elf_offset_non_zero) {
198   VerifyWithinSameMap(true);
199 }
200 
201 // Verify that when reading from multiple non-zero offsets in the same map
202 // that when cached, all of the elf objects are the same.
VerifyWithinSameMapNeverReadAtZero(bool cache_enabled)203 void ElfCacheTest::VerifyWithinSameMapNeverReadAtZero(bool cache_enabled) {
204   if (!cache_enabled) {
205     Elf::SetCachingEnabled(false);
206   }
207 
208   TemporaryFile tf;
209   ASSERT_TRUE(tf.fd != -1);
210   WriteElfFile(0, &tf, EM_ARM);
211   lseek(tf.fd, 0x500, SEEK_SET);
212   uint8_t value = 0;
213   write(tf.fd, &value, 1);
214   close(tf.fd);
215 
216   uint64_t start = 0x1000;
217   uint64_t end = 0x20000;
218   // Multiple info sections at different offsets will have non-zero elf offsets.
219   MapInfo info300_1(nullptr, nullptr, start, end, 0x300, 0x5, tf.path);
220   MapInfo info300_2(nullptr, nullptr, start, end, 0x300, 0x5, tf.path);
221   MapInfo info400_1(nullptr, nullptr, start, end, 0x400, 0x5, tf.path);
222   MapInfo info400_2(nullptr, nullptr, start, end, 0x400, 0x5, tf.path);
223 
224   Elf* elf300_1 = info300_1.GetElf(memory_, ARCH_ARM);
225   ASSERT_TRUE(elf300_1->valid());
226   EXPECT_EQ(ARCH_ARM, elf300_1->arch());
227   Elf* elf300_2 = info300_2.GetElf(memory_, ARCH_ARM);
228   ASSERT_TRUE(elf300_2->valid());
229   EXPECT_EQ(ARCH_ARM, elf300_2->arch());
230   EXPECT_EQ(0x300U, info300_1.elf_offset());
231   EXPECT_EQ(0x300U, info300_2.elf_offset());
232   if (cache_enabled) {
233     EXPECT_EQ(elf300_1, elf300_2);
234   } else {
235     EXPECT_NE(elf300_1, elf300_2);
236   }
237 
238   Elf* elf400_1 = info400_1.GetElf(memory_, ARCH_ARM);
239   ASSERT_TRUE(elf400_1->valid());
240   EXPECT_EQ(ARCH_ARM, elf400_1->arch());
241   Elf* elf400_2 = info400_2.GetElf(memory_, ARCH_ARM);
242   ASSERT_TRUE(elf400_2->valid());
243   EXPECT_EQ(ARCH_ARM, elf400_2->arch());
244   EXPECT_EQ(0x400U, info400_1.elf_offset());
245   EXPECT_EQ(0x400U, info400_2.elf_offset());
246   if (cache_enabled) {
247     EXPECT_EQ(elf400_1, elf400_2);
248     EXPECT_EQ(elf300_1, elf400_1);
249   } else {
250     EXPECT_NE(elf400_1, elf400_2);
251     EXPECT_NE(elf300_1, elf400_1);
252   }
253 }
254 
TEST_F(ElfCacheTest,no_caching_valid_elf_offset_non_zero_never_read_at_zero)255 TEST_F(ElfCacheTest, no_caching_valid_elf_offset_non_zero_never_read_at_zero) {
256   VerifyWithinSameMapNeverReadAtZero(false);
257 }
258 
TEST_F(ElfCacheTest,caching_valid_elf_offset_non_zero_never_read_at_zero)259 TEST_F(ElfCacheTest, caching_valid_elf_offset_non_zero_never_read_at_zero) {
260   VerifyWithinSameMapNeverReadAtZero(true);
261 }
262 
263 }  // namespace unwindstack
264