Is there a point to trapping “segfault”?

后端 未结 6 1681
时光说笑
时光说笑 2021-01-03 04:03

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 w

相关标签:
6条回答
  • 2021-01-03 04:18

    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.

    0 讨论(0)
  • 2021-01-03 04:21

    To log a crash stack trace, for example.

    0 讨论(0)
  • 2021-01-03 04:21

    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.

    0 讨论(0)
  • 2021-01-03 04:33

    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)

    0 讨论(0)
  • 2021-01-03 04:33

    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.

    0 讨论(0)
  • 2021-01-03 04:34

    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.
    0 讨论(0)
提交回复
热议问题