| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 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 <gmock/gmock.h> | 
|  | 20 | #include <gtest/gtest.h> | 
|  | 21 |  | 
|  | 22 | #include "DwarfEhFrame.h" | 
|  | 23 | #include "DwarfEncoding.h" | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 24 | #include "DwarfError.h" | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | #include "LogFake.h" | 
|  | 27 | #include "MemoryFake.h" | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 28 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 29 | namespace unwindstack { | 
|  | 30 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 31 | template <typename TypeParam> | 
|  | 32 | class MockDwarfEhFrame : public DwarfEhFrame<TypeParam> { | 
|  | 33 | public: | 
|  | 34 | MockDwarfEhFrame(Memory* memory) : DwarfEhFrame<TypeParam>(memory) {} | 
|  | 35 | ~MockDwarfEhFrame() = default; | 
|  | 36 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 37 | void TestSetFdeCount(uint64_t count) { this->fde_count_ = count; } | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 38 | void TestSetOffset(uint64_t offset) { this->entries_offset_ = offset; } | 
|  | 39 | void TestSetEndOffset(uint64_t offset) { this->entries_end_ = offset; } | 
|  | 40 | void TestPushFdeInfo(const typename DwarfEhFrame<TypeParam>::FdeInfo& info) { | 
|  | 41 | this->fdes_.push_back(info); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 44 | uint64_t TestGetFdeCount() { return this->fde_count_; } | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 45 | uint8_t TestGetOffset() { return this->offset_; } | 
|  | 46 | uint8_t TestGetEndOffset() { return this->end_offset_; } | 
|  | 47 | void TestGetFdeInfo(size_t index, typename DwarfEhFrame<TypeParam>::FdeInfo* info) { | 
|  | 48 | *info = this->fdes_[index]; | 
|  | 49 | } | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 50 | }; | 
|  | 51 |  | 
|  | 52 | template <typename TypeParam> | 
|  | 53 | class DwarfEhFrameTest : public ::testing::Test { | 
|  | 54 | protected: | 
|  | 55 | void SetUp() override { | 
|  | 56 | memory_.Clear(); | 
|  | 57 | eh_frame_ = new MockDwarfEhFrame<TypeParam>(&memory_); | 
|  | 58 | ResetLogs(); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | void TearDown() override { delete eh_frame_; } | 
|  | 62 |  | 
|  | 63 | MemoryFake memory_; | 
|  | 64 | MockDwarfEhFrame<TypeParam>* eh_frame_ = nullptr; | 
|  | 65 | }; | 
|  | 66 | TYPED_TEST_CASE_P(DwarfEhFrameTest); | 
|  | 67 |  | 
|  | 68 | // NOTE: All test class variables need to be referenced as this->. | 
|  | 69 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 70 | TYPED_TEST_P(DwarfEhFrameTest, Init32) { | 
|  | 71 | // CIE 32 information. | 
|  | 72 | this->memory_.SetData32(0x5000, 0xfc); | 
|  | 73 | this->memory_.SetData32(0x5004, 0); | 
|  | 74 | this->memory_.SetData8(0x5008, 1); | 
|  | 75 | this->memory_.SetData8(0x5009, '\0'); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 76 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 77 | // FDE 32 information. | 
|  | 78 | this->memory_.SetData32(0x5100, 0xfc); | 
|  | 79 | this->memory_.SetData32(0x5104, 0x104); | 
|  | 80 | this->memory_.SetData32(0x5108, 0x1500); | 
|  | 81 | this->memory_.SetData32(0x510c, 0x200); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 82 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 83 | this->memory_.SetData32(0x5200, 0xfc); | 
|  | 84 | this->memory_.SetData32(0x5204, 0x204); | 
|  | 85 | this->memory_.SetData32(0x5208, 0x2500); | 
|  | 86 | this->memory_.SetData32(0x520c, 0x300); | 
|  | 87 |  | 
|  | 88 | // CIE 32 information. | 
|  | 89 | this->memory_.SetData32(0x5300, 0xfc); | 
|  | 90 | this->memory_.SetData32(0x5304, 0); | 
|  | 91 | this->memory_.SetData8(0x5308, 1); | 
|  | 92 | this->memory_.SetData8(0x5309, '\0'); | 
|  | 93 |  | 
|  | 94 | // FDE 32 information. | 
|  | 95 | this->memory_.SetData32(0x5400, 0xfc); | 
|  | 96 | this->memory_.SetData32(0x5404, 0x104); | 
|  | 97 | this->memory_.SetData32(0x5408, 0x3500); | 
|  | 98 | this->memory_.SetData32(0x540c, 0x400); | 
|  | 99 |  | 
|  | 100 | this->memory_.SetData32(0x5500, 0xfc); | 
|  | 101 | this->memory_.SetData32(0x5504, 0x204); | 
|  | 102 | this->memory_.SetData32(0x5508, 0x4500); | 
|  | 103 | this->memory_.SetData32(0x550c, 0x500); | 
|  | 104 |  | 
|  | 105 | ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x600)); | 
|  | 106 | ASSERT_EQ(4U, this->eh_frame_->TestGetFdeCount()); | 
|  | 107 |  | 
|  | 108 | typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0); | 
|  | 109 |  | 
|  | 110 | this->eh_frame_->TestGetFdeInfo(0, &info); | 
|  | 111 | EXPECT_EQ(0x5100U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 112 | EXPECT_EQ(0x6608U, info.start); | 
|  | 113 | EXPECT_EQ(0x6808U, info.end); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 114 |  | 
|  | 115 | this->eh_frame_->TestGetFdeInfo(1, &info); | 
|  | 116 | EXPECT_EQ(0x5200U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 117 | EXPECT_EQ(0x7708U, info.start); | 
|  | 118 | EXPECT_EQ(0x7a08U, info.end); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 119 |  | 
|  | 120 | this->eh_frame_->TestGetFdeInfo(2, &info); | 
|  | 121 | EXPECT_EQ(0x5400U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 122 | EXPECT_EQ(0x8908U, info.start); | 
|  | 123 | EXPECT_EQ(0x8d08U, info.end); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 124 |  | 
|  | 125 | this->eh_frame_->TestGetFdeInfo(3, &info); | 
|  | 126 | EXPECT_EQ(0x5500U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 127 | EXPECT_EQ(0x9a08U, info.start); | 
|  | 128 | EXPECT_EQ(0x9f08U, info.end); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 131 | TYPED_TEST_P(DwarfEhFrameTest, Init32_fde_not_following_cie) { | 
|  | 132 | // CIE 32 information. | 
|  | 133 | this->memory_.SetData32(0x5000, 0xfc); | 
|  | 134 | this->memory_.SetData32(0x5004, 0); | 
|  | 135 | this->memory_.SetData8(0x5008, 1); | 
|  | 136 | this->memory_.SetData8(0x5009, '\0'); | 
|  | 137 |  | 
|  | 138 | // FDE 32 information. | 
|  | 139 | this->memory_.SetData32(0x5100, 0xfc); | 
|  | 140 | this->memory_.SetData32(0x5104, 0x1000); | 
|  | 141 | this->memory_.SetData32(0x5108, 0x1500); | 
|  | 142 | this->memory_.SetData32(0x510c, 0x200); | 
|  | 143 |  | 
|  | 144 | ASSERT_FALSE(this->eh_frame_->Init(0x5000, 0x600)); | 
|  | 145 | ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->eh_frame_->last_error()); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 148 | TYPED_TEST_P(DwarfEhFrameTest, Init64) { | 
|  | 149 | // CIE 64 information. | 
|  | 150 | this->memory_.SetData32(0x5000, 0xffffffff); | 
|  | 151 | this->memory_.SetData64(0x5004, 0xf4); | 
|  | 152 | this->memory_.SetData64(0x500c, 0); | 
|  | 153 | this->memory_.SetData8(0x5014, 1); | 
|  | 154 | this->memory_.SetData8(0x5015, '\0'); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 155 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 156 | // FDE 64 information. | 
|  | 157 | this->memory_.SetData32(0x5100, 0xffffffff); | 
|  | 158 | this->memory_.SetData64(0x5104, 0xf4); | 
|  | 159 | this->memory_.SetData64(0x510c, 0x10c); | 
|  | 160 | this->memory_.SetData64(0x5114, 0x1500); | 
|  | 161 | this->memory_.SetData64(0x511c, 0x200); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 162 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 163 | this->memory_.SetData32(0x5200, 0xffffffff); | 
|  | 164 | this->memory_.SetData64(0x5204, 0xf4); | 
|  | 165 | this->memory_.SetData64(0x520c, 0x20c); | 
|  | 166 | this->memory_.SetData64(0x5214, 0x2500); | 
|  | 167 | this->memory_.SetData64(0x521c, 0x300); | 
|  | 168 |  | 
|  | 169 | // CIE 64 information. | 
|  | 170 | this->memory_.SetData32(0x5300, 0xffffffff); | 
|  | 171 | this->memory_.SetData64(0x5304, 0xf4); | 
|  | 172 | this->memory_.SetData64(0x530c, 0); | 
|  | 173 | this->memory_.SetData8(0x5314, 1); | 
|  | 174 | this->memory_.SetData8(0x5315, '\0'); | 
|  | 175 |  | 
|  | 176 | // FDE 64 information. | 
|  | 177 | this->memory_.SetData32(0x5400, 0xffffffff); | 
|  | 178 | this->memory_.SetData64(0x5404, 0xf4); | 
|  | 179 | this->memory_.SetData64(0x540c, 0x10c); | 
|  | 180 | this->memory_.SetData64(0x5414, 0x3500); | 
|  | 181 | this->memory_.SetData64(0x541c, 0x400); | 
|  | 182 |  | 
|  | 183 | this->memory_.SetData32(0x5500, 0xffffffff); | 
|  | 184 | this->memory_.SetData64(0x5504, 0xf4); | 
|  | 185 | this->memory_.SetData64(0x550c, 0x20c); | 
|  | 186 | this->memory_.SetData64(0x5514, 0x4500); | 
|  | 187 | this->memory_.SetData64(0x551c, 0x500); | 
|  | 188 |  | 
|  | 189 | ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x600)); | 
|  | 190 | ASSERT_EQ(4U, this->eh_frame_->TestGetFdeCount()); | 
|  | 191 |  | 
|  | 192 | typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0); | 
|  | 193 |  | 
|  | 194 | this->eh_frame_->TestGetFdeInfo(0, &info); | 
|  | 195 | EXPECT_EQ(0x5100U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 196 | EXPECT_EQ(0x6618U, info.start); | 
|  | 197 | EXPECT_EQ(0x6818U, info.end); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 198 |  | 
|  | 199 | this->eh_frame_->TestGetFdeInfo(1, &info); | 
|  | 200 | EXPECT_EQ(0x5200U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 201 | EXPECT_EQ(0x7718U, info.start); | 
|  | 202 | EXPECT_EQ(0x7a18U, info.end); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 203 |  | 
|  | 204 | this->eh_frame_->TestGetFdeInfo(2, &info); | 
|  | 205 | EXPECT_EQ(0x5400U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 206 | EXPECT_EQ(0x8918U, info.start); | 
|  | 207 | EXPECT_EQ(0x8d18U, info.end); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 208 |  | 
|  | 209 | this->eh_frame_->TestGetFdeInfo(3, &info); | 
|  | 210 | EXPECT_EQ(0x5500U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 211 | EXPECT_EQ(0x9a18U, info.start); | 
|  | 212 | EXPECT_EQ(0x9f18U, info.end); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 213 | } | 
|  | 214 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 215 | TYPED_TEST_P(DwarfEhFrameTest, Init64_fde_not_following_cie) { | 
|  | 216 | // CIE 64 information. | 
|  | 217 | this->memory_.SetData32(0x5000, 0xffffffff); | 
|  | 218 | this->memory_.SetData64(0x5004, 0xf4); | 
|  | 219 | this->memory_.SetData64(0x500c, 0); | 
|  | 220 | this->memory_.SetData8(0x5014, 1); | 
|  | 221 | this->memory_.SetData8(0x5015, '\0'); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 222 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 223 | // FDE 64 information. | 
|  | 224 | this->memory_.SetData32(0x5100, 0xffffffff); | 
|  | 225 | this->memory_.SetData64(0x5104, 0xf4); | 
|  | 226 | this->memory_.SetData64(0x510c, 0x1000); | 
|  | 227 | this->memory_.SetData64(0x5114, 0x1500); | 
|  | 228 | this->memory_.SetData64(0x511c, 0x200); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 229 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 230 | ASSERT_FALSE(this->eh_frame_->Init(0x5000, 0x600)); | 
|  | 231 | ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->eh_frame_->last_error()); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 232 | } | 
|  | 233 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 234 | TYPED_TEST_P(DwarfEhFrameTest, Init_version1) { | 
|  | 235 | // CIE 32 information. | 
|  | 236 | this->memory_.SetData32(0x5000, 0xfc); | 
|  | 237 | this->memory_.SetData32(0x5004, 0); | 
|  | 238 | this->memory_.SetData8(0x5008, 1); | 
|  | 239 | // Augment string. | 
|  | 240 | this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'R', 'P', 'L', '\0'}); | 
|  | 241 | // Code alignment factor. | 
|  | 242 | this->memory_.SetMemory(0x500e, std::vector<uint8_t>{0x80, 0x00}); | 
|  | 243 | // Data alignment factor. | 
|  | 244 | this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00}); | 
|  | 245 | // Return address register | 
|  | 246 | this->memory_.SetData8(0x5014, 0x84); | 
|  | 247 | // Augmentation length | 
|  | 248 | this->memory_.SetMemory(0x5015, std::vector<uint8_t>{0x84, 0x00}); | 
|  | 249 | // R data. | 
|  | 250 | this->memory_.SetData8(0x5017, DW_EH_PE_pcrel | DW_EH_PE_udata2); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 251 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 252 | // FDE 32 information. | 
|  | 253 | this->memory_.SetData32(0x5100, 0xfc); | 
|  | 254 | this->memory_.SetData32(0x5104, 0x104); | 
|  | 255 | this->memory_.SetData16(0x5108, 0x1500); | 
|  | 256 | this->memory_.SetData16(0x510a, 0x200); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 257 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 258 | ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x200)); | 
|  | 259 | ASSERT_EQ(1U, this->eh_frame_->TestGetFdeCount()); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 260 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 261 | typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0); | 
|  | 262 | this->eh_frame_->TestGetFdeInfo(0, &info); | 
|  | 263 | EXPECT_EQ(0x5100U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 264 | EXPECT_EQ(0x6606U, info.start); | 
|  | 265 | EXPECT_EQ(0x6806U, info.end); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 268 | TYPED_TEST_P(DwarfEhFrameTest, Init_version4) { | 
|  | 269 | // CIE 32 information. | 
|  | 270 | this->memory_.SetData32(0x5000, 0xfc); | 
|  | 271 | this->memory_.SetData32(0x5004, 0); | 
|  | 272 | this->memory_.SetData8(0x5008, 4); | 
|  | 273 | // Augment string. | 
|  | 274 | this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'L', 'P', 'R', '\0'}); | 
|  | 275 | // Address size. | 
|  | 276 | this->memory_.SetData8(0x500e, 4); | 
|  | 277 | // Segment size. | 
|  | 278 | this->memory_.SetData8(0x500f, 0); | 
|  | 279 | // Code alignment factor. | 
|  | 280 | this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x80, 0x00}); | 
|  | 281 | // Data alignment factor. | 
|  | 282 | this->memory_.SetMemory(0x5012, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00}); | 
|  | 283 | // Return address register | 
|  | 284 | this->memory_.SetMemory(0x5016, std::vector<uint8_t>{0x85, 0x10}); | 
|  | 285 | // Augmentation length | 
|  | 286 | this->memory_.SetMemory(0x5018, std::vector<uint8_t>{0x84, 0x00}); | 
|  | 287 | // L data. | 
|  | 288 | this->memory_.SetData8(0x501a, 0x10); | 
|  | 289 | // P data. | 
|  | 290 | this->memory_.SetData8(0x501b, DW_EH_PE_udata4); | 
|  | 291 | this->memory_.SetData32(0x501c, 0x100); | 
|  | 292 | // R data. | 
|  | 293 | this->memory_.SetData8(0x5020, DW_EH_PE_pcrel | DW_EH_PE_udata2); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 294 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 295 | // FDE 32 information. | 
|  | 296 | this->memory_.SetData32(0x5100, 0xfc); | 
|  | 297 | this->memory_.SetData32(0x5104, 0x104); | 
|  | 298 | this->memory_.SetData16(0x5108, 0x1500); | 
|  | 299 | this->memory_.SetData16(0x510a, 0x200); | 
|  | 300 |  | 
|  | 301 | ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x200)); | 
|  | 302 | ASSERT_EQ(1U, this->eh_frame_->TestGetFdeCount()); | 
|  | 303 |  | 
|  | 304 | typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0); | 
|  | 305 | this->eh_frame_->TestGetFdeInfo(0, &info); | 
|  | 306 | EXPECT_EQ(0x5100U, info.offset); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 307 | EXPECT_EQ(0x6606U, info.start); | 
|  | 308 | EXPECT_EQ(0x6806U, info.end); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 309 | } | 
|  | 310 |  | 
|  | 311 | TYPED_TEST_P(DwarfEhFrameTest, GetFdeOffsetFromPc) { | 
|  | 312 | typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0); | 
|  | 313 | for (size_t i = 0; i < 9; i++) { | 
|  | 314 | info.start = 0x1000 * (i + 1); | 
|  | 315 | info.end = 0x1000 * (i + 2) - 0x10; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 316 | info.offset = 0x5000 + i * 0x20; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 317 | this->eh_frame_->TestPushFdeInfo(info); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 318 | } | 
|  | 319 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 320 | this->eh_frame_->TestSetFdeCount(0); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 321 | uint64_t fde_offset; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 322 | ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x1000, &fde_offset)); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 323 | ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->last_error()); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 324 |  | 
|  | 325 | this->eh_frame_->TestSetFdeCount(9); | 
|  | 326 | ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x100, &fde_offset)); | 
|  | 327 | ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->last_error()); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 328 | // Odd number of elements. | 
|  | 329 | for (size_t i = 0; i < 9; i++) { | 
|  | 330 | TypeParam pc = 0x1000 * (i + 1); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 331 | ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 332 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 333 | ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index " << i; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 334 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 335 | ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset)) | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 336 | << "Failed at index " << i; | 
|  | 337 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 338 | ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset)) | 
|  | 339 | << "Failed at index " << i; | 
|  | 340 | ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->last_error()); | 
|  | 341 | } | 
|  | 342 |  | 
|  | 343 | // Even number of elements. | 
|  | 344 | this->eh_frame_->TestSetFdeCount(10); | 
|  | 345 | info.start = 0xa000; | 
|  | 346 | info.end = 0xaff0; | 
|  | 347 | info.offset = 0x5120; | 
|  | 348 | this->eh_frame_->TestPushFdeInfo(info); | 
|  | 349 |  | 
|  | 350 | for (size_t i = 0; i < 10; i++) { | 
|  | 351 | TypeParam pc = 0x1000 * (i + 1); | 
|  | 352 | ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i; | 
|  | 353 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; | 
|  | 354 | ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index " << i; | 
|  | 355 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; | 
|  | 356 | ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset)) | 
|  | 357 | << "Failed at index " << i; | 
|  | 358 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; | 
|  | 359 | ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset)) | 
|  | 360 | << "Failed at index " << i; | 
|  | 361 | ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->last_error()); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 362 | } | 
|  | 363 | } | 
|  | 364 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 365 | TYPED_TEST_P(DwarfEhFrameTest, GetCieFde32) { | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 366 | this->eh_frame_->TestSetOffset(0x4000); | 
|  | 367 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 368 | // CIE 32 information. | 
|  | 369 | this->memory_.SetData32(0xf000, 0x100); | 
|  | 370 | this->memory_.SetData32(0xf004, 0); | 
|  | 371 | this->memory_.SetData8(0xf008, 0x1); | 
|  | 372 | this->memory_.SetData8(0xf009, '\0'); | 
|  | 373 | this->memory_.SetData8(0xf00a, 4); | 
|  | 374 | this->memory_.SetData8(0xf00b, 8); | 
|  | 375 | this->memory_.SetData8(0xf00c, 0x20); | 
|  | 376 |  | 
|  | 377 | // FDE 32 information. | 
|  | 378 | this->memory_.SetData32(0x14000, 0x20); | 
|  | 379 | this->memory_.SetData32(0x14004, 0x5004); | 
|  | 380 | this->memory_.SetData32(0x14008, 0x9000); | 
|  | 381 | this->memory_.SetData32(0x1400c, 0x100); | 
|  | 382 |  | 
|  | 383 | const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x14000); | 
|  | 384 | ASSERT_TRUE(fde != nullptr); | 
|  | 385 | EXPECT_EQ(0x14010U, fde->cfa_instructions_offset); | 
|  | 386 | EXPECT_EQ(0x14024U, fde->cfa_instructions_end); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 387 | EXPECT_EQ(0x1d008U, fde->pc_start); | 
|  | 388 | EXPECT_EQ(0x1d108U, fde->pc_end); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 389 | EXPECT_EQ(0xf000U, fde->cie_offset); | 
|  | 390 | EXPECT_EQ(0U, fde->lsda_address); | 
|  | 391 |  | 
|  | 392 | ASSERT_TRUE(fde->cie != nullptr); | 
|  | 393 | EXPECT_EQ(1U, fde->cie->version); | 
|  | 394 | EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding); | 
|  | 395 | EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding); | 
|  | 396 | EXPECT_EQ(0U, fde->cie->segment_size); | 
|  | 397 | EXPECT_EQ(1U, fde->cie->augmentation_string.size()); | 
|  | 398 | EXPECT_EQ('\0', fde->cie->augmentation_string[0]); | 
|  | 399 | EXPECT_EQ(0U, fde->cie->personality_handler); | 
|  | 400 | EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset); | 
|  | 401 | EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end); | 
|  | 402 | EXPECT_EQ(4U, fde->cie->code_alignment_factor); | 
|  | 403 | EXPECT_EQ(8, fde->cie->data_alignment_factor); | 
|  | 404 | EXPECT_EQ(0x20U, fde->cie->return_address_register); | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | TYPED_TEST_P(DwarfEhFrameTest, GetCieFde64) { | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 408 | this->eh_frame_->TestSetOffset(0x2000); | 
|  | 409 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 410 | // CIE 64 information. | 
|  | 411 | this->memory_.SetData32(0x6000, 0xffffffff); | 
|  | 412 | this->memory_.SetData64(0x6004, 0x100); | 
|  | 413 | this->memory_.SetData64(0x600c, 0); | 
|  | 414 | this->memory_.SetData8(0x6014, 0x1); | 
|  | 415 | this->memory_.SetData8(0x6015, '\0'); | 
|  | 416 | this->memory_.SetData8(0x6016, 4); | 
|  | 417 | this->memory_.SetData8(0x6017, 8); | 
|  | 418 | this->memory_.SetData8(0x6018, 0x20); | 
|  | 419 |  | 
|  | 420 | // FDE 64 information. | 
|  | 421 | this->memory_.SetData32(0x8000, 0xffffffff); | 
|  | 422 | this->memory_.SetData64(0x8004, 0x200); | 
|  | 423 | this->memory_.SetData64(0x800c, 0x200c); | 
|  | 424 | this->memory_.SetData64(0x8014, 0x5000); | 
|  | 425 | this->memory_.SetData64(0x801c, 0x300); | 
|  | 426 |  | 
|  | 427 | const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x8000); | 
|  | 428 | ASSERT_TRUE(fde != nullptr); | 
|  | 429 | EXPECT_EQ(0x8024U, fde->cfa_instructions_offset); | 
|  | 430 | EXPECT_EQ(0x820cU, fde->cfa_instructions_end); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 431 | EXPECT_EQ(0xd018U, fde->pc_start); | 
|  | 432 | EXPECT_EQ(0xd318U, fde->pc_end); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 433 | EXPECT_EQ(0x6000U, fde->cie_offset); | 
|  | 434 | EXPECT_EQ(0U, fde->lsda_address); | 
|  | 435 |  | 
|  | 436 | ASSERT_TRUE(fde->cie != nullptr); | 
|  | 437 | EXPECT_EQ(1U, fde->cie->version); | 
|  | 438 | EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding); | 
|  | 439 | EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding); | 
|  | 440 | EXPECT_EQ(0U, fde->cie->segment_size); | 
|  | 441 | EXPECT_EQ(1U, fde->cie->augmentation_string.size()); | 
|  | 442 | EXPECT_EQ('\0', fde->cie->augmentation_string[0]); | 
|  | 443 | EXPECT_EQ(0U, fde->cie->personality_handler); | 
|  | 444 | EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset); | 
|  | 445 | EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end); | 
|  | 446 | EXPECT_EQ(4U, fde->cie->code_alignment_factor); | 
|  | 447 | EXPECT_EQ(8, fde->cie->data_alignment_factor); | 
|  | 448 | EXPECT_EQ(0x20U, fde->cie->return_address_register); | 
|  | 449 | } | 
|  | 450 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 451 | REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameTest, Init32, Init32_fde_not_following_cie, Init64, | 
|  | 452 | Init64_fde_not_following_cie, Init_version1, Init_version4, | 
|  | 453 | GetFdeOffsetFromPc, GetCieFde32, GetCieFde64); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 454 |  | 
|  | 455 | typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameTestTypes; | 
|  | 456 | INSTANTIATE_TYPED_TEST_CASE_P(, DwarfEhFrameTest, DwarfEhFrameTestTypes); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 457 |  | 
|  | 458 | }  // namespace unwindstack |