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