summaryrefslogtreecommitdiffstats
path: root/src/runtime/testdata/testprogcgo/sigfwd.go
blob: f6a0c031d739de6e9b23f543ecb599cfa383a3ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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 unix

package main

import (
	"fmt"
	"os"
)

/*
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

sig_atomic_t expectCSigsegv;
int *sigfwdP;

static void sigsegv() {
	expectCSigsegv = 1;
	*sigfwdP = 1;
	fprintf(stderr, "ERROR: C SIGSEGV not thrown on caught?.\n");
	exit(2);
}

static void segvhandler(int signum) {
	if (signum == SIGSEGV) {
		if (expectCSigsegv == 0) {
			fprintf(stderr, "SIGSEGV caught in C unexpectedly\n");
			exit(1);
		}
		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.Fprintf(os.Stderr, "ERROR: couldn't raise SIGSEGV in Go\n")
			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()
}