What Is Signal 11 SIGSEGV Error?

February 28, 2023

Introduction

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.

What is Signal 11  SIGSEGV Error?

Prerequisites

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

1. Create a file and open it with a code editor. For example, if using Vim, run:

vim null_pointer.c

2. Add the following code to the file:

#include <stddef.h>
#include <stdio.h>

int main(){
	int *ptr = NULL;
	printf("%d", *ptr);
}

The code creates a null pointer and tries to read the memory address from the pointer with the printf .

3. Save the file and close Vim.

4. Compile the code with:

gcc -g null_pointer.c -o null_pointer
Compiled code null_pointer terminal output

The compiler does not detect the error and compiles the code correctly.

5. Run the code to see the result:

./null_pointer
Segmentation fault error terminal output

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.

Debug Code

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.

gdb SIGSEGV error output

Another method is to print out all variables to check if all of them contain an expected value.

null pointer memory address print gdb

A simple programming mistake often leads to referencing invalid memory locations.

Enable Warnings

Use the -Wall and -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.

gcc -Wall uninitialized variable error terminal output

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[3698]: segfault at 0 ip 0000556991208161 sp 00007ffe6af914d0 error 4 in null_pointer[556991208000+1000]
syslog segfault terminal output

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:
    • 4 reading from an unallocated location.
    • 5 reading from a write-only location.
    • 6 writing to an unallocated location.
    • 7 writing 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.

Conclusion

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.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
Bash trap Command Explained
December 16, 2021

A Bash shell script can run into problems during its execution, resulting in an error signal that interrupts the script...
Read more
What Is SELinux (Security-Enhanced Linux)?
January 12, 2023

Security-Enhanced Linux (SELinux) is a Mandatory Access Control (MAC) system built into...
Read more
Journalctl: How to Read and Edit Systemd Logs
February 28, 2023

Systemd is a service and system manager that logs data. The journalctl command helps access...
Read more
16 Best Syslog Servers for Linux and Windows
March 31, 2022

A syslog server gathers and organizes log messages from various devices on the network...
Read more