diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h --- a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h +++ b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h @@ -704,11 +704,11 @@ /// Set the protection flags for this section. void setMemProt(orc::MemProt Prot) { this->Prot = Prot; } - /// Get the deallocation policy for this section. - orc::MemDeallocPolicy getMemDeallocPolicy() const { return MDP; } + /// Get the memory lifetime policy for this section. + orc::MemLifetimePolicy getMemLifetimePolicy() const { return MLP; } - /// Set the deallocation policy for this section. - void setMemDeallocPolicy(orc::MemDeallocPolicy MDP) { this->MDP = MDP; } + /// Set the memory lifetime policy for this section. + void setMemLifetimePolicy(orc::MemLifetimePolicy MLP) { this->MLP = MLP; } /// Returns the ordinal for this section. SectionOrdinal getOrdinal() const { return SecOrdinal; } @@ -773,7 +773,7 @@ StringRef Name; orc::MemProt Prot; - orc::MemDeallocPolicy MDP = orc::MemDeallocPolicy::Standard; + orc::MemLifetimePolicy MLP = orc::MemLifetimePolicy::Standard; SectionOrdinal SecOrdinal = 0; BlockSet Blocks; SymbolSet Symbols; diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h b/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h --- a/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h +++ b/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h @@ -291,6 +291,9 @@ /// Segment. Clients can obtain a pointer to the working memory and executor /// address of that block using the Segment's AllocGroup. Once memory has been /// populated, clients can call finalize to finalize the memory. +/// +/// Note: Segments with MemLifetimePolicy::NoAlloc are not permitted, since +/// they would not be useful, and their presence is likely to indicate a bug. class SimpleSegmentAlloc { public: /// Describes a segment to be allocated. diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h --- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h @@ -65,26 +65,43 @@ return MP; } -/// Describes a memory deallocation policy for memory to be allocated by a +/// Describes a memory lifetime policy for memory to be allocated by a /// JITLinkMemoryManager. /// /// All memory allocated by a call to JITLinkMemoryManager::allocate should be /// deallocated if a call is made to /// JITLinkMemoryManager::InFlightAllocation::abandon. The policies below apply /// to finalized allocations. -enum class MemDeallocPolicy { - /// Standard memory should be deallocated when the deallocate method is called - /// for the finalized allocation. +enum class MemLifetimePolicy { + /// Standard memory should be allocated by the allocator and then deallocated + /// when the deallocate method is called for the finalized allocation. Standard, - /// Finalize memory should be overwritten and then deallocated after all - /// finalization functions have been run. - Finalize + /// Finalize memory should be allocated by the allocator, and then be + /// overwritten and deallocated after all finalization functions have been + /// run. + Finalize, + + /// NoAlloc memory should not be allocated by the JITLinkMemoryManager at + /// all. It is used for sections that don't need to be transferred to the + /// executor process, typically metadata sections. + NoAlloc }; /// Print a MemDeallocPolicy. -inline raw_ostream &operator<<(raw_ostream &OS, MemDeallocPolicy MDP) { - return OS << (MDP == MemDeallocPolicy::Standard ? "standard" : "finalize"); +inline raw_ostream &operator<<(raw_ostream &OS, MemLifetimePolicy MLP) { + switch (MLP) { + case MemLifetimePolicy::Standard: + OS << "standard"; + break; + case MemLifetimePolicy::Finalize: + OS << "finalize"; + break; + case MemLifetimePolicy::NoAlloc: + OS << "noalloc"; + break; + } + return OS; } /// A pair of memory protections and allocation policies. @@ -95,34 +112,34 @@ using underlying_type = uint8_t; static constexpr unsigned BitsForProt = 3; - static constexpr unsigned BitsForDeallocPolicy = 1; + static constexpr unsigned BitsForLifetimePolicy = 2; static constexpr unsigned MaxIdentifiers = - 1U << (BitsForProt + BitsForDeallocPolicy); + 1U << (BitsForProt + BitsForLifetimePolicy); public: static constexpr unsigned NumGroups = MaxIdentifiers; /// Create a default AllocGroup. No memory protections, standard - /// deallocation policy. + /// lifetime policy. AllocGroup() = default; /// Create an AllocGroup from a MemProt only -- uses - /// MemoryDeallocationPolicy::Standard. + /// MemLifetimePolicy::Standard. AllocGroup(MemProt MP) : Id(static_cast(MP)) {} - /// Create an AllocGroup from a MemProt and a MemoryDeallocationPolicy. - AllocGroup(MemProt MP, MemDeallocPolicy MDP) + /// Create an AllocGroup from a MemProt and a MemLifetimePolicy. + AllocGroup(MemProt MP, MemLifetimePolicy MLP) : Id(static_cast(MP) | - (static_cast(MDP) << BitsForProt)) {} + (static_cast(MLP) << BitsForProt)) {} /// Returns the MemProt for this group. MemProt getMemProt() const { return static_cast(Id & ((1U << BitsForProt) - 1)); } - /// Returns the MemoryDeallocationPolicy for this group. - MemDeallocPolicy getMemDeallocPolicy() const { - return static_cast(Id >> BitsForProt); + /// Returns the MemLifetimePolicy for this group. + MemLifetimePolicy getMemLifetimePolicy() const { + return static_cast(Id >> BitsForProt); } friend bool operator==(const AllocGroup &LHS, const AllocGroup &RHS) { @@ -186,7 +203,7 @@ /// Print an AllocGroup. inline raw_ostream &operator<<(raw_ostream &OS, AllocGroup AG) { - return OS << '(' << AG.getMemProt() << ", " << AG.getMemDeallocPolicy() + return OS << '(' << AG.getMemProt() << ", " << AG.getMemLifetimePolicy() << ')'; } diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h --- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h @@ -30,8 +30,24 @@ namespace orc { namespace tpctypes { +struct RemoteAllocGroup { + RemoteAllocGroup() = default; + RemoteAllocGroup(MemProt Prot) : Prot(Prot) {} + RemoteAllocGroup(MemProt Prot, bool FinalizeLifetime) + : Prot(Prot), FinalizeLifetime(FinalizeLifetime) {} + RemoteAllocGroup(const AllocGroup &AG) : Prot(AG.getMemProt()) { + assert(AG.getMemLifetimePolicy() != orc::MemLifetimePolicy::NoAlloc && + "Cannot use no-alloc memory in a remote alloc request"); + FinalizeLifetime = + AG.getMemLifetimePolicy() == orc::MemLifetimePolicy::Finalize; + } + + MemProt Prot; + bool FinalizeLifetime = false; +}; + struct SegFinalizeRequest { - AllocGroup AG; + RemoteAllocGroup RAG; ExecutorAddr Addr; uint64_t Size; ArrayRef Content; @@ -43,7 +59,7 @@ }; struct SharedMemorySegFinalizeRequest { - AllocGroup AG; + RemoteAllocGroup RAG; ExecutorAddr Addr; uint64_t Size; }; @@ -93,16 +109,16 @@ namespace shared { -class SPSAllocGroup {}; +class SPSRemoteAllocGroup; using SPSSegFinalizeRequest = - SPSTuple>; + SPSTuple>; using SPSFinalizeRequest = SPSTuple, SPSSequence>; using SPSSharedMemorySegFinalizeRequest = - SPSTuple; + SPSTuple; using SPSSharedMemoryFinalizeRequest = SPSTuple, @@ -118,7 +134,8 @@ using SPSMemoryAccessBufferWrite = SPSTuple>; -template <> class SPSSerializationTraits { +template <> +class SPSSerializationTraits { enum WireBits { ReadBit = 1 << 0, WriteBit = 1 << 1, @@ -127,25 +144,26 @@ }; public: - static size_t size(const AllocGroup &AG) { + static size_t size(const tpctypes::RemoteAllocGroup &RAG) { // All AllocGroup values encode to the same size. return SPSArgList::size(uint8_t(0)); } - static bool serialize(SPSOutputBuffer &OB, const AllocGroup &AG) { + static bool serialize(SPSOutputBuffer &OB, + const tpctypes::RemoteAllocGroup &RAG) { uint8_t WireValue = 0; - if ((AG.getMemProt() & MemProt::Read) != MemProt::None) + if ((RAG.Prot & MemProt::Read) != MemProt::None) WireValue |= ReadBit; - if ((AG.getMemProt() & MemProt::Write) != MemProt::None) + if ((RAG.Prot & MemProt::Write) != MemProt::None) WireValue |= WriteBit; - if ((AG.getMemProt() & MemProt::Exec) != MemProt::None) + if ((RAG.Prot & MemProt::Exec) != MemProt::None) WireValue |= ExecBit; - if (AG.getMemDeallocPolicy() == MemDeallocPolicy::Finalize) + if (RAG.FinalizeLifetime) WireValue |= FinalizeBit; return SPSArgList::serialize(OB, WireValue); } - static bool deserialize(SPSInputBuffer &IB, AllocGroup &AG) { + static bool deserialize(SPSInputBuffer &IB, tpctypes::RemoteAllocGroup &RAG) { uint8_t Val; if (!SPSArgList::deserialize(IB, Val)) return false; @@ -156,9 +174,8 @@ MP |= MemProt::Write; if (Val & ExecBit) MP |= MemProt::Exec; - MemDeallocPolicy MDP = (Val & FinalizeBit) ? MemDeallocPolicy::Finalize - : MemDeallocPolicy::Standard; - AG = AllocGroup(MP, MDP); + bool FinalizeLifetime = (Val & FinalizeBit) ? true : false; + RAG = {MP, FinalizeLifetime}; return true; } }; @@ -170,17 +187,17 @@ public: static size_t size(const tpctypes::SegFinalizeRequest &SFR) { - return SFRAL::size(SFR.AG, SFR.Addr, SFR.Size, SFR.Content); + return SFRAL::size(SFR.RAG, SFR.Addr, SFR.Size, SFR.Content); } static bool serialize(SPSOutputBuffer &OB, const tpctypes::SegFinalizeRequest &SFR) { - return SFRAL::serialize(OB, SFR.AG, SFR.Addr, SFR.Size, SFR.Content); + return SFRAL::serialize(OB, SFR.RAG, SFR.Addr, SFR.Size, SFR.Content); } static bool deserialize(SPSInputBuffer &IB, tpctypes::SegFinalizeRequest &SFR) { - return SFRAL::deserialize(IB, SFR.AG, SFR.Addr, SFR.Size, SFR.Content); + return SFRAL::deserialize(IB, SFR.RAG, SFR.Addr, SFR.Size, SFR.Content); } }; @@ -210,17 +227,17 @@ public: static size_t size(const tpctypes::SharedMemorySegFinalizeRequest &SFR) { - return SFRAL::size(SFR.AG, SFR.Addr, SFR.Size); + return SFRAL::size(SFR.RAG, SFR.Addr, SFR.Size); } static bool serialize(SPSOutputBuffer &OB, const tpctypes::SharedMemorySegFinalizeRequest &SFR) { - return SFRAL::serialize(OB, SFR.AG, SFR.Addr, SFR.Size); + return SFRAL::serialize(OB, SFR.RAG, SFR.Addr, SFR.Size); } static bool deserialize(SPSInputBuffer &IB, tpctypes::SharedMemorySegFinalizeRequest &SFR) { - return SFRAL::deserialize(IB, SFR.AG, SFR.Addr, SFR.Size); + return SFRAL::deserialize(IB, SFR.RAG, SFR.Addr, SFR.Size); } }; diff --git a/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp --- a/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp @@ -152,8 +152,11 @@ // Look for existing sections first. auto *GraphSec = G->findSectionByName(SectionName); - if (!GraphSec) + if (!GraphSec) { GraphSec = &G->createSection(SectionName, Prot); + if ((*Sec)->Characteristics & COFF::IMAGE_SCN_LNK_REMOVE) + GraphSec->setMemLifetimePolicy(orc::MemLifetimePolicy::NoAlloc); + } if (GraphSec->getMemProt() != Prot) return make_error("MemProt should match"); diff --git a/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h --- a/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h +++ b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h @@ -319,16 +319,6 @@ continue; } - // Skip non-SHF_ALLOC sections - if (!(Sec.sh_flags & ELF::SHF_ALLOC)) { - LLVM_DEBUG({ - dbgs() << " " << SecIndex << ": \"" << *Name - << "\" is not an SHF_ALLOC section: " - "No graph section will be created.\n"; - }); - continue; - } - LLVM_DEBUG({ dbgs() << " " << SecIndex << ": Creating section for \"" << *Name << "\"\n"; @@ -343,8 +333,19 @@ // Look for existing sections first. auto *GraphSec = G->findSectionByName(*Name); - if (!GraphSec) + if (!GraphSec) { GraphSec = &G->createSection(*Name, Prot); + // Non-SHF_ALLOC sections get NoAlloc memory lifetimes. + if (!(Sec.sh_flags & ELF::SHF_ALLOC)) { + GraphSec->setMemLifetimePolicy(orc::MemLifetimePolicy::NoAlloc); + LLVM_DEBUG({ + dbgs() << " " << SecIndex << ": \"" << *Name + << "\" is not a SHF_ALLOC section. Using NoAlloc lifetime"; + }); + } + continue; + } + assert(GraphSec->getMemProt() == Prot && "MemProt should match"); Block *B = nullptr; diff --git a/llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.h b/llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.h --- a/llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.h +++ b/llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.h @@ -123,26 +123,47 @@ Error fixUpBlocks(LinkGraph &G) const override { LLVM_DEBUG(dbgs() << "Fixing up blocks:\n"); - for (auto *B : G.blocks()) { - LLVM_DEBUG(dbgs() << " " << *B << ":\n"); - - // Copy Block data and apply fixups. - LLVM_DEBUG(dbgs() << " Applying fixups.\n"); - assert((!B->isZeroFill() || all_of(B->edges(), - [](const Edge &E) { - return E.getKind() == - Edge::KeepAlive; - })) && - "Non-KeepAlive edges in zero-fill block?"); - for (auto &E : B->edges()) { - - // Skip non-relocation edges. - if (!E.isRelocation()) - continue; - - // Dispatch to LinkerImpl for fixup. - if (auto Err = impl().applyFixup(G, *B, E)) - return Err; + for (auto &Sec : G.sections()) { + bool NoAllocSection = + Sec.getMemLifetimePolicy() == orc::MemLifetimePolicy::NoAlloc; + + for (auto *B : Sec.blocks()) { + LLVM_DEBUG(dbgs() << " " << *B << ":\n"); + + // Copy Block data and apply fixups. + LLVM_DEBUG(dbgs() << " Applying fixups.\n"); + assert((!B->isZeroFill() || all_of(B->edges(), + [](const Edge &E) { + return E.getKind() == + Edge::KeepAlive; + })) && + "Non-KeepAlive edges in zero-fill block?"); + + // If this is a no-alloc section then copy the block content into + // memory allocated on the Graph's allocator (if it hasn't been + // already). + if (NoAllocSection) + (void)B->getMutableContent(G); + + for (auto &E : B->edges()) { + + // Skip non-relocation edges. + if (!E.isRelocation()) + continue; + + // If B is a block in a Standard or Finalize section then make sure + // that no edges point to symbols in NoAlloc sections. + assert( + (NoAllocSection || !E.getTarget().isDefined() || + E.getTarget().getBlock().getSection().getMemLifetimePolicy() != + orc::MemLifetimePolicy::NoAlloc) && + "Block in allocated section has edge pointing to no-alloc " + "section"); + + // Dispatch to LinkerImpl for fixup. + if (auto Err = impl().applyFixup(G, *B, E)) + return Err; + } } } diff --git a/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp b/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp --- a/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp @@ -24,11 +24,12 @@ BasicLayout::BasicLayout(LinkGraph &G) : G(G) { for (auto &Sec : G.sections()) { - // Skip empty sections. - if (Sec.blocks().empty()) + // Skip empty sections, and sections with NoAlloc lifetime policies. + if (Sec.blocks().empty() || + Sec.getMemLifetimePolicy() == orc::MemLifetimePolicy::NoAlloc) continue; - auto &Seg = Segments[{Sec.getMemProt(), Sec.getMemDeallocPolicy()}]; + auto &Seg = Segments[{Sec.getMemProt(), Sec.getMemLifetimePolicy()}]; for (auto *B : Sec.blocks()) if (LLVM_LIKELY(!B->isZeroFill())) Seg.ContentBlocks.push_back(B); @@ -89,7 +90,7 @@ inconvertibleErrorCode()); uint64_t SegSize = alignTo(Seg.ContentSize + Seg.ZeroFillSize, PageSize); - if (AG.getMemDeallocPolicy() == orc::MemDeallocPolicy::Standard) + if (AG.getMemLifetimePolicy() == orc::MemLifetimePolicy::Standard) SegsSizes.StandardSegs += SegSize; else SegsSizes.FinalizeSegs += SegSize; @@ -146,7 +147,7 @@ const JITLinkDylib *JD, SegmentMap Segments, OnCreatedFunction OnCreated) { - static_assert(orc::AllocGroup::NumGroups == 16, + static_assert(orc::AllocGroup::NumGroups == 32, "AllocGroup has changed. Section names below must be updated"); StringRef AGSectionNames[] = { "__---.standard", "__R--.standard", "__-W-.standard", "__RW-.standard", @@ -163,12 +164,15 @@ auto &AG = KV.first; auto &Seg = KV.second; + assert(AG.getMemLifetimePolicy() != orc::MemLifetimePolicy::NoAlloc && + "NoAlloc segments are not supported by SimpleSegmentAlloc"); + auto AGSectionName = AGSectionNames[static_cast(AG.getMemProt()) | - static_cast(AG.getMemDeallocPolicy()) << 3]; + static_cast(AG.getMemLifetimePolicy()) << 3]; auto &Sec = G->createSection(AGSectionName, AG.getMemProt()); - Sec.setMemDeallocPolicy(AG.getMemDeallocPolicy()); + Sec.setMemLifetimePolicy(AG.getMemLifetimePolicy()); if (Seg.ContentSize != 0) { NextAddr = @@ -416,7 +420,7 @@ auto &Seg = KV.second; auto &SegAddr = - (AG.getMemDeallocPolicy() == orc::MemDeallocPolicy::Standard) + (AG.getMemLifetimePolicy() == orc::MemLifetimePolicy::Standard) ? NextStandardSegAddr : NextFinalizeSegAddr; diff --git a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp --- a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp @@ -189,6 +189,10 @@ NSec.GraphSection = &G->createSection( StringRef(FullyQualifiedName.data(), FullyQualifiedName.size()), Prot); + // TODO: Are there any other criteria for NoAlloc lifetime? + if (NSec.Flags & MachO::S_ATTR_DEBUG) + NSec.GraphSection->setMemLifetimePolicy(orc::MemLifetimePolicy::NoAlloc); + IndexToSection.insert(std::make_pair(SecIndex, std::move(NSec))); } diff --git a/llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp b/llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp --- a/llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp +++ b/llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp @@ -235,7 +235,7 @@ for (unsigned I = 0; I != 3; ++I) { FR.Segments.push_back({}); auto &Seg = FR.Segments.back(); - Seg.AG = SegMemProts[I]; + Seg.RAG = SegMemProts[I]; Seg.Addr = RemoteAddrs[I]->Start; for (auto &SecAlloc : *SegSections[I]) { Seg.Size = alignTo(Seg.Size, SecAlloc.Align); diff --git a/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp b/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp --- a/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp +++ b/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp @@ -322,7 +322,8 @@ std::memset(Base + Segment.ContentSize, 0, Segment.ZeroFillSize); tpctypes::SharedMemorySegFinalizeRequest SegReq; - SegReq.AG = Segment.AG; + SegReq.RAG = {Segment.AG.getMemProt(), Segment.AG.getMemLifetimePolicy() == + MemLifetimePolicy::Finalize}; SegReq.Addr = AI.MappingBase + Segment.Offset; SegReq.Size = Segment.ContentSize + Segment.ZeroFillSize; diff --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.cpp --- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.cpp +++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.cpp @@ -132,11 +132,11 @@ #if defined(LLVM_ON_UNIX) int NativeProt = 0; - if ((Segment.AG.getMemProt() & MemProt::Read) == MemProt::Read) + if ((Segment.RAG.Prot & MemProt::Read) == MemProt::Read) NativeProt |= PROT_READ; - if ((Segment.AG.getMemProt() & MemProt::Write) == MemProt::Write) + if ((Segment.RAG.Prot & MemProt::Write) == MemProt::Write) NativeProt |= PROT_WRITE; - if ((Segment.AG.getMemProt() & MemProt::Exec) == MemProt::Exec) + if ((Segment.RAG.Prot & MemProt::Exec) == MemProt::Exec) NativeProt |= PROT_EXEC; if (mprotect(Segment.Addr.toPtr(), Segment.Size, NativeProt)) @@ -144,8 +144,7 @@ #elif defined(_WIN32) - DWORD NativeProt = - getWindowsProtectionFlags(Segment.AG.getMemProt()); + DWORD NativeProt = getWindowsProtectionFlags(Segment.RAG.Prot); if (!VirtualProtect(Segment.Addr.toPtr(), Segment.Size, NativeProt, &NativeProt)) @@ -153,7 +152,7 @@ #endif - if ((Segment.AG.getMemProt() & MemProt::Exec) == MemProt::Exec) + if ((Segment.RAG.Prot & MemProt::Exec) == MemProt::Exec) sys::Memory::InvalidateInstructionCache(Segment.Addr.toPtr(), Segment.Size); } diff --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.cpp --- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.cpp +++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.cpp @@ -132,9 +132,9 @@ assert(Seg.Size <= std::numeric_limits::max()); if (auto EC = sys::Memory::protectMappedMemory( {Mem, static_cast(Seg.Size)}, - toSysMemoryProtectionFlags(Seg.AG.getMemProt()))) + toSysMemoryProtectionFlags(Seg.RAG.Prot))) return BailOut(errorCodeToError(EC)); - if ((Seg.AG.getMemProt() & MemProt::Exec) == MemProt::Exec) + if ((Seg.RAG.Prot & MemProt::Exec) == MemProt::Exec) sys::Memory::InvalidateInstructionCache(Mem, Seg.Size); } diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp --- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp +++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp @@ -511,8 +511,7 @@ auto FixedAI = std::move(AI); FixedAI.MappingBase -= DeltaAddr; for (auto &Seg : FixedAI.Segments) - Seg.AG = AllocGroup(MemProt::Read | MemProt::Write, - Seg.AG.getMemDeallocPolicy()); + Seg.AG = {MemProt::Read | MemProt::Write, Seg.AG.getMemLifetimePolicy()}; FixedAI.Actions.clear(); InProcessMemoryMapper::initialize( FixedAI, [this, OnInitialized = std::move(OnInitialized)]( diff --git a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp --- a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp +++ b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp @@ -756,3 +756,31 @@ EXPECT_TRUE(isCStringBlock(SizeOneZeroFillBlock)); EXPECT_FALSE(isCStringBlock(LargerZeroFillBlock)); } + +TEST(LinkGraphTest, BasicLayoutHonorsNoAlloc) { + // Check that BasicLayout honors NoAlloc. + LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, + getGenericEdgeKindName); + + // Create a regular section and block. + auto &Sec1 = + G.createSection("__data", orc::MemProt::Read | orc::MemProt::Write); + G.createContentBlock(Sec1, BlockContent.slice(0, 8), orc::ExecutorAddr(), 8, + 0); + + // Create a NoAlloc section and block. + auto &Sec2 = + G.createSection("__metadata", orc::MemProt::Read | orc::MemProt::Write); + Sec2.setMemLifetimePolicy(orc::MemLifetimePolicy::NoAlloc); + G.createContentBlock(Sec2, BlockContent.slice(0, 8), orc::ExecutorAddr(), 8, + 0); + + BasicLayout BL(G); + + EXPECT_EQ(std::distance(BL.segments().begin(), BL.segments().end()), 1U); + EXPECT_EQ(BL.segments().begin()->first, + orc::MemProt::Read | orc::MemProt::Write); + auto &SegInfo = BL.segments().begin()->second; + EXPECT_EQ(SegInfo.Alignment, 8U); + EXPECT_EQ(SegInfo.ContentSize, 8U); +} diff --git a/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp b/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp --- a/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp +++ b/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp @@ -46,9 +46,9 @@ assert(Seg.Size <= std::numeric_limits::max()); if (auto EC = sys::Memory::protectMappedMemory( {Mem, static_cast(Seg.Size)}, - toSysMemoryProtectionFlags(Seg.AG.getMemProt()))) + toSysMemoryProtectionFlags(Seg.RAG.Prot))) return errorCodeToError(EC); - if ((Seg.AG.getMemProt() & MemProt::Exec) != MemProt::Exec) + if ((Seg.RAG.Prot & MemProt::Exec) != MemProt::Exec) sys::Memory::InvalidateInstructionCache(Mem, Seg.Size); } return Error::success();