Solved

mmap gives segmentation fault

Posted on 2010-09-24
23
491 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:Kdo
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
 
LVL 45

Expert Comment

by:Kdo
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:
Kdo 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:Kdo
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 45

Expert Comment

by:Kdo
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:Kdo
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:Kdo
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:Kdo
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:Kdo
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now