Segmentation fault
This crash occurs when a program attempts to read or write to a memory location it does not have permission to access.
Usually happens because:
- ☑ Process read/write request to restricted physical memory addresses
- ☑ Null pointer dereference or stack recursion exhaustion
- ☑ Native compiled binary package incompatibility
🔍 Quick Checklist:
What is Segmentation fault?
A Segmentation Fault (SIGSEGV) is a signal sent by the Linux kernel to a running process when it detects an invalid memory access violation. This happens when a compiled application (C, C++, Rust, Go, or Node binary modules) attempts to access memory outside its allocated virtual memory space, dereferences a null pointer, performs a stack overflow, or attempts to write to read-only memory pages.
Common Causes
- Null pointer dereferencing: Attempting to read or write memory using a pointer that is null (0x0).
- Buffer overflow: Writing data beyond the bounds of an allocated array, corrupting the call stack memory.
- Stack overflow: Excessive recursion or massive local variables consuming all available process stack space.
| Cause | Frequency |
|---|---|
| Null pointer dereferences in native code | ⭐⭐⭐⭐⭐ |
| Out of bounds buffer array write access | ⭐⭐⭐⭐ |
| Incompatible precompiled binary dependencies | ⭐⭐⭐ |
Common Mistakes
- Assuming segmentation faults in Node.js or Python can be caught with try/catch blocks (native crashes terminate the entire virtual machine directly without raising runtime exceptions).
- Forgetting to supply debugging symbols (`-g` compiler flags) when compile-building binary files, making core dump backtraces unreadable.
How to Fix
Linux Operations & Verification
Configure the shell limits to write core dump logs on crash, then load GDB to locate the trace.
# 1. Enable core files generation
$ ulimit -c unlimited
# 2. Run binary to trigger crash and dump file
$ ./buggy_program
Segmentation fault (core dumped)
# 3. Open core file in GDB debugger
$ gdb ./buggy_program core
(gdb) bt
# Shows stack frame trace pointing to the crashing line of codePlatform Specific Fixes
Rebuild native C/C++ packages matching node runtime to fix V8 engine segfaults.
# Clean and rebuild native node-gyp packages
npm rebuildBest Practices
- Employ modern memory-safe programming languages (like Rust) that prevent memory layout pointer hazards at compile time.
- Perform boundary verification checks before writing user-supplied input data to buffer frames.
Frequently Asked Questions (FAQ)
Q: What is a segmentation fault?
It is a crash triggered when a program violates memory access permissions set by the hardware's Memory Management Unit (MMU) and OS kernel.
Q: How do I debug a segfault?
Enable core dumps: 'ulimit -c unlimited', reproduce the crash, and analyze the resulting core dump using a debugger like GDB: 'gdb <program> <core-file>'. Type 'bt' (backtrace) to see the crash line.
Q: Why does Node.js throw a segmentation fault?
Pure JavaScript cannot cause a segfault. Node.js segfaults are caused by bugs in native C++ addons (like bcrypt, sass, or database drivers) or the Node.js V8 engine itself.
Q: What is Valgrind?
Valgrind is a programming tool for memory debugging, memory leak detection, and profiling. Run: 'valgrind ./myprogram' to locate memory leaks.