aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/testdata
diff options
context:
space:
mode:
authorAustin Clements <austin@google.com>2022-10-14 14:50:19 -0400
committerAustin Clements <austin@google.com>2022-10-17 15:15:33 +0000
commitdacf88e40a0a3d395988d226b5e43e046dd7e68f (patch)
tree0d3b3c7c3ef8990f0dadc6ed28b5d67435f50f98 /src/runtime/testdata
parentabd592b3d7c3e05eaa9dd6a69749e497b1973002 (diff)
downloadgo-dacf88e40a0a3d395988d226b5e43e046dd7e68f.tar.xz
misc/cgo/testsigfwd: move to runtime/testprog/testprogcgo
This migrates testsigfwd, which uses some one-off build infrastructure, to be part of the runtime's testprogcgo. The test is largely unchanged. Because it's part of a larger binary, this CL renames a few things and gates the constructor-time signal handler registration on an environment variable. This CL also replaces an errant fmt.Errorf with fmt.Fprintf. For #37486, since it eliminates a non-go-test from dist. Change-Id: I0efd146ea0a0a3f0b361431349a419af0f0ecc61 Reviewed-on: https://go-review.googlesource.com/c/go/+/443068 Run-TryBot: Austin Clements <austin@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/runtime/testdata')
-rw-r--r--src/runtime/testdata/testprogcgo/sigfwd.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/runtime/testdata/testprogcgo/sigfwd.go b/src/runtime/testdata/testprogcgo/sigfwd.go
new file mode 100644
index 0000000000..1694289700
--- /dev/null
+++ b/src/runtime/testdata/testprogcgo/sigfwd.go
@@ -0,0 +1,80 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build linux
+
+package main
+
+import (
+ "fmt"
+ "os"
+)
+
+/*
+#include <signal.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+int *sigfwdP;
+static void sigsegv() {
+ *sigfwdP = 1;
+ fprintf(stderr, "ERROR: C SIGSEGV not thrown on caught?.\n");
+ exit(2);
+}
+
+static void segvhandler(int signum) {
+ if (signum == SIGSEGV) {
+ fprintf(stdout, "OK\n");
+ exit(0); // success
+ }
+}
+
+static void __attribute__ ((constructor)) sigsetup(void) {
+ if (getenv("GO_TEST_CGOSIGFWD") == NULL) {
+ return;
+ }
+
+ struct sigaction act;
+
+ memset(&act, 0, sizeof act);
+ act.sa_handler = segvhandler;
+ sigaction(SIGSEGV, &act, NULL);
+}
+*/
+import "C"
+
+func init() {
+ register("CgoSigfwd", CgoSigfwd)
+}
+
+var nilPtr *byte
+
+func f() (ret bool) {
+ defer func() {
+ if recover() == nil {
+ fmt.Errorf("ERROR: couldn't raise SIGSEGV in Go.")
+ C.exit(2)
+ }
+ ret = true
+ }()
+ *nilPtr = 1
+ return false
+}
+
+func CgoSigfwd() {
+ if os.Getenv("GO_TEST_CGOSIGFWD") == "" {
+ fmt.Fprintf(os.Stderr, "test must be run with GO_TEST_CGOSIGFWD set\n")
+ os.Exit(1)
+ }
+
+ // Test that the signal originating in Go is handled (and recovered) by Go.
+ if !f() {
+ fmt.Fprintf(os.Stderr, "couldn't recover from SIGSEGV in Go.\n")
+ C.exit(2)
+ }
+
+ // Test that the signal originating in C is handled by C.
+ C.sigsegv()
+}