1 /* 2 * Copyright (c) 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 <string> 18 #include <unistd.h> 19 #include <vector> 20 21 #include "dfx_define.h" 22 #include "dfx_test_util.h" 23 #include "dfx_util.h" 24 #include "dfx_coredump_service.h" 25 #include "dfx_coredump_writer.h" 26 27 using namespace OHOS::HiviewDFX; 28 using namespace testing::ext; 29 using namespace std; 30 31 namespace OHOS { 32 namespace HiviewDFX { 33 class DfxCoreDumpWriterTest : public testing::Test { 34 public: SetUpTestCase(void)35 static void SetUpTestCase(void) {}; TearDownTestCase(void)36 static void TearDownTestCase(void) {} SetUp()37 void SetUp() {}; TearDown()38 void TearDown() {} 39 }; 40 } // namespace HiviewDFX 41 } // namespace OHOS 42 43 namespace { 44 /** 45 * @tc.name: DfxCoreDumpWriterTest001 46 * @tc.desc: test ProgramSegmentHeaderWriter 47 * @tc.type: FUNC 48 */ 49 HWTEST_F(DfxCoreDumpWriterTest, DfxCoreDumpWriterTest001, TestSize.Level2) 50 { 51 GTEST_LOG_(INFO) << "DfxCoreDumpWriterTest001: start."; 52 constexpr size_t bufferSize = 4096; 53 std::vector<char> buffer(bufferSize, 0); 54 char* mappedMemory = buffer.data(); 55 char* currentPointer = mappedMemory; 56 Elf64_Half phNum = 0; 57 DumpMemoryRegions dummyRegion1 { 58 .startHex = 0x1000, 59 .endHex = 0x2000, 60 .offsetHex = 5, 61 .memorySizeHex = 0x1000 62 }; 63 (void)strcpy_s(dummyRegion1.priority, 5, "r-xp"); 64 65 DumpMemoryRegions dummyRegion2 { 66 .startHex = 0x3000, 67 .endHex = 0x4000, 68 .offsetHex = 5, 69 .memorySizeHex = 0x1000 70 }; 71 (void)strcpy_s(dummyRegion2.priority, 5, "rw-p"); 72 73 std::vector<DumpMemoryRegions> maps = { dummyRegion1, dummyRegion2 }; 74 ProgramSegmentHeaderWriter writer(mappedMemory, currentPointer, phNum, maps); 75 76 currentPointer = writer.Write(); 77 78 ASSERT_TRUE(static_cast<const void*>(currentPointer) != static_cast<const void*>(mappedMemory)); 79 ASSERT_TRUE(phNum > 0); 80 81 GTEST_LOG_(INFO) << "DfxCoreDumpWriterTest001: end."; 82 } 83 84 /** 85 * @tc.name: DfxCoreDumpWriterTest002 86 * @tc.desc: test LoadSegmentWriter 87 * @tc.type: FUNC 88 */ 89 HWTEST_F(DfxCoreDumpWriterTest, DfxCoreDumpWriterTest002, TestSize.Level2) 90 { 91 GTEST_LOG_(INFO) << "DfxCoreDumpWriterTest002: start."; 92 93 constexpr size_t kRegionSize = 64; 94 auto* sourceBuffer = static_cast<char*>(malloc(kRegionSize)); 95 (void)memset_s(sourceBuffer, kRegionSize, 0xA5, kRegionSize); 96 97 uintptr_t fakeVaddr = reinterpret_cast<uintptr_t>(sourceBuffer); 98 99 constexpr size_t bufferSize = 8192; 100 std::vector<char> buffer(bufferSize, 0); 101 char* mappedMemory = buffer.data(); 102 char* currentPointer = mappedMemory + sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr); 103 104 Elf64_Phdr* ptLoad = reinterpret_cast<Elf64_Phdr*>(mappedMemory + sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr)); 105 106 ptLoad->p_vaddr = fakeVaddr; 107 ptLoad->p_memsz = kRegionSize; 108 109 pid_t testPid = getpid(); 110 Elf64_Half phNum = 2; 111 112 LoadSegmentWriter writer(mappedMemory, currentPointer, testPid, phNum); 113 114 char* result = writer.Write(); 115 116 ASSERT_TRUE(static_cast<const void*>(currentPointer) != static_cast<const void*>(result)); 117 118 GTEST_LOG_(INFO) << "DfxCoreDumpWriterTest002: end."; 119 } 120 121 /** 122 * @tc.name: DfxCoreDumpWriterTest003 123 * @tc.desc: test SectionHeaderTableWriter 124 * @tc.type: FUNC 125 */ 126 HWTEST_F(DfxCoreDumpWriterTest, DfxCoreDumpWriterTest003, TestSize.Level2) 127 { 128 GTEST_LOG_(INFO) << "DfxCoreDumpWriterTest003: start."; 129 constexpr size_t bufferSize = 4096; 130 std::vector<char> buffer(bufferSize, 0); 131 char* mappedMemory = buffer.data(); 132 char* currentPointer = mappedMemory; 133 134 SectionHeaderTableWriter writer(mappedMemory, currentPointer); 135 char* result = writer.Write(); 136 137 ASSERT_TRUE(static_cast<const void*>(currentPointer) != static_cast<const void*>(result)); 138 GTEST_LOG_(INFO) << "DfxCoreDumpWriterTest003: end."; 139 } 140 141 /** 142 * @tc.name: DfxCoreDumpWriterTest004 143 * @tc.desc: test NoteSegmentWriter 144 * @tc.type: FUNC 145 */ 146 HWTEST_F(DfxCoreDumpWriterTest, DfxCoreDumpWriterTest004, TestSize.Level2) 147 { 148 GTEST_LOG_(INFO) << "DfxCoreDumpWriterTest004: start."; 149 constexpr size_t bufferSize = 4096; 150 std::vector<char> buffer(bufferSize, 0); 151 char* mappedMemory = buffer.data(); 152 char* currentPointer = mappedMemory; 153 154 CoreDumpThread coreDumpThread; 155 coreDumpThread.targetPid = getpid(); 156 coreDumpThread.targetTid = gettid(); 157 DumpMemoryRegions dummyRegion { 158 .startHex = 0x1000, 159 .endHex = 0x2000, 160 .offsetHex = 5, 161 .memorySizeHex = 0x1000 162 }; 163 std::vector<DumpMemoryRegions> maps = { dummyRegion }; 164 std::shared_ptr<DfxRegs> regs = DfxRegs::Create(); 165 166 NoteSegmentWriter writer(mappedMemory, currentPointer, coreDumpThread, maps, regs); 167 char* result = writer.Write(); 168 169 ASSERT_TRUE(static_cast<const void*>(currentPointer) != static_cast<const void*>(result)); 170 GTEST_LOG_(INFO) << "DfxCoreDumpWriterTest004: end."; 171 } 172 }