Index: include/llvm/ExecutionEngine/SectionMemoryManager.h =================================================================== --- include/llvm/ExecutionEngine/SectionMemoryManager.h +++ include/llvm/ExecutionEngine/SectionMemoryManager.h @@ -84,6 +84,7 @@ private: struct MemoryGroup { + SmallVector PendingMem; SmallVector AllocatedMem; SmallVector FreeMem; sys::MemoryBlock Near; Index: lib/ExecutionEngine/SectionMemoryManager.cpp =================================================================== --- lib/ExecutionEngine/SectionMemoryManager.cpp +++ lib/ExecutionEngine/SectionMemoryManager.cpp @@ -83,7 +83,7 @@ // Save this address as the basis for our next request MemGroup.Near = MB; - MemGroup.AllocatedMem.push_back(MB); + MemGroup.PendingMem.push_back(MB); Addr = (uintptr_t)MB.base(); uintptr_t EndOfBlock = Addr + MB.size(); @@ -138,6 +138,13 @@ // relocations) will get to the data cache but not to the instruction cache. invalidateInstructionCache(); + // Now, remember that we have successfully applied the permissions to avoid + // having to apply them again. + CodeMem.AllocatedMem.append(CodeMem.PendingMem.begin(),CodeMem.PendingMem.end()); + RODataMem.AllocatedMem.append(RODataMem.PendingMem.begin(),RODataMem.PendingMem.end()); + CodeMem.PendingMem.clear(); + RODataMem.PendingMem.clear(); + return false; } @@ -145,7 +152,7 @@ SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup, unsigned Permissions) { - for (sys::MemoryBlock &MB : MemGroup.AllocatedMem) + for (sys::MemoryBlock &MB : MemGroup.PendingMem) if (std::error_code EC = sys::Memory::protectMappedMemory(MB, Permissions)) return EC; @@ -153,7 +160,7 @@ } void SectionMemoryManager::invalidateInstructionCache() { - for (sys::MemoryBlock &Block : CodeMem.AllocatedMem) + for (sys::MemoryBlock &Block : CodeMem.PendingMem) sys::Memory::InvalidateInstructionCache(Block.base(), Block.size()); }