delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2001/01/23/19:48:10

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sources DOT redhat DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sources DOT redhat DOT com>
List-Help: <mailto:cygwin-help AT sources DOT redhat DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT sources DOT redhat DOT com
Delivered-To: mailing list cygwin AT sources DOT redhat DOT com
X-Authentication-Warning: csd.cs.technion.ac.il: emild owned process doing -bs
Date: Wed, 24 Jan 2001 02:41:00 +0200 (IST)
From: Kohn Emil Dan <emild AT cs DOT Technion DOT AC DOT IL>
X-Sender: emild AT csd
To: cygwin AT cygwin DOT com
Subject: mmap problem in cygwin 1.1.7
Message-ID: <Pine.GSO.3.95-heb-2.07.1010124021946.21884A-100000@csd>
MIME-Version: 1.0


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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019