Merge pull request #10889 from krishalivaibhav/fix/macos-mlock-prot-none

fix(libkernel): refuse mlock on PROT_NONE ranges on macOS
This commit is contained in:
Adrià authored and GitHub committed 2026-10-27 07:58:57 +01:00
commit 589799aa53
1 file changed
+18
@@ -21,6 +21,10 @@
#include <sstream>
#include <sys/mman.h>
#include <sys/resource.h>
#ifdef __APPLE__
#include <mach/mach.h>
#include <mach/mach_vm.h>
#endif
#endif
#include <algorithm>
#include <cassert>
@@ -690,6 +694,20 @@ int UnlockHostPages(std::uintptr_t start, std::uintptr_t end) {
int LockHostPages(std::uintptr_t start, std::uintptr_t end) {
auto* const address = reinterpret_cast<void*>(start);
const std::size_t bytes = end - start;
#ifdef __APPLE__
for (mach_vm_address_t cursor = start; cursor < end;) {
mach_vm_address_t region = cursor;
mach_vm_size_t size = 0;
vm_region_basic_info_data_64_t info{};
mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64;
mach_port_t object = MACH_PORT_NULL;
const kern_return_t status = mach_vm_region(mach_task_self(), &region, &size, VM_REGION_BASIC_INFO_64, reinterpret_cast<vm_region_info_t>(&info), &count, &object);
if (status == KERN_INVALID_ADDRESS) return SCE_KERNEL_ERROR_ENOMEM;
if (status != KERN_SUCCESS) throw std::runtime_error("mach_vm_region failed");
if (region > cursor || (info.protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0) return SCE_KERNEL_ERROR_ENOMEM;
cursor = region + size;
}
#endif
if (::mlock(address, bytes) == 0) {
RecordLockedRange(start, end);
return 0;