Segmentation fault (Operating Systems)

Segmentation fault  — software error that occurs when you try to access the inaccessible areas of memory for recording or when you try to change the memory forbidden way.

Memory segmentation is one of the approaches to the management and memory protection in the operating system. For most purposes it has been superseded paged memory, but the documentation for the tradition of using the term "segmentation fault". Some operating systems still use segmentation to some logical levels, and Page Memory is used as the main memory management policy.

Memory segmentation is one of the approaches to the management and memory protection in the operating system. For most purposes it has been superseded paged memory, but the documentation for the tradition of using the term "segmentation fault". Some operating systems still use segmentation to some logical levels, and Page Memory is used as the main memory management policy.

General concepts

'Memory segmentation' - a logic circuit in the computer memory addressing x86 architecture. Linear address specific memory cells, which in some modes of operation of the processor is the same as the physical address, is divided into two parts: the segment and offset. A segment is called conditionally selected area of ​​the address space of a certain size, and offset - the memory address with respect to the segment. It is a linear segment base address (an address relative to the total amount of memory) which indicates the beginning of a segment in the address space. The result is a segmented (logical) address that corresponds to the line segment base address + offset and which is exposed on the processor address bus.

'Error occurs segmentation' when a program tries to access a memory location to which it has access rights, or tries to access a memory cell in such a way that is not allowed conditions of the system.

The term "segmentation" is used in various fields of computing. In the context of the topic, the term used since 1950, and belongs to the address space of the program. Thus, when trying to read outside the address space of the program to record or fragment read-only address space segment, leads to segmentation errors, hence the name.

In systems using hardware memory segmentation tools for virtual memory, segmentation fault occurs when the system hardware detects an attempt to appeal to a nonexistent program segment, and is trying to refer to a place outside the boundaries of the segment, or to refer to the place for which it does not have permission.

In systems that use only paging invalid page usually leads to segmentation, segmentation fault or page fault, since the virtual memory system device algorithms.

'Page' - this is the area of the physical memory of a fixed size, and the smallest indivisible unit of memory, where the operating system can handle.

At the hardware level, the problem is caused by a reaction memory management unit (MMU) on illegal access to memory.

At the operating system level, this error is caught and the signal is transmitted to the unit «offending process», where the error is processed:

  • In UNIX - like operating system process, referring to invalid memory areas, it receives a signal «SIGSEGV».
  • In Microsoft Windows, the process of receiving access to invalid memory areas, creates an exception «STATUS_ACCESS_VIOLATION», and, as a rule, the application prompts you to start the debugger Dr. Watson, which shows the user prompted to submit a report on the Microsoft error.

To sum up, you can tell when a user process wants to access memory, he asks MMU redirect it. However, if the received address is erroneous, - located outside the physical segment or if the segment is not necessary rights (write attempt read only-segment), - the default OS sends SIGSEGV signal that leads to an interruption of a process and issue messages "segmentation fault ".

Reasons for segmentation

'The most common causes of segmentation faults:'

  • A null pointer dereference
  • An attempt to access a non-existent memory address (outside the process address space)
  • Trying to access the program memory, which are not allowed on this part of the memory
  • Attempt to write to the memory, a read-only

'The reasons are often caused by programming errors that lead to errors associated with memory' :

  • Create a pointer dereference operations or the establishment of an uninitialized pointer (a pointer that points to a random memory address)
  • Dereferencing or assignment of the liberated pointer (use ragged pointer that points to memory that has been released or reassigned).
  • Buffer overflow
  • Stack Overflow
  • An attempt to run a program that does not compile correctly. (Some compilers will output an executable file, despite the presence of errors at compile time.)

Example Segmentation Fault

Consider the following example code to ANSI C, which results in a segmentation fault due to the presence of const qualifier - type:

 const char * s = "hello world";
 * (Char *) s = 'H';
</ Syntaxhighlight>
When a program containing this code is compiled, the line «hello world» is available in the program section of a binary marked as "read only." When you run the operating system puts it with other strings and constants in the memory segment read-only. After starting the variable s points to the row address, and attempt to assign a value to a character constant H through memory variable results in a segmentation fault.

Compiling and running programs on the [[OpenBSD]] 4.0 causes the following runtime error:
<console> 
##bl##$##!bl## ##i##gcc segfault.c -g -o segfault
##bl##$##!bl## ##i##./segfault
##r## Segmentation fault
</console>

Conclusion debugger gdb:

<console>
 Program received signal SIGSEGV, Segmentation fault.
 0x1c0005c2 in main () at segfault.c:6
 6               *s = 'H';
</console>

In contrast to this, gcc 4.1.1 on GNU / Linux returns an error even at compile time:
<console>
 ##bl##$##!bl## ##i##gcc segfault.c -g -o segfault
 segfault.c: In function main:
 segfault.c:4: error: assignment of read-only location
</console>
The conditions under which violations occur segmentation, and how they depend on the existence of an operating system.
This sample code creates a null pointer and tries to assign a value to a non-existent address. This causes a segmentation fault during the execution of the program on many systems.
<syntaxhighlight lang="c">
 int* ptr = (int*)0;
 *ptr = 1;

Another way to cause a segmentation fault is to cause the main function recursively, which will lead to a stack overflow:

int main()
 {
    main();
 }

Typically, segmentation error occurs because a pointer or zero, or indicates a random memory (perhaps because it has not been initialized) or points to a remote memory location. For example:

 char* p1 = NULL;  /* initialized as zero, what it is not bad, but on many systems, it can not be dereferenced * /
  char * p2; / * Do not initialized * /
  char * p3 = (char *) malloc (20); / * Good chunk of memory allocated * /
  free (p3); / * But now it no longer exists * /

Now dereference any of these pointers can cause a segmentation fault. Also, when using arrays, if you accidentally specify an uninitialized variable as the array size. Here is an example:

 int main()
 { 
     int const nmax=10;
     int i,n,a[n];
 }

This error can not be traced G ++ when linking, but when you run the application will cause a segmentation fault.

Video Example Segmentation Fault With the example:

Sources