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 package main 6 7 import "fmt" 8 9 /* 10 #include <signal.h> 11 #include <stdlib.h> 12 #include <stdio.h> 13 14 int *p; 15 static void sigsegv() { 16 *p = 1; 17 fprintf(stderr, "ERROR: C SIGSEGV not thrown on caught?.\n"); 18 exit(2); 19 } 20 21 static void sighandler(int signum) { 22 if (signum == SIGSEGV) { 23 exit(0); // success 24 } 25 } 26 27 static void __attribute__ ((constructor)) sigsetup(void) { 28 struct sigaction act; 29 act.sa_handler = &sighandler; 30 sigaction(SIGSEGV, &act, 0); 31 } 32 */ 33 import "C" 34 35 var p *byte 36 37 func f() (ret bool) { 38 defer func() { 39 if recover() == nil { 40 fmt.Errorf("ERROR: couldn't raise SIGSEGV in Go.") 41 C.exit(2) 42 } 43 ret = true 44 }() 45 *p = 1 46 return false 47 } 48 49 func main() { 50 // Test that the signal originating in Go is handled (and recovered) by Go. 51 if !f() { 52 fmt.Errorf("couldn't recover from SIGSEGV in Go.") 53 C.exit(2) 54 } 55 56 // Test that the signal originating in C is handled by C. 57 C.sigsegv() 58 } 59