This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/15560] New: munmap subregion of memory maped by mmap


http://sourceware.org/bugzilla/show_bug.cgi?id=15560

            Bug ID: 15560
           Summary: munmap subregion of memory maped by mmap
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: minor
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: zylcf818 at 163 dot com
                CC: drepper.fsp at gmail dot com

in rhel6.2, I mmap 3 pages, and munmap the middle page, but the behavior of
munmap() seems to be strange, below is code:
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <cstring>

int main(int argc, char *argv[])
{
    char* addr = (char*)mmap(NULL, 4096*3, PROT_READ|PROT_WRITE,
        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 
    if (addr == MAP_FAILED)
        printf("Failed to mmap\n");

    memcpy(addr, "abc", 3); 
    memcpy(addr+4096, "def", 3); 
    memcpy(addr+8192, "ghi", 3); 

//    printf("%10p\n", addr);
//    printf("%10p\n", addr+4096);
//    printf("%10p\n", addr+8192);

//    printf("%s\n", addr);
//    printf("%s\n", addr+4096);
//    printf("%s\n", addr+8192);

    int rs = munmap(addr+4096, 4096);
    if ( rs != 0 ) 
        printf("failed to munmap\n");

    printf("%s\n", addr);
    printf("%s\n", addr+4096);
    printf("%s\n", addr+8192);

    memcpy(addr, "123", 3); 
    memcpy(addr+4096, "456", 3); 
    memcpy(addr+8192, "789", 3); 

    printf("%s\n", addr);
    printf("%s\n", addr+4096);
    printf("%s\n", addr+8192);

    exit(EXIT_SUCCESS);
} /* main */

>g++ -g -o mmap mmap.cpp
>./mmap
abc
abc

ghi
123
123

789
if I comment off printf... in code, the result is what I expect:
0x7f3c4a274000
0x7f3c4a275000
0x7f3c4a276000
abc
def
ghi
abc
Segmentation fault (core dumped) <---  what I expect

-- 
You are receiving this mail because:
You are on the CC list for the bug.


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