Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [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 <elf.h> |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <vector> |
| 22 | |
Christopher Ferris | 5391416 | 2018-02-08 19:27:47 -0800 | [diff] [blame] | 23 | #include <unwindstack/MachineArm.h> |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 24 | #include <unwindstack/RegsArm.h> |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 25 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 26 | #include "ElfInterfaceArm.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 27 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 28 | #include "ElfFake.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 29 | #include "MemoryFake.h" |
| 30 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 31 | namespace unwindstack { |
| 32 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 33 | class ElfInterfaceArmTest : public ::testing::Test { |
| 34 | protected: |
| 35 | void SetUp() override { |
| 36 | memory_.Clear(); |
| 37 | process_memory_.Clear(); |
| 38 | } |
| 39 | |
| 40 | MemoryFake memory_; |
| 41 | MemoryFake process_memory_; |
| 42 | }; |
| 43 | |
| 44 | TEST_F(ElfInterfaceArmTest, GetPrel32Addr) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 45 | ElfInterfaceArmFake interface(&memory_); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 46 | memory_.SetData32(0x1000, 0x230000); |
| 47 | |
| 48 | uint32_t value; |
| 49 | ASSERT_TRUE(interface.GetPrel31Addr(0x1000, &value)); |
| 50 | ASSERT_EQ(0x231000U, value); |
| 51 | |
| 52 | memory_.SetData32(0x1000, 0x80001000); |
| 53 | ASSERT_TRUE(interface.GetPrel31Addr(0x1000, &value)); |
| 54 | ASSERT_EQ(0x2000U, value); |
| 55 | |
| 56 | memory_.SetData32(0x1000, 0x70001000); |
| 57 | ASSERT_TRUE(interface.GetPrel31Addr(0x1000, &value)); |
| 58 | ASSERT_EQ(0xf0002000U, value); |
| 59 | } |
| 60 | |
| 61 | TEST_F(ElfInterfaceArmTest, FindEntry_start_zero) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 62 | ElfInterfaceArmFake interface(&memory_); |
| 63 | interface.FakeSetStartOffset(0); |
| 64 | interface.FakeSetTotalEntries(10); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 65 | |
| 66 | uint64_t entry_offset; |
| 67 | ASSERT_FALSE(interface.FindEntry(0x1000, &entry_offset)); |
| 68 | } |
| 69 | |
| 70 | TEST_F(ElfInterfaceArmTest, FindEntry_no_entries) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 71 | ElfInterfaceArmFake interface(&memory_); |
| 72 | interface.FakeSetStartOffset(0x100); |
| 73 | interface.FakeSetTotalEntries(0); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 74 | |
| 75 | uint64_t entry_offset; |
| 76 | ASSERT_FALSE(interface.FindEntry(0x1000, &entry_offset)); |
| 77 | } |
| 78 | |
| 79 | TEST_F(ElfInterfaceArmTest, FindEntry_no_valid_memory) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 80 | ElfInterfaceArmFake interface(&memory_); |
| 81 | interface.FakeSetStartOffset(0x100); |
| 82 | interface.FakeSetTotalEntries(2); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 83 | |
| 84 | uint64_t entry_offset; |
| 85 | ASSERT_FALSE(interface.FindEntry(0x1000, &entry_offset)); |
| 86 | } |
| 87 | |
| 88 | TEST_F(ElfInterfaceArmTest, FindEntry_ip_before_first) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 89 | ElfInterfaceArmFake interface(&memory_); |
| 90 | interface.FakeSetStartOffset(0x1000); |
| 91 | interface.FakeSetTotalEntries(1); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 92 | memory_.SetData32(0x1000, 0x6000); |
| 93 | |
| 94 | uint64_t entry_offset; |
| 95 | ASSERT_FALSE(interface.FindEntry(0x1000, &entry_offset)); |
| 96 | } |
| 97 | |
| 98 | TEST_F(ElfInterfaceArmTest, FindEntry_single_entry_negative_value) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 99 | ElfInterfaceArmFake interface(&memory_); |
| 100 | interface.FakeSetStartOffset(0x8000); |
| 101 | interface.FakeSetTotalEntries(1); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 102 | memory_.SetData32(0x8000, 0x7fffff00); |
| 103 | |
| 104 | uint64_t entry_offset; |
| 105 | ASSERT_TRUE(interface.FindEntry(0x7ff0, &entry_offset)); |
| 106 | ASSERT_EQ(0x8000U, entry_offset); |
| 107 | } |
| 108 | |
| 109 | TEST_F(ElfInterfaceArmTest, FindEntry_two_entries) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 110 | ElfInterfaceArmFake interface(&memory_); |
| 111 | interface.FakeSetStartOffset(0x1000); |
| 112 | interface.FakeSetTotalEntries(2); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 113 | memory_.SetData32(0x1000, 0x6000); |
| 114 | memory_.SetData32(0x1008, 0x7000); |
| 115 | |
| 116 | uint64_t entry_offset; |
| 117 | ASSERT_TRUE(interface.FindEntry(0x7000, &entry_offset)); |
| 118 | ASSERT_EQ(0x1000U, entry_offset); |
| 119 | } |
| 120 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 121 | TEST_F(ElfInterfaceArmTest, FindEntry_last_check_single_entry) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 122 | ElfInterfaceArmFake interface(&memory_); |
| 123 | interface.FakeSetStartOffset(0x1000); |
| 124 | interface.FakeSetTotalEntries(1); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 125 | memory_.SetData32(0x1000, 0x6000); |
| 126 | |
| 127 | uint64_t entry_offset; |
| 128 | ASSERT_TRUE(interface.FindEntry(0x7000, &entry_offset)); |
| 129 | ASSERT_EQ(0x1000U, entry_offset); |
| 130 | |
| 131 | // To guarantee that we are using the cache on the second run, |
| 132 | // set the memory to a different value. |
| 133 | memory_.SetData32(0x1000, 0x8000); |
| 134 | ASSERT_TRUE(interface.FindEntry(0x7004, &entry_offset)); |
| 135 | ASSERT_EQ(0x1000U, entry_offset); |
| 136 | } |
| 137 | |
| 138 | TEST_F(ElfInterfaceArmTest, FindEntry_last_check_multiple_entries) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 139 | ElfInterfaceArmFake interface(&memory_); |
| 140 | interface.FakeSetStartOffset(0x1000); |
| 141 | interface.FakeSetTotalEntries(2); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 142 | memory_.SetData32(0x1000, 0x6000); |
| 143 | memory_.SetData32(0x1008, 0x8000); |
| 144 | |
| 145 | uint64_t entry_offset; |
| 146 | ASSERT_TRUE(interface.FindEntry(0x9008, &entry_offset)); |
| 147 | ASSERT_EQ(0x1008U, entry_offset); |
| 148 | |
| 149 | // To guarantee that we are using the cache on the second run, |
| 150 | // set the memory to a different value. |
| 151 | memory_.SetData32(0x1000, 0x16000); |
| 152 | memory_.SetData32(0x1008, 0x18000); |
| 153 | ASSERT_TRUE(interface.FindEntry(0x9100, &entry_offset)); |
| 154 | ASSERT_EQ(0x1008U, entry_offset); |
| 155 | } |
| 156 | |
| 157 | TEST_F(ElfInterfaceArmTest, FindEntry_multiple_entries_even) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 158 | ElfInterfaceArmFake interface(&memory_); |
| 159 | interface.FakeSetStartOffset(0x1000); |
| 160 | interface.FakeSetTotalEntries(4); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 161 | memory_.SetData32(0x1000, 0x6000); |
| 162 | memory_.SetData32(0x1008, 0x7000); |
| 163 | memory_.SetData32(0x1010, 0x8000); |
| 164 | memory_.SetData32(0x1018, 0x9000); |
| 165 | |
| 166 | uint64_t entry_offset; |
| 167 | ASSERT_TRUE(interface.FindEntry(0x9100, &entry_offset)); |
| 168 | ASSERT_EQ(0x1010U, entry_offset); |
| 169 | |
| 170 | // To guarantee that we are using the cache on the second run, |
| 171 | // set the memory to a different value. |
| 172 | memory_.SetData32(0x1000, 0x16000); |
| 173 | memory_.SetData32(0x1008, 0x17000); |
| 174 | memory_.SetData32(0x1010, 0x18000); |
| 175 | memory_.SetData32(0x1018, 0x19000); |
| 176 | ASSERT_TRUE(interface.FindEntry(0x9100, &entry_offset)); |
| 177 | ASSERT_EQ(0x1010U, entry_offset); |
| 178 | } |
| 179 | |
| 180 | TEST_F(ElfInterfaceArmTest, FindEntry_multiple_entries_odd) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 181 | ElfInterfaceArmFake interface(&memory_); |
| 182 | interface.FakeSetStartOffset(0x1000); |
| 183 | interface.FakeSetTotalEntries(5); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 184 | memory_.SetData32(0x1000, 0x5000); |
| 185 | memory_.SetData32(0x1008, 0x6000); |
| 186 | memory_.SetData32(0x1010, 0x7000); |
| 187 | memory_.SetData32(0x1018, 0x8000); |
| 188 | memory_.SetData32(0x1020, 0x9000); |
| 189 | |
| 190 | uint64_t entry_offset; |
| 191 | ASSERT_TRUE(interface.FindEntry(0x8100, &entry_offset)); |
| 192 | ASSERT_EQ(0x1010U, entry_offset); |
| 193 | |
| 194 | // To guarantee that we are using the cache on the second run, |
| 195 | // set the memory to a different value. |
| 196 | memory_.SetData32(0x1000, 0x15000); |
| 197 | memory_.SetData32(0x1008, 0x16000); |
| 198 | memory_.SetData32(0x1010, 0x17000); |
| 199 | memory_.SetData32(0x1018, 0x18000); |
| 200 | memory_.SetData32(0x1020, 0x19000); |
| 201 | ASSERT_TRUE(interface.FindEntry(0x8100, &entry_offset)); |
| 202 | ASSERT_EQ(0x1010U, entry_offset); |
| 203 | } |
| 204 | |
| 205 | TEST_F(ElfInterfaceArmTest, iterate) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 206 | ElfInterfaceArmFake interface(&memory_); |
| 207 | interface.FakeSetStartOffset(0x1000); |
| 208 | interface.FakeSetTotalEntries(5); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 209 | memory_.SetData32(0x1000, 0x5000); |
| 210 | memory_.SetData32(0x1008, 0x6000); |
| 211 | memory_.SetData32(0x1010, 0x7000); |
| 212 | memory_.SetData32(0x1018, 0x8000); |
| 213 | memory_.SetData32(0x1020, 0x9000); |
| 214 | |
| 215 | std::vector<uint32_t> entries; |
| 216 | for (auto addr : interface) { |
| 217 | entries.push_back(addr); |
| 218 | } |
| 219 | ASSERT_EQ(5U, entries.size()); |
| 220 | ASSERT_EQ(0x6000U, entries[0]); |
| 221 | ASSERT_EQ(0x7008U, entries[1]); |
| 222 | ASSERT_EQ(0x8010U, entries[2]); |
| 223 | ASSERT_EQ(0x9018U, entries[3]); |
| 224 | ASSERT_EQ(0xa020U, entries[4]); |
| 225 | |
| 226 | // Make sure the iterate cached the entries. |
| 227 | memory_.SetData32(0x1000, 0x11000); |
| 228 | memory_.SetData32(0x1008, 0x12000); |
| 229 | memory_.SetData32(0x1010, 0x13000); |
| 230 | memory_.SetData32(0x1018, 0x14000); |
| 231 | memory_.SetData32(0x1020, 0x15000); |
| 232 | |
| 233 | entries.clear(); |
| 234 | for (auto addr : interface) { |
| 235 | entries.push_back(addr); |
| 236 | } |
| 237 | ASSERT_EQ(5U, entries.size()); |
| 238 | ASSERT_EQ(0x6000U, entries[0]); |
| 239 | ASSERT_EQ(0x7008U, entries[1]); |
| 240 | ASSERT_EQ(0x8010U, entries[2]); |
| 241 | ASSERT_EQ(0x9018U, entries[3]); |
| 242 | ASSERT_EQ(0xa020U, entries[4]); |
| 243 | } |
| 244 | |
Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 245 | TEST_F(ElfInterfaceArmTest, HandleUnknownType_arm_exidx) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 246 | ElfInterfaceArmFake interface(&memory_); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 247 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 248 | interface.FakeSetStartOffset(0x1000); |
| 249 | interface.FakeSetTotalEntries(100); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 250 | |
Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 251 | // Verify that if the type is not the one we want, we don't set the values. |
| 252 | interface.HandleUnknownType(0x70000000, 0x2000, 320); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 253 | ASSERT_EQ(0x1000U, interface.start_offset()); |
| 254 | ASSERT_EQ(100U, interface.total_entries()); |
| 255 | |
| 256 | // Everything is correct and present. |
Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 257 | interface.HandleUnknownType(0x70000001, 0x2000, 320); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 258 | ASSERT_EQ(0x2000U, interface.start_offset()); |
Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 259 | ASSERT_EQ(40U, interface.total_entries()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | TEST_F(ElfInterfaceArmTest, StepExidx) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 263 | ElfInterfaceArmFake interface(&memory_); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 264 | |
| 265 | // FindEntry fails. |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 266 | bool finished; |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 267 | ASSERT_FALSE(interface.StepExidx(0x7000, nullptr, nullptr, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 268 | EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 269 | |
| 270 | // ExtractEntry should fail. |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 271 | interface.FakeSetStartOffset(0x1000); |
| 272 | interface.FakeSetTotalEntries(2); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 273 | memory_.SetData32(0x1000, 0x6000); |
| 274 | memory_.SetData32(0x1008, 0x8000); |
| 275 | |
| 276 | RegsArm regs; |
| 277 | regs[ARM_REG_SP] = 0x1000; |
| 278 | regs[ARM_REG_LR] = 0x20000; |
| 279 | regs.set_sp(regs[ARM_REG_SP]); |
| 280 | regs.set_pc(0x1234); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 281 | ASSERT_FALSE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 282 | EXPECT_EQ(ERROR_MEMORY_INVALID, interface.LastErrorCode()); |
| 283 | EXPECT_EQ(0x1004U, interface.LastErrorAddress()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 284 | |
| 285 | // Eval should fail. |
| 286 | memory_.SetData32(0x1004, 0x81000000); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 287 | ASSERT_FALSE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 288 | EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 289 | |
| 290 | // Everything should pass. |
| 291 | memory_.SetData32(0x1004, 0x80b0b0b0); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 292 | ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 293 | EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode()); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 294 | ASSERT_FALSE(finished); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 295 | ASSERT_EQ(0x1000U, regs.sp()); |
| 296 | ASSERT_EQ(0x1000U, regs[ARM_REG_SP]); |
| 297 | ASSERT_EQ(0x20000U, regs.pc()); |
| 298 | ASSERT_EQ(0x20000U, regs[ARM_REG_PC]); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 299 | |
| 300 | // Load bias is non-zero. |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 301 | interface.set_load_bias(0x1000); |
| 302 | ASSERT_TRUE(interface.StepExidx(0x8000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 303 | EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode()); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 304 | |
| 305 | // Pc too small. |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 306 | interface.set_load_bias(0x9000); |
| 307 | ASSERT_FALSE(interface.StepExidx(0x8000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 308 | EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST_F(ElfInterfaceArmTest, StepExidx_pc_set) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 312 | ElfInterfaceArmFake interface(&memory_); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 313 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 314 | interface.FakeSetStartOffset(0x1000); |
| 315 | interface.FakeSetTotalEntries(2); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 316 | memory_.SetData32(0x1000, 0x6000); |
| 317 | memory_.SetData32(0x1004, 0x808800b0); |
| 318 | memory_.SetData32(0x1008, 0x8000); |
| 319 | process_memory_.SetData32(0x10000, 0x10); |
| 320 | |
| 321 | RegsArm regs; |
| 322 | regs[ARM_REG_SP] = 0x10000; |
| 323 | regs[ARM_REG_LR] = 0x20000; |
| 324 | regs.set_sp(regs[ARM_REG_SP]); |
| 325 | regs.set_pc(0x1234); |
| 326 | |
| 327 | // Everything should pass. |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 328 | bool finished; |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 329 | ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 330 | EXPECT_EQ(ERROR_NONE, interface.LastErrorCode()); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 331 | ASSERT_FALSE(finished); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 332 | ASSERT_EQ(0x10004U, regs.sp()); |
| 333 | ASSERT_EQ(0x10004U, regs[ARM_REG_SP]); |
| 334 | ASSERT_EQ(0x10U, regs.pc()); |
| 335 | ASSERT_EQ(0x10U, regs[ARM_REG_PC]); |
| 336 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 337 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 338 | TEST_F(ElfInterfaceArmTest, StepExidx_cant_unwind) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 339 | ElfInterfaceArmFake interface(&memory_); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 340 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 341 | interface.FakeSetStartOffset(0x1000); |
| 342 | interface.FakeSetTotalEntries(1); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 343 | memory_.SetData32(0x1000, 0x6000); |
| 344 | memory_.SetData32(0x1004, 1); |
| 345 | |
| 346 | RegsArm regs; |
| 347 | regs[ARM_REG_SP] = 0x10000; |
| 348 | regs[ARM_REG_LR] = 0x20000; |
| 349 | regs.set_sp(regs[ARM_REG_SP]); |
| 350 | regs.set_pc(0x1234); |
| 351 | |
| 352 | bool finished; |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 353 | ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 354 | EXPECT_EQ(ERROR_NONE, interface.LastErrorCode()); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 355 | ASSERT_TRUE(finished); |
| 356 | ASSERT_EQ(0x10000U, regs.sp()); |
| 357 | ASSERT_EQ(0x10000U, regs[ARM_REG_SP]); |
| 358 | ASSERT_EQ(0x1234U, regs.pc()); |
| 359 | } |
| 360 | |
| 361 | TEST_F(ElfInterfaceArmTest, StepExidx_refuse_unwind) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 362 | ElfInterfaceArmFake interface(&memory_); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 363 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 364 | interface.FakeSetStartOffset(0x1000); |
| 365 | interface.FakeSetTotalEntries(1); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 366 | memory_.SetData32(0x1000, 0x6000); |
| 367 | memory_.SetData32(0x1004, 0x808000b0); |
| 368 | |
| 369 | RegsArm regs; |
| 370 | regs[ARM_REG_SP] = 0x10000; |
| 371 | regs[ARM_REG_LR] = 0x20000; |
| 372 | regs.set_sp(regs[ARM_REG_SP]); |
| 373 | regs.set_pc(0x1234); |
| 374 | |
| 375 | bool finished; |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 376 | ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 377 | EXPECT_EQ(ERROR_NONE, interface.LastErrorCode()); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 378 | ASSERT_TRUE(finished); |
| 379 | ASSERT_EQ(0x10000U, regs.sp()); |
| 380 | ASSERT_EQ(0x10000U, regs[ARM_REG_SP]); |
| 381 | ASSERT_EQ(0x1234U, regs.pc()); |
| 382 | } |
| 383 | |
Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 384 | TEST_F(ElfInterfaceArmTest, StepExidx_pc_zero) { |
| 385 | ElfInterfaceArmFake interface(&memory_); |
| 386 | |
| 387 | interface.FakeSetStartOffset(0x1000); |
| 388 | interface.FakeSetTotalEntries(1); |
| 389 | memory_.SetData32(0x1000, 0x6000); |
| 390 | // Set the pc using a pop r15 command. |
| 391 | memory_.SetData32(0x1004, 0x808800b0); |
| 392 | |
| 393 | // pc value of zero. |
| 394 | process_memory_.SetData32(0x10000, 0); |
| 395 | |
| 396 | RegsArm regs; |
| 397 | regs[ARM_REG_SP] = 0x10000; |
| 398 | regs[ARM_REG_LR] = 0x20000; |
| 399 | regs.set_sp(regs[ARM_REG_SP]); |
| 400 | regs.set_pc(0x1234); |
| 401 | |
| 402 | bool finished; |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 403 | ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 404 | EXPECT_EQ(ERROR_NONE, interface.LastErrorCode()); |
Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 405 | ASSERT_TRUE(finished); |
| 406 | ASSERT_EQ(0U, regs.pc()); |
| 407 | |
| 408 | // Now set the pc from the lr register (pop r14). |
| 409 | memory_.SetData32(0x1004, 0x808400b0); |
| 410 | |
| 411 | regs[ARM_REG_SP] = 0x10000; |
| 412 | regs[ARM_REG_LR] = 0x20000; |
| 413 | regs.set_sp(regs[ARM_REG_SP]); |
| 414 | regs.set_pc(0x1234); |
| 415 | |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 416 | ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 417 | EXPECT_EQ(ERROR_NONE, interface.LastErrorCode()); |
Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 418 | ASSERT_TRUE(finished); |
| 419 | ASSERT_EQ(0U, regs.pc()); |
| 420 | } |
| 421 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 422 | } // namespace unwindstack |