What a Bus Error Means
Bus Error (signal SIGBUS) is an error sent by the system to a process when it attempts invalid access to physical memory. Unlike a Segmentation Fault (SIGSEGV), which relates to virtual addressing, SIGBUS typically indicates:
- Alignment violations (e.g., attempting to read a 4-byte value from a misaligned address on some architectures, such as SPARC or older ARM).
- Hardware failures — issues with RAM, the bus, or the processor.
- Data corruption during I/O (e.g., an error reading from a disk into a memory-mapped file).
- Errors in drivers or the kernel leading to incorrect memory mapping.
A typical log message (dmesg or system journal):
[12345.678] myapp[1234]: unaligned access to address 0x7fffdcba
[12345.679] myapp[1234]: Bus error (core dumped)
Or in the terminal:
Bus error (core dumped)
The error often manifests in high-load systems, on Raspberry Pi, when working with memory-mapped files (mmap), or when using low-level operations (e.g., direct access to /dev/mem).
Common Causes
- Incorrect Data Alignment
The program attempts to access data at an address not aligned to the size of the data type (e.g., a 4-byteintat address0x1001). On some architectures (SPARC, Alpha, ARM in certain modes), this triggersSIGBUS. - Faulty RAM
Issues with RAM modules, overheating, or incompatibility often lead to bus errors during read/write operations. - Disk or File System Problems
Bad sectors on the disk, file system errors (e.g., after a sudden power loss) can cause a memory-mapped file to contain invalid data. - Hardware Driver or Kernel Bugs
A driver crash (e.g., graphics or network) might incorrectly set up memory page tables, causingSIGBUSon access. - CPU Overheating or Unstable Overclocks
On Raspberry Pi and other single-board computers, overheating or an unstable overclock frequently causes bus errors. - Misuse of System Calls
For example, passing an invalid pointer or length toread()/write()for a memory-mapped file that was truncated by another process.
Solution 1: Test and Replace RAM
Hardware RAM issues are the most common cause of SIGBUS on Linux servers and workstations.
- Boot into memtest86+
Reboot the system and select memtest86+ from the bootloader menu (GRUB). If it's not present, install it:sudo apt update && sudo apt install memtest86+
Or for RHEL/CentOS:sudo yum install memtest86+ - Run the Test
Memtest will start automatically. Let it run for at least 4 full passes (this may take several hours). Any errors (red lines) indicate a faulty RAM stick. - If Errors Are Found
- Power off the computer and unplug the power cable.
- Remove all RAM modules, clean the contacts with isopropyl alcohol.
- Install modules one by one, testing each slot and stick.
- Replace any faulty modules.
- Check for Overheating
Usesensors(installlm-sensors):sensors
RAM temperature (if supported) should not exceed 85–90°C.
Solution 2: Update System and Kernel
Outdated drivers or a kernel with bugs can cause SIGBUS, especially with newer hardware.
- Update All Packages
For Ubuntu/Debian:sudo apt update && sudo apt full-upgrade
For CentOS/RHEL 8+:sudo dnf update
For Raspberry Pi OS:sudo apt update && sudo apt full-upgrade - Install the Latest Kernel
On Ubuntu:sudo apt install linux-generic-hwe-22.04
On CentOS:sudo dnf install kernel
Reboot after installation. - Check Kernel Version
uname -r
Ensure a recent version is in use (e.g.,5.15.0-78-genericor newer). - If the Problem Persists After an Update
Boot into the previous kernel from the GRUB menu (Advanced options) and check if the error disappears. If it does, the new kernel is likely the issue—report the bug to your distribution's bug tracker.
Solution 3: Diagnose Disk and File System
Read errors from disk into a memory-mapped file (mmap) are a common cause of SIGBUS.
- Check Disk SMART Status
Installsmartmontools:sudo apt install smartmontools # Debian/Ubuntu sudo dnf install smartmontools # RHEL/CentOS
Check disk/dev/sda:sudo smartctl -a /dev/sda
Look forSMART overall-health self-assessment test result: PASSEDand attributes likeReallocated_Sector_Ct,Current_Pending_Sector. Non-zero values indicate wear. - Run a SMART Test
sudo smartctl -t long /dev/sda
After it completes (may take hours), view results:sudo smartctl -a /dev/sda | grep -A5 " SMART" - Check the File System
For unmounted partitions:sudo fsck -f /dev/sda1
Forext4, add-cto scan for bad blocks:sudo fsck -fcc /dev/sda1
Warning: Do not runfsckon a mounted root partition. Use a LiveCD or boot into rescue mode. - Verify Integrity of Memory-Mapped Files
If the error occurs in a specific application (e.g., a database), check its data files:# For PostgreSQL sudo -u postgres pg_checksums -D /var/lib/postgresql/14/main # For MySQL sudo mysqlcheck -u root -p --all-databases --auto-repair
Solution 4: Debug the Problematic Application
If the error only occurs in one program, the issue is likely in its code.
- Run the Program Under Valgrind
Valgrind detects memory access errors:valgrind --tool=memcheck ./your_program
Look forInvalid addressorMisaligned addressmessages. - Use GDB to Analyze the Core Dump
If the program dumps a core file (usuallycoreorcore.<pid>in the current directory):gdb ./your_program core
Inside GDB:(gdb) bt (gdb) info registers
These commands show the call stack and the address that causedSIGBUS. - Check Alignment in Code
If you are the developer, ensure:- Data structures are properly aligned (
alignasin C++,__attribute__((aligned))in C). - Aligned-access functions are used (e.g.,
memcpyinstead of direct pointer copying when alignment is uncertain). - There is no access beyond array bounds.
- Data structures are properly aligned (
- Try Compiling with Sanitizer Flags
Add to yourCFLAGS:-fsanitize=address,undefined -fno-omit-frame-pointer
This helps catch errors at compile/run time.
Prevention
- Regularly Test RAM
Run memtest86+ quarterly, especially after hardware changes or after overheating incidents. - Monitor Temperatures
Installlm-sensorsandpsensor. On Raspberry Pi, usevcgencmd measure_temp. - Use Stable Overclocks
On Raspberry Pi and other SBCs, avoid aggressive overclocking. Verify stability with stress tests (stress-ng). - Update Firmware
For Raspberry Pi:sudo rpi-eeprom-update -a
For servers—update BIOS/UEFI and CPU microcode (intel-microcodeoramd-microcode). - Handle Memory-Mapped Files Correctly
In your code:- Always check
mmapresults forMAP_FAILED. - Ensure the file size doesn't change due to another process during mapping.
- Use
msyncto synchronize changes.
- Always check
- Choose Quality Components
When building a system, prefer ECC memory (for servers) and certified disks. For Raspberry Pi, use the official power supply and a high-quality SD card (Class 10/UHS-I). - Set Up Monitoring
Add a filter forSIGBUSinsyslog:sudo grep -i "bus error" /var/log/syslog
Configure alerts (e.g., vialogwatchorfail2ban) for recurring errors.