Mail Archives: cygwin/1998/08/06/11:20:51
Hi,
I came across what I think is a bug while porting some stuff that uses
mmap() in conjunction with
a fd opened to /dev/zero to blank out the mapped memory. The first call to
mmap works ok, but when I try to remap it either gets mapped to the wrong
location, or bombs out with a permission denied or invalid argument error.
I'm emulating /dev/zero with a zero length file for the moment, however as
both mmap calls use the same file descriptor this shouldn't be an issue,
right....?
Here's some sample code to illustrate what I mean...
-------------------------
/*
Test mmap implementation.
This is a very simple test which asks for an area of memory
mapped to /dev/zero, then explicitly remaps it to same, both
operations via mmap.
The net effect is no change, but in principle the second
call would be able to arbitrarily change page locations about
within the virtual space returned by the first call.
*/
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
#define MEMSIZE 0x200000
int main(int argc, char**argv)
{
int fd;
caddr_t vaddr,newaddr;
/* open /dev/zero for zero'd memory */
fd = open("/dev/zero",O_RDWR);
if( fd == -1 )
{
perror("open");
exit(errno);
}
/* mmap a couple meg */
vaddr = mmap( 0,
MEMSIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED,
fd,
0);
if( (int)vaddr == -1 )
{
perror("mmap#1");
exit(errno);
}
/* and try and remap it (we don't change the mapping,
but the principle should be the same) */
newaddr = mmap( vaddr,
MEMSIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED,
fd,
0);
if( (int)vaddr == -1 )
{
perror("mmap#2");
exit(errno);
}
if( newaddr != vaddr )
{
printf("bizarre - remapped to a new location %x -> %x\n",
vaddr,newaddr);
exit(-1);
}
printf("Success!\nAllocated 0x%x bytes at 0x%x,",
MEMSIZE,vaddr);
printf("then remapped them to same location\n");
exit(0);
---------------------------
Regards,
Brendan Alford
}
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".
- Raw text -