pkgsrc-WIP-changes archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
llvm-netbsd: Add allocator+deallocator for RWX
Module Name: pkgsrc-wip
Committed By: Kamil Rytarowski <n54%gmx.com@localhost>
Pushed By: kamil
Date: Sun Jun 4 00:52:26 2017 +0200
Changeset: b43ec7929f81727036c046e462193960108f6e1b
Modified Files:
llvm-netbsd/distinfo
Added Files:
llvm-netbsd/patches/patch-lib_Support_Unix_Memory.inc
Log Message:
llvm-netbsd: Add allocator+deallocator for RWX
This is used for JIT code.
Sponsored by <The NetBSD Foundation>
To see a diff of this commit:
https://wip.pkgsrc.org/cgi-bin/gitweb.cgi?p=pkgsrc-wip.git;a=commitdiff;h=b43ec7929f81727036c046e462193960108f6e1b
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
diffstat:
llvm-netbsd/distinfo | 1 +
.../patches/patch-lib_Support_Unix_Memory.inc | 84 ++++++++++++++++++++++
2 files changed, 85 insertions(+)
diffs:
diff --git a/llvm-netbsd/distinfo b/llvm-netbsd/distinfo
index 0f0e9add59..b4883f32ea 100644
--- a/llvm-netbsd/distinfo
+++ b/llvm-netbsd/distinfo
@@ -13,3 +13,4 @@ SHA1 (llvm-3.6.2.src.tar.xz) = 7a00257eb2bc9431e4c77c3a36b033072c54bc7e
RMD160 (llvm-3.6.2.src.tar.xz) = 521cbc5fe2925ea3c6e90c7a31f752a04045c972
Size (llvm-3.6.2.src.tar.xz) = 12802380 bytes
SHA1 (patch-include_llvm_ADT_Triple.h) = be08dde07a038259a3314ec7791d2ccf8a682c0c
+SHA1 (patch-lib_Support_Unix_Memory.inc) = 3b98071466cd109c634852298565d68d07ce655a
diff --git a/llvm-netbsd/patches/patch-lib_Support_Unix_Memory.inc b/llvm-netbsd/patches/patch-lib_Support_Unix_Memory.inc
new file mode 100644
index 0000000000..950d486947
--- /dev/null
+++ b/llvm-netbsd/patches/patch-lib_Support_Unix_Memory.inc
@@ -0,0 +1,84 @@
+$NetBSD$
+
+--- lib/Support/Unix/Memory.inc.orig 2016-12-16 22:52:53.000000000 +0000
++++ lib/Support/Unix/Memory.inc
+@@ -177,7 +177,16 @@ Memory::AllocateRWX(size_t NumBytes, con
+ if (NumBytes == 0) return MemoryBlock();
+
+ static const size_t PageSize = Process::getPageSize();
+- size_t NumPages = (NumBytes+PageSize-1)/PageSize;
++
++ static const size_t overhead =
++#if __NetBSD_Version__ - 0 >= 799007200
++ sizeof(void*)
++#else
++ 0
++#endif
++ ;
++
++ size_t NumPages = (NumBytes+overhead+PageSize-1)/PageSize;
+
+ int fd = -1;
+
+@@ -195,6 +204,9 @@ Memory::AllocateRWX(size_t NumBytes, con
+ #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
+ void *pa = ::mmap(start, PageSize*NumPages, PROT_READ|PROT_EXEC,
+ flags, fd, 0);
++#elif __NetBSD_Version__ - 0 >= 799007200
++ void *pa = ::mmap(start, PageSize*NumPages, PROT_READ | PROT_WRITE | PROT_MPROTECT(PROT_EXEC),
++ flags, fd, 0);
+ #else
+ void *pa = ::mmap(start, PageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
+ flags, fd, 0);
+@@ -223,19 +235,49 @@ Memory::AllocateRWX(size_t NumBytes, con
+ MakeErrMsg(ErrMsg, "vm_protect RW failed");
+ return MemoryBlock();
+ }
++#elif __NetBSD_Version__ - 0 >= 799007200
++ void *codeseg = mremap(pa, PageSize*NumPages, NULL, PageSize*NumPages, MAP_REMAPDUP);
++ if (codeseg == MAP_FAILED) {
++ munmap(pa, PageSize*NumPages);
++
++ if (NearBlock) //Try again without a near hint
++ return AllocateRWX(NumBytes, nullptr);
++
++ MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
++ return MemoryBlock();
++ }
++ if (mprotect(codeseg, PageSize*NumPages, PROT_READ | PROT_EXEC) == -1) {
++ munmap(pa, PageSize*NumPages);
++ munmap(codeseg, PageSize*NumPages);
++ if (NearBlock) //Try again without a near hint
++ return AllocateRWX(NumBytes, nullptr);
++
++ MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
++ return MemoryBlock();
++ }
++ // Rembember code segment pointer, to be able to free it later
++ memcpy(pa, &codeseg, sizeof(void *));
+ #endif
+
+ MemoryBlock result;
+- result.Address = pa;
+- result.Size = NumPages*PageSize;
++ result.Address = (void *)((uintptr_t)pa + overhead);
++ result.Size = NumPages*PageSize - overhead;
+
+ return result;
+ }
+
+ bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
+ if (M.Address == nullptr || M.Size == 0) return false;
++#if __NetBSD_Version__ - 0 >= 799007200
++ static const size_t overhead = sizeof(void *);
++ void *codeseg;
++ memcpy(&codeseg, (void *)((uintptr_t)M.Address - overhead), sizeof(void *));
++ if (0 != ::munmap(codeseg, M.Size + overhead) || 0 != ::munmap(M.Address, M.Size + overhead))
++ return MakeErrMsg(ErrMsg, "Can't release RWX Memory");
++#else
+ if (0 != ::munmap(M.Address, M.Size))
+ return MakeErrMsg(ErrMsg, "Can't release RWX Memory");
++#endif
+ return false;
+ }
+
Home |
Main Index |
Thread Index |
Old Index