From cf34e41adb0815e37fcf8cae08c85301eecc93c9 Mon Sep 17 00:00:00 2001 From: Nuno Marques Date: Thu, 7 May 2026 18:04:49 -0700 Subject: [PATCH] fix(windows-shim): reject mmap MAP_FIXED with ENOTSUP The Windows mmap shim cannot honour MAP_FIXED safely: VirtualAlloc / MapViewOfFile cannot replace existing mappings at a caller-supplied address, so accepting the flag would silently fall through to a non-fixed allocation and the caller would get back an address that does not match the one it asked for. The shim header documents this contract; the implementation now matches it by returning MAP_FAILED with errno set to ENOTSUP when MAP_FIXED is requested. Fixes the MAP_FIXED expectation in the mmap unit tests, which now get the expected MAP_FAILED return instead of a surprise fallback mapping at a different address. Signed-off-by: Nuno Marques --- platforms/posix/src/px4/windows/posix/fs/mman.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/platforms/posix/src/px4/windows/posix/fs/mman.cpp b/platforms/posix/src/px4/windows/posix/fs/mman.cpp index c817f7aa97..38d083996f 100644 --- a/platforms/posix/src/px4/windows/posix/fs/mman.cpp +++ b/platforms/posix/src/px4/windows/posix/fs/mman.cpp @@ -60,6 +60,14 @@ static DWORD mman_page_protect(int prot) extern "C" void *mmap(void *, size_t length, int prot, int flags, int fd, off_t offset) { + /* MAP_FIXED is rejected because the Windows backend (VirtualAlloc / + * MapViewOfFile) cannot safely replace existing mappings at a caller + * supplied address. The shim header documents this contract. */ + if (flags & MAP_FIXED) { + errno = ENOTSUP; + return MAP_FAILED; + } + const bool anon = (flags & (MAP_ANON | MAP_ANONYMOUS)) != 0; if (anon || fd < 0) {