github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/testdata/testprogcgo/segv.go (about)

     1  // Copyright 2020 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  // +build !plan9,!windows
     6  
     7  package main
     8  
     9  // static void nop() {}
    10  import "C"
    11  
    12  import (
    13  	"syscall"
    14  	"time"
    15  )
    16  
    17  func init() {
    18  	register("Segv", Segv)
    19  	register("SegvInCgo", SegvInCgo)
    20  }
    21  
    22  var Sum int
    23  
    24  func Segv() {
    25  	c := make(chan bool)
    26  	go func() {
    27  		close(c)
    28  		for i := 0; ; i++ {
    29  			Sum += i
    30  		}
    31  	}()
    32  
    33  	<-c
    34  
    35  	syscall.Kill(syscall.Getpid(), syscall.SIGSEGV)
    36  
    37  	// Give the OS time to deliver the signal.
    38  	time.Sleep(time.Second)
    39  }
    40  
    41  func SegvInCgo() {
    42  	c := make(chan bool)
    43  	go func() {
    44  		close(c)
    45  		for {
    46  			C.nop()
    47  		}
    48  	}()
    49  
    50  	<-c
    51  
    52  	syscall.Kill(syscall.Getpid(), syscall.SIGSEGV)
    53  
    54  	// Give the OS time to deliver the signal.
    55  	time.Sleep(time.Second)
    56  }