views:

256

answers:

6

I know that, given enough context, one could hope to use constructively (i.e. recover) from a segfault condition.

But, is the effort worth it? If yes, in what situation(s) ?

+1  A: 

To log a crash stack trace, for example.

Alex B
+1: I should have listed this possibility in the question (i.e. I already had considered this situation): so, my fault for not doing so.
jldupont
+3  A: 

a Segmentation Fault is really accessing memory that you do not have permission to access ( either because it's not mapped, you don't have permissions, invalid virtual address, etc. ).

Depending on the underlying reason, you may want to trap and handle the segmentation fault. For instance, if your program is passed an invalid virtual address, it may log that segfault and then do some damage control.

A segfault does not necessarily mean that the program heap is corrupted. Reading an invalid address ( eg. null pointer ) may result in a segfault, but that does not mean that the heap is corrupted. Also, an application can have multiple heaps depending on the C runtime.

kervin
@kervin: what kind of damage control? the heap might be a heap of junk by that time
Alon
@Alon: Edited to address your question. A segfault does not necessarily mean the heap is corrupted. Also, your handler does not have to be using the same heap or address space that's affected by the segfault.
kervin
Segmentation faults have been used by garbage collection algorithms approaches to set up a barrier at the end of the heap, when accessed, and sigsegv is delivered, is an indication that a gc needs to be done.
Christian
@Christian: I had forgotten about this interesting fact... thanks!
jldupont
+8  A: 

A number of the reasons:

  1. To provide more application specific information to debug a crash. For instance, I crashed at stage 3 processing file 'x'.
  2. To probe whether certain memory regions are accessible. This was mostly to satisfy an API for an embedded system. We would try to write to the memory region and catch the segfault that told us that the memory was read-only.
  3. The segfault usually originates with a signal from the MMU, which is used by the operating system to swap in pages of memory if necessary. If the OS doesn't have that page of memory, it then forwards the signal onto the application.
Chris Arguin
+1: ....thanks.
jldupont
@Chris: your answer is great (from my point of view) but I can only accept one unfortunately. Many thanks for your contribution.
jldupont
+1  A: 

No. I think it is a waste of time - a seg fault indicates there is something wrong in your code, and you will be better advised to find this by examining a core dump and/or your source code. The one time I tried trapping a seg fault lead me off into a hall of mirrors which I could have avoided by simply thinking about the source code. Never again.

anon
+1: thanks for the educated advice.
jldupont
+9  A: 

You can't really hope to recover from a segfault. You can detect that it happened, and dump out relevant application-specific state if possible, but you can't continue the process. This is because (amongst others)

  • The thread which failed cannot be continued, so your only options are longjmp or terminating the thread. Neither is safe in most cases.
  • Either way, you may leave a mutex / lock in a locked state which causes other threads to wait forever
  • Even if that doesn't happen, you may leak resources
  • Even if you don't do either of those things, the thread which segfaulted may have left the internal state of the application inconsistent when it failed. An inconsistent internal state could cause data errors or further bad behaviour subsequently which causes more problems than simply quitting

So in general, there is no point in trapping it and doing anything EXCEPT terminating the process in a fairly abrupt fashion. There's no point in attempting to write (important) data back to disc, or continue to do other useful work. There is some point in dumping out state to logs- which many applications do - and then quitting.

A possibly useful thing to do might be to exec() your own process, or have a watchdog process which restarts it in the case of a crash. (NB: exec does not always have well defined behaviour if your process has >1 thread)

MarkR
+1: marvelous answer... thanks so much.
jldupont
A good summary, but I disagree for the reason I mentioned in my answer; there are times where you are doing a specific thing that might cause a segfault, and "recovery" is not an issue.
Chris Arguin
+2  A: 

There are very advanced techniques that one might implementing by catching a segmentation fault, if you know the segmentation fault isn't an error. For example, you can protect pages so that you can't read from them, and then trap the SIGSEGV to perform "magical" behavior before the read completes. (See Tomasz Węgrzanowski "Segfaulting own programs for fun and profit" for an example of what you might do, but usually the overhead is pretty high so it's not worth doing.)

A similar principle applies to catching trapping an illegal instruction exception (usually in the kernel) to emulate an instruction that's not implemented on your processor.

Ken Bloom
+1: thanks for this.
jldupont