| //===- unittest/ProfileData/SampleProfTest.cpp ------------------*- C++ -*-===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #include "llvm/ProfileData/SampleProf.h" | |
| #include "llvm/ADT/ArrayRef.h" | |
| #include "llvm/ADT/StringMap.h" | |
| #include "llvm/ADT/StringRef.h" | |
| #include "llvm/Config/llvm-config.h" | |
| #include "llvm/IR/DebugInfoMetadata.h" | |
| #include "llvm/IR/LLVMContext.h" | |
| #include "llvm/IR/Metadata.h" | |
| #include "llvm/IR/Module.h" | |
| #include "llvm/ProfileData/SampleProfReader.h" | |
| #include "llvm/ProfileData/SampleProfWriter.h" | |
| #include "llvm/Support/Casting.h" | |
| #include "llvm/Support/Error.h" | |
| #include "llvm/Support/ErrorOr.h" | |
| #include "llvm/Support/FileSystem.h" | |
| #include "llvm/Support/LEB128.h" | |
| #include "llvm/Support/Memory.h" | |
| #include "llvm/Support/MemoryBuffer.h" | |
| #include "llvm/Support/Process.h" | |
| #include "llvm/Support/VirtualFileSystem.h" | |
| #include "llvm/Support/raw_ostream.h" | |
| #include "llvm/Testing/Support/Error.h" | |
| #include "llvm/Testing/Support/SupportHelpers.h" | |
| #include "gtest/gtest.h" | |
| #include <algorithm> | |
| #include <limits> | |
| #include <memory> | |
| #include <optional> | |
| #include <string> | |
| #include <system_error> | |
| #include <vector> | |
| #if defined(_WIN32) | |
| #include "llvm/Support/Windows/WindowsSupport.h" | |
| #include "llvm/Support/WindowsError.h" | |
| #elif defined(LLVM_ON_UNIX) && !defined(__MVS__) | |
| #include <sys/mman.h> | |
| #endif | |
| using namespace llvm; | |
| using namespace sampleprof; | |
| using llvm::unittest::TempFile; | |
| static ::testing::AssertionResult NoError(std::error_code EC) { | |
| if (!EC) | |
| return ::testing::AssertionSuccess(); | |
| return ::testing::AssertionFailure() << "error " << EC.value() << ": " | |
| << EC.message(); | |
| } | |
| namespace { | |
| /// Expose string decoding for focused buffer-boundary tests. | |
| class StringDataTestReader final : public SampleProfileReaderRawBinary { | |
| public: | |
| /// Construct a reader over the supplied test buffer. | |
| StringDataTestReader(std::unique_ptr<MemoryBuffer> Buffer, | |
| LLVMContext &Context) | |
| : SampleProfileReaderRawBinary(std::move(Buffer), Context) {} | |
| /// Decode one string using the production binary-reader path. | |
| ErrorOr<StringRef> readCString() { | |
| Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); | |
| End = reinterpret_cast<const uint8_t *>(Buffer->getBufferEnd()); | |
| return readString(); | |
| } | |
| /// Decode one string with the cursor at the logical buffer end. | |
| ErrorOr<StringRef> readCStringAtEnd() { | |
| Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferEnd()); | |
| End = Data; | |
| return readString(); | |
| } | |
| /// Return whether the decoder consumed the complete test buffer. | |
| bool atEnd() const { return Data == End; } | |
| }; | |
| /// Expose ExtBinary header reading for fixed-width number boundary tests. | |
| class ExtBinaryDataTestReader final : public SampleProfileReaderExtBinary { | |
| public: | |
| /// Construct a reader over the supplied test buffer. | |
| ExtBinaryDataTestReader(std::unique_ptr<MemoryBuffer> Buffer, | |
| LLVMContext &Context) | |
| : SampleProfileReaderExtBinary(std::move(Buffer), Context) {} | |
| /// Read the ExtBinary header using the production reader path. | |
| std::error_code readProfileHeader() { return readHeader(); } | |
| }; | |
| /// Create a non-owning MemoryBuffer view over \p Bytes. | |
| static std::unique_ptr<MemoryBuffer> | |
| createTestMemoryBuffer(ArrayRef<uint8_t> Bytes) { | |
| return MemoryBuffer::getMemBuffer( | |
| StringRef(reinterpret_cast<const char *>(Bytes.data()), Bytes.size()), | |
| "binary-data", /*RequiresNullTerminator=*/false); | |
| } | |
| /// Own a buffer ending immediately before an inaccessible guard page. | |
| struct GuardedMemoryBuffer { | |
| sys::OwningMemoryBlock Storage; | |
| std::unique_ptr<MemoryBuffer> Buffer; | |
| }; | |
| /// Return whether this host exposes an API for no-access memory pages. | |
| static constexpr bool guardPagesSupported() { | |
| #if defined(_WIN32) || (defined(LLVM_ON_UNIX) && !defined(__MVS__)) | |
| return true; | |
| #else | |
| return false; | |
| #endif | |
| } | |
| /// Make \p GuardPage inaccessible so an out-of-bounds read faults immediately. | |
| static std::error_code protectGuardPage(sys::MemoryBlock GuardPage) { | |
| #if defined(_WIN32) | |
| DWORD OldProtection; | |
| if (!VirtualProtect(GuardPage.base(), GuardPage.allocatedSize(), | |
| PAGE_NOACCESS, &OldProtection)) | |
| return mapLastWindowsError(); | |
| return std::error_code(); | |
| #elif defined(LLVM_ON_UNIX) && !defined(__MVS__) | |
| if (::mprotect(GuardPage.base(), GuardPage.allocatedSize(), PROT_NONE)) | |
| return errnoAsErrorCode(); | |
| return std::error_code(); | |
| #else | |
| return std::make_error_code(std::errc::not_supported); | |
| #endif | |
| } | |
| /// Copy \p Bytes to the end of a readable page followed by a no-access page. | |
| static Expected<GuardedMemoryBuffer> | |
| createGuardedMemoryBuffer(ArrayRef<uint8_t> Bytes) { | |
| auto PageSizeOrErr = sys::Process::getPageSize(); | |
| if (!PageSizeOrErr) | |
| return PageSizeOrErr.takeError(); | |
| const size_t PageSize = *PageSizeOrErr; | |
| if (Bytes.empty() || Bytes.size() > PageSize) | |
| return createStringError(std::errc::invalid_argument, | |
| "test buffer must fit in one memory page"); | |
| std::error_code EC; | |
| sys::MemoryBlock Allocation = sys::Memory::allocateMappedMemory( | |
| 2 * PageSize, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC); | |
| if (EC) | |
| return errorCodeToError(EC); | |
| sys::OwningMemoryBlock Storage(Allocation); | |
| auto *GuardStart = static_cast<uint8_t *>(Allocation.base()) + PageSize; | |
| sys::MemoryBlock GuardPage(GuardStart, PageSize); | |
| EC = protectGuardPage(GuardPage); | |
| if (EC) | |
| return errorCodeToError(EC); | |
| auto *BufferStart = GuardStart - Bytes.size(); | |
| std::copy(Bytes.begin(), Bytes.end(), BufferStart); | |
| auto Buffer = | |
| createTestMemoryBuffer(ArrayRef<uint8_t>(BufferStart, Bytes.size())); | |
| return GuardedMemoryBuffer{std::move(Storage), std::move(Buffer)}; | |
| } | |
| struct SampleProfTest : ::testing::Test { | |
| LLVMContext Context; | |
| std::unique_ptr<SampleProfileWriter> Writer; | |
| std::unique_ptr<SampleProfileReader> Reader; | |
| SampleProfTest() : Writer(), Reader() {} | |
| void createWriter(SampleProfileFormat Format, StringRef Profile) { | |
| std::error_code EC; | |
| std::unique_ptr<raw_ostream> OS( | |
| new raw_fd_ostream(Profile, EC, sys::fs::OF_None)); | |
| auto WriterOrErr = SampleProfileWriter::create(OS, Format); | |
| ASSERT_TRUE(NoError(WriterOrErr.getError())); | |
| Writer = std::move(WriterOrErr.get()); | |
| } | |
| void readProfile(const Module &M, StringRef Profile, | |
| StringRef RemapFile = "") { | |
| auto FS = vfs::getRealFileSystem(); | |
| auto ReaderOrErr = SampleProfileReader::create( | |
| std::string(Profile), Context, *FS, FSDiscriminatorPass::Base, | |
| std::string(RemapFile)); | |
| ASSERT_TRUE(NoError(ReaderOrErr.getError())); | |
| Reader = std::move(ReaderOrErr.get()); | |
| Reader->setModule(&M); | |
| } | |
| TempFile createRemapFile() { | |
| return TempFile("remapfile", "", R"( | |
| # Types 'int' and 'long' are equivalent | |
| type i l | |
| # Function names 'foo' and 'faux' are equivalent | |
| name 3foo 4faux | |
| )", | |
| /*Unique*/ true); | |
| } | |
| // Verify profile summary is consistent in the roundtrip to and from | |
| // Metadata. \p AddPartialField is to choose whether the Metadata | |
| // contains the IsPartialProfile field which is optional. | |
| void verifyProfileSummary(ProfileSummary &Summary, Module &M, | |
| const bool AddPartialField, | |
| const bool AddPartialProfileRatioField) { | |
| LLVMContext &Context = M.getContext(); | |
| const bool IsPartialProfile = Summary.isPartialProfile(); | |
| const double PartialProfileRatio = Summary.getPartialProfileRatio(); | |
| auto VerifySummary = [IsPartialProfile, PartialProfileRatio]( | |
| ProfileSummary &Summary) mutable { | |
| ASSERT_EQ(ProfileSummary::PSK_Sample, Summary.getKind()); | |
| ASSERT_EQ(138211u, Summary.getTotalCount()); | |
| ASSERT_EQ(10u, Summary.getNumCounts()); | |
| ASSERT_EQ(4u, Summary.getNumFunctions()); | |
| ASSERT_EQ(1437u, Summary.getMaxFunctionCount()); | |
| ASSERT_EQ(60351u, Summary.getMaxCount()); | |
| ASSERT_EQ(IsPartialProfile, Summary.isPartialProfile()); | |
| ASSERT_EQ(PartialProfileRatio, Summary.getPartialProfileRatio()); | |
| uint32_t Cutoff = 800000; | |
| auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) { | |
| return PE.Cutoff == Cutoff; | |
| }; | |
| const std::vector<ProfileSummaryEntry> &Details = | |
| Summary.getDetailedSummary(); | |
| auto EightyPerc = find_if(Details, Predicate); | |
| Cutoff = 900000; | |
| auto NinetyPerc = find_if(Details, Predicate); | |
| Cutoff = 950000; | |
| auto NinetyFivePerc = find_if(Details, Predicate); | |
| Cutoff = 990000; | |
| auto NinetyNinePerc = find_if(Details, Predicate); | |
| ASSERT_EQ(60000u, EightyPerc->MinCount); | |
| ASSERT_EQ(12557u, NinetyPerc->MinCount); | |
| ASSERT_EQ(12557u, NinetyFivePerc->MinCount); | |
| ASSERT_EQ(600u, NinetyNinePerc->MinCount); | |
| }; | |
| VerifySummary(Summary); | |
| // Test that conversion of summary to and from Metadata works. | |
| Metadata *MD = | |
| Summary.getMD(Context, AddPartialField, AddPartialProfileRatioField); | |
| ASSERT_TRUE(MD); | |
| ProfileSummary *PS = ProfileSummary::getFromMD(MD); | |
| ASSERT_TRUE(PS); | |
| VerifySummary(*PS); | |
| delete PS; | |
| // Test that summary can be attached to and read back from module. | |
| M.eraseNamedMetadata(M.getOrInsertModuleFlagsMetadata()); | |
| M.setProfileSummary(MD, ProfileSummary::PSK_Sample); | |
| MD = M.getProfileSummary(/* IsCS */ false); | |
| ASSERT_TRUE(MD); | |
| PS = ProfileSummary::getFromMD(MD); | |
| ASSERT_TRUE(PS); | |
| VerifySummary(*PS); | |
| delete PS; | |
| } | |
| // Write the supplied profile map, optionally requesting a specific format | |
| // version, to an in-memory buffer. | |
| ErrorOr<SmallVector<char, 128>> | |
| writeProfileToBuffer(const SampleProfileMap &Profiles, | |
| std::optional<uint64_t> Version = std::nullopt, | |
| bool UseComposite = false) { | |
| SmallVector<char, 128> Buffer; | |
| std::unique_ptr<raw_ostream> OS = | |
| std::make_unique<raw_svector_ostream>(Buffer); | |
| auto WriterOrErr = | |
| SampleProfileWriter::create(OS, SampleProfileFormat::SPF_Ext_Binary); | |
| if (std::error_code EC = WriterOrErr.getError()) | |
| return EC; | |
| auto Writer = std::move(WriterOrErr.get()); | |
| if (Version) | |
| Writer->setFormatVersion(*Version); | |
| else if (UseComposite) | |
| Writer->setFormatVersion(CompositeProfileVersion); | |
| Writer->setUseCompositeProfile(UseComposite); | |
| if (std::error_code EC = Writer->write(Profiles)) | |
| return EC; | |
| Writer->getOutputStream().flush(); | |
| Writer.reset(); | |
| return Buffer; | |
| } | |
| // Write a minimal profile, optionally requesting a specific format version, | |
| // to an in-memory buffer. | |
| ErrorOr<SmallVector<char, 128>> | |
| writeProfileToBuffer(std::optional<uint64_t> Version = std::nullopt, | |
| bool UseComposite = false) { | |
| StringRef FooName("_Z3fooi"); | |
| FunctionSamples FooSamples; | |
| FooSamples.setFunction(FunctionId(FooName)); | |
| FooSamples.addTotalSamples(1); | |
| SampleProfileMap Profiles; | |
| Profiles[FooName] = std::move(FooSamples); | |
| return writeProfileToBuffer(Profiles, Version, UseComposite); | |
| } | |
| // Write a raw profile header (Magic + Version) directly to a buffer. | |
| // This bypasses the writer validation and is used to test reader error | |
| // handling. | |
| SmallVector<char, 128> writeRawHeaderToBuffer(uint64_t Version) { | |
| SmallVector<char, 128> Buffer; | |
| raw_svector_ostream OS(Buffer); | |
| encodeULEB128(SPMagic(SPF_Ext_Binary), OS); | |
| encodeULEB128(Version, OS); | |
| return Buffer; | |
| } | |
| // Read a binary header through the production reader path. | |
| std::error_code readBinaryHeaderFromBuffer(ArrayRef<uint8_t> Encoded) { | |
| auto Buffer = createTestMemoryBuffer(Encoded); | |
| SampleProfileReaderRawBinary TestReader(std::move(Buffer), Context); | |
| return TestReader.readHeader(); | |
| } | |
| // Decode one string through the production reader path. | |
| std::error_code readStringErrorFromBuffer(ArrayRef<uint8_t> Encoded) { | |
| auto Buffer = createTestMemoryBuffer(Encoded); | |
| StringDataTestReader TestReader(std::move(Buffer), Context); | |
| return TestReader.readCString().getError(); | |
| } | |
| // Decode one string with the cursor at the supplied buffer's end. | |
| std::error_code readStringAtEndError(ArrayRef<uint8_t> Encoded) { | |
| auto Buffer = createTestMemoryBuffer(Encoded); | |
| StringDataTestReader TestReader(std::move(Buffer), Context); | |
| return TestReader.readCStringAtEnd().getError(); | |
| } | |
| // Build an ExtBinary header whose section-count field is truncated mid-value. | |
| SmallVector<uint8_t, 32> writeTruncatedExtBinarySecCount() { | |
| SmallVector<char, 32> Header; | |
| raw_svector_ostream OS(Header); | |
| encodeULEB128(SPMagic(SPF_Ext_Binary), OS); | |
| encodeULEB128(103, OS); | |
| SmallVector<uint8_t, 32> Encoded(Header.begin(), Header.end()); | |
| // Four bytes are not enough for the fixed-width section-count uint64_t. | |
| Encoded.append({1, 2, 3, 4}); | |
| return Encoded; | |
| } | |
| // Read an ExtBinary header through the production reader path. | |
| std::error_code readExtBinaryHeaderFromBuffer(ArrayRef<uint8_t> Encoded) { | |
| auto Buffer = createTestMemoryBuffer(Encoded); | |
| ExtBinaryDataTestReader TestReader(std::move(Buffer), Context); | |
| return TestReader.readProfileHeader(); | |
| } | |
| // Read the profile from an in-memory buffer, verify its payload, and | |
| // return the format version. | |
| ErrorOr<uint64_t> readVersionFromBuffer(ArrayRef<char> Buffer) { | |
| std::unique_ptr<MemoryBuffer> MemBuffer = MemoryBuffer::getMemBuffer( | |
| StringRef(Buffer.data(), Buffer.size()), "profile", | |
| /*RequiresNullTerminator*/ false); | |
| auto FS = vfs::getRealFileSystem(); | |
| auto ReaderOrErr = SampleProfileReader::create(MemBuffer, Context, *FS); | |
| if (std::error_code EC = ReaderOrErr.getError()) | |
| return EC; | |
| auto Reader = std::move(ReaderOrErr.get()); | |
| if (std::error_code EC = Reader->read()) | |
| return EC; | |
| if (Reader->getProfiles().size() != 1) | |
| return sampleprof_error::malformed; | |
| FunctionSamples *Samples = Reader->getSamplesFor("_Z3fooi"); | |
| if (!Samples || Samples->getTotalSamples() != 1) | |
| return sampleprof_error::malformed; | |
| return Reader->getFormatVersion(); | |
| } | |
| void testRoundTrip(SampleProfileFormat Format, bool Remap, bool UseMD5, | |
| bool UseMD5ProfSymList = false, | |
| bool UseMD5IndexedTables = false, | |
| bool UseComposite = false) { | |
| TempFile ProfileFile("profile", "", "", /*Unique*/ true); | |
| createWriter(Format, ProfileFile.path()); | |
| if (Format == SampleProfileFormat::SPF_Ext_Binary) { | |
| if (UseMD5) | |
| Writer->setUseMD5(); | |
| if (UseMD5ProfSymList) | |
| Writer->setUseMD5ProfileSymbolList(); | |
| if (UseMD5IndexedTables) | |
| Writer->setUseMD5IndexedTables(); | |
| if (UseComposite) | |
| Writer->setFormatVersion(CompositeProfileVersion); | |
| Writer->setUseCompositeProfile(UseComposite); | |
| } | |
| StringRef FooName("_Z3fooi"); | |
| FunctionSamples FooSamples; | |
| FooSamples.setFunction(FunctionId(FooName)); | |
| FooSamples.addTotalSamples(7711); | |
| FooSamples.addHeadSamples(610); | |
| FooSamples.addBodySamples(1, 0, 610); | |
| FooSamples.addBodySamples(2, 0, 600); | |
| FooSamples.addBodySamples(4, 0, 60000); | |
| FooSamples.addBodySamples(8, 0, 60351); | |
| FooSamples.addBodySamples(10, 0, 605); | |
| // Add inline instance with name "_Z3gooi". | |
| FunctionId GooName(StringRef("_Z3gooi")); | |
| auto &GooSamples = | |
| FooSamples.functionSamplesAt(LineLocation(7, 0))[GooName]; | |
| GooSamples.setFunction(GooName); | |
| GooSamples.addTotalSamples(502); | |
| GooSamples.addBodySamples(3, 0, 502); | |
| // Add inline instance with name "_Z3hooi". | |
| FunctionId HooName(StringRef("_Z3hooi")); | |
| auto &HooSamples = | |
| GooSamples.functionSamplesAt(LineLocation(9, 0))[HooName]; | |
| HooSamples.setFunction(HooName); | |
| HooSamples.addTotalSamples(317); | |
| HooSamples.addBodySamples(4, 0, 317); | |
| StringRef BarName("_Z3bari"); | |
| FunctionSamples BarSamples; | |
| BarSamples.setFunction(FunctionId(BarName)); | |
| BarSamples.addTotalSamples(20301); | |
| BarSamples.addHeadSamples(1437); | |
| BarSamples.addBodySamples(1, 0, 1437); | |
| // Test how reader/writer handles unmangled names. | |
| StringRef MconstructName("_M_construct<char *>"); | |
| StringRef StringviewName("string_view<std::allocator<char> >"); | |
| BarSamples.addCalledTargetSamples(1, 0, FunctionId(MconstructName), 1000); | |
| BarSamples.addCalledTargetSamples(1, 0, FunctionId(StringviewName), 437); | |
| StringRef BazName("_Z3bazi"); | |
| FunctionSamples BazSamples; | |
| BazSamples.setFunction(FunctionId(BazName)); | |
| BazSamples.addTotalSamples(12557); | |
| BazSamples.addHeadSamples(1257); | |
| BazSamples.addBodySamples(1, 0, 12557); | |
| StringRef BooName("_Z3booi"); | |
| FunctionSamples BooSamples; | |
| BooSamples.setFunction(FunctionId(BooName)); | |
| BooSamples.addTotalSamples(1232); | |
| BooSamples.addHeadSamples(1); | |
| BooSamples.addBodySamples(1, 0, 1232); | |
| SampleProfileMap Profiles; | |
| Profiles[FooName] = std::move(FooSamples); | |
| Profiles[BarName] = std::move(BarSamples); | |
| Profiles[BazName] = std::move(BazSamples); | |
| Profiles[BooName] = std::move(BooSamples); | |
| Module M("my_module", Context); | |
| FunctionType *fn_type = | |
| FunctionType::get(Type::getVoidTy(Context), {}, false); | |
| TempFile RemapFile(createRemapFile()); | |
| if (Remap) { | |
| FooName = "_Z4fauxi"; | |
| BarName = "_Z3barl"; | |
| GooName = FunctionId(StringRef("_Z3gool")); | |
| HooName = FunctionId(StringRef("_Z3hool")); | |
| } | |
| M.getOrInsertFunction(FooName, fn_type); | |
| M.getOrInsertFunction(BarName, fn_type); | |
| M.getOrInsertFunction(BooName, fn_type); | |
| ProfileSymbolList List; | |
| if (Format == SampleProfileFormat::SPF_Ext_Binary) { | |
| List.add("zoo", true); | |
| List.add("moo", true); | |
| } | |
| Writer->setProfileSymbolList(&List); | |
| std::error_code EC; | |
| EC = Writer->write(Profiles); | |
| ASSERT_TRUE(NoError(EC)); | |
| Writer->getOutputStream().flush(); | |
| readProfile(M, ProfileFile.path(), RemapFile.path()); | |
| EC = Reader->read(); | |
| ASSERT_TRUE(NoError(EC)); | |
| if (Format == SampleProfileFormat::SPF_Ext_Binary) { | |
| EXPECT_EQ(UseComposite, Reader->hasCompositeProfileSection()); | |
| std::unique_ptr<ProfileSymbolList> ReaderList = | |
| Reader->getProfileSymbolList(); | |
| ReaderList->contains("zoo"); | |
| ReaderList->contains("moo"); | |
| } | |
| FunctionSamples *ReadFooSamples = Reader->getSamplesFor(FooName); | |
| ASSERT_TRUE(ReadFooSamples != nullptr); | |
| if (!UseMD5) { | |
| ASSERT_EQ("_Z3fooi", ReadFooSamples->getFunction().str()); | |
| } | |
| ASSERT_EQ(7711u, ReadFooSamples->getTotalSamples()); | |
| ASSERT_EQ(610u, ReadFooSamples->getHeadSamples()); | |
| // Try to find a FunctionSamples with GooName at given callsites containing | |
| // inline instance for GooName. Test the correct FunctionSamples can be | |
| // found with Remapper support. | |
| const FunctionSamples *ReadGooSamples = | |
| ReadFooSamples->findFunctionSamplesAt(LineLocation(7, 0), | |
| GooName.stringRef(), | |
| Reader->getRemapper()); | |
| ASSERT_TRUE(ReadGooSamples != nullptr); | |
| ASSERT_EQ(502u, ReadGooSamples->getTotalSamples()); | |
| // Try to find a FunctionSamples with GooName at given callsites containing | |
| // no inline instance for GooName. Test no FunctionSamples will be | |
| // found with Remapper support. | |
| const FunctionSamples *ReadGooSamplesAgain = | |
| ReadFooSamples->findFunctionSamplesAt(LineLocation(9, 0), | |
| GooName.stringRef(), | |
| Reader->getRemapper()); | |
| ASSERT_TRUE(ReadGooSamplesAgain == nullptr); | |
| // The inline instance of Hoo is inside of the inline instance of Goo. | |
| // Try to find a FunctionSamples with HooName at given callsites containing | |
| // inline instance for HooName. Test the correct FunctionSamples can be | |
| // found with Remapper support. | |
| const FunctionSamples *ReadHooSamples = | |
| ReadGooSamples->findFunctionSamplesAt(LineLocation(9, 0), | |
| HooName.stringRef(), | |
| Reader->getRemapper()); | |
| ASSERT_TRUE(ReadHooSamples != nullptr); | |
| ASSERT_EQ(317u, ReadHooSamples->getTotalSamples()); | |
| FunctionSamples *ReadBarSamples = Reader->getSamplesFor(BarName); | |
| ASSERT_TRUE(ReadBarSamples != nullptr); | |
| if (!UseMD5) { | |
| ASSERT_EQ("_Z3bari", ReadBarSamples->getFunction().str()); | |
| } | |
| ASSERT_EQ(20301u, ReadBarSamples->getTotalSamples()); | |
| ASSERT_EQ(1437u, ReadBarSamples->getHeadSamples()); | |
| ErrorOr<SampleRecord::CallTargetMap> CTMap = | |
| ReadBarSamples->findCallTargetMapAt(1, 0); | |
| ASSERT_FALSE(CTMap.getError()); | |
| // Because _Z3bazi is not defined in module M, expect _Z3bazi's profile | |
| // is not loaded when the profile is ExtBinary format because this format | |
| // supports loading function profiles on demand. | |
| FunctionSamples *ReadBazSamples = Reader->getSamplesFor(BazName); | |
| if (Format == SampleProfileFormat::SPF_Ext_Binary) { | |
| ASSERT_TRUE(ReadBazSamples == nullptr); | |
| ASSERT_EQ(3u, Reader->getProfiles().size()); | |
| } else { | |
| ASSERT_TRUE(ReadBazSamples != nullptr); | |
| ASSERT_EQ(12557u, ReadBazSamples->getTotalSamples()); | |
| ASSERT_EQ(4u, Reader->getProfiles().size()); | |
| } | |
| FunctionSamples *ReadBooSamples = Reader->getSamplesFor(BooName); | |
| ASSERT_TRUE(ReadBooSamples != nullptr); | |
| ASSERT_EQ(1232u, ReadBooSamples->getTotalSamples()); | |
| FunctionId MconstructRep = getRepInFormat(MconstructName); | |
| FunctionId StringviewRep = getRepInFormat(StringviewName); | |
| ASSERT_EQ(1000u, CTMap.get()[MconstructRep]); | |
| ASSERT_EQ(437u, CTMap.get()[StringviewRep]); | |
| ProfileSummary &Summary = Reader->getSummary(); | |
| Summary.setPartialProfile(true); | |
| verifyProfileSummary(Summary, M, true, false); | |
| Summary.setPartialProfile(false); | |
| verifyProfileSummary(Summary, M, true, false); | |
| verifyProfileSummary(Summary, M, false, false); | |
| Summary.setPartialProfile(true); | |
| Summary.setPartialProfileRatio(0.5); | |
| verifyProfileSummary(Summary, M, true, true); | |
| } | |
| void addFunctionSamples(SampleProfileMap *Smap, const char *Fname, | |
| uint64_t TotalSamples, uint64_t HeadSamples) { | |
| StringRef Name(Fname); | |
| FunctionSamples FcnSamples; | |
| FcnSamples.setFunction(FunctionId(Name)); | |
| FcnSamples.addTotalSamples(TotalSamples); | |
| FcnSamples.addHeadSamples(HeadSamples); | |
| FcnSamples.addBodySamples(1, 0, HeadSamples); | |
| (*Smap)[Name] = FcnSamples; | |
| } | |
| SampleProfileMap setupFcnSamplesForElisionTest(StringRef Policy) { | |
| SampleProfileMap Smap; | |
| addFunctionSamples(&Smap, "foo", uint64_t(20301), uint64_t(1437)); | |
| if (Policy == "" || Policy == "all") | |
| return Smap; | |
| addFunctionSamples(&Smap, "foo.bar", uint64_t(20303), uint64_t(1439)); | |
| if (Policy == "selected") | |
| return Smap; | |
| addFunctionSamples(&Smap, "foo.llvm.2465", uint64_t(20305), uint64_t(1441)); | |
| return Smap; | |
| } | |
| void createFunctionWithSampleProfileElisionPolicy(Module *M, | |
| const char *Fname, | |
| StringRef Policy) { | |
| FunctionType *FnType = | |
| FunctionType::get(Type::getVoidTy(Context), {}, false); | |
| auto Inserted = M->getOrInsertFunction(Fname, FnType); | |
| auto Fcn = cast<Function>(Inserted.getCallee()); | |
| if (Policy != "") | |
| Fcn->addFnAttr("sample-profile-suffix-elision-policy", Policy); | |
| } | |
| void setupModuleForElisionTest(Module *M, StringRef Policy) { | |
| createFunctionWithSampleProfileElisionPolicy(M, "foo", Policy); | |
| createFunctionWithSampleProfileElisionPolicy(M, "foo.bar", Policy); | |
| createFunctionWithSampleProfileElisionPolicy(M, "foo.llvm.2465", Policy); | |
| } | |
| void testSuffixElisionPolicy(SampleProfileFormat Format, StringRef Policy, | |
| const StringMap<uint64_t> &Expected) { | |
| TempFile ProfileFile("profile", "", "", /*Unique*/ true); | |
| Module M("my_module", Context); | |
| setupModuleForElisionTest(&M, Policy); | |
| SampleProfileMap ProfMap = setupFcnSamplesForElisionTest(Policy); | |
| // write profile | |
| createWriter(Format, ProfileFile.path()); | |
| std::error_code EC; | |
| EC = Writer->write(ProfMap); | |
| ASSERT_TRUE(NoError(EC)); | |
| Writer->getOutputStream().flush(); | |
| // read profile | |
| readProfile(M, ProfileFile.path()); | |
| EC = Reader->read(); | |
| ASSERT_TRUE(NoError(EC)); | |
| for (auto I = Expected.begin(); I != Expected.end(); ++I) { | |
| uint64_t Esamples = uint64_t(-1); | |
| FunctionSamples *Samples = Reader->getSamplesFor(I->getKey()); | |
| if (Samples != nullptr) | |
| Esamples = Samples->getTotalSamples(); | |
| ASSERT_EQ(I->getValue(), Esamples); | |
| if (Format == SampleProfileFormat::SPF_Ext_Binary) { | |
| ASSERT_TRUE(Reader->contains(I->getKey())); | |
| ASSERT_TRUE(Reader->contains(FunctionId(I->getKey()).getHashCode())); | |
| } | |
| } | |
| if (Format == SampleProfileFormat::SPF_Ext_Binary) { | |
| StringRef FakeSymbol = "non_existent_symbol_for_test"; | |
| ASSERT_FALSE(Reader->contains(FakeSymbol)); | |
| ASSERT_FALSE(Reader->contains(FunctionId(FakeSymbol).getHashCode())); | |
| } | |
| } | |
| }; | |
| /// Run buffer-boundary tests only where the host supports inaccessible pages. | |
| struct GuardedSampleProfTest : SampleProfTest { | |
| /// Skip guard-page tests on hosts without the required memory protection. | |
| void SetUp() override { | |
| SampleProfTest::SetUp(); | |
| if (!guardPagesSupported()) | |
| GTEST_SKIP() << "guard pages are not supported on this host"; | |
| } | |
| /// Read a binary header ending at a guard page. | |
| Expected<std::error_code> | |
| readBinaryHeaderFromGuardedBuffer(ArrayRef<uint8_t> Encoded) { | |
| auto GuardedOrErr = createGuardedMemoryBuffer(Encoded); | |
| if (!GuardedOrErr) | |
| return GuardedOrErr.takeError(); | |
| GuardedMemoryBuffer Guarded = std::move(*GuardedOrErr); | |
| SampleProfileReaderRawBinary TestReader(std::move(Guarded.Buffer), Context); | |
| return TestReader.readHeader(); | |
| } | |
| /// Decode one string ending at a guard page. | |
| Expected<std::error_code> | |
| readStringErrorFromGuardedBuffer(ArrayRef<uint8_t> Encoded) { | |
| auto GuardedOrErr = createGuardedMemoryBuffer(Encoded); | |
| if (!GuardedOrErr) | |
| return GuardedOrErr.takeError(); | |
| GuardedMemoryBuffer Guarded = std::move(*GuardedOrErr); | |
| StringDataTestReader TestReader(std::move(Guarded.Buffer), Context); | |
| return TestReader.readCString().getError(); | |
| } | |
| /// Read an ExtBinary header ending at a guard page. | |
| Expected<std::error_code> | |
| readExtBinaryHeaderFromGuardedBuffer(ArrayRef<uint8_t> Encoded) { | |
| auto GuardedOrErr = createGuardedMemoryBuffer(Encoded); | |
| if (!GuardedOrErr) | |
| return GuardedOrErr.takeError(); | |
| GuardedMemoryBuffer Guarded = std::move(*GuardedOrErr); | |
| ExtBinaryDataTestReader TestReader(std::move(Guarded.Buffer), Context); | |
| return TestReader.readProfileHeader(); | |
| } | |
| }; | |
| TEST_F(SampleProfTest, roundtrip_text_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Text, false, false); | |
| } | |
| TEST_F(SampleProfTest, roundtrip_raw_binary_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Binary, false, false); | |
| } | |
| TEST_F(SampleProfTest, roundtrip_ext_binary_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, false, false); | |
| } | |
| // Verify the full ExtBinary round trip through composite profile sections. | |
| TEST_F(SampleProfTest, roundtrip_composite_ext_binary_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, false, false, | |
| /*UseMD5ProfSymList=*/false, | |
| /*UseMD5IndexedTables=*/false, /*UseComposite=*/true); | |
| } | |
| // Verify that the writer and reader handle a multi-byte ULEB128 payload size. | |
| TEST_F(SampleProfTest, roundtrip_large_composite_ext_binary_profile) { | |
| constexpr uint32_t BodyRecordCount = 128; | |
| StringRef FunctionName("_Z5largev"); | |
| // Build one function whose encoded LBR payload is necessarily larger than the | |
| // single-byte ULEB128 range. | |
| FunctionSamples LargeSamples; | |
| LargeSamples.setFunction(FunctionId(FunctionName)); | |
| ASSERT_EQ(sampleprof_error::success, | |
| LargeSamples.addTotalSamples(BodyRecordCount)); | |
| ASSERT_EQ(sampleprof_error::success, LargeSamples.addHeadSamples(1)); | |
| for (uint32_t LineOffset = 1; LineOffset <= BodyRecordCount; ++LineOffset) | |
| ASSERT_EQ(sampleprof_error::success, | |
| LargeSamples.addBodySamples(LineOffset, 0, 1)); | |
| SampleProfileMap Profiles; | |
| Profiles[FunctionName] = std::move(LargeSamples); | |
| auto BufferOrErr = | |
| writeProfileToBuffer(Profiles, std::nullopt, /*UseComposite=*/true); | |
| ASSERT_TRUE(NoError(BufferOrErr.getError())); | |
| // Inspect the decoded block structure to verify that the writer emitted a | |
| // payload size outside the single-byte ULEB128 range. | |
| std::unique_ptr<MemoryBuffer> MemBuffer = MemoryBuffer::getMemBufferCopy( | |
| StringRef(BufferOrErr->data(), BufferOrErr->size()), "profile"); | |
| auto FS = vfs::getRealFileSystem(); | |
| auto ReaderOrErr = SampleProfileReader::create(MemBuffer, Context, *FS); | |
| ASSERT_TRUE(NoError(ReaderOrErr.getError())); | |
| auto ProfileReader = std::move(ReaderOrErr.get()); | |
| std::string ProfileTypeInfo; | |
| raw_string_ostream InfoOS(ProfileTypeInfo); | |
| ASSERT_TRUE(NoError(ProfileReader->dumpProfileTypeInfo(InfoOS))); | |
| StringRef PayloadSizeText = | |
| StringRef(ProfileTypeInfo).split("Payload size: ").second; | |
| ASSERT_FALSE(PayloadSizeText.empty()); | |
| PayloadSizeText = PayloadSizeText.split('\n').first; | |
| uint64_t PayloadSize = 0; | |
| ASSERT_FALSE(PayloadSizeText.getAsInteger(10, PayloadSize)); | |
| EXPECT_GE(PayloadSize, 128u); | |
| // Confirm that the same read preserved every body record, including both | |
| // ends of the generated range. | |
| FunctionSamples *ReadSamples = ProfileReader->getSamplesFor(FunctionName); | |
| ASSERT_NE(ReadSamples, nullptr); | |
| EXPECT_EQ(ReadSamples->getBodySamples().size(), BodyRecordCount); | |
| auto FirstSample = ReadSamples->findSamplesAt(1, 0); | |
| ASSERT_TRUE(NoError(FirstSample.getError())); | |
| EXPECT_EQ(FirstSample.get(), 1u); | |
| auto LastSample = ReadSamples->findSamplesAt(BodyRecordCount, 0); | |
| ASSERT_TRUE(NoError(LastSample.getError())); | |
| EXPECT_EQ(LastSample.get(), 1u); | |
| } | |
| // Verify that reconfiguring one ExtBinary writer in both directions updates the | |
| // profile and offset section types. Version 105 supports both representations. | |
| TEST_F(SampleProfTest, ext_binary_writer_toggles_composite_layout) { | |
| SmallVector<char, 128> Buffer; | |
| std::unique_ptr<raw_ostream> OS = | |
| std::make_unique<raw_svector_ostream>(Buffer); | |
| auto WriterOrErr = | |
| SampleProfileWriter::create(OS, SampleProfileFormat::SPF_Ext_Binary); | |
| ASSERT_TRUE(NoError(WriterOrErr.getError())); | |
| auto ProfileWriter = std::move(WriterOrErr.get()); | |
| ProfileWriter->setFormatVersion(CompositeProfileVersion); | |
| ProfileWriter->setUseCompositeProfile(true); | |
| StringRef FooName("_Z3fooi"); | |
| FunctionSamples FooSamples; | |
| FooSamples.setFunction(FunctionId(FooName)); | |
| FooSamples.addTotalSamples(1); | |
| SampleProfileMap Profiles; | |
| Profiles[FooName] = std::move(FooSamples); | |
| auto VerifyFormat = [&](bool IsComposite, uint64_t ExpectedVersion) { | |
| std::unique_ptr<MemoryBuffer> MemBuffer = MemoryBuffer::getMemBufferCopy( | |
| StringRef(Buffer.data(), Buffer.size()), "profile"); | |
| auto FS = vfs::getRealFileSystem(); | |
| auto ReaderOrErr = SampleProfileReader::create(MemBuffer, Context, *FS); | |
| ASSERT_TRUE(NoError(ReaderOrErr.getError())); | |
| auto ProfileReader = std::move(ReaderOrErr.get()); | |
| ASSERT_TRUE(NoError(ProfileReader->read())); | |
| EXPECT_EQ(IsComposite, ProfileReader->hasCompositeProfileSection()); | |
| EXPECT_EQ(ExpectedVersion, ProfileReader->getFormatVersion()); | |
| }; | |
| ASSERT_TRUE(NoError(ProfileWriter->write(Profiles))); | |
| ProfileWriter->getOutputStream().flush(); | |
| VerifyFormat(true, CompositeProfileVersion); | |
| Buffer.clear(); | |
| ProfileWriter->setUseCompositeProfile(false); | |
| ASSERT_TRUE(NoError(ProfileWriter->write(Profiles))); | |
| ProfileWriter->getOutputStream().flush(); | |
| VerifyFormat(false, CompositeProfileVersion); | |
| Buffer.clear(); | |
| ProfileWriter->setUseCompositeProfile(true); | |
| ASSERT_TRUE(NoError(ProfileWriter->write(Profiles))); | |
| ProfileWriter->getOutputStream().flush(); | |
| VerifyFormat(true, CompositeProfileVersion); | |
| } | |
| TEST_F(SampleProfTest, roundtrip_md5_ext_binary_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, false, true); | |
| } | |
| TEST_F(SampleProfTest, roundtrip_eytzinger_ext_binary_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, false, false, | |
| /*UseMD5ProfSymList=*/true); | |
| } | |
| TEST_F(SampleProfTest, roundtrip_eytzinger_name_table_ext_binary_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, false, true, | |
| /*UseMD5ProfSymList=*/false, /*UseMD5IndexedTables=*/true); | |
| } | |
| // Verify composite ExtBinary round trips when the name table uses MD5 hashes. | |
| TEST_F(SampleProfTest, roundtrip_composite_md5_ext_binary_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, false, true, | |
| /*UseMD5ProfSymList=*/false, | |
| /*UseMD5IndexedTables=*/false, /*UseComposite=*/true); | |
| } | |
| TEST_F(SampleProfTest, remap_text_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Text, true, false); | |
| } | |
| TEST_F(SampleProfTest, remap_raw_binary_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Binary, true, false); | |
| } | |
| TEST_F(SampleProfTest, remap_ext_binary_profile) { | |
| testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, true, false); | |
| } | |
| TEST_F(SampleProfTest, sample_overflow_saturation) { | |
| const uint64_t Max = std::numeric_limits<uint64_t>::max(); | |
| sampleprof_error Result; | |
| FunctionSamples FooSamples; | |
| Result = FooSamples.addTotalSamples(1); | |
| ASSERT_EQ(Result, sampleprof_error::success); | |
| Result = FooSamples.addHeadSamples(1); | |
| ASSERT_EQ(Result, sampleprof_error::success); | |
| Result = FooSamples.addBodySamples(10, 0, 1); | |
| ASSERT_EQ(Result, sampleprof_error::success); | |
| Result = FooSamples.addTotalSamples(Max); | |
| ASSERT_EQ(Result, sampleprof_error::counter_overflow); | |
| ASSERT_EQ(FooSamples.getTotalSamples(), Max); | |
| Result = FooSamples.addHeadSamples(Max); | |
| ASSERT_EQ(Result, sampleprof_error::counter_overflow); | |
| ASSERT_EQ(FooSamples.getHeadSamples(), Max); | |
| Result = FooSamples.addBodySamples(10, 0, Max); | |
| ASSERT_EQ(Result, sampleprof_error::counter_overflow); | |
| ErrorOr<uint64_t> BodySamples = FooSamples.findSamplesAt(10, 0); | |
| ASSERT_FALSE(BodySamples.getError()); | |
| ASSERT_EQ(BodySamples.get(), Max); | |
| } | |
| TEST_F(SampleProfTest, default_suffix_elision_text) { | |
| // Default suffix elision policy: strip everything after first dot. | |
| // This implies that all suffix variants will map to "foo", so | |
| // we don't expect to see any entries for them in the sample | |
| // profile. | |
| StringMap<uint64_t> Expected; | |
| Expected["foo"] = uint64_t(20301); | |
| Expected["foo.bar"] = uint64_t(-1); | |
| Expected["foo.llvm.2465"] = uint64_t(-1); | |
| testSuffixElisionPolicy(SampleProfileFormat::SPF_Text, "", Expected); | |
| } | |
| TEST_F(SampleProfTest, selected_suffix_elision_text) { | |
| // Profile is created and searched using the "selected" | |
| // suffix elision policy: we only strip a .XXX suffix if | |
| // it matches a pattern known to be generated by the compiler | |
| // (e.g. ".llvm.<digits>"). | |
| StringMap<uint64_t> Expected; | |
| Expected["foo"] = uint64_t(20301); | |
| Expected["foo.bar"] = uint64_t(20303); | |
| Expected["foo.llvm.2465"] = uint64_t(-1); | |
| testSuffixElisionPolicy(SampleProfileFormat::SPF_Text, "selected", Expected); | |
| } | |
| TEST_F(SampleProfTest, none_suffix_elision_text) { | |
| // Profile is created and searched using the "none" | |
| // suffix elision policy: no stripping of suffixes at all. | |
| // Here we expect to see all variants in the profile. | |
| StringMap<uint64_t> Expected; | |
| Expected["foo"] = uint64_t(20301); | |
| Expected["foo.bar"] = uint64_t(20303); | |
| Expected["foo.llvm.2465"] = uint64_t(20305); | |
| testSuffixElisionPolicy(SampleProfileFormat::SPF_Text, "none", Expected); | |
| } | |
| TEST_F(SampleProfTest, SampleProfileFuncOffsetTableInMemory) { | |
| SampleProfileFuncOffsetTable Table(InMemoryMode, 2); | |
| // Test empty table | |
| EXPECT_EQ(Table.lookup(0x1111ULL), std::nullopt); | |
| // Test insert and lookup | |
| Table.insert(0x11112222ULL, 100); | |
| Table.insert(0x33334444ULL, 200); | |
| EXPECT_EQ(Table.lookup(0x11112222ULL), 100); | |
| EXPECT_EQ(Table.lookup(0x33334444ULL), 200); | |
| EXPECT_EQ(Table.lookup(0x55556666ULL), std::nullopt); | |
| } | |
| #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) | |
| // Verify that function-offset flags are rejected for unrelated section types. | |
| TEST(SampleProfSectionFlagTest, RejectsMismatchedSectionSpecificFlag) { | |
| SecHdrTableEntry Entry{SecLBRProfile, 0, 0, 0, 0}; | |
| EXPECT_DEATH( | |
| static_cast<void>(hasSecFlag(Entry, SecFuncOffsetFlags::SecFlagOrdered)), | |
| "Misuse of a flag in an incompatible section"); | |
| } | |
| #endif | |
| // Verify that a non-terminated ULEB is reported as truncated. | |
| TEST_F(SampleProfTest, TruncatedULEBIsTruncated) { | |
| const uint8_t Encoded[] = {0x80}; | |
| EXPECT_EQ(readBinaryHeaderFromBuffer(Encoded), sampleprof_error::truncated); | |
| } | |
| // Verify that a non-terminated ULEB at a guard-page boundary does not read past | |
| // the supplied MemoryBuffer. | |
| TEST_F(GuardedSampleProfTest, TruncatedULEBDoesNotReadPastBufferEnd) { | |
| const uint8_t Encoded[] = {0x80}; | |
| auto ECOrErr = readBinaryHeaderFromGuardedBuffer(Encoded); | |
| ASSERT_THAT_EXPECTED(ECOrErr, Succeeded()); | |
| EXPECT_EQ(*ECOrErr, sampleprof_error::truncated); | |
| } | |
| // Verify that an in-bounds ULEB value exceeding uint64_t is malformed rather | |
| // than silently decoded as zero. | |
| TEST_F(SampleProfTest, OversizedULEBIsMalformed) { | |
| SmallVector<uint8_t, 10> Encoded(10, 0xff); | |
| EXPECT_EQ(readBinaryHeaderFromBuffer(Encoded), sampleprof_error::malformed); | |
| } | |
| // Verify that format detection rejects a truncated magic value. | |
| TEST_F(SampleProfTest, TruncatedMagicIsRejected) { | |
| const uint8_t Encoded[] = {0x80}; | |
| auto Buffer = createTestMemoryBuffer(Encoded); | |
| EXPECT_FALSE(SampleProfileReaderRawBinary::hasFormat(*Buffer)); | |
| EXPECT_FALSE(SampleProfileReaderExtBinary::hasFormat(*Buffer)); | |
| } | |
| // Verify that format detection rejects a truncated magic without reading into | |
| // the guard page beyond the supplied MemoryBuffer. | |
| TEST_F(GuardedSampleProfTest, TruncatedMagicDoesNotReadPastBufferEnd) { | |
| const uint8_t Encoded[] = {0x80}; | |
| auto GuardedOrErr = createGuardedMemoryBuffer(Encoded); | |
| ASSERT_THAT_EXPECTED(GuardedOrErr, Succeeded()); | |
| GuardedMemoryBuffer Guarded = std::move(*GuardedOrErr); | |
| EXPECT_FALSE(SampleProfileReaderRawBinary::hasFormat(*Guarded.Buffer)); | |
| EXPECT_FALSE(SampleProfileReaderExtBinary::hasFormat(*Guarded.Buffer)); | |
| } | |
| // Verify all accepted and rejected forms of the GCC format magic. | |
| TEST_F(SampleProfTest, GCCMagicDetection) { | |
| const uint8_t Truncated[] = {'a'}; | |
| auto ShortBuffer = createTestMemoryBuffer(Truncated); | |
| EXPECT_FALSE(SampleProfileReaderGCC::hasFormat(*ShortBuffer)); | |
| const uint8_t MagicOnly[] = {'a', 'd', 'c', 'g', '*', '7', '0', '4'}; | |
| auto MagicOnlyBuffer = createTestMemoryBuffer(MagicOnly); | |
| EXPECT_TRUE(SampleProfileReaderGCC::hasFormat(*MagicOnlyBuffer)); | |
| const uint8_t Magic[] = {'a', 'd', 'c', 'g', '*', '7', '0', '4', 0}; | |
| auto MagicBuffer = createTestMemoryBuffer(Magic); | |
| EXPECT_TRUE(SampleProfileReaderGCC::hasFormat(*MagicBuffer)); | |
| const uint8_t NonTerminated[] = {'a', 'd', 'c', 'g', '*', '7', '0', '4', 'x'}; | |
| auto NonTerminatedBuffer = createTestMemoryBuffer(NonTerminated); | |
| EXPECT_FALSE(SampleProfileReaderGCC::hasFormat(*NonTerminatedBuffer)); | |
| } | |
| // Verify that GCC magic detection does not read beyond a magic-only buffer. | |
| TEST_F(GuardedSampleProfTest, GCCMagicDoesNotReadPastBufferEnd) { | |
| const uint8_t MagicOnly[] = {'a', 'd', 'c', 'g', '*', '7', '0', '4'}; | |
| auto GuardedOrErr = createGuardedMemoryBuffer(MagicOnly); | |
| ASSERT_THAT_EXPECTED(GuardedOrErr, Succeeded()); | |
| GuardedMemoryBuffer Guarded = std::move(*GuardedOrErr); | |
| EXPECT_TRUE(SampleProfileReaderGCC::hasFormat(*Guarded.Buffer)); | |
| } | |
| // Verify that an unterminated string is reported as truncated. | |
| TEST_F(SampleProfTest, UnterminatedStringIsTruncated) { | |
| const uint8_t Encoded[] = {'n', 'o'}; | |
| EXPECT_EQ(readStringErrorFromBuffer(Encoded), sampleprof_error::truncated); | |
| } | |
| // Verify that an unterminated string does not search into the guard page beyond | |
| // the supplied MemoryBuffer. | |
| TEST_F(GuardedSampleProfTest, UnterminatedStringDoesNotReadPastBufferEnd) { | |
| const uint8_t Encoded[] = {'n', 'o'}; | |
| auto ECOrErr = readStringErrorFromGuardedBuffer(Encoded); | |
| ASSERT_THAT_EXPECTED(ECOrErr, Succeeded()); | |
| EXPECT_EQ(*ECOrErr, sampleprof_error::truncated); | |
| } | |
| // Verify that readString accepts a terminator in the final buffer byte and | |
| // advances the input cursor exactly to End. | |
| TEST_F(SampleProfTest, ValidStringEndsAtBufferEnd) { | |
| const uint8_t Encoded[] = {'o', 'k', 0}; | |
| auto Buffer = createTestMemoryBuffer(Encoded); | |
| StringDataTestReader TestReader(std::move(Buffer), Context); | |
| auto StringOrErr = TestReader.readCString(); | |
| ASSERT_TRUE(NoError(StringOrErr.getError())); | |
| EXPECT_EQ(*StringOrErr, "ok"); | |
| EXPECT_TRUE(TestReader.atEnd()); | |
| } | |
| // Verify that string decoding rejects a cursor already at the buffer end. | |
| TEST_F(SampleProfTest, StringReaderAtEndIsTruncated) { | |
| const uint8_t Data[] = {0}; | |
| EXPECT_EQ(readStringAtEndError(Data), sampleprof_error::truncated); | |
| } | |
| // Verify that a short fixed-width ExtBinary section count is truncated. | |
| TEST_F(SampleProfTest, ShortUnencodedNumberIsTruncated) { | |
| auto Encoded = writeTruncatedExtBinarySecCount(); | |
| EXPECT_EQ(readExtBinaryHeaderFromBuffer(Encoded), | |
| sampleprof_error::truncated); | |
| } | |
| // Verify that a short fixed-width ExtBinary section count does not read into | |
| // the guard page. | |
| TEST_F(GuardedSampleProfTest, ShortUnencodedNumberDoesNotReadPastBufferEnd) { | |
| auto Encoded = writeTruncatedExtBinarySecCount(); | |
| auto ECOrErr = readExtBinaryHeaderFromGuardedBuffer(Encoded); | |
| ASSERT_THAT_EXPECTED(ECOrErr, Succeeded()); | |
| EXPECT_EQ(*ECOrErr, sampleprof_error::truncated); | |
| } | |
| // Verify that requesting format version 103 results in a version 103 profile. | |
| TEST_F(SampleProfTest, SampleProfileFormatVersion103) { | |
| auto BufferOrErr = writeProfileToBuffer(103); | |
| ASSERT_TRUE(NoError(BufferOrErr.getError())); | |
| auto Buffer = std::move(*BufferOrErr); | |
| auto ReadVersionOrErr = readVersionFromBuffer(Buffer); | |
| ASSERT_TRUE(NoError(ReadVersionOrErr.getError())); | |
| EXPECT_EQ(*ReadVersionOrErr, 103u); | |
| } | |
| // Verify that requesting format version 104 results in a version 104 profile. | |
| TEST_F(SampleProfTest, SampleProfileFormatVersion104) { | |
| auto BufferOrErr = writeProfileToBuffer(104); | |
| ASSERT_TRUE(NoError(BufferOrErr.getError())); | |
| auto Buffer = std::move(*BufferOrErr); | |
| auto ReadVersionOrErr = readVersionFromBuffer(Buffer); | |
| ASSERT_TRUE(NoError(ReadVersionOrErr.getError())); | |
| EXPECT_EQ(*ReadVersionOrErr, 104u); | |
| } | |
| // Verify that requesting format version 102 (below minimum supported) is | |
| // rejected by the reader. | |
| TEST_F(SampleProfTest, SampleProfileFormatVersion102) { | |
| auto Buffer = writeRawHeaderToBuffer(102); | |
| auto ReadVersionOrErr = readVersionFromBuffer(Buffer); | |
| EXPECT_EQ(ReadVersionOrErr.getError(), sampleprof_error::unsupported_version); | |
| } | |
| // Verify that requesting format version 105 results in a version 105 legacy | |
| // profile. | |
| TEST_F(SampleProfTest, SampleProfileFormatVersion105) { | |
| auto BufferOrErr = writeProfileToBuffer(105); | |
| ASSERT_TRUE(NoError(BufferOrErr.getError())); | |
| auto Buffer = std::move(*BufferOrErr); | |
| auto ReadVersionOrErr = readVersionFromBuffer(Buffer); | |
| ASSERT_TRUE(NoError(ReadVersionOrErr.getError())); | |
| EXPECT_EQ(*ReadVersionOrErr, CompositeProfileVersion); | |
| } | |
| // Verify that requesting format version 106 (above latest supported) is | |
| // rejected by the reader. | |
| TEST_F(SampleProfTest, SampleProfileFormatVersion106) { | |
| auto Buffer = writeRawHeaderToBuffer(106); | |
| auto ReadVersionOrErr = readVersionFromBuffer(Buffer); | |
| EXPECT_EQ(ReadVersionOrErr.getError(), sampleprof_error::unsupported_version); | |
| } | |
| // Verify composite output with the first compatible ExtBinary version. | |
| TEST_F(SampleProfTest, CompositeProfileSupportsVersion105) { | |
| auto BufferOrErr = writeProfileToBuffer(std::nullopt, /*UseComposite=*/true); | |
| ASSERT_TRUE(NoError(BufferOrErr.getError())); | |
| auto ReadVersionOrErr = readVersionFromBuffer(*BufferOrErr); | |
| ASSERT_TRUE(NoError(ReadVersionOrErr.getError())); | |
| EXPECT_EQ(*ReadVersionOrErr, CompositeProfileVersion); | |
| } | |
| // Verify that the writer rejects a programmatic legacy version instead of | |
| // silently overriding it when composite output is required. | |
| TEST_F(SampleProfTest, CompositeProfileRejectsProgrammaticLegacyVersions) { | |
| for (uint64_t Version : {103u, 104u}) { | |
| auto BufferOrErr = writeProfileToBuffer(Version, /*UseComposite=*/true); | |
| EXPECT_EQ(BufferOrErr.getError(), sampleprof_error::unsupported_version); | |
| } | |
| } | |
| TEST_F(SampleProfTest, ProfileSymbolListMD5) { | |
| std::vector<uint64_t> Keys = {FunctionId("foo").getHashCode(), | |
| FunctionId("bar").getHashCode()}; | |
| auto Table = | |
| llvm::EytzingerTable<support::ulittle64_t>::create(std::move(Keys)); | |
| ProfileSymbolList List; | |
| List.setColdGUIDTable( | |
| EytzingerTableSpan<support::ulittle64_t>(Table.data(), Table.size())); | |
| EXPECT_TRUE(List.contains("foo")); | |
| EXPECT_TRUE(List.contains("bar")); | |
| EXPECT_FALSE(List.contains("baz")); | |
| EXPECT_EQ(2u, List.size()); | |
| } | |
| struct ScopedHasUniqSuffix { | |
| bool OldVal; | |
| ScopedHasUniqSuffix(bool NewVal) : OldVal(FunctionSamples::HasUniqSuffix) { | |
| FunctionSamples::HasUniqSuffix = NewVal; | |
| } | |
| ~ScopedHasUniqSuffix() { FunctionSamples::HasUniqSuffix = OldVal; } | |
| }; | |
| TEST(SampleProfCanonicalNameTest, SelectedPolicyDefault) { | |
| // Plain names without dots. | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo")); | |
| EXPECT_EQ("_Z3barv", FunctionSamples::getCanonicalFnName("_Z3barv")); | |
| EXPECT_EQ("", FunctionSamples::getCanonicalFnName("")); | |
| // Suffix elision policy "selected" is default. | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo", "selected")); | |
| // Names with dot but no matching suffix. | |
| EXPECT_EQ("foo.bar", FunctionSamples::getCanonicalFnName("foo.bar")); | |
| EXPECT_EQ("foo.1", FunctionSamples::getCanonicalFnName("foo.1")); | |
| EXPECT_EQ("foo.part", FunctionSamples::getCanonicalFnName("foo.part")); | |
| EXPECT_EQ("foo.llvm", FunctionSamples::getCanonicalFnName("foo.llvm")); | |
| EXPECT_EQ("foo.__uniq", FunctionSamples::getCanonicalFnName("foo.__uniq")); | |
| EXPECT_EQ(".foo", FunctionSamples::getCanonicalFnName(".foo")); | |
| // .llvm. suffix (ThinLTO promotion / renaming). | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.llvm.1234")); | |
| EXPECT_EQ("_Z3foov", | |
| FunctionSamples::getCanonicalFnName("_Z3foov.llvm.1234567890")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.llvm.")); | |
| EXPECT_EQ("foo.llvm.1234.bar", | |
| FunctionSamples::getCanonicalFnName("foo.llvm.1234.bar")); | |
| EXPECT_EQ("my_llvm_func", | |
| FunctionSamples::getCanonicalFnName("my_llvm_func")); | |
| // .part. suffix. | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.part.1")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.part.42")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.part.")); | |
| EXPECT_EQ("foo.part.1.bar", | |
| FunctionSamples::getCanonicalFnName("foo.part.1.bar")); | |
| // Combined suffixes in canonical order. | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.part.1.llvm.456")); | |
| // Suffixes in non-canonical order: .llvm. cannot be stripped if .part. | |
| // follows it, but .part. can be stripped leaving .llvm. | |
| EXPECT_EQ("foo.llvm.1", | |
| FunctionSamples::getCanonicalFnName("foo.llvm.1.part.2")); | |
| // Repeated suffix: only the last one is stripped. | |
| EXPECT_EQ("foo.llvm.1", | |
| FunctionSamples::getCanonicalFnName("foo.llvm.1.llvm.2")); | |
| } | |
| TEST(SampleProfCanonicalNameTest, SelectedPolicyHasUniqSuffix) { | |
| // When HasUniqSuffix is false, .__uniq. is stripped. | |
| { | |
| ScopedHasUniqSuffix Scope(false); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.__uniq.123")); | |
| EXPECT_EQ("foo", | |
| FunctionSamples::getCanonicalFnName("foo.__uniq.123.llvm.456")); | |
| EXPECT_EQ("foo", | |
| FunctionSamples::getCanonicalFnName("foo.__uniq.123.part.1")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName( | |
| "foo.__uniq.123.part.1.llvm.456")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.llvm.456")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.part.1")); | |
| } | |
| // When HasUniqSuffix is true, .__uniq. is preserved. | |
| { | |
| ScopedHasUniqSuffix Scope(true); | |
| EXPECT_EQ("foo.__uniq.123", | |
| FunctionSamples::getCanonicalFnName("foo.__uniq.123")); | |
| EXPECT_EQ("foo.__uniq.123", | |
| FunctionSamples::getCanonicalFnName("foo.__uniq.123.llvm.456")); | |
| EXPECT_EQ("foo.__uniq.123", | |
| FunctionSamples::getCanonicalFnName("foo.__uniq.123.part.1")); | |
| EXPECT_EQ("foo.__uniq.123", FunctionSamples::getCanonicalFnName( | |
| "foo.__uniq.123.part.1.llvm.456")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.llvm.456")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.part.1")); | |
| } | |
| } | |
| TEST(SampleProfCanonicalNameTest, AllPolicy) { | |
| for (StringRef Attr : {"all", ""}) { | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo", Attr)); | |
| EXPECT_EQ("foo", | |
| FunctionSamples::getCanonicalFnName("foo.llvm.1234", Attr)); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.part.1", Attr)); | |
| EXPECT_EQ("foo", | |
| FunctionSamples::getCanonicalFnName("foo.__uniq.1234", Attr)); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.bar", Attr)); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.1.2.3", Attr)); | |
| EXPECT_EQ("", FunctionSamples::getCanonicalFnName(".foo", Attr)); | |
| EXPECT_EQ("", FunctionSamples::getCanonicalFnName("", Attr)); | |
| } | |
| } | |
| TEST(SampleProfCanonicalNameTest, NonePolicy) { | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo", "none")); | |
| EXPECT_EQ("foo.llvm.1234", | |
| FunctionSamples::getCanonicalFnName("foo.llvm.1234", "none")); | |
| EXPECT_EQ("foo.part.1", | |
| FunctionSamples::getCanonicalFnName("foo.part.1", "none")); | |
| EXPECT_EQ("foo.__uniq.1234", | |
| FunctionSamples::getCanonicalFnName("foo.__uniq.1234", "none")); | |
| EXPECT_EQ("foo.bar", FunctionSamples::getCanonicalFnName("foo.bar", "none")); | |
| EXPECT_EQ("", FunctionSamples::getCanonicalFnName("", "none")); | |
| } | |
| TEST(SampleProfCanonicalNameTest, CoroFnName) { | |
| // Default "selected" policy. | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalCoroFnName("foo.cleanup")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalCoroFnName("foo.destroy")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalCoroFnName("foo.resume")); | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalCoroFnName("foo.llvm.1234")); | |
| EXPECT_EQ("_Zfoo", | |
| FunctionSamples::getCanonicalCoroFnName("_Zfoo.llvm.1234.cleanup")); | |
| EXPECT_EQ("_Zfoo", | |
| FunctionSamples::getCanonicalCoroFnName("_Zfoo.llvm.1234.destroy")); | |
| EXPECT_EQ("_Zfoo", | |
| FunctionSamples::getCanonicalCoroFnName("_Zfoo.llvm.1234.resume")); | |
| // Suffixes without trailing dot require an exact match. | |
| EXPECT_EQ("foo.cleanupper", | |
| FunctionSamples::getCanonicalCoroFnName("foo.cleanupper")); | |
| EXPECT_EQ("foo.destroyer", | |
| FunctionSamples::getCanonicalCoroFnName("foo.destroyer")); | |
| EXPECT_EQ("foo.resumed", | |
| FunctionSamples::getCanonicalCoroFnName("foo.resumed")); | |
| EXPECT_EQ("foo.other", FunctionSamples::getCanonicalCoroFnName("foo.other")); | |
| // Policy "none". | |
| EXPECT_EQ("_Zfoo.llvm.1234.cleanup", FunctionSamples::getCanonicalCoroFnName( | |
| "_Zfoo.llvm.1234.cleanup", "none")); | |
| // Policy "all". | |
| EXPECT_EQ("_Zfoo", FunctionSamples::getCanonicalCoroFnName( | |
| "_Zfoo.llvm.1234.cleanup", "all")); | |
| } | |
| TEST(SampleProfCanonicalNameTest, CustomSuffixes) { | |
| const SmallVector<StringRef, 2> Suffixes{".custom.", ".cfi"}; | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.cfi", Suffixes)); | |
| EXPECT_EQ("foo", | |
| FunctionSamples::getCanonicalFnName("foo.custom.123", Suffixes)); | |
| // In Suffixes {".custom.", ".cfi"}, .custom. is evaluated before .cfi. | |
| // Because .cfi was appended after .custom.123, .custom. cannot be stripped | |
| // first, leaving "foo.custom.123". | |
| EXPECT_EQ("foo.custom.123", FunctionSamples::getCanonicalFnName( | |
| "foo.custom.123.cfi", Suffixes)); | |
| // Conversely, when ordered {".cfi", ".custom."}, both are stripped. | |
| const SmallVector<StringRef, 2> OrderedSuffixes{".cfi", ".custom."}; | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.custom.123.cfi", | |
| OrderedSuffixes)); | |
| // Suffix without trailing dot requires an exact match; it does not match | |
| // as a prefix of a component. | |
| EXPECT_EQ("foo.cfiUnrelated", | |
| FunctionSamples::getCanonicalFnName("foo.cfiUnrelated", Suffixes)); | |
| EXPECT_EQ("foo.cfiSomething.123", FunctionSamples::getCanonicalFnName( | |
| "foo.cfiSomething.123", Suffixes)); | |
| EXPECT_EQ("foo.cfi_jt", | |
| FunctionSamples::getCanonicalFnName("foo.cfi_jt", Suffixes)); | |
| // Suffix not in the list is preserved. | |
| EXPECT_EQ("foo.llvm.123", | |
| FunctionSamples::getCanonicalFnName("foo.llvm.123", Suffixes)); | |
| // Empty suffixes list preserves the name under "selected". | |
| EXPECT_EQ("foo.llvm.123", FunctionSamples::getCanonicalFnName( | |
| "foo.llvm.123", {}, "selected")); | |
| // Policies "all" and "none" with custom suffixes. | |
| EXPECT_EQ("foo", FunctionSamples::getCanonicalFnName("foo.custom.123", | |
| Suffixes, "all")); | |
| EXPECT_EQ("foo.custom.123", FunctionSamples::getCanonicalFnName( | |
| "foo.custom.123", Suffixes, "none")); | |
| } | |
| TEST(SampleProfCanonicalNameTest, FromLLVMFunction) { | |
| LLVMContext Ctx; | |
| auto M = std::make_unique<Module>("test", Ctx); | |
| auto *FTy = FunctionType::get(Type::getVoidTy(Ctx), false); | |
| // Without attribute, default policy is "" (same as "all"). | |
| auto *FDefault = Function::Create(FTy, GlobalValue::ExternalLinkage, | |
| "f_default.llvm.1234", M.get()); | |
| EXPECT_EQ("f_default", FunctionSamples::getCanonicalFnName(*FDefault)); | |
| auto *FDefaultDot = Function::Create(FTy, GlobalValue::ExternalLinkage, | |
| "f_default_dot.bar", M.get()); | |
| EXPECT_EQ("f_default_dot", FunctionSamples::getCanonicalFnName(*FDefaultDot)); | |
| // With attribute "selected". | |
| auto *FSelectedLLVM = Function::Create(FTy, GlobalValue::ExternalLinkage, | |
| "f_selected.llvm.1234", M.get()); | |
| FSelectedLLVM->addFnAttr("sample-profile-suffix-elision-policy", "selected"); | |
| EXPECT_EQ("f_selected", FunctionSamples::getCanonicalFnName(*FSelectedLLVM)); | |
| auto *FSelectedDot = Function::Create(FTy, GlobalValue::ExternalLinkage, | |
| "f_selected_dot.bar", M.get()); | |
| FSelectedDot->addFnAttr("sample-profile-suffix-elision-policy", "selected"); | |
| EXPECT_EQ("f_selected_dot.bar", | |
| FunctionSamples::getCanonicalFnName(*FSelectedDot)); | |
| // With attribute "none". | |
| auto *FNone = Function::Create(FTy, GlobalValue::ExternalLinkage, | |
| "f_none.llvm.1234", M.get()); | |
| FNone->addFnAttr("sample-profile-suffix-elision-policy", "none"); | |
| EXPECT_EQ("f_none.llvm.1234", FunctionSamples::getCanonicalFnName(*FNone)); | |
| // With attribute "all". | |
| auto *FAll = Function::Create(FTy, GlobalValue::ExternalLinkage, | |
| "f_all.bar.1234", M.get()); | |
| FAll->addFnAttr("sample-profile-suffix-elision-policy", "all"); | |
| EXPECT_EQ("f_all", FunctionSamples::getCanonicalFnName(*FAll)); | |
| } | |
| #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | |
| TEST(SampleProfCanonicalNameTest, InvalidPolicyDeathTest) { | |
| EXPECT_DEATH(FunctionSamples::getCanonicalFnName("foo", "invalid_policy"), | |
| "unknown suffix elision policy"); | |
| } | |
| #endif | |
| } // end anonymous namespace |