From df37488ebdeea0d9719084a6e365467e3b57fe9f Mon Sep 17 00:00:00 2001 From: tkchia Date: Thu, 6 Oct 2022 10:31:04 +0000 Subject: [PATCH] [metal] Fix sys_mmap_metal() issues for non-MAP_FIXED case - correctly check that virtual region we want to use is unmapped, rather than accidentally clobbering existing pages - avoid placing mmap'd memory at null virtual address --- libc/intrin/directmap-metal.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libc/intrin/directmap-metal.c b/libc/intrin/directmap-metal.c index d7d0550e4..4616340ff 100644 --- a/libc/intrin/directmap-metal.c +++ b/libc/intrin/directmap-metal.c @@ -39,8 +39,10 @@ noasan struct DirectMap sys_mmap_metal(void *paddr, size_t size, int prot, size = ROUNDUP(size, 4096); addr = (uint64_t)paddr; if (!(flags & MAP_FIXED)) { + if (!addr) + addr = 4096; for (i = 0; i < size; i += 4096) { - pte = __get_virtual(mm, pml4t, addr, false); + pte = __get_virtual(mm, pml4t, addr + i, false); if (pte && (*pte & PAGE_V)) { addr = MAX(addr, sys_mmap_metal_break) + i + 4096; i = 0;