From e41f22b992dc0bbeb8a2f262e6d677644166d9f0 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Wed, 3 Jul 2024 00:08:10 +0100 Subject: [PATCH] fix: plugin verification panics due to missing SA_ONSTACK signal --- internal/native/message_server.go | 3 +- internal/native/mock_server.go | 1 + internal/native/signal.go | 87 +++++++++++++++++++++++++++++++ internal/native/verifier.go | 2 +- 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 internal/native/signal.go diff --git a/internal/native/message_server.go b/internal/native/message_server.go index 723ff6952..8102e7370 100644 --- a/internal/native/message_server.go +++ b/internal/native/message_server.go @@ -329,7 +329,8 @@ func (m *MessageServer) UsingPlugin(pluginName string, pluginVersion string) err defer free(cPluginName) cPluginVersion := C.CString(pluginVersion) defer free(cPluginVersion) - + + InstallSignalHandlers() r := C.pactffi_using_plugin(m.messagePact.handle, cPluginName, cPluginVersion) // 1 - A general panic was caught. diff --git a/internal/native/mock_server.go b/internal/native/mock_server.go index 17b148e9c..d3adcfc85 100644 --- a/internal/native/mock_server.go +++ b/internal/native/mock_server.go @@ -555,6 +555,7 @@ func (m *MockServer) UsingPlugin(pluginName string, pluginVersion string) error cPluginVersion := C.CString(pluginVersion) defer free(cPluginVersion) + InstallSignalHandlers() r := C.pactffi_using_plugin(m.pact.handle, cPluginName, cPluginVersion) // 1 - A general panic was caught. diff --git a/internal/native/signal.go b/internal/native/signal.go new file mode 100644 index 000000000..8757e47d7 --- /dev/null +++ b/internal/native/signal.go @@ -0,0 +1,87 @@ +//go:build cgo +// +build cgo + +package native + +/* +#if defined(__APPLE__) || defined(__linux__) +// https://github.com/wailsapp/wails/pull/2152/files#diff-d4a0fa73df7b0ab971e550f95249e358b634836e925ace96f7400480916ac09e +#include +#include +#include +#include + +static void fix_signal(int signum) +{ + struct sigaction st; + + if (sigaction(signum, NULL, &st) < 0) { + goto fix_signal_error; + } + st.sa_flags |= SA_ONSTACK; + if (sigaction(signum, &st, NULL) < 0) { + goto fix_signal_error; + } + return; +fix_signal_error: + fprintf(stderr, "error fixing handler for signal %d, please " + "report this issue to " + "https://github.com/pact-foundation/pact-go: %s\n", + signum, strerror(errno)); +} + +static void install_signal_handlers() +{ +#if defined(SIGCHLD) + fix_signal(SIGCHLD); +#endif +#if defined(SIGHUP) + fix_signal(SIGHUP); +#endif +#if defined(SIGINT) + fix_signal(SIGINT); +#endif +#if defined(SIGQUIT) + fix_signal(SIGQUIT); +#endif +#if defined(SIGABRT) + fix_signal(SIGABRT); +#endif +#if defined(SIGFPE) + fix_signal(SIGFPE); +#endif +#if defined(SIGTERM) + fix_signal(SIGTERM); +#endif +#if defined(SIGBUS) + fix_signal(SIGBUS); +#endif +#if defined(SIGSEGV) + fix_signal(SIGSEGV); +#endif +#if defined(SIGXCPU) + fix_signal(SIGXCPU); +#endif +#if defined(SIGXFSZ) + fix_signal(SIGXFSZ); +#endif +} +#else + static void install_signal_handlers() + { + } +#endif +*/ +import "C" +import ( + "os" + "runtime" +) + +func InstallSignalHandlers() { + if os.Getenv("PACT_GO_INSTALL_SIGNAL_HANDLERS") != "0" { + if runtime.GOOS != "windows" { + C.install_signal_handlers() + } + } +} diff --git a/internal/native/verifier.go b/internal/native/verifier.go index ae781840a..8fc948c4b 100644 --- a/internal/native/verifier.go +++ b/internal/native/verifier.go @@ -226,7 +226,7 @@ func (v *Verifier) SetPublishOptions(providerVersion string, buildUrl string, pr func (v *Verifier) Execute() error { // TODO: Validate - + InstallSignalHandlers() result := C.pactffi_verifier_execute(v.handle) /// | Error | Description |