diff --git a/ntoskrnl/include/internal/mm.h b/ntoskrnl/include/internal/mm.h index cb017a5be72..6782fc1ab39 100644 --- a/ntoskrnl/include/internal/mm.h +++ b/ntoskrnl/include/internal/mm.h @@ -618,6 +618,13 @@ MmLocateMemoryAreaByRegion( SIZE_T Length ); +BOOLEAN +NTAPI +MmIsAddressRangeFree( + _In_ PMMSUPPORT AddressSpace, + _In_ PVOID Address, + _In_ ULONG_PTR Length); + PVOID NTAPI MmFindGap( diff --git a/ntoskrnl/mm/marea.c b/ntoskrnl/mm/marea.c index 4ca18459065..22c2369448c 100644 --- a/ntoskrnl/mm/marea.c +++ b/ntoskrnl/mm/marea.c @@ -115,6 +115,28 @@ MmLocateMemoryAreaByRegion( return MemoryArea; } +BOOLEAN +NTAPI +MmIsAddressRangeFree( + _In_ PMMSUPPORT AddressSpace, + _In_ PVOID Address, + _In_ ULONG_PTR Length) +{ + ULONG_PTR StartVpn = (ULONG_PTR)Address / PAGE_SIZE; + ULONG_PTR EndVpn = ((ULONG_PTR)Address + Length - 1) / PAGE_SIZE; + PEPROCESS Process; + PMM_AVL_TABLE Table; + PMMADDRESS_NODE Node; + TABLE_SEARCH_RESULT Result; + + Process = MmGetAddressSpaceOwner(AddressSpace); + Table = (Process != NULL) ? &Process->VadRoot : &MiRosKernelVadRoot; + + Result = MiCheckForConflictingNode(StartVpn, EndVpn, Table, &Node); + + return (Result != TableFoundNode); +} + VOID NTAPI MiInsertVad(IN PMMVAD Vad, @@ -460,9 +482,7 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace, /* No need to check ARM3 owned memory areas, the range MUST be free */ if (MemoryArea->Type != MEMORY_AREA_OWNED_BY_ARM3) { - if (MmLocateMemoryAreaByRegion(AddressSpace, - *BaseAddress, - tmpLength) != NULL) + if (!MmIsAddressRangeFree(AddressSpace, *BaseAddress, tmpLength)) { DPRINT("Memory area already occupied\n"); if (!(Type & MEMORY_AREA_STATIC)) ExFreePoolWithTag(MemoryArea, TAG_MAREA); diff --git a/ntoskrnl/mm/section.c b/ntoskrnl/mm/section.c index d570dbc3cec..9adcd6f3a86 100644 --- a/ntoskrnl/mm/section.c +++ b/ntoskrnl/mm/section.c @@ -4104,8 +4104,7 @@ MmMapViewOfSection(IN PVOID SectionObject, } /* Check there is enough space to map the section at that point. */ - if (MmLocateMemoryAreaByRegion(AddressSpace, (PVOID)ImageBase, - PAGE_ROUND_UP(ImageSize)) != NULL) + if (!MmIsAddressRangeFree(AddressSpace, (PVOID)ImageBase, PAGE_ROUND_UP(ImageSize))) { /* Fail if the user requested a fixed base address. */ if ((*BaseAddress) != NULL)