Solved

mmap gives segmentation fault

Posted on 2010-09-24
23
513 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ho to creat split-view application in iPad that display PDF file?? 3 153
Estimate porting issues. 9 119
Inorder binary search tree 5 163
C++ vs C compilers 13 159
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

776 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