Signal 11, also known as SIGSEGV (signal segmentation violation) is an error signal in Unix systems (Linux). The error appears when a program or code tries to access an invalid memory location. The operating system sends the signal 11 error to the program as a response.
This article provides an in-depth explanation of the signal 11 SIGSEGV error.
- Access to the command line/terminal.
- A text editor, such as Vim or nano.
- Administrator permissions to check kernel logs.
What Is SIGSEGV?
The SIGSEGV error is an error code sent to a process trying to access an inaccessible memory location. The attempt to read or write to the memory location leads to a segmentation error.
The signal appears when attempting to run code written in languages such as C and C++. The error code is serious and leads to potential problems with data corruption, crashes, and other unpredictable behaviors.
What Causes SIGSEGV?
The SIGSEGV error happens for several reasons. Some causes for the error are:
- Dereferencing null pointers - A null pointer is a reserved value that does not point to any object. As a result, the null pointer does not point to any valid memory location. Attempting to access a memory location through a null pointer leads to a SIGSEGV error.
- Buffer overflows - Buffers have a fixed memory size. Writing data that does not fit into a buffer leads to overflowing to adjacent memory locations.
- Invalid permissions - Some memory locations are read-only and reserved for operating system resources. The SIGSEGV error happens when attempting to write data to a read-only memory location leads.
- Freed pointers - If a program references a pointer that is freed, the OS returns a SIGSEGV error.
- Stack overflow - When a program runs out of memory in the call stack and does not have enough allocated memory to run, it leads to a SIGSEGV error.
In all cases, the error appears when attempting an invalid memory location access. The example below shows how a SIGSEGV error happens when referencing a null pointer:
2. Add the following code to the file:
int *ptr = NULL;
The code creates a null pointer and tries to read the memory address from the pointer with the
3. Save the file and close Vim.
4. Compile the code with:
gcc -g null_pointer.c -o null_pointer
The compiler does not detect the error and compiles the code correctly.
5. Run the code to see the result:
The output shows a segmentation error because the code attempts to read an invalid memory location.
How To Solve SIGSEGV Errors?
Solving SIGSEGV errors requires pinpointing the source of the error and fixing the problem. Check if the program is trying to perform multiple actions on the same memory location, if all loops end, and if there are uninitialized variables.
Analyze all program conditions and inputs to verify there is no combination that produces the error. Since the issue is not straightforward, below are several ways to detect and approach a signal 11 SIGSEGV error.
The best approach is to debug the code line by line to locate the line that causes the error. Set up a debugger to generate core files and analyze the files using the GNU debugger (gdb). Alternatively, use a specialized tool such as Valgrind to debug memory issues.
Another method is to print out all variables to check if all of them contain an expected value.
A simple programming mistake often leads to referencing invalid memory locations.
-Wextra tags when compiling the code through GCC to enable detecting construction warnings. The warnings help detect various issues, including the use of uninitialized variables and statements with no effect.
Although the warnings will not detect a SIGSEGV error directly, the descriptive error output can provide a hint to the location of statements that lead to the segmentation error.
Check Kernel Logs
A segmentation error always creates a kernel log message. To access the kernel logs, run the following command:
sudo tail -f /var/log/syslog
The command outputs a log stream in the terminal. Reproduce the error to see the message in the logs. The target message looks like the following:
Feb 28 12:41:53 phoenixNAP kernel: [ 3363.006471] null_pointer: segfault at 0 ip 0000556991208161 sp 00007ffe6af914d0 error 4 in null_pointer[556991208000+1000]
The log contains the following information:
at <address>is the memory location the code tried to access.
ip <pointer>is the memory address of the code where the error happened.
sp <pointer>is the stack pointer that has the last program request.
error <code>shows the attempted operation, where the most common codes are:
4reading from an unallocated location.
5reading from a write-only location.
6writing to an unallocated location.
7writing to a location with no write access.
From the provided example, the error gives a clue that the code attempted to read data from an unallocated location.
SIGSEGV in Kubernetes
The SIGSEGV error is a common reason for container termination in Kubernetes. If a container terminates due to a segmentation error, the system returns an error code 139.
There are several reasons why the error appears in Kubernetes:
- Coding error - A coding problem can cause a SIGSEGV error and make a process fail to initialize, or cause a process to access invalid memory.
- Incompatibility error - When an existing binary file is incompatible with a shared library, the incompatibility can cause the binary file to access an invalid memory location.
- Hardware misconfiguration - If the error appears across different libraries, the memory repositories are incorrectly allocated or the hardware settings are not set up correctly.
To resolve the issue, do the following:
- Check the container logs - Use the container logs to try and identify the steps which lead to the error.
- Revert most recent changes - If the issue comes from a recently updated shared library, the error is most likely from incompatibility issues with existing binaries.
- Check Linux kernel logs - Linux kernel logs provide an insight into what command leads to the error, and what the code attempted to execute. Accessing the logs requires root access to the host machine and cannot be viewed from the container directly.
SIGSEGV vs. Other Exit Codes
SIGSEGV signal 11 is one of the many Linux error code signals. Different signal codes help differentiate between the various types of system interruptions. Below is a brief comparison between the SIGSEGV exit code and some other possible codes when accessing memory.
SIGSEGV vs. SIGBUS
The main difference between the SIGSEGV and SIGBUS exit codes is:
- SIGSEGV indicates an invalid access attempt to a valid physical address.
- SIGBUS (signal 10) indicates an invalid physical address.
The SIGBUS (signal bus) exit code is a memory access error that happens when a CPU cannot address a physical memory. The address is either unrecognized or not bit aligned in a way the CPU can read the address.
A SIGSEGV error, the physical address is valid, but the access method is not.
SIGSEGV vs. SIGABRT
The primary difference between the SIGSEGV and SIGABRT error codes is:
- SIGSEGV is an error triggered by the operating system.
- SIGABRT is an error triggered by a process.
The SIGABRT (signal abort) is a signal triggered by a system process to abruptly terminate itself. The error happens when a process abnormally terminates.
A SIGSEGV error happens when the operating system detects invalid memory access and terminates a process as a result.
After reading this guide, you know what the SIGSEGV error is and how to handle the exit code. The signal 11 error is serious and should be handled immediately.
Next, learn how to use the trap command to catch signals and system interruptions.