]> Cypherpunks.ru repositories - gostls13.git/commitdiff
misc/cgo/testsigfwd: move to runtime/testprog/testprogcgo
authorAustin Clements <austin@google.com>
Fri, 14 Oct 2022 18:50:19 +0000 (14:50 -0400)
committerAustin Clements <austin@google.com>
Mon, 17 Oct 2022 15:15:33 +0000 (15:15 +0000)
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>

src/cmd/dist/test.go
src/runtime/crash_cgo_test.go
src/runtime/testdata/testprogcgo/sigfwd.go [moved from misc/cgo/testsigfwd/main.go with 68% similarity]

index 8f83aedd3ec023a1966e6395f124bd25fe101910..fa79e7e8ae1b9aefdbb04eb44a4ff1efaac34302 100644 (file)
@@ -831,9 +831,6 @@ func (t *tester) registerTests() {
                if t.hasBash() && goos != "android" && !t.iOS() && gohostos != "windows" {
                        t.registerHostTest("cgo_errors", "../misc/cgo/errors", "misc/cgo/errors", ".")
                }
-               if gohostos == "linux" && t.extLink() {
-                       t.registerTest("testsigfwd", "../misc/cgo/testsigfwd", "go", "run", ".")
-               }
        }
 
        if goos != "android" && !t.iOS() {
index 1bf5d50b830b60c3b64c992e2230bce3e3c42654..441a704ebeebd1198ba31c8b4762587ce1155f32 100644 (file)
@@ -8,6 +8,7 @@ package runtime_test
 
 import (
        "fmt"
+       "internal/goos"
        "internal/testenv"
        "os"
        "os/exec"
@@ -753,3 +754,15 @@ func TestCgoTraceParserWithOneProc(t *testing.T) {
                t.Fatalf("GOMAXPROCS=1, want %s, got %s\n", want, output)
        }
 }
+
+func TestCgoSigfwd(t *testing.T) {
+       t.Parallel()
+       if goos.IsLinux == 0 {
+               t.Skipf("only supported on Linux")
+       }
+
+       got := runTestProg(t, "testprogcgo", "CgoSigfwd", "GO_TEST_CGOSIGFWD=1")
+       if want := "OK\n"; got != want {
+               t.Fatalf("expected %q, but got:\n%s", want, got)
+       }
+}
similarity index 68%
rename from misc/cgo/testsigfwd/main.go
rename to src/runtime/testdata/testprogcgo/sigfwd.go
index 1d8633971d595851b5f3ed06b8c8098c96f14f68..16942897005771ef7509e5513fceff4123de67c1 100644 (file)
@@ -2,9 +2,14 @@
 // 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"
+import (
+       "fmt"
+       "os"
+)
 
 /*
 #include <signal.h>
@@ -12,21 +17,25 @@ import "fmt"
 #include <stdio.h>
 #include <string.h>
 
-int *p;
+int *sigfwdP;
 static void sigsegv() {
-       *p = 1;
+       *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\ttestsigfwd\n");
+               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);
@@ -36,7 +45,11 @@ static void __attribute__ ((constructor)) sigsetup(void) {
 */
 import "C"
 
-var p *byte
+func init() {
+       register("CgoSigfwd", CgoSigfwd)
+}
+
+var nilPtr *byte
 
 func f() (ret bool) {
        defer func() {
@@ -46,14 +59,19 @@ func f() (ret bool) {
                }
                ret = true
        }()
-       *p = 1
+       *nilPtr = 1
        return false
 }
 
-func main() {
+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.Errorf("couldn't recover from SIGSEGV in Go.")
+               fmt.Fprintf(os.Stderr, "couldn't recover from SIGSEGV in Go.\n")
                C.exit(2)
        }