#How to disable segfault handler

10 messages · Page 1 of 1 (latest)

wild jasper
#

I need a target executable for some test code that exits due to a SIGSEGV

Right now I have some code like this:


//go:generate bash -c "cc -x c  <(echo 'int main() { return *(int*)0; }') -o segv.bin"

func TestSignalledSegV(t *testing.T) {
    if _, err := os.Stat("./segv.bin"); errors.Is(err, os.ErrNotExist) {
        t.Skipf("To enable this test, run go generate ./...")
    }
    cwd, err := os.Getwd()
    require.NoError(t, err)

    k := scriptJobTest(t, `
        echo start
        exec `+cwd+`/segv.bin
    `)
    k.requireOutput("start")
    k.requireTerminalState("signal: segmentation fault (core dumped)")
}

I'd like to eliminate the need for an external C compiler for the test.

I tried some code like this:

    var f func()
    f()

Or even this:

    p := unsafe.Pointer(uintptr(0x1000))
    f := *(*func())(p)
    f()

but instead of segfaulting, it hits the fault handler and does an exit(2)

How do I disable the fault handler?

wild jasper
fleet tusk
#

go is managed so nil ptr deref are caught / cause panic instead of crash

#

actually never mind, this works:

    var p *int
    // Dereferencing a nil pointer
    println(*p)
#

well... maybe it's caught

anic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1044908ec]

goroutine 1 [running]:
main.main()
    /Users/dl/dev/grol-io/grol/foo.go:6 +0x1c
exit status 2
#

I suppose some cgo stuff could do it (ie crash on C side)

wild jasper
#

I was thinking that the C approach also probably wouldn't work, as the sighandler is on the thread

fleet tusk
#

could make a pthread and crash there

wild jasper
#

Thats getting to be a bit much... I was hoping there was some ENV variable I could set or call runtime.DisableFaultHandler() or something