mmap failing with MAP_FIXED

David Allsopp
Fri Jul 1 16:33:31 GMT 2022

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 |
      error(1, 0, "Commit failed");

Is this something that's expected to fail for Cygwin, or a bug? The example
is extracted from OCaml 5.0's runtime, which reserves an area of address
space and then commits chunks of it as required. The above snippet comes
from the Linux side, on Windows we're using VirtualAlloc with PAGE_NOACCESS
to reserve the address space and then VirtualAlloc with MEM_COMMIT and
PAGE_READWRITE to commit smaller portions of it.

Is there a way to do that with Cygwin's mmap?



More information about the Cygwin mailing list