Solved

mmap gives segmentation fault

Posted on 2010-09-24
23
524 Views
Last Modified: 2012-05-10
hi,

i have a file that i am trying to mmap.
and i get the error in valgrind

 Invalid read of size 4
==31586==    at 0x8048F06: filec(char*) (test.c:65)
==31586==    by 0x80489D4: main (c1.c:33)
==31586==  Address 0xFFFFFFFF is not stack'd, malloc'd or (recently) free'd
==31586==
==31586== Process terminating with default action of signal 11 (SIGSEGV)
==31586==  General Protection Fault

the reason is because , i am trying to access a value on of the mapped file. it works fine if the file is so much smaller but it shoudl work with large files too..thanks.

0
Comment
Question by:zizi21
  • 13
  • 9
23 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 33753215
Please post the code that goes with this.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33753270
Hi zizi,

The error message indicates that you're trying to read address 0xFFFFFFFF.  Coincidentally, that is -1, the value returned my mmap when the mapping is unsuccessful.

After calling mmap, you should check that the mapping is successful.  ERRNO should contain the reason that the mapping failed.


Good Luck,
Kent
0
 

Author Comment

by:zizi21
ID: 33753303
     this is the code

        struct stat sts;
        fp = open(f,0|O_RDWR);
        if(fp == -1)
        {
                fprintf(stderr," No open.");
                exit(1);
        }

        if(fstat (fp, &st) == -1)
        {
                fprintf(stderr, " No status.");
                exit(1);
        }
       
        s = sts.st_size;
        x = (unsigned int*) mmap(NULL, s, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);
        if(x == 0)
        {
                fprintf(stderr," No map");
                exit(1);
        }
       
        j = s/sizeof(unsigned int);
       
        printf("x[0] %d\n",x[0]);

this is the code
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33753387
I think that you want to test:

        x = (unsigned int*) mmap(NULL, s, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);
        if(x == -1)
        {
                fprintf(stderr," No map");
                exit(1);
        }
0
 

Author Comment

by:zizi21
ID: 33753503
i tried testing that but i got this error

 error: ISO C++ forbids comparison between pointer and integer

and when i did
*x == -1,

i got segmentation fault.
0
 

Author Comment

by:zizi21
ID: 33753510
how do i check errno to find the reason ?
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 33753564
You need to test x, not the dereferenced pointer.

        x = (unsigned int*) mmap(NULL, s, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);
        if((int)x == -1)
        {
                fprintf(stderr," No map");
                exit(1);
        }


Sorry -- I missed the casting the first time around.


Kent
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33753574

in the globals block (outside of any function) declare errno.

  extern int errno.


Then print the value of errno if the mapping fails.  The reason codes are in errno.h, which should be in your standard includes directory.


Kent
0
 

Author Comment

by:zizi21
ID: 33753590
i checked the errno and it says errno 12 Cannot allocate memory

0
 

Author Comment

by:zizi21
ID: 33753611
the mapping failed...how is this possible,...i mean,  i though with mmap , that i could mmap file bigger than the existing ram...
0
 

Author Comment

by:zizi21
ID: 33753646
thanks for all the help.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33753653
Is there enough swap space for it?

How big is the file?  It looks like you're trying to map all of it, so it might not hurt to print the key values (like length) as you debug this.


Kent

0
 

Author Comment

by:zizi21
ID: 33753743
when i tried printing the value for sts.size, i got a negative value.

but when i did ls -lh, the size of the file 3.2 gb
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33753821

Ok.  When you store 3.2GB in an integer, all 32 bits are used.  This overflows into the sign bit so the value that you see printed is negative.

Display the value as an unsigned integer and it should display just fine.

You may be trying to allocate more virtual memory than you have defined.  


Kent
0
 

Author Comment

by:zizi21
ID: 33753860
i tried using %ld and i still got negative number
0
 

Author Comment

by:zizi21
ID: 33753874
i get the value of the size from struct stat sts.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33753877

%ld formats a long integer.  use %u.

0
 

Author Comment

by:zizi21
ID: 33753894
also, the size of the file is larger than the existing ram.
0
 

Author Comment

by:zizi21
ID: 33753921
yes, i got a positive number now with %u
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33755534

How large is the swap file?
0
 

Author Comment

by:zizi21
ID: 33759113
its 1 gb
0
 

Author Comment

by:zizi21
ID: 33759114
thanks for all the help.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33760857
Hi zizi,

Did you get this resolved?

The swap file should probably be at least as large as RAM.


Kent
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question