Correct mprotect page boundries to round up end page. Fixes PR30905.
authorAlina Sbirlea <asbirlea@google.com>
Sat, 5 Nov 2016 04:22:15 +0000 (04:22 +0000)
committerAlina Sbirlea <asbirlea@google.com>
Sat, 5 Nov 2016 04:22:15 +0000 (04:22 +0000)
Summary:
Update the boundries for mprotect.
Patch by Andrew Adams. Fixes PR30905.

Reviewers: loladiro, andrew.w.kaylor, chandlerc

Subscribers: abadams, llvm-commits

Differential Revision: https://reviews.llvm.org/D26312

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286032 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Unix/Memory.inc

index 6bbaf509d81f257fa9d1af91c86c5b33a94bcb02..edbc7938f0cbf2b3f0dc88d76261c26634e75c52 100644 (file)
@@ -153,7 +153,10 @@ Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) {
 
   int Protect = getPosixProtectionFlags(Flags);
 
-  int Result = ::mprotect((void*)((uintptr_t)M.Address & ~(PageSize-1)), PageSize*((M.Size+PageSize-1)/PageSize), Protect);
+  uintptr_t Start = alignAddr((uint8_t *)M.Address - PageSize + 1, PageSize);
+  uintptr_t End = alignAddr((uint8_t *)M.Address + M.Size, PageSize);
+  int Result = ::mprotect((void *)Start, End - Start, Protect);
+
   if (Result != 0)
     return std::error_code(errno, std::generic_category());