Solved

mmap gives segmentation fault - pls help

Posted on 2010-09-12
10
660 Views
Last Modified: 2012-05-10
hi,

i tried  a big file and the program gave segmentation fault but when i  tried with a smaller file, mmap works fine. i thought that with mmap, the system only loads the required page. so, it is not necessary to have enough memory on the server.

but now, it looks like when i give the length of the file to mmap, it maps that number of bytes. is this true ?

so, if the system doesn't have enough memory, there would be seg fault ??

thanks a million
0
Comment
Question by:zizi21
  • 4
  • 3
  • 3
10 Comments
 
LVL 3

Accepted Solution

by:
cconstable_work earned 300 total points
ID: 33659478
0
 

Author Comment

by:zizi21
ID: 33659512
thanks for the link but it is confusing. some of the expert advised that the whole file will be loaded and some advised that it will not be loaded.
0
 
LVL 3

Expert Comment

by:cconstable_work
ID: 33659763
A seg fault is not caused by the system not having enough memory.
A seg fault would be from something like trying to write to a location in memory that is not allowed (read only) or not initialized.
perhaps you are unmapping too early?

Can you show the code where you are setting the length?
0
 

Author Comment

by:zizi21
ID: 33659791
i used gdb and i am getting seg fault at memcpy. the code is long, so i am putting parts of it
while(1)
{
      memcpy(&x,&a[x],sizeof(unsigned int));
}

size = s.st_size;
        a = (unsigned int*) mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);

0
 
LVL 5

Expert Comment

by:shajithchandran
ID: 33659805
how is the while loop terminating..

i assume.. you map the file first and used it in the loop.

If the address where file is mapped is 'a' and the size mapped is size, then only memory from a to (a+size - 1)  is valid. If you cross that , you will get seg fault. Ofcourse, the end of the mapped segment might be rounded up to a page boundary, but from the programs logic, it shouldnt cross a+size -1 while reading from the mmapped region...

so put a terminating condition on the loop.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

by:cconstable_work
ID: 33659918
Ah I see you are trying to write to 0, this will cause a seq fault on certain operating systems that implement mmap_min_addr protection using LSM http://en.wikipedia.org/wiki/Linux_Security_Modules.
0
 
LVL 5

Expert Comment

by:shajithchandran
ID: 33659949
while(1)
{
      memcpy(&x,&a[x],sizeof(unsigned int));
}


also &a[x] dont look correct to me here.... if x is very large.. you will very easily get seg fault
0
 
LVL 5

Assisted Solution

by:shajithchandran
shajithchandran earned 200 total points
ID: 33659952
int pointer = 0;

size = s.st_size;
        a = (unsigned int*) mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);


while(pointer < size)
{
      memcpy(&x,&a[pointer],sizeof(unsigned int));
      pointer++;
      ... rest of the code....
}
0
 

Author Comment

by:zizi21
ID: 33660303
trying it  now..thakns a millon
0
 
LVL 5

Expert Comment

by:shajithchandran
ID: 33660342
Also put a check for the return value for mmap... make sure its not failing before proceeding further..
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use switch statements in the C programming language.

759 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

20 Experts available now in Live!

Get 1:1 Help Now