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