mmap and MAP_FIXED

Evgeny Stambulchik evgeny@stambulchik.net
Fri Feb 25 08:12:00 GMT 2005


Hello,

There is something strange about mmap(addr, ..., MAP_FIXED) under 
Cygwin. For a reason, it always fails if addr is not on a 16xPAGE_SIZE 
boundary. Here is a short demo:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>

#define NRUNS                 20
#define CHUNK_SIZE          4096
#define START_ADDR      0x640000

int main(void)
{
     unsigned int i, pagesize, prot, flags;
     void *addr, *maddr;

     pagesize = getpagesize();
     prot = PROT_READ | PROT_WRITE | PROT_EXEC;
     flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;

     for (i = 0; i < NRUNS; i++) {
         addr = (void *) START_ADDR + pagesize*i;
         maddr = mmap(addr, CHUNK_SIZE, prot, flags, 0, 0);
         if (maddr != MAP_FAILED) {
             fprintf(stderr, "OK:   %p -> %p\n", addr, maddr);
         } else {
             fprintf(stderr, "FAIL: %p (%s)\n", addr, strerror(errno));
         }
     }
     exit(0);
}

And the output is:

OK:   0x640000 -> 0x640000
FAIL: 0x641000 (Permission denied)
FAIL: 0x642000 (Permission denied)
FAIL: 0x643000 (Permission denied)
FAIL: 0x644000 (Permission denied)
FAIL: 0x645000 (Permission denied)
FAIL: 0x646000 (Permission denied)
FAIL: 0x647000 (Permission denied)
FAIL: 0x648000 (Permission denied)
FAIL: 0x649000 (Permission denied)
FAIL: 0x64a000 (Permission denied)
FAIL: 0x64b000 (Permission denied)
FAIL: 0x64c000 (Permission denied)
FAIL: 0x64d000 (Permission denied)
FAIL: 0x64e000 (Permission denied)
FAIL: 0x64f000 (Permission denied)
OK:   0x650000 -> 0x650000
FAIL: 0x651000 (Permission denied)
FAIL: 0x652000 (Permission denied)
FAIL: 0x653000 (Permission denied)

On the other hand, if 'MAP_FIXED' is removed, mmap() happily uses the 
address hint in all cases:

OK:   0x640000 -> 0x640000
OK:   0x641000 -> 0x641000
OK:   0x642000 -> 0x642000
OK:   0x643000 -> 0x643000
OK:   0x644000 -> 0x644000
OK:   0x645000 -> 0x645000
OK:   0x646000 -> 0x646000
OK:   0x647000 -> 0x647000
OK:   0x648000 -> 0x648000
OK:   0x649000 -> 0x649000
OK:   0x64a000 -> 0x64a000
OK:   0x64b000 -> 0x64b000
OK:   0x64c000 -> 0x64c000
OK:   0x64d000 -> 0x64d000
OK:   0x64e000 -> 0x64e000
OK:   0x64f000 -> 0x64f000
OK:   0x650000 -> 0x650000
OK:   0x651000 -> 0x651000
OK:   0x652000 -> 0x652000
OK:   0x653000 -> 0x653000

Any insight?

Regards,

Evgeny

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list