Source file src/runtime/testdata/testprog/crash.go

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

View as plain text