mmap failing with MAP_FIXED

Corinna Vinschen corinna-cygwin@cygwin.com
Mon Jul 4 08:19:43 GMT 2022


On Jul  1 17:33, David Allsopp wrote:
> This program fails at the second mmap call with EINVAL:
> 
>   #include <stdio.h>
>   #include <sys/mman.h>
>   #include <error.h>
> 
>   int main (void) {
>     void * mem;
>     /* Reserve 256MB address space for the minor heaps */
>     mem = mmap(0, 268439552, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>     if (mem == MAP_FAILED)
>       error(1, 0, "Reservation failed");
>     /* Commit the first 2MB heap */
>     if (mmap(mem, 2097152, PROT_READ | PROT_WRITE, MAP_PRIVATE |
> MAP_ANONYMOUS | MAP_FIXED, -1, 0) == MAP_FAILED)
>       error(1, 0, "Commit failed");
>   }
> 
> Is this something that's expected to fail for Cygwin, or a bug?

Depends on the point of view, I guess.  PROT_NONE pages are not just
reserved pages in the Windows sense.  mmap'ed pages are always commited
and PROT_NONE is just a page protection.  Using mmap to change the page
protection of already commited memory is not implemented.  To change
this, use mprotect.


Corinna


More information about the Cygwin mailing list