]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/testdata/testprogcgo/sigfwd.go
misc/cgo/testsigfwd: move to runtime/testprog/testprogcgo
[gostls13.git] / src / runtime / testdata / testprogcgo / sigfwd.go
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build linux
6
7 package main
8
9 import (
10         "fmt"
11         "os"
12 )
13
14 /*
15 #include <signal.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 int *sigfwdP;
21 static void sigsegv() {
22         *sigfwdP = 1;
23         fprintf(stderr, "ERROR: C SIGSEGV not thrown on caught?.\n");
24         exit(2);
25 }
26
27 static void segvhandler(int signum) {
28         if (signum == SIGSEGV) {
29                 fprintf(stdout, "OK\n");
30                 exit(0);  // success
31         }
32 }
33
34 static void __attribute__ ((constructor)) sigsetup(void) {
35         if (getenv("GO_TEST_CGOSIGFWD") == NULL) {
36                 return;
37         }
38
39         struct sigaction act;
40
41         memset(&act, 0, sizeof act);
42         act.sa_handler = segvhandler;
43         sigaction(SIGSEGV, &act, NULL);
44 }
45 */
46 import "C"
47
48 func init() {
49         register("CgoSigfwd", CgoSigfwd)
50 }
51
52 var nilPtr *byte
53
54 func f() (ret bool) {
55         defer func() {
56                 if recover() == nil {
57                         fmt.Errorf("ERROR: couldn't raise SIGSEGV in Go.")
58                         C.exit(2)
59                 }
60                 ret = true
61         }()
62         *nilPtr = 1
63         return false
64 }
65
66 func CgoSigfwd() {
67         if os.Getenv("GO_TEST_CGOSIGFWD") == "" {
68                 fmt.Fprintf(os.Stderr, "test must be run with GO_TEST_CGOSIGFWD set\n")
69                 os.Exit(1)
70         }
71
72         // Test that the signal originating in Go is handled (and recovered) by Go.
73         if !f() {
74                 fmt.Fprintf(os.Stderr, "couldn't recover from SIGSEGV in Go.\n")
75                 C.exit(2)
76         }
77
78         // Test that the signal originating in C is handled by C.
79         C.sigsegv()
80 }