From 5ab27e4bdcd9075717fe93438e84fd4cf1e94137 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 27 Jan 2021 20:48:38 +0100 Subject: [PATCH] Kernel: sys$mmap() without MAP_FIXED should consider address a hint If we can't use that specific address, it's still okay to put it anywhere else in VM. --- Kernel/Syscalls/mmap.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Kernel/Syscalls/mmap.cpp b/Kernel/Syscalls/mmap.cpp index b461893fc5..3e5522fcca 100644 --- a/Kernel/Syscalls/mmap.cpp +++ b/Kernel/Syscalls/mmap.cpp @@ -134,14 +134,17 @@ void* Process::sys$mmap(Userspace user_params) Region* region = nullptr; auto range = allocate_range(VirtualAddress(addr), size, alignment); - if (!range.is_valid()) + if (!range.is_valid()) { + if (addr && !map_fixed) { + // If there's an address but MAP_FIXED wasn't specified, the address is just a hint. + range = allocate_range({}, size, alignment); + } return (void*)-ENOMEM; + } if (map_anonymous) { auto strategy = map_noreserve ? AllocationStrategy::None : AllocationStrategy::Reserve; auto region_or_error = allocate_region(range, !name.is_null() ? name : "mmap", prot, strategy); - if (region_or_error.is_error() && (!map_fixed && addr != 0)) - region_or_error = allocate_region(allocate_range({}, size), !name.is_null() ? name : "mmap", prot, strategy); if (region_or_error.is_error()) return (void*)region_or_error.error().error(); region = region_or_error.value();