From 2363d28d57776068ea5089b7f3ec36f443d3874c Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 3 Nov 2025 18:31:27 +0800 Subject: [PATCH] feat(runtime): add SIGSEGV signal handler to convert nil pointer dereference to recoverable panic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add z_rt_default.go with signal handler for SIGSEGV on non-wasm platforms - Convert segmentation faults from nil pointer access to Go panic - Enable recover() to catch nil pointer dereference errors - Use build tag (!wasm) to maintain wasm platform compatibility - Remove commented-out signal handling code from z_rt.go This aligns llgo's behavior with standard Go, where accessing nil pointer fields triggers a recoverable panic instead of immediate program crash. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- _demo/go/export/libexport.h.want | 3 ++ runtime/internal/runtime/z_rt.go | 12 ------- runtime/internal/runtime/z_signal.go | 48 ++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 runtime/internal/runtime/z_signal.go diff --git a/_demo/go/export/libexport.h.want b/_demo/go/export/libexport.h.want index 26357c32..d652c758 100644 --- a/_demo/go/export/libexport.h.want +++ b/_demo/go/export/libexport.h.want @@ -297,6 +297,9 @@ github_com_goplus_llgo_runtime_internal_clite_pthread_init(void) GO_SYMBOL_RENAM void github_com_goplus_llgo_runtime_internal_clite_pthread_sync_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/clite/pthread/sync.init") +void +github_com_goplus_llgo_runtime_internal_clite_signal_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/clite/signal.init") + void github_com_goplus_llgo_runtime_internal_runtime_goarch_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/runtime/goarch.init") diff --git a/runtime/internal/runtime/z_rt.go b/runtime/internal/runtime/z_rt.go index 107696de..7bf5959e 100644 --- a/runtime/internal/runtime/z_rt.go +++ b/runtime/internal/runtime/z_rt.go @@ -109,18 +109,6 @@ 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))))) -// } -// }) -// } - // ----------------------------------------------------------------------------- type SigjmpBuf struct { diff --git a/runtime/internal/runtime/z_signal.go b/runtime/internal/runtime/z_signal.go new file mode 100644 index 00000000..fdee24c8 --- /dev/null +++ b/runtime/internal/runtime/z_signal.go @@ -0,0 +1,48 @@ +//go:build !wasm && !baremetal + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package runtime + +import ( + c "github.com/goplus/llgo/runtime/internal/clite" + "github.com/goplus/llgo/runtime/internal/clite/signal" +) + +const ( + // SIGSEGV is signal number 11 on all Unix-like systems (Linux, Darwin, BSD, etc.) + // Using a hardcoded constant avoids importing the syscall package, which would + // introduce dependencies on errors and internal/reflectlite packages that cause + // linking issues in c-shared and c-archive build modes. + SIGSEGV = c.Int(0xb) +) + +// This file contains platform-specific runtime initialization for non-wasm targets. +// The SIGSEGV signal handler enables Go-style panic recovery for nil pointer dereferences +// instead of immediate process termination. +// +// For wasm platform compatibility, signal handling is excluded via build tags. +// See PR #1059 for wasm platform requirements. +func init() { + signal.Signal(SIGSEGV, func(v c.Int) { + if v == SIGSEGV { + panic(errorString("invalid memory address or nil pointer dereference")) + } + var buf [20]byte + panic(errorString("unexpected signal value: " + string(itoa(buf[:], uint64(v))))) + }) +}