blob: e8d53e647e20e51349b792bc3109f886497d221c [file] [log] [blame]
Christopher Ferris61d40972017-06-12 19:14:20 -07001/*
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 <gmock/gmock.h>
20#include <gtest/gtest.h>
21
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080022#include <unwindstack/DwarfError.h>
23
Christopher Ferris61d40972017-06-12 19:14:20 -070024#include "DwarfEhFrame.h"
25#include "DwarfEncoding.h"
26
27#include "LogFake.h"
28#include "MemoryFake.h"
Christopher Ferris61d40972017-06-12 19:14:20 -070029
Christopher Ferrisd226a512017-07-14 10:37:19 -070030namespace unwindstack {
31
Christopher Ferris61d40972017-06-12 19:14:20 -070032template <typename TypeParam>
33class MockDwarfEhFrame : public DwarfEhFrame<TypeParam> {
34 public:
35 MockDwarfEhFrame(Memory* memory) : DwarfEhFrame<TypeParam>(memory) {}
36 ~MockDwarfEhFrame() = default;
37
Christopher Ferris61d40972017-06-12 19:14:20 -070038 void TestSetFdeCount(uint64_t count) { this->fde_count_ = count; }
Christopher Ferrisc9dee842017-11-03 14:50:27 -070039 void TestSetOffset(uint64_t offset) { this->entries_offset_ = offset; }
40 void TestSetEndOffset(uint64_t offset) { this->entries_end_ = offset; }
41 void TestPushFdeInfo(const typename DwarfEhFrame<TypeParam>::FdeInfo& info) {
42 this->fdes_.push_back(info);
Christopher Ferris61d40972017-06-12 19:14:20 -070043 }
44
Christopher Ferris61d40972017-06-12 19:14:20 -070045 uint64_t TestGetFdeCount() { return this->fde_count_; }
Christopher Ferrisc9dee842017-11-03 14:50:27 -070046 uint8_t TestGetOffset() { return this->offset_; }
47 uint8_t TestGetEndOffset() { return this->end_offset_; }
48 void TestGetFdeInfo(size_t index, typename DwarfEhFrame<TypeParam>::FdeInfo* info) {
49 *info = this->fdes_[index];
50 }
Christopher Ferris61d40972017-06-12 19:14:20 -070051};
52
53template <typename TypeParam>
54class DwarfEhFrameTest : public ::testing::Test {
55 protected:
56 void SetUp() override {
57 memory_.Clear();
58 eh_frame_ = new MockDwarfEhFrame<TypeParam>(&memory_);
59 ResetLogs();
60 }
61
62 void TearDown() override { delete eh_frame_; }
63
64 MemoryFake memory_;
65 MockDwarfEhFrame<TypeParam>* eh_frame_ = nullptr;
66};
67TYPED_TEST_CASE_P(DwarfEhFrameTest);
68
69// NOTE: All test class variables need to be referenced as this->.
70
Christopher Ferrisc9dee842017-11-03 14:50:27 -070071TYPED_TEST_P(DwarfEhFrameTest, Init32) {
72 // CIE 32 information.
73 this->memory_.SetData32(0x5000, 0xfc);
74 this->memory_.SetData32(0x5004, 0);
75 this->memory_.SetData8(0x5008, 1);
76 this->memory_.SetData8(0x5009, '\0');
Christopher Ferris61d40972017-06-12 19:14:20 -070077
Christopher Ferrisc9dee842017-11-03 14:50:27 -070078 // FDE 32 information.
79 this->memory_.SetData32(0x5100, 0xfc);
80 this->memory_.SetData32(0x5104, 0x104);
81 this->memory_.SetData32(0x5108, 0x1500);
82 this->memory_.SetData32(0x510c, 0x200);
Christopher Ferris61d40972017-06-12 19:14:20 -070083
Christopher Ferrisc9dee842017-11-03 14:50:27 -070084 this->memory_.SetData32(0x5200, 0xfc);
85 this->memory_.SetData32(0x5204, 0x204);
86 this->memory_.SetData32(0x5208, 0x2500);
87 this->memory_.SetData32(0x520c, 0x300);
88
89 // CIE 32 information.
90 this->memory_.SetData32(0x5300, 0xfc);
91 this->memory_.SetData32(0x5304, 0);
92 this->memory_.SetData8(0x5308, 1);
93 this->memory_.SetData8(0x5309, '\0');
94
95 // FDE 32 information.
96 this->memory_.SetData32(0x5400, 0xfc);
97 this->memory_.SetData32(0x5404, 0x104);
98 this->memory_.SetData32(0x5408, 0x3500);
99 this->memory_.SetData32(0x540c, 0x400);
100
101 this->memory_.SetData32(0x5500, 0xfc);
102 this->memory_.SetData32(0x5504, 0x204);
103 this->memory_.SetData32(0x5508, 0x4500);
104 this->memory_.SetData32(0x550c, 0x500);
105
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700106 ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x600, 0));
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700107 ASSERT_EQ(4U, this->eh_frame_->TestGetFdeCount());
108
109 typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
110
111 this->eh_frame_->TestGetFdeInfo(0, &info);
112 EXPECT_EQ(0x5100U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800113 EXPECT_EQ(0x6608U, info.start);
114 EXPECT_EQ(0x6808U, info.end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700115
116 this->eh_frame_->TestGetFdeInfo(1, &info);
117 EXPECT_EQ(0x5200U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800118 EXPECT_EQ(0x7708U, info.start);
119 EXPECT_EQ(0x7a08U, info.end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700120
121 this->eh_frame_->TestGetFdeInfo(2, &info);
122 EXPECT_EQ(0x5400U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800123 EXPECT_EQ(0x8908U, info.start);
124 EXPECT_EQ(0x8d08U, info.end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700125
126 this->eh_frame_->TestGetFdeInfo(3, &info);
127 EXPECT_EQ(0x5500U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800128 EXPECT_EQ(0x9a08U, info.start);
129 EXPECT_EQ(0x9f08U, info.end);
Christopher Ferris61d40972017-06-12 19:14:20 -0700130}
131
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700132TYPED_TEST_P(DwarfEhFrameTest, Init32_fde_not_following_cie) {
133 // CIE 32 information.
134 this->memory_.SetData32(0x5000, 0xfc);
135 this->memory_.SetData32(0x5004, 0);
136 this->memory_.SetData8(0x5008, 1);
137 this->memory_.SetData8(0x5009, '\0');
138
139 // FDE 32 information.
140 this->memory_.SetData32(0x5100, 0xfc);
141 this->memory_.SetData32(0x5104, 0x1000);
142 this->memory_.SetData32(0x5108, 0x1500);
143 this->memory_.SetData32(0x510c, 0x200);
144
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700145 ASSERT_FALSE(this->eh_frame_->Init(0x5000, 0x600, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800146 ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->eh_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700147}
148
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700149TYPED_TEST_P(DwarfEhFrameTest, Init64) {
150 // CIE 64 information.
151 this->memory_.SetData32(0x5000, 0xffffffff);
152 this->memory_.SetData64(0x5004, 0xf4);
153 this->memory_.SetData64(0x500c, 0);
154 this->memory_.SetData8(0x5014, 1);
155 this->memory_.SetData8(0x5015, '\0');
Christopher Ferris61d40972017-06-12 19:14:20 -0700156
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700157 // FDE 64 information.
158 this->memory_.SetData32(0x5100, 0xffffffff);
159 this->memory_.SetData64(0x5104, 0xf4);
160 this->memory_.SetData64(0x510c, 0x10c);
161 this->memory_.SetData64(0x5114, 0x1500);
162 this->memory_.SetData64(0x511c, 0x200);
Christopher Ferris61d40972017-06-12 19:14:20 -0700163
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700164 this->memory_.SetData32(0x5200, 0xffffffff);
165 this->memory_.SetData64(0x5204, 0xf4);
166 this->memory_.SetData64(0x520c, 0x20c);
167 this->memory_.SetData64(0x5214, 0x2500);
168 this->memory_.SetData64(0x521c, 0x300);
169
170 // CIE 64 information.
171 this->memory_.SetData32(0x5300, 0xffffffff);
172 this->memory_.SetData64(0x5304, 0xf4);
173 this->memory_.SetData64(0x530c, 0);
174 this->memory_.SetData8(0x5314, 1);
175 this->memory_.SetData8(0x5315, '\0');
176
177 // FDE 64 information.
178 this->memory_.SetData32(0x5400, 0xffffffff);
179 this->memory_.SetData64(0x5404, 0xf4);
180 this->memory_.SetData64(0x540c, 0x10c);
181 this->memory_.SetData64(0x5414, 0x3500);
182 this->memory_.SetData64(0x541c, 0x400);
183
184 this->memory_.SetData32(0x5500, 0xffffffff);
185 this->memory_.SetData64(0x5504, 0xf4);
186 this->memory_.SetData64(0x550c, 0x20c);
187 this->memory_.SetData64(0x5514, 0x4500);
188 this->memory_.SetData64(0x551c, 0x500);
189
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700190 ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x600, 0));
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700191 ASSERT_EQ(4U, this->eh_frame_->TestGetFdeCount());
192
193 typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
194
195 this->eh_frame_->TestGetFdeInfo(0, &info);
196 EXPECT_EQ(0x5100U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800197 EXPECT_EQ(0x6618U, info.start);
198 EXPECT_EQ(0x6818U, info.end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700199
200 this->eh_frame_->TestGetFdeInfo(1, &info);
201 EXPECT_EQ(0x5200U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800202 EXPECT_EQ(0x7718U, info.start);
203 EXPECT_EQ(0x7a18U, info.end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700204
205 this->eh_frame_->TestGetFdeInfo(2, &info);
206 EXPECT_EQ(0x5400U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800207 EXPECT_EQ(0x8918U, info.start);
208 EXPECT_EQ(0x8d18U, info.end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700209
210 this->eh_frame_->TestGetFdeInfo(3, &info);
211 EXPECT_EQ(0x5500U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800212 EXPECT_EQ(0x9a18U, info.start);
213 EXPECT_EQ(0x9f18U, info.end);
Christopher Ferris61d40972017-06-12 19:14:20 -0700214}
215
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700216TYPED_TEST_P(DwarfEhFrameTest, Init64_fde_not_following_cie) {
217 // CIE 64 information.
218 this->memory_.SetData32(0x5000, 0xffffffff);
219 this->memory_.SetData64(0x5004, 0xf4);
220 this->memory_.SetData64(0x500c, 0);
221 this->memory_.SetData8(0x5014, 1);
222 this->memory_.SetData8(0x5015, '\0');
Christopher Ferris61d40972017-06-12 19:14:20 -0700223
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700224 // FDE 64 information.
225 this->memory_.SetData32(0x5100, 0xffffffff);
226 this->memory_.SetData64(0x5104, 0xf4);
227 this->memory_.SetData64(0x510c, 0x1000);
228 this->memory_.SetData64(0x5114, 0x1500);
229 this->memory_.SetData64(0x511c, 0x200);
Christopher Ferris61d40972017-06-12 19:14:20 -0700230
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700231 ASSERT_FALSE(this->eh_frame_->Init(0x5000, 0x600, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800232 ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->eh_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700233}
234
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700235TYPED_TEST_P(DwarfEhFrameTest, Init_non_zero_load_bias) {
236 // CIE 32 information.
237 this->memory_.SetData32(0x5000, 0xfc);
238 this->memory_.SetData32(0x5004, 0);
239 this->memory_.SetData8(0x5008, 1);
240 this->memory_.SetData8(0x5009, 'z');
241 this->memory_.SetData8(0x500a, 'R');
242 this->memory_.SetData8(0x500b, '\0');
243 this->memory_.SetData8(0x500c, 0);
244 this->memory_.SetData8(0x500d, 0);
245 this->memory_.SetData8(0x500e, 0);
246 this->memory_.SetData8(0x500f, 0);
247 this->memory_.SetData8(0x5010, 0x1b);
248
249 // FDE 32 information.
250 this->memory_.SetData32(0x5100, 0xfc);
251 this->memory_.SetData32(0x5104, 0x104);
252 this->memory_.SetData32(0x5108, 0x1500);
253 this->memory_.SetData32(0x510c, 0x200);
254 this->memory_.SetData8(0x5110, 0);
255 this->memory_.SetData8(0x5111, 0);
256
257 ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x200, 0x2000));
258 ASSERT_EQ(1U, this->eh_frame_->TestGetFdeCount());
259
260 typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
261
262 this->eh_frame_->TestGetFdeInfo(0, &info);
263 EXPECT_EQ(0x5100U, info.offset);
264 EXPECT_EQ(0x8608U, info.start);
265 EXPECT_EQ(0x8808U, info.end);
266
267 const DwarfFde* fde = this->eh_frame_->GetFdeFromPc(0x8700);
268 ASSERT_TRUE(fde != nullptr);
269 EXPECT_EQ(0x8608U, fde->pc_start);
270 EXPECT_EQ(0x8808U, fde->pc_end);
271}
272
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700273TYPED_TEST_P(DwarfEhFrameTest, Init_version1) {
274 // CIE 32 information.
275 this->memory_.SetData32(0x5000, 0xfc);
276 this->memory_.SetData32(0x5004, 0);
277 this->memory_.SetData8(0x5008, 1);
278 // Augment string.
279 this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'R', 'P', 'L', '\0'});
280 // Code alignment factor.
281 this->memory_.SetMemory(0x500e, std::vector<uint8_t>{0x80, 0x00});
282 // Data alignment factor.
283 this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
284 // Return address register
285 this->memory_.SetData8(0x5014, 0x84);
286 // Augmentation length
287 this->memory_.SetMemory(0x5015, std::vector<uint8_t>{0x84, 0x00});
288 // R data.
289 this->memory_.SetData8(0x5017, DW_EH_PE_pcrel | DW_EH_PE_udata2);
Christopher Ferris61d40972017-06-12 19:14:20 -0700290
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700291 // FDE 32 information.
292 this->memory_.SetData32(0x5100, 0xfc);
293 this->memory_.SetData32(0x5104, 0x104);
294 this->memory_.SetData16(0x5108, 0x1500);
295 this->memory_.SetData16(0x510a, 0x200);
Christopher Ferris61d40972017-06-12 19:14:20 -0700296
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700297 ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x200, 0));
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700298 ASSERT_EQ(1U, this->eh_frame_->TestGetFdeCount());
Christopher Ferris61d40972017-06-12 19:14:20 -0700299
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700300 typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
301 this->eh_frame_->TestGetFdeInfo(0, &info);
302 EXPECT_EQ(0x5100U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800303 EXPECT_EQ(0x6606U, info.start);
304 EXPECT_EQ(0x6806U, info.end);
Christopher Ferris61d40972017-06-12 19:14:20 -0700305}
306
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700307TYPED_TEST_P(DwarfEhFrameTest, Init_version4) {
308 // CIE 32 information.
309 this->memory_.SetData32(0x5000, 0xfc);
310 this->memory_.SetData32(0x5004, 0);
311 this->memory_.SetData8(0x5008, 4);
312 // Augment string.
313 this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'L', 'P', 'R', '\0'});
314 // Address size.
315 this->memory_.SetData8(0x500e, 4);
316 // Segment size.
317 this->memory_.SetData8(0x500f, 0);
318 // Code alignment factor.
319 this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x80, 0x00});
320 // Data alignment factor.
321 this->memory_.SetMemory(0x5012, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
322 // Return address register
323 this->memory_.SetMemory(0x5016, std::vector<uint8_t>{0x85, 0x10});
324 // Augmentation length
325 this->memory_.SetMemory(0x5018, std::vector<uint8_t>{0x84, 0x00});
326 // L data.
327 this->memory_.SetData8(0x501a, 0x10);
328 // P data.
329 this->memory_.SetData8(0x501b, DW_EH_PE_udata4);
330 this->memory_.SetData32(0x501c, 0x100);
331 // R data.
332 this->memory_.SetData8(0x5020, DW_EH_PE_pcrel | DW_EH_PE_udata2);
Christopher Ferris61d40972017-06-12 19:14:20 -0700333
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700334 // FDE 32 information.
335 this->memory_.SetData32(0x5100, 0xfc);
336 this->memory_.SetData32(0x5104, 0x104);
337 this->memory_.SetData16(0x5108, 0x1500);
338 this->memory_.SetData16(0x510a, 0x200);
339
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700340 ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x200, 0));
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700341 ASSERT_EQ(1U, this->eh_frame_->TestGetFdeCount());
342
343 typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
344 this->eh_frame_->TestGetFdeInfo(0, &info);
345 EXPECT_EQ(0x5100U, info.offset);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800346 EXPECT_EQ(0x6606U, info.start);
347 EXPECT_EQ(0x6806U, info.end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700348}
349
350TYPED_TEST_P(DwarfEhFrameTest, GetFdeOffsetFromPc) {
351 typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
352 for (size_t i = 0; i < 9; i++) {
353 info.start = 0x1000 * (i + 1);
354 info.end = 0x1000 * (i + 2) - 0x10;
Christopher Ferris61d40972017-06-12 19:14:20 -0700355 info.offset = 0x5000 + i * 0x20;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700356 this->eh_frame_->TestPushFdeInfo(info);
Christopher Ferris61d40972017-06-12 19:14:20 -0700357 }
358
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700359 this->eh_frame_->TestSetFdeCount(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700360 uint64_t fde_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700361 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x1000, &fde_offset));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800362 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700363
364 this->eh_frame_->TestSetFdeCount(9);
365 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800366 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700367 // Odd number of elements.
368 for (size_t i = 0; i < 9; i++) {
369 TypeParam pc = 0x1000 * (i + 1);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700370 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
Christopher Ferris61d40972017-06-12 19:14:20 -0700371 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700372 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index " << i;
Christopher Ferris61d40972017-06-12 19:14:20 -0700373 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700374 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
Christopher Ferris61d40972017-06-12 19:14:20 -0700375 << "Failed at index " << i;
376 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700377 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
378 << "Failed at index " << i;
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800379 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700380 }
381
382 // Even number of elements.
383 this->eh_frame_->TestSetFdeCount(10);
384 info.start = 0xa000;
385 info.end = 0xaff0;
386 info.offset = 0x5120;
387 this->eh_frame_->TestPushFdeInfo(info);
388
389 for (size_t i = 0; i < 10; i++) {
390 TypeParam pc = 0x1000 * (i + 1);
391 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
392 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
393 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index " << i;
394 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
395 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
396 << "Failed at index " << i;
397 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
398 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
399 << "Failed at index " << i;
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800400 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700401 }
402}
403
Christopher Ferris61d40972017-06-12 19:14:20 -0700404TYPED_TEST_P(DwarfEhFrameTest, GetCieFde32) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700405 this->eh_frame_->TestSetOffset(0x4000);
406
Christopher Ferris61d40972017-06-12 19:14:20 -0700407 // CIE 32 information.
408 this->memory_.SetData32(0xf000, 0x100);
409 this->memory_.SetData32(0xf004, 0);
410 this->memory_.SetData8(0xf008, 0x1);
411 this->memory_.SetData8(0xf009, '\0');
412 this->memory_.SetData8(0xf00a, 4);
413 this->memory_.SetData8(0xf00b, 8);
414 this->memory_.SetData8(0xf00c, 0x20);
415
416 // FDE 32 information.
417 this->memory_.SetData32(0x14000, 0x20);
418 this->memory_.SetData32(0x14004, 0x5004);
419 this->memory_.SetData32(0x14008, 0x9000);
420 this->memory_.SetData32(0x1400c, 0x100);
421
422 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x14000);
423 ASSERT_TRUE(fde != nullptr);
424 EXPECT_EQ(0x14010U, fde->cfa_instructions_offset);
425 EXPECT_EQ(0x14024U, fde->cfa_instructions_end);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800426 EXPECT_EQ(0x1d008U, fde->pc_start);
427 EXPECT_EQ(0x1d108U, fde->pc_end);
Christopher Ferris61d40972017-06-12 19:14:20 -0700428 EXPECT_EQ(0xf000U, fde->cie_offset);
429 EXPECT_EQ(0U, fde->lsda_address);
430
431 ASSERT_TRUE(fde->cie != nullptr);
432 EXPECT_EQ(1U, fde->cie->version);
433 EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding);
434 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
435 EXPECT_EQ(0U, fde->cie->segment_size);
436 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
437 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
438 EXPECT_EQ(0U, fde->cie->personality_handler);
439 EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset);
440 EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end);
441 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
442 EXPECT_EQ(8, fde->cie->data_alignment_factor);
443 EXPECT_EQ(0x20U, fde->cie->return_address_register);
444}
445
446TYPED_TEST_P(DwarfEhFrameTest, GetCieFde64) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700447 this->eh_frame_->TestSetOffset(0x2000);
448
Christopher Ferris61d40972017-06-12 19:14:20 -0700449 // CIE 64 information.
450 this->memory_.SetData32(0x6000, 0xffffffff);
451 this->memory_.SetData64(0x6004, 0x100);
452 this->memory_.SetData64(0x600c, 0);
453 this->memory_.SetData8(0x6014, 0x1);
454 this->memory_.SetData8(0x6015, '\0');
455 this->memory_.SetData8(0x6016, 4);
456 this->memory_.SetData8(0x6017, 8);
457 this->memory_.SetData8(0x6018, 0x20);
458
459 // FDE 64 information.
460 this->memory_.SetData32(0x8000, 0xffffffff);
461 this->memory_.SetData64(0x8004, 0x200);
462 this->memory_.SetData64(0x800c, 0x200c);
463 this->memory_.SetData64(0x8014, 0x5000);
464 this->memory_.SetData64(0x801c, 0x300);
465
466 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x8000);
467 ASSERT_TRUE(fde != nullptr);
468 EXPECT_EQ(0x8024U, fde->cfa_instructions_offset);
469 EXPECT_EQ(0x820cU, fde->cfa_instructions_end);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800470 EXPECT_EQ(0xd018U, fde->pc_start);
471 EXPECT_EQ(0xd318U, fde->pc_end);
Christopher Ferris61d40972017-06-12 19:14:20 -0700472 EXPECT_EQ(0x6000U, fde->cie_offset);
473 EXPECT_EQ(0U, fde->lsda_address);
474
475 ASSERT_TRUE(fde->cie != nullptr);
476 EXPECT_EQ(1U, fde->cie->version);
477 EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding);
478 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
479 EXPECT_EQ(0U, fde->cie->segment_size);
480 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
481 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
482 EXPECT_EQ(0U, fde->cie->personality_handler);
483 EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset);
484 EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end);
485 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
486 EXPECT_EQ(8, fde->cie->data_alignment_factor);
487 EXPECT_EQ(0x20U, fde->cie->return_address_register);
488}
489
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700490REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameTest, Init32, Init32_fde_not_following_cie, Init64,
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700491 Init64_fde_not_following_cie, Init_non_zero_load_bias, Init_version1,
492 Init_version4, GetFdeOffsetFromPc, GetCieFde32, GetCieFde64);
Christopher Ferris61d40972017-06-12 19:14:20 -0700493
494typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameTestTypes;
495INSTANTIATE_TYPED_TEST_CASE_P(, DwarfEhFrameTest, DwarfEhFrameTestTypes);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700496
497} // namespace unwindstack