public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* mmap failing with MAP_FIXED
@ 2022-07-01 16:33 David Allsopp
  2022-07-01 16:45 ` gs-cygwin.com
  2022-07-04  8:19 ` Corinna Vinschen
  0 siblings, 2 replies; 3+ messages in thread
From: David Allsopp @ 2022-07-01 16:33 UTC (permalink / raw)
  To: cygwin

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? 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?

Thanks,


David


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: mmap failing with MAP_FIXED
  2022-07-01 16:33 mmap failing with MAP_FIXED David Allsopp
@ 2022-07-01 16:45 ` gs-cygwin.com
  2022-07-04  8:19 ` Corinna Vinschen
  1 sibling, 0 replies; 3+ messages in thread
From: gs-cygwin.com @ 2022-07-01 16:45 UTC (permalink / raw)
  To: David Allsopp; +Cc: cygwin

On Fri, Jul 01, 2022 at 05:33:31PM +0100, 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? 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?

Have you tried 'msync()' with MS_ASYNC flag as an alternative approach?

Cheers, Glenn

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: mmap failing with MAP_FIXED
  2022-07-01 16:33 mmap failing with MAP_FIXED David Allsopp
  2022-07-01 16:45 ` gs-cygwin.com
@ 2022-07-04  8:19 ` Corinna Vinschen
  1 sibling, 0 replies; 3+ messages in thread
From: Corinna Vinschen @ 2022-07-04  8:19 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-07-04  8:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-01 16:33 mmap failing with MAP_FIXED David Allsopp
2022-07-01 16:45 ` gs-cygwin.com
2022-07-04  8:19 ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).