Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a "bus error"?

Tags:

c++

bus-error

I am trying very hard to get a bus error.

One way is misaligned access and I have tried the examples given here and here, but no error for me - the programs execute just fine.

Is there some situation which is sure to produce a bus error?

like image 266
Lazer Avatar asked Jan 15 '10 04:01

Lazer


People also ask

What could cause a bus error?

Bus errors can result from either a programming error or device corruption on your system. Some common causes of bus errors are: invalid file descriptors, unreasonable I/O requests, bad memory allocation, misaligned data structures, compiler bugs, and corrupt boot blocks.

What is bus error in C programming?

In computing, a bus error is a fault raised by hardware, notifying an operating system (OS) that a process is trying to access memory that the CPU cannot physically address: an invalid address for the address bus, hence the name.

What can cause SIGSEGV?

SIGSEGV is triggered by the operating system, which detects that a process is carrying out a memory violation, and may terminate it as a result.

What is a bus error slurm?

On the Yale clusters, a bus error usually means your job ran out of memory (RAM). If you cannot reduce the memory usage of your code, you can request additional memory for your job using the --mem-per-cpu or --mem Slurm flags.


4 Answers

This should reliably result in a SIGBUS on a POSIX-compliant system.

#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
int main() {
    FILE *f = tmpfile();
    int *m = mmap(0, 4, PROT_WRITE, MAP_PRIVATE, fileno(f), 0);
    *m = 0;
    return 0;
}

From the Single Unix Specification, mmap:

References within the address range starting at pa and continuing for len bytes to whole pages following the end of an object shall result in delivery of a SIGBUS signal.

like image 118
ephemient Avatar answered Oct 05 '22 06:10

ephemient


Bus errors can only be invoked on hardware platforms that:

  1. Require aligned access, and
  2. Don't compensate for an unaligned access by performing two aligned accesses and combining the results.

You probably do not have access to such a system.

like image 40
Ignacio Vazquez-Abrams Avatar answered Oct 05 '22 04:10

Ignacio Vazquez-Abrams


Try something along the lines of:

#include <signal.h>
int main(void)
{
    raise(SIGBUS);
    return 0;
}

(I know, probably not the answer you want, but it's almost sure to get you a "bus error"!)

like image 21
Alok Singhal Avatar answered Oct 05 '22 04:10

Alok Singhal


As others have mentioned this is very platform specific. On the ARM system I'm working with (which doesn't have virtual memory) there are large portions of the address space which have no memory or peripheral assigned. If I read or write one of those addresses, I get a bus error.

You can also get a bus error if there's actually a hardware problem on the bus.

If you're running on a platform with virtual memory, you might not be able to intentionally generate a bus error with your program unless it's a device driver or other kernel mode software. An invalid memory access would likely be trapped as an access violation or similar by the memory manager (and it never even has a chance to hit the bus).

like image 3
Michael Burr Avatar answered Oct 05 '22 06:10

Michael Burr