2

Can anyone explain why stack overflow during recursive calls causes bus error and not segmentation fault (Mac OS)?

I have a function that is called recursively for a few thousand times. As I expected it hits the stack limit but in other cases that caused segmentation fault and that's logical but why is it bus error 10? As I know referencing unaligned memory causes bus error but why is that when hitting stack limit?

Max Langhof
  • 23,383
  • 5
  • 39
  • 72
  • It is OS dependent how MMU exeptions are handled. However, since a stackoverflow could result in an access to an invalid address (or an address the process does not own), SIGBUS is a valid option. – user1810087 Jul 31 '19 at 09:33
  • Yes, but on the very same machine I had segmentation fault but now it's bus error 10 and cannot understand why ... – shota silagadze Jul 31 '19 at 09:37
  • `A stackoverflow could result...` does not mean it have to. If the overflow hits an address which is not owned by the process, you should get a SIGSEGV. If it hist an invalid address (or according to the comments in [this](https://stackoverflow.com/questions/32488353/diagnosing-sigbus-error-on-os-x-yosemite) question, memory which is marked readonly) you should get a SIGBUS. It is not reliable... Your Mac could also catch fire... – user1810087 Jul 31 '19 at 09:54
  • A bus error and a segmentation violation each result from a program accessing memory it shouldn't, but they are detected and signaled by different mechanisms. Under unix, if the OS detects a process accessing memory that the OS hasn't allocated to that process (e.g. allocated to another process), it sends a SIGSEGV signal to the offending process. However, if the hardware detects an access of hardware resources that don't physically exist, it raises a hardware fault that is trapped by the OS, which sends a SIGBUS signal to the process executing the offending instruction. – Peter Jul 31 '19 at 10:25
  • @Peter That might make a good _answer_... – Lightness Races in Orbit Jul 31 '19 at 10:31
  • @LightnessRacesinOrbit - Yeah, I wrote that pretty quickly with fingers on auto-drive, but reading it now, it's more complete than I thought. I'll tidy it up a little (and add a couple of extra details) and post as an answer shortly. – Peter Jul 31 '19 at 10:52

1 Answers1

3

A bus error and a segmentation violation each result from a program accessing memory it shouldn't, but they are detected and signaled by different mechanisms.

The details do vary with operating system. The following observations are, more or less, how things happen with a unix host.

If the OS detects a process accessing memory that the OS hasn't allocated to that process (e.g. allocated to another process), it sends a SIGSEGV signal to the offending process. The process/program then terminates, reporting a segmentation violation.

However, if the hardware detects an access of hardware resources that don't physically exist, it raises a hardware fault that is trapped by the OS, which sends a SIGBUS signal to the process executing the offending instruction. This can also happen with unaligned memory accesses - a hardware fault is raised, trapped by the operating system which sends a signal to the originating process.

Although the details vary (e.g. how the OS or kernel responds to hardware faults, or what signals it sends to an offending process) the general ideas are similar.

Peter
  • 35,646
  • 4
  • 32
  • 74