github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/runtime/testdata/testprog/crash.go (about)

     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 (
     8  	"fmt"
     9  	"runtime"
    10  )
    11  
    12  func init() {
    13  	register("Crash", Crash)
    14  	register("DoublePanic", DoublePanic)
    15  }
    16  
    17  var NilPointer *string
    18  
    19  func test(name string) {
    20  	defer func() {
    21  		if x := recover(); x != nil {
    22  			fmt.Printf(" recovered")
    23  		}
    24  		fmt.Printf(" done\n")
    25  	}()
    26  	fmt.Printf("%s:", name)
    27  	*NilPointer = name
    28  	fmt.Print("SHOULD NOT BE HERE")
    29  }
    30  
    31  func testInNewThread(name string) {
    32  	c := make(chan bool)
    33  	go func() {
    34  		runtime.LockOSThread()
    35  		test(name)
    36  		c <- true
    37  	}()
    38  	<-c
    39  }
    40  
    41  func Crash() {
    42  	runtime.LockOSThread()
    43  	test("main")
    44  	testInNewThread("new-thread")
    45  	testInNewThread("second-new-thread")
    46  	test("main-again")
    47  }
    48  
    49  type P string
    50  
    51  func (p P) String() string {
    52  	// Try to free the "YYY" string header when the "XXX"
    53  	// panic is stringified.
    54  	runtime.GC()
    55  	runtime.GC()
    56  	runtime.GC()
    57  	return string(p)
    58  }
    59  
    60  // Test that panic message is not clobbered.
    61  // See issue 30150.
    62  func DoublePanic() {
    63  	defer func() {
    64  		panic(P("YYY"))
    65  	}()
    66  	panic(P("XXX"))
    67  }