Recover from segfault in C code

343 views
Skip to first unread message

Peter Kleiweg

unread,
Sep 29, 2015, 6:25:46 PM9/29/15
to golang-nuts
Is there a way to recover from a segmentation fault triggered by embedded/linked C code? Using a deferred call to recover() doesn't work.

Ian Lance Taylor

unread,
Sep 29, 2015, 8:59:20 PM9/29/15
to Peter Kleiweg, golang-nuts
On Tue, Sep 29, 2015 at 3:25 PM, Peter Kleiweg <pkle...@xs4all.nl> wrote:
> Is there a way to recover from a segmentation fault triggered by embedded/linked C code? Using a deferred call to recover() doesn't work.

There is not. We don't currently support unwinding the stack from a C
function. I think it might be possible in principle--we would have to
discard the current call on the system stack--but it's definitely
tricky.

Ian

Klaus Post

unread,
Sep 30, 2015, 7:57:22 AM9/30/15
to golang-nuts
On Wednesday, 30 September 2015 00:25:46 UTC+2, Peter Kleiweg wrote:
Is there a way to recover from a segmentation fault triggered by embedded/linked C code? Using a deferred call to recover() doesn't work.

I know you are an experienced developer, so I assume you know that segfaults are much worse in C, and in general shouldn't be recovered, since you likely have 'dirty' memory. Depending on the error there is a large probability the code has overwritten portions of your memory before it hit a memory barrier that triggered the segfault.

Is it impossible for you to fix the segfault in the first place?


/Klaus

Egon

unread,
Sep 30, 2015, 4:09:40 PM9/30/15
to golang-nuts
I would probably run such C code in a separate process and monitor it. (Obviously, if possible and no high perf needed etc.)

dave.t...@gmail.com

unread,
Oct 2, 2015, 2:34:47 PM10/2/15
to golang-nuts, pkle...@xs4all.nl

    It might be better to take the more general approach of providing a safe setjmp() / longjmp() implementation to C code - that might be easier than trying to unwind, could be used for this purpose, and probably many others as well.  (Might it even work as-is if the goroutine is locked to the OS thread?   Or do the usual implementations of those functions attempt to parse the stack instead of just treating it like a blob?)
Reply all
Reply to author
Forward
0 new messages