This is the mail archive of the cygwin@sources.redhat.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

mmap problem in cygwin 1.1.7




Hi all,

It seems to me that there is a problem with the current implementation of
mmap() in the 1.1.7 release of the cygwin dll.

Whenever a non-zero offset is passed to mmap(), it will fail with EACCESS.

Example:

#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE       65536
#endif



int main()
{
   int fd;
   void *ptr;
   int rc;
   char c = 'a';
   off_t new_off;

   fd = open("crap",O_RDWR|O_CREAT|O_BINARY);

   if (fd < 0) {
      perror("open");
      goto bad_open;
   }

   new_off = lseek(fd,2*PAGE_SIZE,SEEK_CUR);

   if (new_off == (off_t)-1) {
      perror("lseek");
      goto bad_lseek;
   }


   rc = write(fd,&c,1);
   if (rc <= 0) {
      perror("write");
      goto bad_write;
   }


   ptr = mmap(NULL,PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,fd,PAGE_SIZE);

   if (ptr == MAP_FAILED) {
      perror("mmap");
      goto bad_mmap;
   }

   printf("mmap succeeded: ptr=%p\n",ptr);


   rc = munmap(ptr,PAGE_SIZE);
   rc = munmap(ptr,PAGE_SIZE);

   if (rc < 0) {
      perror("munmap2");
      goto bad_munmap;
   }


   close(fd);
   printf("OK till now\n");

   return 0;

bad_mmap:
bad_write:
bad_lseek:
bad_munmap:
   close(fd);
bad_open:
   return 1;
}

the program will run OK if the last parameter is set to 0.

I have traced the problem to the implementation of the mmap()

file: mmap.cc
line:451

 HANDLE h = CreateFileMapping (get_handle(), &sec_none, protect, 0, len,NULL);

The problem is that the file mapping object has *exactly* the size len.
Now if an attempt is made to map the view at a non-zero offset, this will
cause the mapped area to exceed the size of the file mapping object and
Windows (NT 4.0 at least) will return ACCESS denied. 

The fix should be easy. Replace the len parameter passed to
CreatefileMapping with 0. This will cause the entire file to be mapped.


							Regards,

								Emil


--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]