1 /*
2  * Copyright (C) 2016 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 <vector>
20 
21 #include <gtest/gtest.h>
22 
23 #include <unwindstack/DwarfError.h>
24 
25 #include "DwarfDebugFrame.h"
26 #include "DwarfEncoding.h"
27 
28 #include "LogFake.h"
29 #include "MemoryFake.h"
30 
31 namespace unwindstack {
32 
33 template <typename TypeParam>
34 class DwarfDebugFrameTest : public ::testing::Test {
35  protected:
SetUp()36   void SetUp() override {
37     memory_.Clear();
38     debug_frame_ = new DwarfDebugFrame<TypeParam>(&memory_);
39     ResetLogs();
40   }
41 
TearDown()42   void TearDown() override { delete debug_frame_; }
43 
44   MemoryFake memory_;
45   DwarfDebugFrame<TypeParam>* debug_frame_ = nullptr;
46 };
47 TYPED_TEST_SUITE_P(DwarfDebugFrameTest);
48 
49 // NOTE: All test class variables need to be referenced as this->.
50 
SetCie32(MemoryFake * memory,uint64_t offset,uint32_t length,std::vector<uint8_t> data)51 static void SetCie32(MemoryFake* memory, uint64_t offset, uint32_t length,
52                      std::vector<uint8_t> data) {
53   memory->SetData32(offset, length);
54   offset += 4;
55   // Indicates this is a cie.
56   memory->SetData32(offset, 0xffffffff);
57   offset += 4;
58   memory->SetMemory(offset, data);
59 }
60 
SetCie64(MemoryFake * memory,uint64_t offset,uint64_t length,std::vector<uint8_t> data)61 static void SetCie64(MemoryFake* memory, uint64_t offset, uint64_t length,
62                      std::vector<uint8_t> data) {
63   memory->SetData32(offset, 0xffffffff);
64   offset += 4;
65   memory->SetData64(offset, length);
66   offset += 8;
67   // Indicates this is a cie.
68   memory->SetData64(offset, 0xffffffffffffffffUL);
69   offset += 8;
70   memory->SetMemory(offset, data);
71 }
72 
SetFde32(MemoryFake * memory,uint64_t offset,uint32_t length,uint64_t cie_offset,uint32_t pc_start,uint32_t pc_length,uint64_t segment_length=0,std::vector<uint8_t> * data=nullptr)73 static void SetFde32(MemoryFake* memory, uint64_t offset, uint32_t length, uint64_t cie_offset,
74                      uint32_t pc_start, uint32_t pc_length, uint64_t segment_length = 0,
75                      std::vector<uint8_t>* data = nullptr) {
76   memory->SetData32(offset, length);
77   offset += 4;
78   memory->SetData32(offset, cie_offset);
79   offset += 4 + segment_length;
80   memory->SetData32(offset, pc_start);
81   offset += 4;
82   memory->SetData32(offset, pc_length);
83   if (data != nullptr) {
84     offset += 4;
85     memory->SetMemory(offset, *data);
86   }
87 }
88 
SetFde64(MemoryFake * memory,uint64_t offset,uint64_t length,uint64_t cie_offset,uint64_t pc_start,uint64_t pc_length,uint64_t segment_length=0,std::vector<uint8_t> * data=nullptr)89 static void SetFde64(MemoryFake* memory, uint64_t offset, uint64_t length, uint64_t cie_offset,
90                      uint64_t pc_start, uint64_t pc_length, uint64_t segment_length = 0,
91                      std::vector<uint8_t>* data = nullptr) {
92   memory->SetData32(offset, 0xffffffff);
93   offset += 4;
94   memory->SetData64(offset, length);
95   offset += 8;
96   memory->SetData64(offset, cie_offset);
97   offset += 8 + segment_length;
98   memory->SetData64(offset, pc_start);
99   offset += 8;
100   memory->SetData64(offset, pc_length);
101   if (data != nullptr) {
102     offset += 8;
103     memory->SetMemory(offset, *data);
104   }
105 }
106 
SetFourFdes32(MemoryFake * memory)107 static void SetFourFdes32(MemoryFake* memory) {
108   SetCie32(memory, 0x5000, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1});
109 
110   // FDE 32 information.
111   SetFde32(memory, 0x5100, 0xfc, 0, 0x1500, 0x200);
112   SetFde32(memory, 0x5200, 0xfc, 0, 0x2500, 0x300);
113 
114   // CIE 32 information.
115   SetCie32(memory, 0x5300, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1});
116 
117   // FDE 32 information.
118   SetFde32(memory, 0x5400, 0xfc, 0x300, 0x3500, 0x400);
119   SetFde32(memory, 0x5500, 0xfc, 0x300, 0x4500, 0x500);
120 }
121 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdes32)122 TYPED_TEST_P(DwarfDebugFrameTest, GetFdes32) {
123   SetFourFdes32(&this->memory_);
124   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
125 
126   std::vector<const DwarfFde*> fdes;
127   this->debug_frame_->GetFdes(&fdes);
128 
129   ASSERT_EQ(4U, fdes.size());
130 
131   EXPECT_EQ(0x5000U, fdes[0]->cie_offset);
132   EXPECT_EQ(0x5110U, fdes[0]->cfa_instructions_offset);
133   EXPECT_EQ(0x5200U, fdes[0]->cfa_instructions_end);
134   EXPECT_EQ(0x1500U, fdes[0]->pc_start);
135   EXPECT_EQ(0x1700U, fdes[0]->pc_end);
136   EXPECT_EQ(0U, fdes[0]->lsda_address);
137   EXPECT_TRUE(fdes[0]->cie != nullptr);
138 
139   EXPECT_EQ(0x5000U, fdes[1]->cie_offset);
140   EXPECT_EQ(0x5210U, fdes[1]->cfa_instructions_offset);
141   EXPECT_EQ(0x5300U, fdes[1]->cfa_instructions_end);
142   EXPECT_EQ(0x2500U, fdes[1]->pc_start);
143   EXPECT_EQ(0x2800U, fdes[1]->pc_end);
144   EXPECT_EQ(0U, fdes[1]->lsda_address);
145   EXPECT_TRUE(fdes[1]->cie != nullptr);
146 
147   EXPECT_EQ(0x5300U, fdes[2]->cie_offset);
148   EXPECT_EQ(0x5410U, fdes[2]->cfa_instructions_offset);
149   EXPECT_EQ(0x5500U, fdes[2]->cfa_instructions_end);
150   EXPECT_EQ(0x3500U, fdes[2]->pc_start);
151   EXPECT_EQ(0x3900U, fdes[2]->pc_end);
152   EXPECT_EQ(0U, fdes[2]->lsda_address);
153   EXPECT_TRUE(fdes[2]->cie != nullptr);
154 
155   EXPECT_EQ(0x5300U, fdes[3]->cie_offset);
156   EXPECT_EQ(0x5510U, fdes[3]->cfa_instructions_offset);
157   EXPECT_EQ(0x5600U, fdes[3]->cfa_instructions_end);
158   EXPECT_EQ(0x4500U, fdes[3]->pc_start);
159   EXPECT_EQ(0x4a00U, fdes[3]->pc_end);
160   EXPECT_EQ(0U, fdes[3]->lsda_address);
161   EXPECT_TRUE(fdes[3]->cie != nullptr);
162 }
163 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdes32_after_GetFdeFromPc)164 TYPED_TEST_P(DwarfDebugFrameTest, GetFdes32_after_GetFdeFromPc) {
165   SetFourFdes32(&this->memory_);
166   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
167 
168   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x3600);
169   ASSERT_TRUE(fde != nullptr);
170   EXPECT_EQ(0x3500U, fde->pc_start);
171   EXPECT_EQ(0x3900U, fde->pc_end);
172 
173   std::vector<const DwarfFde*> fdes;
174   this->debug_frame_->GetFdes(&fdes);
175   ASSERT_EQ(4U, fdes.size());
176 
177   // Verify that they got added in the correct order.
178   EXPECT_EQ(0x1500U, fdes[0]->pc_start);
179   EXPECT_EQ(0x1700U, fdes[0]->pc_end);
180   EXPECT_EQ(0x2500U, fdes[1]->pc_start);
181   EXPECT_EQ(0x2800U, fdes[1]->pc_end);
182   EXPECT_EQ(0x3500U, fdes[2]->pc_start);
183   EXPECT_EQ(0x3900U, fdes[2]->pc_end);
184   EXPECT_EQ(0x4500U, fdes[3]->pc_start);
185   EXPECT_EQ(0x4a00U, fdes[3]->pc_end);
186 }
187 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdes32_not_in_section)188 TYPED_TEST_P(DwarfDebugFrameTest, GetFdes32_not_in_section) {
189   SetFourFdes32(&this->memory_);
190   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0));
191 
192   std::vector<const DwarfFde*> fdes;
193   this->debug_frame_->GetFdes(&fdes);
194 
195   ASSERT_EQ(3U, fdes.size());
196 }
197 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromPc32)198 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc32) {
199   SetFourFdes32(&this->memory_);
200   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
201 
202   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x1600);
203   ASSERT_TRUE(fde != nullptr);
204   EXPECT_EQ(0x1500U, fde->pc_start);
205 
206   fde = this->debug_frame_->GetFdeFromPc(0x2600);
207   ASSERT_TRUE(fde != nullptr);
208   EXPECT_EQ(0x2500U, fde->pc_start);
209 
210   fde = this->debug_frame_->GetFdeFromPc(0x3600);
211   ASSERT_TRUE(fde != nullptr);
212   EXPECT_EQ(0x3500U, fde->pc_start);
213 
214   fde = this->debug_frame_->GetFdeFromPc(0x4600);
215   ASSERT_TRUE(fde != nullptr);
216   EXPECT_EQ(0x4500U, fde->pc_start);
217 
218   fde = this->debug_frame_->GetFdeFromPc(0);
219   ASSERT_TRUE(fde == nullptr);
220 }
221 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromPc32_reverse)222 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc32_reverse) {
223   SetFourFdes32(&this->memory_);
224   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
225 
226   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600);
227   ASSERT_TRUE(fde != nullptr);
228   EXPECT_EQ(0x4500U, fde->pc_start);
229 
230   fde = this->debug_frame_->GetFdeFromPc(0x3600);
231   ASSERT_TRUE(fde != nullptr);
232   EXPECT_EQ(0x3500U, fde->pc_start);
233 
234   fde = this->debug_frame_->GetFdeFromPc(0x2600);
235   ASSERT_TRUE(fde != nullptr);
236   EXPECT_EQ(0x2500U, fde->pc_start);
237 
238   fde = this->debug_frame_->GetFdeFromPc(0x1600);
239   ASSERT_TRUE(fde != nullptr);
240   EXPECT_EQ(0x1500U, fde->pc_start);
241 
242   fde = this->debug_frame_->GetFdeFromPc(0);
243   ASSERT_TRUE(fde == nullptr);
244 }
245 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromPc32_not_in_section)246 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc32_not_in_section) {
247   SetFourFdes32(&this->memory_);
248   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0));
249 
250   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600);
251   ASSERT_TRUE(fde == nullptr);
252 }
253 
SetFourFdes64(MemoryFake * memory)254 static void SetFourFdes64(MemoryFake* memory) {
255   // CIE 64 information.
256   SetCie64(memory, 0x5000, 0xf4, std::vector<uint8_t>{1, '\0', 0, 0, 1});
257 
258   // FDE 64 information.
259   SetFde64(memory, 0x5100, 0xf4, 0, 0x1500, 0x200);
260   SetFde64(memory, 0x5200, 0xf4, 0, 0x2500, 0x300);
261 
262   // CIE 64 information.
263   SetCie64(memory, 0x5300, 0xf4, std::vector<uint8_t>{1, '\0', 0, 0, 1});
264 
265   // FDE 64 information.
266   SetFde64(memory, 0x5400, 0xf4, 0x300, 0x3500, 0x400);
267   SetFde64(memory, 0x5500, 0xf4, 0x300, 0x4500, 0x500);
268 }
269 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdes64)270 TYPED_TEST_P(DwarfDebugFrameTest, GetFdes64) {
271   SetFourFdes64(&this->memory_);
272   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
273 
274   std::vector<const DwarfFde*> fdes;
275   this->debug_frame_->GetFdes(&fdes);
276 
277   ASSERT_EQ(4U, fdes.size());
278 
279   EXPECT_EQ(0x5000U, fdes[0]->cie_offset);
280   EXPECT_EQ(0x5124U, fdes[0]->cfa_instructions_offset);
281   EXPECT_EQ(0x5200U, fdes[0]->cfa_instructions_end);
282   EXPECT_EQ(0x1500U, fdes[0]->pc_start);
283   EXPECT_EQ(0x1700U, fdes[0]->pc_end);
284   EXPECT_EQ(0U, fdes[0]->lsda_address);
285   EXPECT_TRUE(fdes[0]->cie != nullptr);
286 
287   EXPECT_EQ(0x5000U, fdes[1]->cie_offset);
288   EXPECT_EQ(0x5224U, fdes[1]->cfa_instructions_offset);
289   EXPECT_EQ(0x5300U, fdes[1]->cfa_instructions_end);
290   EXPECT_EQ(0x2500U, fdes[1]->pc_start);
291   EXPECT_EQ(0x2800U, fdes[1]->pc_end);
292   EXPECT_EQ(0U, fdes[1]->lsda_address);
293   EXPECT_TRUE(fdes[1]->cie != nullptr);
294 
295   EXPECT_EQ(0x5300U, fdes[2]->cie_offset);
296   EXPECT_EQ(0x5424U, fdes[2]->cfa_instructions_offset);
297   EXPECT_EQ(0x5500U, fdes[2]->cfa_instructions_end);
298   EXPECT_EQ(0x3500U, fdes[2]->pc_start);
299   EXPECT_EQ(0x3900U, fdes[2]->pc_end);
300   EXPECT_EQ(0U, fdes[2]->lsda_address);
301   EXPECT_TRUE(fdes[2]->cie != nullptr);
302 
303   EXPECT_EQ(0x5300U, fdes[3]->cie_offset);
304   EXPECT_EQ(0x5524U, fdes[3]->cfa_instructions_offset);
305   EXPECT_EQ(0x5600U, fdes[3]->cfa_instructions_end);
306   EXPECT_EQ(0x4500U, fdes[3]->pc_start);
307   EXPECT_EQ(0x4a00U, fdes[3]->pc_end);
308   EXPECT_EQ(0U, fdes[3]->lsda_address);
309   EXPECT_TRUE(fdes[3]->cie != nullptr);
310 }
311 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdes64_after_GetFdeFromPc)312 TYPED_TEST_P(DwarfDebugFrameTest, GetFdes64_after_GetFdeFromPc) {
313   SetFourFdes64(&this->memory_);
314   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
315 
316   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x2600);
317   ASSERT_TRUE(fde != nullptr);
318   EXPECT_EQ(0x2500U, fde->pc_start);
319   EXPECT_EQ(0x2800U, fde->pc_end);
320 
321   std::vector<const DwarfFde*> fdes;
322   this->debug_frame_->GetFdes(&fdes);
323   ASSERT_EQ(4U, fdes.size());
324 
325   // Verify that they got added in the correct order.
326   EXPECT_EQ(0x1500U, fdes[0]->pc_start);
327   EXPECT_EQ(0x1700U, fdes[0]->pc_end);
328   EXPECT_EQ(0x2500U, fdes[1]->pc_start);
329   EXPECT_EQ(0x2800U, fdes[1]->pc_end);
330   EXPECT_EQ(0x3500U, fdes[2]->pc_start);
331   EXPECT_EQ(0x3900U, fdes[2]->pc_end);
332   EXPECT_EQ(0x4500U, fdes[3]->pc_start);
333   EXPECT_EQ(0x4a00U, fdes[3]->pc_end);
334 }
335 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdes64_not_in_section)336 TYPED_TEST_P(DwarfDebugFrameTest, GetFdes64_not_in_section) {
337   SetFourFdes64(&this->memory_);
338   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0));
339 
340   std::vector<const DwarfFde*> fdes;
341   this->debug_frame_->GetFdes(&fdes);
342 
343   ASSERT_EQ(3U, fdes.size());
344 }
345 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromPc64)346 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc64) {
347   SetFourFdes64(&this->memory_);
348   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
349 
350   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x1600);
351   ASSERT_TRUE(fde != nullptr);
352   EXPECT_EQ(0x1500U, fde->pc_start);
353 
354   fde = this->debug_frame_->GetFdeFromPc(0x2600);
355   ASSERT_TRUE(fde != nullptr);
356   EXPECT_EQ(0x2500U, fde->pc_start);
357 
358   fde = this->debug_frame_->GetFdeFromPc(0x3600);
359   ASSERT_TRUE(fde != nullptr);
360   EXPECT_EQ(0x3500U, fde->pc_start);
361 
362   fde = this->debug_frame_->GetFdeFromPc(0x4600);
363   ASSERT_TRUE(fde != nullptr);
364   EXPECT_EQ(0x4500U, fde->pc_start);
365 
366   fde = this->debug_frame_->GetFdeFromPc(0);
367   ASSERT_TRUE(fde == nullptr);
368 }
369 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromPc64_reverse)370 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc64_reverse) {
371   SetFourFdes64(&this->memory_);
372   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
373 
374   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600);
375   ASSERT_TRUE(fde != nullptr);
376   EXPECT_EQ(0x4500U, fde->pc_start);
377 
378   fde = this->debug_frame_->GetFdeFromPc(0x3600);
379   ASSERT_TRUE(fde != nullptr);
380   EXPECT_EQ(0x3500U, fde->pc_start);
381 
382   fde = this->debug_frame_->GetFdeFromPc(0x2600);
383   ASSERT_TRUE(fde != nullptr);
384   EXPECT_EQ(0x2500U, fde->pc_start);
385 
386   fde = this->debug_frame_->GetFdeFromPc(0x1600);
387   ASSERT_TRUE(fde != nullptr);
388   EXPECT_EQ(0x1500U, fde->pc_start);
389 
390   fde = this->debug_frame_->GetFdeFromPc(0);
391   ASSERT_TRUE(fde == nullptr);
392 }
393 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromPc64_not_in_section)394 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc64_not_in_section) {
395   SetFourFdes64(&this->memory_);
396   ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0));
397 
398   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600);
399   ASSERT_TRUE(fde == nullptr);
400 }
401 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFde32)402 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde32) {
403   SetCie32(&this->memory_, 0xf000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
404   SetFde32(&this->memory_, 0x14000, 0x20, 0xf000, 0x9000, 0x100);
405 
406   const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x14000);
407   ASSERT_TRUE(fde != nullptr);
408   EXPECT_EQ(0x14010U, fde->cfa_instructions_offset);
409   EXPECT_EQ(0x14024U, fde->cfa_instructions_end);
410   EXPECT_EQ(0x9000U, fde->pc_start);
411   EXPECT_EQ(0x9100U, fde->pc_end);
412   EXPECT_EQ(0xf000U, fde->cie_offset);
413   EXPECT_EQ(0U, fde->lsda_address);
414 
415   ASSERT_TRUE(fde->cie != nullptr);
416   EXPECT_EQ(1U, fde->cie->version);
417   EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding);
418   EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
419   EXPECT_EQ(0U, fde->cie->segment_size);
420   EXPECT_EQ(1U, fde->cie->augmentation_string.size());
421   EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
422   EXPECT_EQ(0U, fde->cie->personality_handler);
423   EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset);
424   EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end);
425   EXPECT_EQ(4U, fde->cie->code_alignment_factor);
426   EXPECT_EQ(8, fde->cie->data_alignment_factor);
427   EXPECT_EQ(0x20U, fde->cie->return_address_register);
428 }
429 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFde64)430 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde64) {
431   SetCie64(&this->memory_, 0x6000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
432   SetFde64(&this->memory_, 0x8000, 0x200, 0x6000, 0x5000, 0x300);
433 
434   const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x8000);
435   ASSERT_TRUE(fde != nullptr);
436   EXPECT_EQ(0x8024U, fde->cfa_instructions_offset);
437   EXPECT_EQ(0x820cU, fde->cfa_instructions_end);
438   EXPECT_EQ(0x5000U, fde->pc_start);
439   EXPECT_EQ(0x5300U, fde->pc_end);
440   EXPECT_EQ(0x6000U, fde->cie_offset);
441   EXPECT_EQ(0U, fde->lsda_address);
442 
443   ASSERT_TRUE(fde->cie != nullptr);
444   EXPECT_EQ(1U, fde->cie->version);
445   EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding);
446   EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
447   EXPECT_EQ(0U, fde->cie->segment_size);
448   EXPECT_EQ(1U, fde->cie->augmentation_string.size());
449   EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
450   EXPECT_EQ(0U, fde->cie->personality_handler);
451   EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset);
452   EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end);
453   EXPECT_EQ(4U, fde->cie->code_alignment_factor);
454   EXPECT_EQ(8, fde->cie->data_alignment_factor);
455   EXPECT_EQ(0x20U, fde->cie->return_address_register);
456 }
457 
VerifyCieVersion(const DwarfCie * cie,uint8_t version,uint8_t segment_size,uint8_t fde_encoding,uint64_t return_address,uint64_t start_offset,uint64_t end_offset)458 static void VerifyCieVersion(const DwarfCie* cie, uint8_t version, uint8_t segment_size,
459                              uint8_t fde_encoding, uint64_t return_address, uint64_t start_offset,
460                              uint64_t end_offset) {
461   EXPECT_EQ(version, cie->version);
462   EXPECT_EQ(fde_encoding, cie->fde_address_encoding);
463   EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
464   EXPECT_EQ(segment_size, cie->segment_size);
465   EXPECT_EQ(1U, cie->augmentation_string.size());
466   EXPECT_EQ('\0', cie->augmentation_string[0]);
467   EXPECT_EQ(0U, cie->personality_handler);
468   EXPECT_EQ(4U, cie->code_alignment_factor);
469   EXPECT_EQ(8, cie->data_alignment_factor);
470   EXPECT_EQ(return_address, cie->return_address_register);
471   EXPECT_EQ(0x5000U + start_offset, cie->cfa_instructions_offset);
472   EXPECT_EQ(0x5000U + end_offset, cie->cfa_instructions_end);
473 }
474 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset32_cie_cached)475 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_cie_cached) {
476   SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
477   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
478   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
479   ASSERT_TRUE(cie != nullptr);
480   VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata4, 0x20, 0xd, 0x104);
481 
482   std::vector<uint8_t> zero(0x100, 0);
483   this->memory_.SetMemory(0x5000, zero);
484   cie = this->debug_frame_->GetCieFromOffset(0x5000);
485   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
486   ASSERT_TRUE(cie != nullptr);
487   VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata4, 0x20, 0xd, 0x104);
488 }
489 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset64_cie_cached)490 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_cie_cached) {
491   SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
492   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
493   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
494   ASSERT_TRUE(cie != nullptr);
495   VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata8, 0x20, 0x19, 0x10c);
496 
497   std::vector<uint8_t> zero(0x100, 0);
498   this->memory_.SetMemory(0x5000, zero);
499   cie = this->debug_frame_->GetCieFromOffset(0x5000);
500   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
501   ASSERT_TRUE(cie != nullptr);
502   VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata8, 0x20, 0x19, 0x10c);
503 }
504 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset32_version1)505 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version1) {
506   SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
507   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
508   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
509   ASSERT_TRUE(cie != nullptr);
510   VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata4, 0x20, 0xd, 0x104);
511 }
512 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset64_version1)513 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version1) {
514   SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
515   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
516   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
517   ASSERT_TRUE(cie != nullptr);
518   VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata8, 0x20, 0x19, 0x10c);
519 }
520 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset32_version3)521 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version3) {
522   SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{3, '\0', 4, 8, 0x81, 3});
523   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
524   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
525   ASSERT_TRUE(cie != nullptr);
526   VerifyCieVersion(cie, 3, 0, DW_EH_PE_sdata4, 0x181, 0xe, 0x104);
527 }
528 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset64_version3)529 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version3) {
530   SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{3, '\0', 4, 8, 0x81, 3});
531   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
532   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
533   ASSERT_TRUE(cie != nullptr);
534   VerifyCieVersion(cie, 3, 0, DW_EH_PE_sdata8, 0x181, 0x1a, 0x10c);
535 }
536 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset32_version4)537 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version4) {
538   SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{4, '\0', 0, 10, 4, 8, 0x81, 3});
539   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
540   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
541   ASSERT_TRUE(cie != nullptr);
542   VerifyCieVersion(cie, 4, 10, DW_EH_PE_sdata4, 0x181, 0x10, 0x104);
543 }
544 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset64_version4)545 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version4) {
546   SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{4, '\0', 0, 10, 4, 8, 0x81, 3});
547   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
548   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
549   ASSERT_TRUE(cie != nullptr);
550   VerifyCieVersion(cie, 4, 10, DW_EH_PE_sdata8, 0x181, 0x1c, 0x10c);
551 }
552 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset32_version5)553 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version5) {
554   SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{5, '\0', 0, 10, 4, 8, 0x81, 3});
555   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
556   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
557   ASSERT_TRUE(cie != nullptr);
558   VerifyCieVersion(cie, 5, 10, DW_EH_PE_sdata4, 0x181, 0x10, 0x104);
559 }
560 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset64_version5)561 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version5) {
562   SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{5, '\0', 0, 10, 4, 8, 0x81, 3});
563   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
564   EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
565   ASSERT_TRUE(cie != nullptr);
566   VerifyCieVersion(cie, 5, 10, DW_EH_PE_sdata8, 0x181, 0x1c, 0x10c);
567 }
568 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset_version_invalid)569 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset_version_invalid) {
570   SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{0, '\0', 1, 2, 3, 4, 5, 6, 7});
571   ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x5000) == nullptr);
572   EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode());
573   SetCie64(&this->memory_, 0x6000, 0x100, std::vector<uint8_t>{0, '\0', 1, 2, 3, 4, 5, 6, 7});
574   ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x6000) == nullptr);
575   EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode());
576 
577   SetCie32(&this->memory_, 0x7000, 0x100, std::vector<uint8_t>{6, '\0', 1, 2, 3, 4, 5, 6, 7});
578   ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x7000) == nullptr);
579   EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode());
580   SetCie64(&this->memory_, 0x8000, 0x100, std::vector<uint8_t>{6, '\0', 1, 2, 3, 4, 5, 6, 7});
581   ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x8000) == nullptr);
582   EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode());
583 }
584 
VerifyCieAugment(const DwarfCie * cie,uint64_t inst_offset,uint64_t inst_end)585 static void VerifyCieAugment(const DwarfCie* cie, uint64_t inst_offset, uint64_t inst_end) {
586   EXPECT_EQ(1U, cie->version);
587   EXPECT_EQ(DW_EH_PE_udata2, cie->fde_address_encoding);
588   EXPECT_EQ(DW_EH_PE_textrel | DW_EH_PE_udata2, cie->lsda_encoding);
589   EXPECT_EQ(0U, cie->segment_size);
590   EXPECT_EQ(5U, cie->augmentation_string.size());
591   EXPECT_EQ('z', cie->augmentation_string[0]);
592   EXPECT_EQ('L', cie->augmentation_string[1]);
593   EXPECT_EQ('P', cie->augmentation_string[2]);
594   EXPECT_EQ('R', cie->augmentation_string[3]);
595   EXPECT_EQ('\0', cie->augmentation_string[4]);
596   EXPECT_EQ(0x12345678U, cie->personality_handler);
597   EXPECT_EQ(4U, cie->code_alignment_factor);
598   EXPECT_EQ(8, cie->data_alignment_factor);
599   EXPECT_EQ(0x10U, cie->return_address_register);
600   EXPECT_EQ(inst_offset, cie->cfa_instructions_offset);
601   EXPECT_EQ(inst_end, cie->cfa_instructions_end);
602 }
603 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset32_augment)604 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_augment) {
605   SetCie32(&this->memory_, 0x5000, 0x100,
606            std::vector<uint8_t>{/* version */ 1,
607                                 /* augment string */ 'z', 'L', 'P', 'R', '\0',
608                                 /* code alignment factor */ 4,
609                                 /* data alignment factor */ 8,
610                                 /* return address register */ 0x10,
611                                 /* augment length */ 0xf,
612                                 /* L data */ DW_EH_PE_textrel | DW_EH_PE_udata2,
613                                 /* P data */ DW_EH_PE_udata4, 0x78, 0x56, 0x34, 0x12,
614                                 /* R data */ DW_EH_PE_udata2});
615 
616   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
617   ASSERT_TRUE(cie != nullptr);
618   VerifyCieAugment(cie, 0x5021, 0x5104);
619 }
620 
TYPED_TEST_P(DwarfDebugFrameTest,GetCieFromOffset64_augment)621 TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_augment) {
622   SetCie64(&this->memory_, 0x5000, 0x100,
623            std::vector<uint8_t>{/* version */ 1,
624                                 /* augment string */ 'z', 'L', 'P', 'R', '\0',
625                                 /* code alignment factor */ 4,
626                                 /* data alignment factor */ 8,
627                                 /* return address register */ 0x10,
628                                 /* augment length */ 0xf,
629                                 /* L data */ DW_EH_PE_textrel | DW_EH_PE_udata2,
630                                 /* P data */ DW_EH_PE_udata4, 0x78, 0x56, 0x34, 0x12,
631                                 /* R data */ DW_EH_PE_udata2});
632 
633   const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
634   ASSERT_TRUE(cie != nullptr);
635   VerifyCieAugment(cie, 0x502d, 0x510c);
636 }
637 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromOffset32_augment)638 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset32_augment) {
639   SetCie32(&this->memory_, 0x5000, 0xfc,
640            std::vector<uint8_t>{/* version */ 4,
641                                 /* augment string */ 'z', '\0',
642                                 /* address size */ 8,
643                                 /* segment size */ 0x10,
644                                 /* code alignment factor */ 16,
645                                 /* data alignment factor */ 32,
646                                 /* return address register */ 10,
647                                 /* augment length */ 0x0});
648 
649   std::vector<uint8_t> data{/* augment length */ 0x80, 0x3};
650   SetFde32(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0x10, &data);
651 
652   const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200);
653   ASSERT_TRUE(fde != nullptr);
654   ASSERT_TRUE(fde->cie != nullptr);
655   EXPECT_EQ(4U, fde->cie->version);
656   EXPECT_EQ(0x5000U, fde->cie_offset);
657   EXPECT_EQ(0x53a2U, fde->cfa_instructions_offset);
658   EXPECT_EQ(0x5504U, fde->cfa_instructions_end);
659   EXPECT_EQ(0x4300U, fde->pc_start);
660   EXPECT_EQ(0x4600U, fde->pc_end);
661   EXPECT_EQ(0U, fde->lsda_address);
662 }
663 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromOffset64_augment)664 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset64_augment) {
665   SetCie64(&this->memory_, 0x5000, 0xfc,
666            std::vector<uint8_t>{/* version */ 4,
667                                 /* augment string */ 'z', '\0',
668                                 /* address size */ 8,
669                                 /* segment size */ 0x10,
670                                 /* code alignment factor */ 16,
671                                 /* data alignment factor */ 32,
672                                 /* return address register */ 10,
673                                 /* augment length */ 0x0});
674 
675   std::vector<uint8_t> data{/* augment length */ 0x80, 0x3};
676   SetFde64(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0x10, &data);
677 
678   const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200);
679   ASSERT_TRUE(fde != nullptr);
680   ASSERT_TRUE(fde->cie != nullptr);
681   EXPECT_EQ(4U, fde->cie->version);
682   EXPECT_EQ(0x5000U, fde->cie_offset);
683   EXPECT_EQ(0x53b6U, fde->cfa_instructions_offset);
684   EXPECT_EQ(0x550cU, fde->cfa_instructions_end);
685   EXPECT_EQ(0x4300U, fde->pc_start);
686   EXPECT_EQ(0x4600U, fde->pc_end);
687   EXPECT_EQ(0U, fde->lsda_address);
688 }
689 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromOffset32_lsda_address)690 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset32_lsda_address) {
691   SetCie32(&this->memory_, 0x5000, 0xfc,
692            std::vector<uint8_t>{/* version */ 1,
693                                 /* augment string */ 'z', 'L', '\0',
694                                 /* address size */ 8,
695                                 /* code alignment factor */ 16,
696                                 /* data alignment factor */ 32,
697                                 /* return address register */ 10,
698                                 /* augment length */ 0x2,
699                                 /* L data */ DW_EH_PE_udata2});
700 
701   std::vector<uint8_t> data{/* augment length */ 0x80, 0x3,
702                             /* lsda address */ 0x20, 0x45};
703   SetFde32(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0, &data);
704 
705   const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200);
706   ASSERT_TRUE(fde != nullptr);
707   ASSERT_TRUE(fde->cie != nullptr);
708   EXPECT_EQ(1U, fde->cie->version);
709   EXPECT_EQ(0x5000U, fde->cie_offset);
710   EXPECT_EQ(0x5392U, fde->cfa_instructions_offset);
711   EXPECT_EQ(0x5504U, fde->cfa_instructions_end);
712   EXPECT_EQ(0x4300U, fde->pc_start);
713   EXPECT_EQ(0x4600U, fde->pc_end);
714   EXPECT_EQ(0x4520U, fde->lsda_address);
715 }
716 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromOffset64_lsda_address)717 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset64_lsda_address) {
718   SetCie64(&this->memory_, 0x5000, 0xfc,
719            std::vector<uint8_t>{/* version */ 1,
720                                 /* augment string */ 'z', 'L', '\0',
721                                 /* address size */ 8,
722                                 /* code alignment factor */ 16,
723                                 /* data alignment factor */ 32,
724                                 /* return address register */ 10,
725                                 /* augment length */ 0x2,
726                                 /* L data */ DW_EH_PE_udata2});
727 
728   std::vector<uint8_t> data{/* augment length */ 0x80, 0x3,
729                             /* lsda address */ 0x20, 0x45};
730   SetFde64(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0, &data);
731 
732   const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200);
733   ASSERT_TRUE(fde != nullptr);
734   ASSERT_TRUE(fde->cie != nullptr);
735   EXPECT_EQ(1U, fde->cie->version);
736   EXPECT_EQ(0x5000U, fde->cie_offset);
737   EXPECT_EQ(0x53a6U, fde->cfa_instructions_offset);
738   EXPECT_EQ(0x550cU, fde->cfa_instructions_end);
739   EXPECT_EQ(0x4300U, fde->pc_start);
740   EXPECT_EQ(0x4600U, fde->pc_end);
741   EXPECT_EQ(0x4520U, fde->lsda_address);
742 }
743 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromPc_interleaved)744 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc_interleaved) {
745   SetCie32(&this->memory_, 0x5000, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1});
746 
747   // FDE 0 (0x100 - 0x200)
748   SetFde32(&this->memory_, 0x5100, 0xfc, 0, 0x100, 0x100);
749   // FDE 1 (0x300 - 0x500)
750   SetFde32(&this->memory_, 0x5200, 0xfc, 0, 0x300, 0x200);
751   // FDE 2 (0x700 - 0x800)
752   SetFde32(&this->memory_, 0x5300, 0xfc, 0, 0x700, 0x100);
753   // FDE 3 (0xa00 - 0xb00)
754   SetFde32(&this->memory_, 0x5400, 0xfc, 0, 0xa00, 0x100);
755   // FDE 4 (0x100 - 0xb00)
756   SetFde32(&this->memory_, 0x5500, 0xfc, 0, 0x150, 0xa00);
757   // FDE 5 (0x50 - 0xa0)
758   SetFde32(&this->memory_, 0x5600, 0xfc, 0, 0x50, 0x50);
759   // FDE 6 (0x0 - 0x50)
760   SetFde32(&this->memory_, 0x5700, 0xfc, 0, 0, 0x50);
761 
762   this->debug_frame_->Init(0x5000, 0x800, 0);
763 
764   // Force reading all entries so no entries are found.
765   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0xfffff);
766   ASSERT_TRUE(fde == nullptr);
767 
768   //   0x50  - 0xa0  FDE 5
769   fde = this->debug_frame_->GetFdeFromPc(0x60);
770   ASSERT_TRUE(fde != nullptr);
771   EXPECT_EQ(0x50U, fde->pc_start);
772   EXPECT_EQ(0xa0U, fde->pc_end);
773 
774   //   0x0   - 0x50   FDE 6
775   fde = this->debug_frame_->GetFdeFromPc(0x10);
776   ASSERT_TRUE(fde != nullptr);
777   EXPECT_EQ(0U, fde->pc_start);
778   EXPECT_EQ(0x50U, fde->pc_end);
779 
780   //   0x100 - 0x200  FDE 0
781   fde = this->debug_frame_->GetFdeFromPc(0x170);
782   ASSERT_TRUE(fde != nullptr);
783   EXPECT_EQ(0x100U, fde->pc_start);
784   EXPECT_EQ(0x200U, fde->pc_end);
785 
786   //   0x200 - 0x300  FDE 4
787   fde = this->debug_frame_->GetFdeFromPc(0x210);
788   ASSERT_TRUE(fde != nullptr);
789   EXPECT_EQ(0x150U, fde->pc_start);
790   EXPECT_EQ(0xb50U, fde->pc_end);
791 
792   //   0x300 - 0x500  FDE 1
793   fde = this->debug_frame_->GetFdeFromPc(0x310);
794   ASSERT_TRUE(fde != nullptr);
795   EXPECT_EQ(0x300U, fde->pc_start);
796   EXPECT_EQ(0x500U, fde->pc_end);
797 
798   //   0x700 - 0x800  FDE 2
799   fde = this->debug_frame_->GetFdeFromPc(0x790);
800   ASSERT_TRUE(fde != nullptr);
801   EXPECT_EQ(0x700U, fde->pc_start);
802   EXPECT_EQ(0x800U, fde->pc_end);
803 
804   //   0x800 - 0x900  FDE 4
805   fde = this->debug_frame_->GetFdeFromPc(0x850);
806   ASSERT_TRUE(fde != nullptr);
807   EXPECT_EQ(0x150U, fde->pc_start);
808   EXPECT_EQ(0xb50U, fde->pc_end);
809 
810   //   0xa00 - 0xb00  FDE 3
811   fde = this->debug_frame_->GetFdeFromPc(0xa35);
812   ASSERT_TRUE(fde != nullptr);
813   EXPECT_EQ(0xa00U, fde->pc_start);
814   EXPECT_EQ(0xb00U, fde->pc_end);
815 
816   //   0xb00 - 0xb50  FDE 4
817   fde = this->debug_frame_->GetFdeFromPc(0xb20);
818   ASSERT_TRUE(fde != nullptr);
819   EXPECT_EQ(0x150U, fde->pc_start);
820   EXPECT_EQ(0xb50U, fde->pc_end);
821 }
822 
TYPED_TEST_P(DwarfDebugFrameTest,GetFdeFromPc_overlap)823 TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc_overlap) {
824   SetCie32(&this->memory_, 0x5000, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1});
825 
826   // FDE 0 (0x100 - 0x200)
827   SetFde32(&this->memory_, 0x5100, 0xfc, 0, 0x100, 0x100);
828   // FDE 1 (0x50 - 0x550)
829   SetFde32(&this->memory_, 0x5200, 0xfc, 0, 0x50, 0x500);
830   // FDE 2 (0x00 - 0x800)
831   SetFde32(&this->memory_, 0x5300, 0xfc, 0, 0x0, 0x800);
832 
833   this->debug_frame_->Init(0x5000, 0x400, 0);
834 
835   // Force reading all entries so no entries are found.
836   const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0xfffff);
837   ASSERT_TRUE(fde == nullptr);
838 
839   //   0x0  - 0x50  FDE 2
840   fde = this->debug_frame_->GetFdeFromPc(0x10);
841   ASSERT_TRUE(fde != nullptr);
842   EXPECT_EQ(0x0U, fde->pc_start);
843   EXPECT_EQ(0x800U, fde->pc_end);
844 
845   //   0x50  - 0x100  FDE 1 or FDE 2
846   fde = this->debug_frame_->GetFdeFromPc(0x60);
847   ASSERT_TRUE(fde != nullptr);
848   EXPECT_EQ(0x00U, fde->pc_start);
849   EXPECT_EQ(0x800U, fde->pc_end);
850 
851   //   0x100 - 0x200  FDE 0, FDE 1, or FDE 2
852   fde = this->debug_frame_->GetFdeFromPc(0x170);
853   ASSERT_TRUE(fde != nullptr);
854   EXPECT_EQ(0x100U, fde->pc_start);
855   EXPECT_EQ(0x200U, fde->pc_end);
856 
857   //   0x200 - 0x550  FDE 1, or FDE 2
858   fde = this->debug_frame_->GetFdeFromPc(0x210);
859   ASSERT_TRUE(fde != nullptr);
860   EXPECT_EQ(0x50U, fde->pc_start);
861   EXPECT_EQ(0x550U, fde->pc_end);
862 
863   //   0x550 - 0x800  FDE 2
864   fde = this->debug_frame_->GetFdeFromPc(0x580);
865   ASSERT_TRUE(fde != nullptr);
866   EXPECT_EQ(0x0U, fde->pc_start);
867   EXPECT_EQ(0x800U, fde->pc_end);
868 
869   fde = this->debug_frame_->GetFdeFromPc(0x810);
870   ASSERT_TRUE(fde == nullptr);
871 }
872 
873 REGISTER_TYPED_TEST_SUITE_P(
874     DwarfDebugFrameTest, GetFdes32, GetFdes32_after_GetFdeFromPc, GetFdes32_not_in_section,
875     GetFdeFromPc32, GetFdeFromPc32_reverse, GetFdeFromPc32_not_in_section, GetFdes64,
876     GetFdes64_after_GetFdeFromPc, GetFdes64_not_in_section, GetFdeFromPc64, GetFdeFromPc64_reverse,
877     GetFdeFromPc64_not_in_section, GetCieFde32, GetCieFde64, GetCieFromOffset32_cie_cached,
878     GetCieFromOffset64_cie_cached, GetCieFromOffset32_version1, GetCieFromOffset64_version1,
879     GetCieFromOffset32_version3, GetCieFromOffset64_version3, GetCieFromOffset32_version4,
880     GetCieFromOffset64_version4, GetCieFromOffset32_version5, GetCieFromOffset64_version5,
881     GetCieFromOffset_version_invalid, GetCieFromOffset32_augment, GetCieFromOffset64_augment,
882     GetFdeFromOffset32_augment, GetFdeFromOffset64_augment, GetFdeFromOffset32_lsda_address,
883     GetFdeFromOffset64_lsda_address, GetFdeFromPc_interleaved, GetFdeFromPc_overlap);
884 
885 typedef ::testing::Types<uint32_t, uint64_t> DwarfDebugFrameTestTypes;
886 INSTANTIATE_TYPED_TEST_SUITE_P(Libunwindstack, DwarfDebugFrameTest, DwarfDebugFrameTestTypes);
887 
888 }  // namespace unwindstack
889