1 /* 2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 #include <ctime> 18 #include <securec.h> 19 #include <string> 20 #include <vector> 21 22 #include "dfx_accessors.h" 23 #include "dfx_elf.h" 24 #include "dfx_maps.h" 25 #include "dfx_memory.h" 26 #include "dfx_regs.h" 27 #include "dfx_regs_get.h" 28 #include "dfx_symbols.h" 29 #include "dfx_ptrace.h" 30 #include "dfx_test_util.h" 31 #include "dwarf_define.h" 32 #include "elf_factory.h" 33 #include "stack_utils.h" 34 35 using namespace OHOS::HiviewDFX; 36 using namespace testing::ext; 37 using namespace std; 38 39 namespace OHOS { 40 namespace HiviewDFX { 41 class DfxMemoryTest : public testing::Test { 42 public: SetUpTestCase(void)43 static void SetUpTestCase(void) {} TearDownTestCase(void)44 static void TearDownTestCase(void) {} SetUp()45 void SetUp() {} TearDown()46 void TearDown() {} 47 }; 48 49 namespace { 50 /** 51 * @tc.name: DfxMemoryTest001 52 * @tc.desc: test DfxMemory class ReadReg 53 * @tc.type: FUNC 54 */ 55 HWTEST_F(DfxMemoryTest, DfxMemoryTest001, TestSize.Level2) 56 { 57 GTEST_LOG_(INFO) << "DfxMemoryTest001: start."; 58 uintptr_t regs[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}; 59 UnwindContext ctx; 60 ctx.regs = DfxRegs::CreateFromRegs(UnwindMode::DWARF_UNWIND, regs, sizeof(regs) / sizeof(regs[0])); 61 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_LOCAL); 62 memory->SetCtx(&ctx); 63 uintptr_t value; 64 bool ret = memory->ReadReg(0, &value); 65 EXPECT_EQ(true, ret) << "DfxMemoryTest001: ret" << ret; 66 EXPECT_EQ(static_cast<uintptr_t>(0x1), value) << "DfxMemoryTest001: value" << value; 67 GTEST_LOG_(INFO) << "DfxMemoryTest001: end."; 68 } 69 70 /** 71 * @tc.name: DfxMemoryTest002 72 * @tc.desc: test DfxMemory class Read 73 * @tc.type: FUNC 74 */ 75 HWTEST_F(DfxMemoryTest, DfxMemoryTest002, TestSize.Level2) 76 { 77 GTEST_LOG_(INFO) << "DfxMemoryTest002: start."; 78 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; 79 80 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 81 uintptr_t value; 82 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_LOCAL); 83 bool ret = memory->Read<uintptr_t>(addr, &value, false); 84 EXPECT_EQ(true, ret) << "DfxMemoryTest002: ret:" << ret; 85 86 87 uint64_t tmp; 88 memory->Read(addr, &tmp, sizeof(uint8_t), false); 89 ASSERT_EQ(tmp, 0x01); 90 91 memory->Read(addr, &tmp, sizeof(uint16_t), false); 92 ASSERT_EQ(tmp, 0x0201); 93 94 memory->Read(addr, &tmp, sizeof(uint32_t), false); 95 ASSERT_EQ(tmp, 0x04030201); 96 97 memory->Read(addr, &tmp, sizeof(uint64_t), false); 98 ASSERT_EQ(tmp, 0x0807060504030201); 99 100 GTEST_LOG_(INFO) << "DfxMemoryTest002: end."; 101 } 102 103 /** 104 * @tc.name: DfxMemoryTest003 105 * @tc.desc: test DfxMemory class Read 106 * @tc.type: FUNC 107 */ 108 HWTEST_F(DfxMemoryTest, DfxMemoryTest003, TestSize.Level2) 109 { 110 GTEST_LOG_(INFO) << "DfxMemoryTest003: start."; 111 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; 112 UnwindContext ctx; 113 ASSERT_TRUE(StackUtils::GetSelfStackRange(ctx.stackBottom, ctx.stackTop)); 114 ctx.stackCheck = true; 115 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_LOCAL); 116 memory->SetCtx(&ctx); 117 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 118 uintptr_t value; 119 ASSERT_TRUE(memory->Read<uintptr_t>(addr, &value, false)); 120 #if defined(__arm__) 121 ASSERT_EQ(value, 0x04030201); 122 #elif defined(__aarch64__) 123 ASSERT_EQ(value, 0x0807060504030201); 124 #endif 125 126 uint64_t tmp; 127 ASSERT_TRUE(memory->Read(addr, &tmp, sizeof(uint8_t), false)); 128 ASSERT_EQ(tmp, 0x01); 129 130 ASSERT_TRUE(memory->Read(addr, &tmp, sizeof(uint16_t), false)); 131 ASSERT_EQ(tmp, 0x0201); 132 133 ASSERT_TRUE(memory->Read(addr, &tmp, sizeof(uint32_t), false)); 134 ASSERT_EQ(tmp, 0x04030201); 135 136 ASSERT_TRUE(memory->Read(addr, &tmp, sizeof(uint64_t), false)); 137 ASSERT_EQ(tmp, 0x0807060504030201); 138 139 GTEST_LOG_(INFO) << "DfxMemoryTest003: end."; 140 } 141 142 /** 143 * @tc.name: DfxMemoryTest004 144 * @tc.desc: test DfxMemory class Read 145 * @tc.type: FUNC 146 */ 147 HWTEST_F(DfxMemoryTest, DfxMemoryTest004, TestSize.Level2) 148 { 149 GTEST_LOG_(INFO) << "DfxMemoryTest004: start."; 150 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; 151 UnwindContext ctx; 152 ASSERT_TRUE(StackUtils::GetSelfStackRange(ctx.stackBottom, ctx.stackTop)); 153 ctx.stackCheck = true; 154 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_LOCAL); 155 memory->SetCtx(&ctx); 156 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 157 uint8_t tmp8; 158 ASSERT_TRUE(memory->Read<uint8_t>(addr, &tmp8, false)); 159 ASSERT_EQ(tmp8, 0x01); 160 uint16_t tmp16; 161 ASSERT_TRUE(memory->Read<uint16_t>(addr, &tmp16, false)); 162 ASSERT_EQ(tmp16, 0x0201); 163 uint32_t tmp32; 164 ASSERT_TRUE(memory->Read<uint32_t>(addr, &tmp32, false)); 165 ASSERT_EQ(tmp32, 0x04030201); 166 uint64_t tmp64; 167 ASSERT_TRUE(memory->Read<uint64_t>(addr, &tmp64, false)); 168 ASSERT_EQ(tmp64, 0x0807060504030201); 169 GTEST_LOG_(INFO) << "DfxMemoryTest004: end."; 170 } 171 172 /** 173 * @tc.name: DfxMemoryTest005 174 * @tc.desc: test DfxMemory class Read 175 * @tc.type: FUNC 176 */ 177 HWTEST_F(DfxMemoryTest, DfxMemoryTest005, TestSize.Level2) 178 { 179 GTEST_LOG_(INFO) << "DfxMemoryTest005: start."; 180 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; 181 UnwindContext ctx; 182 ASSERT_TRUE(StackUtils::GetSelfStackRange(ctx.stackBottom, ctx.stackTop)); 183 ctx.stackCheck = true; 184 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_LOCAL); 185 memory->SetCtx(&ctx); 186 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 187 uintptr_t valuePrel32; 188 ASSERT_TRUE(memory->ReadPrel31(addr, &valuePrel32)); 189 uintptr_t invalidAddr = 0; 190 ASSERT_FALSE(memory->ReadPrel31(invalidAddr, &valuePrel32)); 191 ASSERT_EQ(valuePrel32, 0x04030201 + addr); 192 char testStr[] = "Test ReadString Func"; 193 std::string resultStr; 194 uintptr_t addrStr = reinterpret_cast<uintptr_t>(&testStr[0]); 195 ASSERT_TRUE(memory->ReadString(addrStr, &resultStr, sizeof(testStr)/sizeof(char), false)); 196 ASSERT_EQ(testStr, resultStr); 197 ASSERT_EQ(memory->ReadUleb128(addr), 1U); 198 ASSERT_EQ(memory->ReadSleb128(addr), 2); 199 ASSERT_EQ(memory->ReadSleb128(invalidAddr), 0); 200 GTEST_LOG_(INFO) << "DfxMemoryTest005: end."; 201 } 202 203 /** 204 * @tc.name: DfxMemoryTest006 205 * @tc.desc: test DfxMemory class Read 206 * @tc.type: FUNC 207 */ 208 HWTEST_F(DfxMemoryTest, DfxMemoryTest006, TestSize.Level2) 209 { 210 GTEST_LOG_(INFO) << "DfxMemoryTest006: start."; 211 UnwindContext ctx; 212 ASSERT_TRUE(StackUtils::GetSelfStackRange(ctx.stackBottom, ctx.stackTop)); 213 ctx.stackCheck = true; 214 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_LOCAL); 215 memory->SetCtx(&ctx); 216 ASSERT_EQ(memory->GetEncodedSize(DW_EH_PE_absptr), sizeof(uintptr_t)); 217 ASSERT_EQ(memory->GetEncodedSize(DW_EH_PE_sdata1), 1); 218 ASSERT_EQ(memory->GetEncodedSize(DW_EH_PE_sdata2), 2); 219 ASSERT_EQ(memory->GetEncodedSize(DW_EH_PE_sdata4), 4); 220 ASSERT_EQ(memory->GetEncodedSize(DW_EH_PE_sdata8), 8); 221 ASSERT_EQ(memory->GetEncodedSize(DW_EH_PE_sleb128), 0); 222 ASSERT_EQ(memory->GetEncodedSize(DW_EH_PE_omit), 0); 223 GTEST_LOG_(INFO) << "DfxMemoryTest006: end."; 224 } 225 226 /** 227 * @tc.name: DfxMemoryTest007 228 * @tc.desc: test DfxMemory class ReadReg in remote case 229 * @tc.type: FUNC 230 */ 231 HWTEST_F(DfxMemoryTest, DfxMemoryTest007, TestSize.Level2) 232 { 233 GTEST_LOG_(INFO) << "DfxMemoryTest007: start."; 234 uintptr_t regs[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}; 235 UnwindContext ctx; 236 ctx.regs = DfxRegs::CreateFromRegs(UnwindMode::DWARF_UNWIND, regs, sizeof(regs) / sizeof(regs[0])); 237 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_REMOTE); 238 uintptr_t value; 239 EXPECT_FALSE(memory->ReadReg(0, &value)); 240 memory->SetCtx(&ctx); 241 bool ret = memory->ReadReg(0, &value); 242 EXPECT_EQ(true, ret) << "DfxMemoryTest007: ret" << ret; 243 EXPECT_EQ(static_cast<uintptr_t>(0x1), value) << "DfxMemoryTest007: value" << value; 244 GTEST_LOG_(INFO) << "DfxMemoryTest007: end."; 245 } 246 /** 247 * @tc.name: DfxMemoryTest008 248 * @tc.desc: test DfxMemory class Read in remote case 249 * @tc.type: FUNC 250 */ 251 HWTEST_F(DfxMemoryTest, DfxMemoryTest008, TestSize.Level2) 252 { 253 GTEST_LOG_(INFO) << "DfxMemoryTest008: start."; 254 static pid_t pid = getpid(); 255 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; 256 pid_t child = fork(); 257 if (child == 0) { 258 GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid(); 259 DfxPtrace::Attach(pid); 260 uintptr_t value; 261 UnwindContext ctx; 262 ctx.pid = pid; 263 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_REMOTE); 264 memory->SetCtx(&ctx); 265 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 266 bool ret = memory->Read<uintptr_t>(addr, &value, false); 267 EXPECT_EQ(true, ret) << "DfxMemoryTest008: ret:" << ret; 268 uint64_t tmp; 269 memory->Read(addr, &tmp, sizeof(uint8_t), false); 270 EXPECT_EQ(tmp, 0x01); 271 272 memory->Read(addr, &tmp, sizeof(uint16_t), false); 273 EXPECT_EQ(tmp, 0x0201); 274 275 memory->Read(addr, &tmp, sizeof(uint32_t), false); 276 EXPECT_EQ(tmp, 0x04030201); 277 278 memory->Read(addr, &tmp, sizeof(uint64_t), false); 279 EXPECT_EQ(tmp, 0x0807060504030201); 280 DfxPtrace::Detach(pid); 281 CheckAndExit(HasFailure()); 282 } 283 int status; 284 wait(&status); 285 ASSERT_EQ(status, 0); 286 GTEST_LOG_(INFO) << "DfxMemoryTest008: end."; 287 } 288 289 /** 290 * @tc.name: DfxMemoryTest009 291 * @tc.desc: test DfxMemory class Read in remote case 292 * @tc.type: FUNC 293 */ 294 HWTEST_F(DfxMemoryTest, DfxMemoryTest009, TestSize.Level2) 295 { 296 GTEST_LOG_(INFO) << "DfxMemoryTest009: start."; 297 static pid_t pid = getpid(); 298 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; 299 pid_t child = fork(); 300 if (child == 0) { 301 GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid(); 302 DfxPtrace::Attach(pid); 303 UnwindContext ctx; 304 ctx.pid = pid; 305 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_REMOTE); 306 memory->SetCtx(&ctx); 307 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 308 uintptr_t value; 309 EXPECT_TRUE(memory->Read<uintptr_t>(addr, &value, false)); 310 #if defined(__arm__) 311 EXPECT_EQ(value, 0x04030201); 312 #elif defined(__aarch64__) 313 EXPECT_EQ(value, 0x0807060504030201); 314 #endif 315 316 uint64_t tmp; 317 EXPECT_TRUE(memory->Read(addr, &tmp, sizeof(uint8_t), false)); 318 EXPECT_EQ(tmp, 0x01); 319 320 EXPECT_TRUE(memory->Read(addr, &tmp, sizeof(uint16_t), false)); 321 EXPECT_EQ(tmp, 0x0201); 322 323 EXPECT_TRUE(memory->Read(addr, &tmp, sizeof(uint32_t), false)); 324 EXPECT_EQ(tmp, 0x04030201); 325 326 EXPECT_TRUE(memory->Read(addr, &tmp, sizeof(uint64_t), false)); 327 EXPECT_EQ(tmp, 0x0807060504030201); 328 DfxPtrace::Detach(pid); 329 CheckAndExit(HasFailure()); 330 } 331 int status; 332 wait(&status); 333 ASSERT_EQ(status, 0); 334 GTEST_LOG_(INFO) << "DfxMemoryTest009: end."; 335 } 336 337 /** 338 * @tc.name: DfxMemoryTest010 339 * @tc.desc: test DfxMemory class Read in remote case 340 * @tc.type: FUNC 341 */ 342 HWTEST_F(DfxMemoryTest, DfxMemoryTest010, TestSize.Level2) 343 { 344 GTEST_LOG_(INFO) << "DfxMemoryTest010: start."; 345 static pid_t pid = getpid(); 346 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; 347 pid_t child = fork(); 348 if (child == 0) { 349 GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid(); 350 DfxPtrace::Attach(pid); 351 352 UnwindContext ctx; 353 ctx.pid = pid; 354 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_REMOTE); 355 memory->SetCtx(&ctx); 356 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 357 uint8_t tmp8; 358 EXPECT_TRUE(memory->Read<uint8_t>(addr, &tmp8, false)); 359 EXPECT_EQ(tmp8, 0x01); 360 uint16_t tmp16; 361 EXPECT_TRUE(memory->Read<uint16_t>(addr, &tmp16, false)); 362 EXPECT_EQ(tmp16, 0x0201); 363 uint32_t tmp32; 364 EXPECT_TRUE(memory->Read<uint32_t>(addr, &tmp32, false)); 365 EXPECT_EQ(tmp32, 0x04030201); 366 uint64_t tmp64; 367 EXPECT_TRUE(memory->Read<uint64_t>(addr, &tmp64, false)); 368 EXPECT_EQ(tmp64, 0x0807060504030201); 369 DfxPtrace::Detach(pid); 370 CheckAndExit(HasFailure()); 371 } 372 int status; 373 wait(&status); 374 ASSERT_EQ(status, 0); 375 GTEST_LOG_(INFO) << "DfxMemoryTest010: end."; 376 } 377 378 /** 379 * @tc.name: DfxMemoryTest011 380 * @tc.desc: test DfxMemory class Read in remote case 381 * @tc.type: FUNC 382 */ 383 HWTEST_F(DfxMemoryTest, DfxMemoryTest011, TestSize.Level2) 384 { 385 GTEST_LOG_(INFO) << "DfxMemoryTest011: start."; 386 static pid_t pid = getpid(); 387 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10}; 388 char testStr[] = "Test ReadString Func Test ReadString Func Test ReadString Func \ 389 Test ReadString Func Test ReadString Func Test ReadString Func Test ReadString Func"; 390 pid_t child = fork(); 391 if (child == 0) { 392 GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid(); 393 DfxPtrace::Attach(getppid()); 394 UnwindContext ctx; 395 ctx.pid = getppid(); 396 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_REMOTE); 397 memory->SetCtx(&ctx); 398 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 399 std::string resultStr; 400 uintptr_t addrStr = reinterpret_cast<uintptr_t>(&testStr[0]); 401 EXPECT_TRUE(memory->ReadString(addrStr, &resultStr, sizeof(testStr)/sizeof(char), false)); 402 EXPECT_EQ(testStr, resultStr); 403 EXPECT_TRUE(memory->ReadString(addrStr, &resultStr, sizeof(testStr)/sizeof(char), true)); 404 EXPECT_EQ(testStr, resultStr); 405 EXPECT_FALSE(memory->ReadString(addrStr, nullptr, sizeof(testStr)/sizeof(char), true)); 406 407 uintptr_t val; 408 EXPECT_EQ(memory->ReadUleb128(addr), 1U); 409 EXPECT_EQ(memory->ReadSleb128(addr), 2); 410 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_uleb128); 411 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sleb128); 412 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_udata1); 413 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sdata1); 414 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_udata2); 415 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sdata2); 416 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_udata4); 417 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sdata4); 418 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_udata8); 419 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sdata8); 420 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_omit); 421 DfxPtrace::Detach(pid); 422 CheckAndExit(HasFailure()); 423 } 424 int status; 425 wait(&status); 426 ASSERT_EQ(status, 0); 427 GTEST_LOG_(INFO) << "DfxMemoryTest011: end."; 428 } 429 430 /** 431 * @tc.name: DfxMemoryTest012 432 * @tc.desc: test DfxMemory class Read in remote case 433 * @tc.type: FUNC 434 */ 435 HWTEST_F(DfxMemoryTest, DfxMemoryTest012, TestSize.Level2) 436 { 437 GTEST_LOG_(INFO) << "DfxMemoryTest012: start."; 438 static pid_t pid = getpid(); 439 pid_t child = fork(); 440 if (child == 0) { 441 GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid(); 442 DfxPtrace::Attach(pid); 443 UnwindContext ctx; 444 ctx.pid = pid; 445 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_REMOTE); 446 memory->SetCtx(&ctx); 447 EXPECT_EQ(memory->GetEncodedSize(DW_EH_PE_absptr), sizeof(uintptr_t)); 448 EXPECT_EQ(memory->GetEncodedSize(DW_EH_PE_sdata1), 1); 449 EXPECT_EQ(memory->GetEncodedSize(DW_EH_PE_sdata2), 2); 450 EXPECT_EQ(memory->GetEncodedSize(DW_EH_PE_sdata4), 4); 451 EXPECT_EQ(memory->GetEncodedSize(DW_EH_PE_sdata8), 8); 452 DfxPtrace::Detach(pid); 453 CheckAndExit(HasFailure()); 454 } 455 int status; 456 wait(&status); 457 ASSERT_EQ(status, 0); 458 GTEST_LOG_(INFO) << "DfxMemoryTest012: end."; 459 } 460 461 /** 462 * @tc.name: DfxMemoryTest013 463 * @tc.desc: test DfxMemory class Read in error case 464 * @tc.type: FUNC 465 */ 466 HWTEST_F(DfxMemoryTest, DfxMemoryTest013, TestSize.Level2) 467 { 468 GTEST_LOG_(INFO) << "DfxMemoryTest013: start."; 469 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_LOCAL); 470 uintptr_t val; 471 EXPECT_FALSE(memory->ReadReg(0, &val)); 472 uintptr_t regs[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}; 473 UnwindContext ctx; 474 ctx.regs = DfxRegs::CreateFromRegs(UnwindMode::DWARF_UNWIND, regs, sizeof(regs) / sizeof(regs[0])); 475 memory->SetCtx(&ctx); 476 EXPECT_FALSE(memory->ReadReg(-1, &val)); 477 478 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; 479 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 480 EXPECT_FALSE(memory->ReadMem(addr, nullptr)); 481 EXPECT_FALSE(memory->Read<uintptr_t>(addr, nullptr, false)); 482 EXPECT_FALSE(memory->Read(addr, nullptr, sizeof(uint8_t), false)); 483 EXPECT_FALSE(memory->Read<uint8_t>(addr, nullptr, false)); 484 EXPECT_FALSE(memory->Read<uint16_t>(addr, nullptr, false)); 485 EXPECT_FALSE(memory->Read<uint32_t>(addr, nullptr, false)); 486 EXPECT_FALSE(memory->Read<uint64_t>(addr, nullptr, false)); 487 GTEST_LOG_(INFO) << "DfxMemoryTest013: end."; 488 } 489 490 /** 491 * @tc.name: DfxMemoryTest014 492 * @tc.desc: test DfxMemory class Read in error case 493 * @tc.type: FUNC 494 */ 495 HWTEST_F(DfxMemoryTest, DfxMemoryTest014, TestSize.Level2) 496 { 497 GTEST_LOG_(INFO) << "DfxMemoryTest014: start."; 498 auto memory1 = std::make_shared<DfxMemory>(UNWIND_TYPE_CUSTOMIZE, nullptr); 499 uintptr_t addr = 0; 500 int val = memory1->ReadEncodedValue(addr, DW_EH_PE_omit); 501 EXPECT_EQ(val, 0); 502 memory1->ReadEncodedValue(addr, DW_EH_PE_aligned); 503 std::shared_ptr<DfxMap> map = nullptr; 504 auto memory2 = std::make_shared<DfxMemory>(UNWIND_TYPE_CUSTOMIZE_LOCAL, nullptr); 505 auto memory3 = std::make_shared<DfxMemory>((UnwindType)1, nullptr); 506 val = memory2->GetMapByPc(0, map); 507 EXPECT_EQ(val, UNW_ERROR_INVALID_MEMORY); 508 UnwindTableInfo uti; 509 val = memory2->FindUnwindTable(0, uti); 510 EXPECT_EQ(val, UNW_ERROR_INVALID_MEMORY); 511 bool cur = DfxAccessors::GetMapByPcAndCtx(0, map, nullptr); 512 EXPECT_FALSE(cur); 513 GTEST_LOG_(INFO) << "DfxMemoryTest014: end."; 514 } 515 516 /** 517 * @tc.name: DfxMemoryTest015 518 * @tc.desc: test DfxMemory class 256 Read 519 * @tc.type: FUNC 520 */ 521 HWTEST_F(DfxMemoryTest, DfxMemoryTest015, TestSize.Level2) 522 { 523 GTEST_LOG_(INFO) << "DfxMemoryTest015: start."; 524 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; 525 UnwindContext ctx; 526 ASSERT_TRUE(StackUtils::GetSelfStackRange(ctx.stackBottom, ctx.stackTop)); 527 ctx.stackCheck = true; 528 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_LOCAL); 529 memory->SetCtx(&ctx); 530 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 531 uintptr_t valuePrel32; 532 ASSERT_TRUE(memory->ReadPrel31(addr, &valuePrel32)); 533 uintptr_t invalidAddr = 0; 534 ASSERT_FALSE(memory->ReadPrel31(invalidAddr, &valuePrel32)); 535 ASSERT_EQ(valuePrel32, 0x04030201 + addr); 536 char testStr[] = "Test ReadString Func adfjak adfaaea- adfaf zxcdaa adfae ?dafafe aeacvdcx " 537 "edascccfae egfag xzfafasdaeacvdcx edfae egfag xzfafasd Test ReadString" 538 " Func adfjak adfaaea- adfaf zxcdaa adfae ?dafafe aeacvdcx edascccfae" 539 " egfag xzfafasdaeacvdcx edfae egfag xzfafasdc"; 540 std::string resultStr; 541 uintptr_t addrStr = reinterpret_cast<uintptr_t>(&testStr[0]); 542 ASSERT_TRUE(memory->ReadString(addrStr, &resultStr, sizeof(testStr)/sizeof(char), false)); 543 ASSERT_EQ(testStr, resultStr); 544 ASSERT_EQ(memory->ReadUleb128(addr), 1U); 545 ASSERT_EQ(memory->ReadSleb128(addr), 2); 546 ASSERT_EQ(memory->ReadSleb128(invalidAddr), 0); 547 GTEST_LOG_(INFO) << "DfxMemoryTest015: end."; 548 } 549 550 /** 551 * @tc.name: DfxMemoryTest016 552 * @tc.desc: test GetArkStackRange FUNC 553 * @tc.type: FUNC 554 */ 555 HWTEST_F(DfxMemoryTest, DfxMemoryTest016, TestSize.Level2) 556 { 557 GTEST_LOG_(INFO) << "DfxMemoryTest016: start."; 558 uintptr_t stackBottom; 559 uintptr_t stackTop; 560 bool ret = StackUtils::Instance().GetMainStackRange(stackBottom, stackTop); 561 ASSERT_TRUE(ret); 562 GTEST_LOG_(INFO) << "DfxMemoryTest016: end."; 563 } 564 565 /** 566 * @tc.name: DfxMemoryTest017 567 * @tc.desc: test ReadFormatEncodedValue FUNC 568 * @tc.type: FUNC 569 */ 570 HWTEST_F(DfxMemoryTest, DfxMemoryTest017, TestSize.Level2) 571 { 572 GTEST_LOG_(INFO) << "DfxMemoryTest017: start."; 573 uint8_t values[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10}; 574 auto memory = std::make_shared<DfxMemory>(UNWIND_TYPE_REMOTE); 575 uintptr_t addr = reinterpret_cast<uintptr_t>(&values[0]); 576 uintptr_t val; 577 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_uleb128); 578 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sleb128); 579 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_udata1); 580 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sdata1); 581 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_udata2); 582 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sdata2); 583 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_udata4); 584 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sdata4); 585 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_udata8); 586 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_sdata8); 587 memory->ReadFormatEncodedValue(addr, val, DW_EH_PE_omit); 588 uint8_t formatEncoding = 0x11; 589 memory->ReadFormatEncodedValue(addr, val, formatEncoding); 590 ASSERT_EQ(val, 0); 591 GTEST_LOG_(INFO) << "DfxMemoryTest017: end."; 592 } 593 594 /** 595 * @tc.name: DfxMemoryTest018 596 * @tc.desc: test ReadFormatEncodedValue FUNC 597 * @tc.type: FUNC 598 */ 599 HWTEST_F(DfxMemoryTest, DfxMemoryTest018, TestSize.Level2) 600 { 601 GTEST_LOG_(INFO) << "DfxMemoryTest018: start."; 602 auto accessors = std::make_shared<UnwindAccessors>(); 603 DfxAccessorsCustomize acc(nullptr); 604 acc.AccessReg(0, nullptr, nullptr); 605 UnwindTableInfo uti; 606 acc.FindUnwindTable(0, uti, nullptr); 607 auto map = std::make_shared<DfxMap>(); 608 acc.GetMapByPc(0, map, nullptr); 609 DfxAccessorsCustomize acc1(accessors); 610 acc1.AccessReg(0, nullptr, nullptr); 611 acc1.FindUnwindTable(0, uti, nullptr); 612 int ret = acc1.GetMapByPc(0, map, nullptr); 613 ASSERT_EQ(ret, -1); 614 GTEST_LOG_(INFO) << "DfxMemoryTest018: end."; 615 } 616 } 617 } // namespace HiviewDFX 618 } // namespace OHOS 619