From 7ec6dde83ae66b7b3a01c69d619970cd3280dfca Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Mon, 3 Oct 2022 19:28:56 -0700 Subject: [PATCH] [llvm-jitlink] Teach InProcessDeltaMapper to honor -slab-page-size option. The -slab-page-size option is used to set a simulated page size in -no-exec tests. In order for this to work we need to use read/write permissions only on all simulated pages in order to ensure that no simulated page is made read-only by a permission change to the underlying real page. The aim of this patch is to make it safe to enable ExecutionEngine regression tests on arm64. Those tests will be enabled in a follow-up patch. --- llvm/tools/llvm-jitlink/llvm-jitlink.cpp | 45 ++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp index 6c931d1..9184945 100644 --- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp +++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp @@ -469,10 +469,19 @@ public: DeltaAddr(0) {} static Expected> Create() { - auto PageSize = sys::Process::getPageSize(); - if (!PageSize) - return PageSize.takeError(); - return std::make_unique(*PageSize, SlabAddress); + size_t PageSize = SlabPageSize; + if (!PageSize) { + if (auto PageSizeOrErr = sys::Process::getPageSize()) + PageSize = *PageSizeOrErr; + else + return PageSizeOrErr.takeError(); + } + + if (PageSize == 0) + return make_error("Page size is zero", + inconvertibleErrorCode()); + + return std::make_unique(PageSize, SlabAddress); } void reserve(size_t NumBytes, OnReservedFunction OnReserved) override { @@ -497,8 +506,12 @@ public: } void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override { + // Slide mapping based on delta and make all segments read-writable. auto FixedAI = AI; FixedAI.MappingBase -= DeltaAddr; + for (auto &Seg : FixedAI.Segments) + Seg.AG = AllocGroup(MemProt::Read | MemProt::Write, + Seg.AG.getMemDeallocPolicy()); InProcessMemoryMapper::initialize( FixedAI, [this, OnInitialized = std::move(OnInitialized)]( Expected Result) mutable { @@ -557,23 +570,27 @@ Expected getSlabAllocSize(StringRef SizeString) { } static std::unique_ptr createInProcessMemoryManager() { - if (!SlabAllocateSizeString.empty()) { - auto SlabSize = ExitOnErr(getSlabAllocSize(SlabAllocateSizeString)); + uint64_t SlabSize; +#ifdef _WIN32 + SlabSize = 1024 * 1024; +#else + SlabSize = 1024 * 1024 * 1024; +#endif + if (!SlabAllocateSizeString.empty()) + SlabSize = ExitOnErr(getSlabAllocSize(SlabAllocateSizeString)); + + // If this is a -no-exec case and we're tweaking the slab address or size then + // use the delta mapper. + if (NoExec && (SlabAddress || SlabPageSize)) return ExitOnErr( MapperJITLinkMemoryManager::CreateWithMapper( SlabSize)); - } -#ifdef _WIN32 + // Otherwise use the standard in-process mapper. return ExitOnErr( MapperJITLinkMemoryManager::CreateWithMapper( - 1024 * 1024)); -#else - return ExitOnErr( - MapperJITLinkMemoryManager::CreateWithMapper( - 1024 * 1024 * 1024)); -#endif + SlabSize)); } Expected> -- 2.7.4