Index: lib/Support/Unix/Memory.inc =================================================================== --- lib/Support/Unix/Memory.inc +++ lib/Support/Unix/Memory.inc @@ -153,7 +153,14 @@ int Protect = getPosixProtectionFlags(Flags); - int Result = ::mprotect((void*)((uintptr_t)M.Address & ~(PageSize-1)), PageSize*((M.Size+PageSize-1)/PageSize), Protect); + uintptr_t start = (uintptr_t)M.Address; + uintptr_t end = start + M.Size; + // Round down the start address to a page boundary + start = start & ~(PageSize - 1); + // Round up the end address to a page boundary + end = (end + PageSize - 1) & ~(PageSize - 1); + int Result = ::mprotect((void*)start, end - start, Protect); + if (Result != 0) return std::error_code(errno, std::generic_category());