Why can't your C/C ++ program run? Segmentation fault

Source: Internet
Author: User

Why can't your C/C ++ program run? Segmentation fault
 

 

Tracing segment Error

If you think you have understood the root cause of a segment error and know how to prevent it, you can do so. Otherwise, the following content may be helpful to you.

Malloc

We began to allocate memory for the address pointed to by the pointer:
Vcc8c3Ryb25nPrbOtO3O8zwvc3Ryb25nPrXEx7DPpjo8L3A + DQo8cD48aW1nIGFsdD0 = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/150504/045T33G3-1.png" title = "\"/>

The figure above tells us some information:

The content outlined in the red line is:
Slave Functionfunc1Get the pointer from the local variableDest (char ** type)The address pointed to. The correct address in the Code is0x7fffffffddc0In the error code, the address is0x0.

Next, we will outline the content outlined in the yellow line.

/* Note that the value of rax here is 0x0 in the error code, and that of rdx is the memory address 0x602010 */mov qword ptr [rax], who is written in malloc.

When executing the above Code, the wrong code tries to put the memory address allocated by malloc on the heap as a value as a pointer.dest.

Address: 0x0 value: 0x602010

As mentioned in the previous article,0x0Is an inaccessible addressIt is impossible to complete the assignment operation. So it will appearSegment Error:

What is a segment error?

For many people. The above analysis is more in-depth than the first one, and every step before the truth can be seen.

Now let's see what a segment error is.

It is the description of a process address space. This is an old figure. It is everywhere on the Internet, but it can be used to understand VMA:

What will the above picture tell us?

You cannot access the virtual memory space of the kernel.
User stack, the user process starts to have such a structure, you exceed its upper bound to the kernel virtual memory space, there will be a segment error.
Memory ing to the mmp region.
Heap, malloc, and calloc will find the Address Allocation here (in fact not only that)
Code and data segments include global variables, static variables, code, and data.

If you access the kernel virtual memory space (that is, the space larger than ebp, 1 won't work), code segments, data segments will cause segment errors.

In the previous example, yesSegment error caused by accessing the reserved area circled in red in the figure.

Add x86_64 VMA-layout:


.

Locate the segment error in the code

There are many methods. I only have one. After all, I do not write C/C ++, and I am not a veteran in this field.
Use coredump + gdb.
How to Get coredump:

1. In the shell, run 'ulimit-c unlimited'. 2. Run the program with a segment error.

With coredump, you can use gdb to decompress it.gdb xx xxcoredump.

There are several information:

1. What is signal 11?

2. Segmentation fualt segment Error
3.stack2.cThe error occurs in row 8th.
4. Observe carefully and you will see the Functionfunc1Parametersdest=0x0.

I think the above information is sufficient for such a simple c program.

Others

Maybe I want to see mmap, mm fault Processing, page Exception Processing, and some signal stuff later.
However, this article ends.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.