|
| 1 | +//===- bolt/unittest/Core/MemoryMaps.cpp ----------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "bolt/Core/BinaryContext.h" |
| 10 | +#include "bolt/Profile/DataAggregator.h" |
| 11 | +#include "llvm/BinaryFormat/ELF.h" |
| 12 | +#include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 13 | +#include "llvm/Support/CommandLine.h" |
| 14 | +#include "llvm/Support/TargetSelect.h" |
| 15 | +#include "llvm/Testing/Support/Error.h" |
| 16 | +#include "gtest/gtest.h" |
| 17 | + |
| 18 | +using namespace llvm; |
| 19 | +using namespace llvm::object; |
| 20 | +using namespace llvm::ELF; |
| 21 | +using namespace bolt; |
| 22 | + |
| 23 | +namespace opts { |
| 24 | +extern cl::opt<std::string> ReadPerfEvents; |
| 25 | +} // namespace opts |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +/// Perform checks on memory map events normally captured in perf. Tests use |
| 30 | +/// the 'opts::ReadPerfEvents' flag to emulate these events, passing a custom |
| 31 | +/// 'perf script' output to DataAggregator. |
| 32 | +struct MemoryMapsTester : public testing::TestWithParam<Triple::ArchType> { |
| 33 | + void SetUp() override { |
| 34 | + initalizeLLVM(); |
| 35 | + prepareElf(); |
| 36 | + initializeBOLT(); |
| 37 | + } |
| 38 | + |
| 39 | +protected: |
| 40 | + void initalizeLLVM() { |
| 41 | + llvm::InitializeAllTargetInfos(); |
| 42 | + llvm::InitializeAllTargetMCs(); |
| 43 | + llvm::InitializeAllAsmParsers(); |
| 44 | + llvm::InitializeAllDisassemblers(); |
| 45 | + llvm::InitializeAllTargets(); |
| 46 | + llvm::InitializeAllAsmPrinters(); |
| 47 | + } |
| 48 | + |
| 49 | + void prepareElf() { |
| 50 | + memcpy(ElfBuf, "\177ELF", 4); |
| 51 | + ELF64LE::Ehdr *EHdr = reinterpret_cast<typename ELF64LE::Ehdr *>(ElfBuf); |
| 52 | + EHdr->e_ident[llvm::ELF::EI_CLASS] = llvm::ELF::ELFCLASS64; |
| 53 | + EHdr->e_ident[llvm::ELF::EI_DATA] = llvm::ELF::ELFDATA2LSB; |
| 54 | + EHdr->e_machine = GetParam() == Triple::aarch64 ? EM_AARCH64 : EM_X86_64; |
| 55 | + MemoryBufferRef Source(StringRef(ElfBuf, sizeof(ElfBuf)), "ELF"); |
| 56 | + ObjFile = cantFail(ObjectFile::createObjectFile(Source)); |
| 57 | + } |
| 58 | + |
| 59 | + void initializeBOLT() { |
| 60 | + Relocation::Arch = ObjFile->makeTriple().getArch(); |
| 61 | + BC = cantFail(BinaryContext::createBinaryContext( |
| 62 | + ObjFile->makeTriple(), ObjFile->getFileName(), nullptr, true, |
| 63 | + DWARFContext::create(*ObjFile.get()), {llvm::outs(), llvm::errs()})); |
| 64 | + ASSERT_FALSE(!BC); |
| 65 | + } |
| 66 | + |
| 67 | + char ElfBuf[sizeof(typename ELF64LE::Ehdr)] = {}; |
| 68 | + std::unique_ptr<ObjectFile> ObjFile; |
| 69 | + std::unique_ptr<BinaryContext> BC; |
| 70 | +}; |
| 71 | +} // namespace |
| 72 | + |
| 73 | +#ifdef X86_AVAILABLE |
| 74 | + |
| 75 | +INSTANTIATE_TEST_SUITE_P(X86, MemoryMapsTester, |
| 76 | + ::testing::Values(Triple::x86_64)); |
| 77 | + |
| 78 | +#endif |
| 79 | + |
| 80 | +#ifdef AARCH64_AVAILABLE |
| 81 | + |
| 82 | +INSTANTIATE_TEST_SUITE_P(AArch64, MemoryMapsTester, |
| 83 | + ::testing::Values(Triple::aarch64)); |
| 84 | + |
| 85 | +#endif |
| 86 | + |
| 87 | +/// Check that the correct mmap size is computed when we have multiple text |
| 88 | +/// segment mappings. |
| 89 | +TEST_P(MemoryMapsTester, ParseMultipleSegments) { |
| 90 | + const int Pid = 1234; |
| 91 | + StringRef Filename = "BINARY"; |
| 92 | + opts::ReadPerfEvents = formatv( |
| 93 | + "name 0 [000] 0.000000: PERF_RECORD_MMAP2 {0}/{0}: " |
| 94 | + "[0xabc0000000(0x1000000) @ 0x11c0000 103:01 1573523 0]: r-xp {1}\n" |
| 95 | + "name 0 [000] 0.000000: PERF_RECORD_MMAP2 {0}/{0}: " |
| 96 | + "[0xabc2000000(0x8000000) @ 0x31d0000 103:01 1573523 0]: r-xp {1}\n", |
| 97 | + Pid, Filename); |
| 98 | + |
| 99 | + BC->SegmentMapInfo[0x11da000] = |
| 100 | + SegmentInfo{0x11da000, 0x10da000, 0x11ca000, 0x10da000, 0x10000, true}; |
| 101 | + BC->SegmentMapInfo[0x31d0000] = |
| 102 | + SegmentInfo{0x31d0000, 0x51ac82c, 0x31d0000, 0x3000000, 0x200000, true}; |
| 103 | + |
| 104 | + DataAggregator DA(""); |
| 105 | + BC->setFilename(Filename); |
| 106 | + Error Err = DA.preprocessProfile(*BC); |
| 107 | + |
| 108 | + // Ignore errors from perf2bolt when parsing memory events later on. |
| 109 | + ASSERT_THAT_ERROR(std::move(Err), Succeeded()); |
| 110 | + |
| 111 | + auto &BinaryMMapInfo = DA.getBinaryMMapInfo(); |
| 112 | + auto El = BinaryMMapInfo.find(Pid); |
| 113 | + // Check that memory mapping is present and has the expected size. |
| 114 | + ASSERT_NE(El, BinaryMMapInfo.end()); |
| 115 | + ASSERT_EQ(El->second.Size, static_cast<uint64_t>(0xb1d0000)); |
| 116 | +} |
| 117 | + |
| 118 | +/// Check that DataAggregator aborts when pre-processing an input binary |
| 119 | +/// with multiple text segments that have different base addresses. |
| 120 | +TEST_P(MemoryMapsTester, MultipleSegmentsMismatchedBaseAddress) { |
| 121 | + const int Pid = 1234; |
| 122 | + StringRef Filename = "BINARY"; |
| 123 | + opts::ReadPerfEvents = formatv( |
| 124 | + "name 0 [000] 0.000000: PERF_RECORD_MMAP2 {0}/{0}: " |
| 125 | + "[0xabc0000000(0x1000000) @ 0x11c0000 103:01 1573523 0]: r-xp {1}\n" |
| 126 | + "name 0 [000] 0.000000: PERF_RECORD_MMAP2 {0}/{0}: " |
| 127 | + "[0xabc2000000(0x8000000) @ 0x31d0000 103:01 1573523 0]: r-xp {1}\n", |
| 128 | + Pid, Filename); |
| 129 | + |
| 130 | + BC->SegmentMapInfo[0x11da000] = |
| 131 | + SegmentInfo{0x11da000, 0x10da000, 0x11ca000, 0x10da000, 0x10000, true}; |
| 132 | + // Using '0x31d0fff' FileOffset which triggers a different base address |
| 133 | + // for this second text segment. |
| 134 | + BC->SegmentMapInfo[0x31d0000] = |
| 135 | + SegmentInfo{0x31d0000, 0x51ac82c, 0x31d0fff, 0x3000000, 0x200000, true}; |
| 136 | + |
| 137 | + DataAggregator DA(""); |
| 138 | + BC->setFilename(Filename); |
| 139 | + ASSERT_DEBUG_DEATH( |
| 140 | + { Error Err = DA.preprocessProfile(*BC); }, |
| 141 | + "Base address on multiple segment mappings should match"); |
| 142 | +} |
0 commit comments