runtime: signal SIGSEGV

This commit is contained in:
visualfc
2024-08-14 11:20:07 +08:00
parent cc9de01c99
commit 027d21035e
4 changed files with 71 additions and 0 deletions

32
c/signal/signal.go Normal file
View File

@@ -0,0 +1,32 @@
package signal
import (
"unsafe"
"github.com/goplus/llgo/c"
)
import "C"
const (
LLGoPackage = "link"
)
//llgo:type C
type SignalHandler func(c.Int)
//llgo:type C
type sigactiont struct {
handler SignalHandler
tramp unsafe.Pointer
mask c.Int
flags c.Int
}
//go:linkname sigaction C.sigaction
func sigaction(sig c.Int, act, old *sigactiont) c.Int
func Signal(sig c.Int, hanlder SignalHandler) c.Int {
var act sigactiont
act.handler = hanlder
return sigaction(sig, &act, nil)
}

24
cl/_testgo/sigsegv/in.go Normal file
View File

@@ -0,0 +1,24 @@
package main
type T struct {
s int
}
func f() *T {
return nil
}
func init() {
println("init")
defer func() {
r := recover()
if e, ok := r.(error); ok {
println("recover", e.Error())
}
}()
println(f().s)
}
func main() {
println("main")
}

View File

@@ -0,0 +1 @@
;

View File

@@ -21,6 +21,8 @@ import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/pthread"
"github.com/goplus/llgo/c/signal"
"github.com/goplus/llgo/c/syscall"
)
// -----------------------------------------------------------------------------
@@ -116,4 +118,16 @@ const MaxZero = 1024
var ZeroVal [MaxZero]byte
func init() {
signal.Signal(c.Int(syscall.SIGSEGV), func(v c.Int) {
switch syscall.Signal(v) {
case syscall.SIGSEGV:
panic(errorString("invalid memory address or nil pointer dereference"))
default:
var buf [20]byte
panic(errorString("unexpected signal value: " + string(itoa(buf[:], uint64(v)))))
}
})
}
// -----------------------------------------------------------------------------