This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug manual/6887] mmap documentation bugs, perhaps also 2 functional bugs
- From: "siward at wanadoo dot nl" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sources dot redhat dot com
- Date: 15 Sep 2008 08:35:45 -0000
- Subject: [Bug manual/6887] mmap documentation bugs, perhaps also 2 functional bugs
- References: <20080915082614.6887.siward@wanadoo.nl>
- Reply-to: sourceware-bugzilla at sourceware dot org
------- Additional Comments From siward at wanadoo dot nl 2008-09-15 08:35 -------
In case it is helpfull, here is code i used for testing :
(macros ex(), esx(), and t() are simple error-reporting thingies)
/* test files :
/tmp/test : permissions 0770 : content "#!/bin/bash\necho hello\nexit\n"
/tmp/empty : permissions 0770 : no content
/tmp/small : permissions 0770 : initially 10 bytes random content
*/
/* compile with :
cd /my/test/c
cc mmap2.c -o mmap2 -I /my/c/include -W -Wall
*/
#include "defines.c"
#include <stdio.h>
#include <sys/mman.h> /* mmap() */
#include <unistd.h> /* sysconf() */
#include <fcntl.h> /* open() */
#include <sys/stat.h> /* stat() */
#include <errno.h> /* ENOENT */
#include <string.h> /* strcpy() */
#include "errordef.c"
char* filename ;
int ck ;
struct stat stats ;
size_t filesize ; /* unsigned ! */
int fd ; /* file descriptor */
size_t pagesize ; /* unsigned ! */
void* adres ;
size_t mappedsize ; /* unsigned ! */
void TestMmapFile( void ){
char* pc ;
filename = "/tmp/test" ;
ck = stat( filename, &stats );
if( ck ){ esx("stat nok"); }
filesize = stats.st_size ;
t("filesize = %d", filesize );
fd = open( filename, O_RDWR );
/* bug: O_READ not found, though fcntl.h is included */
if( fd < 0 ){ esx("open nok"); }
t("open ok");
pagesize = sysconf( _SC_PAGESIZE ) ;
if( pagesize <= 0 ){ esx("sysconf nok"); }
t("pagesize = %d", pagesize );
if( pagesize < filesize ){ ex("pagesize is smaller than filesize"); }
adres = mmap( NULL, pagesize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
/* what if mmap permissions don't match permissions with which file was opened
? */
/* open rw , mmap rw , action w -> ok */
/* open rw , mmap r , action w -> segfault */ /* pebcak */
/* open rw , mmap w , action w -> segfault */ /* BUG ! */
/* open r , mmap r , action w -> segfault */ /* pebcak */
/* open r , mmap w -> permission denied */
/* open w , mmap r -> permission denied */
/* open w , mmap w -> permission denied */ /* doc bug */
if( adres == MAP_FAILED ){ esx("mmap nok"); }
t("mmap ok");
mappedsize = pagesize ;
pc = (char*) adres ; pc[21]++ ; /* changes 'echo hello' to 'echo hellp' */
t("change content ok");
ck = munmap( adres, mappedsize );
/* what if i munmap filesize ? -> ok */
if( ck < 0 ){ esx("munmap nok"); }
t("munmap ok");
ck = close( fd );
if( ck < 0 ){ esx("close nok"); }
t("close ok");
}
void TestMmapEmptyfile( void ){
char c ;
filename = "/tmp/empty" ;
ck = stat( filename, &stats );
if( ck ){ esx("stat nok"); }
filesize = stats.st_size ;
t("filesize = %d", filesize );
fd = open( filename, O_RDWR );
/* bug: O_READ not found, though fcntl.h is included */
if( fd < 0 ){ esx("open nok"); }
t("open ok");
pagesize = sysconf( _SC_PAGESIZE ) ;
if( pagesize <= 0 ){ esx("sysconf nok"); }
t("pagesize = %d", pagesize );
if( pagesize < filesize ){ ex("pagesize is smaller than filesize"); }
adres = mmap( NULL, pagesize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
if( adres == MAP_FAILED ){ esx("mmap nok"); }
t("mmap ok ; adres = %p", adres );
mappedsize = 0 ;
c = *((char*) adres) ;
t("read content ok");
}
void TestMmapResize( void ){
size_t newsize ;
int i ;
int imax ;
char* pc ;
filename = "/tmp/small" ;
ck = stat( filename, &stats );
if( ck ){ esx("stat nok"); }
filesize = stats.st_size ;
t("filesize = %d", filesize );
fd = open( filename, O_RDWR );
if( fd < 0 ){ esx("open nok"); }
t("open ok");
pagesize = sysconf( _SC_PAGESIZE ) ;
if( pagesize <= 0 ){ esx("sysconf nok"); }
t("pagesize = %d", pagesize );
adres = mmap( NULL, pagesize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
if( adres == MAP_FAILED ){ esx("mmap nok"); }
t("mmap ok ; adres = %p", adres );
mappedsize = pagesize ;
newsize = 2 * pagesize ;
ck = ftruncate( fd, newsize );
if( ck < 0 ){ esx("ftruncate nok"); }
t("ftruncate ok");
adres = mremap( adres, mappedsize, newsize, MREMAP_MAYMOVE );
t("mremap ok ; new adres = %p", adres );
mappedsize = newsize ;
imax = pagesize + pagesize / 2 ;
pc = (char*) adres ;
for( i=0 ; i<imax ; i++ ){ pc[i] = 'a' ; }
t("write ok ; imax = %d", imax );
ck = ftruncate( fd, imax );
if( ck < 0 ){ esx("ftruncate nok"); }
t("ftruncate ok");
ck = msync( adres, imax, 0 );
/* tested with all 3 possible values of flags ; all work ok */
if( ck < 0 ){ esx("msync nok"); }
t("msync ok");
ck = munmap( adres, mappedsize );
if( ck < 0 ){ esx("munmap nok"); }
t("munmap ok");
ck = close( fd );
if( ck < 0 ){ esx("close nok"); }
t("close ok");
}
void TestMmapAnon( void ){
int i ;
int* pi ;
pagesize = sysconf( _SC_PAGESIZE ) ;
if( pagesize <= 0 ){ esx("sysconf nok"); }
t("pagesize = %d", pagesize );
adres = mmap( NULL, pagesize, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0 );
/* with flags MAP_PRIVATE | MAP_ANON : ok */
/* with flags MAP_SHARED | MAP_ANON : ok */
/* with flags MAP_ANON : nok */
if( adres == MAP_FAILED ){ esx("mmap nok"); }
t("mmap ok ; adres = %p", adres );
mappedsize = pagesize ;
pi = (int*) adres ;
for( i=0 ; i < 100 ; i++ ){ pi[i] = i ; }
t("assign ok");
for( i=0 ; i < 100 ; i++ ){ if( pi[i] != i ){ ex("pi[%d] = %d",i,pi[i] ); } }
t("readback ok");
}
int main( void ){
TestMmapAnon();
return(0);
}
--
http://sourceware.org/bugzilla/show_bug.cgi?id=6887
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.