3 // Copyright 2010 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test of basic recover functionality.
18 // go.tools/ssa/interp still has:
19 // - some lesser bugs in recover()
20 // - incomplete support for reflection
21 interp := os.Getenv("GOSSAINTERP") != ""
71 runtime.Breakpoint() // can't depend on panic
74 func mustRecoverBody(v1, v2, v3, x interface{}) {
77 println("spurious recover", v)
82 println("missing recover", x.(int))
83 die() // panic is useless here
86 println("wrong value", v, x)
90 // the value should be gone now regardless
93 println("recover didn't recover")
98 func doubleRecover() interface{} {
102 func mustRecover(x interface{}) {
103 mustRecoverBody(doubleRecover(), recover(), recover(), x)
106 func mustNotRecover() {
109 println("spurious recover", v)
114 func withoutRecover() {
115 mustNotRecover() // because it's a sub-call
118 func withoutRecoverRecursive(n int) {
120 withoutRecoverRecursive(1)
124 println("spurious recover (recursive)", v)
131 defer mustNotRecover() // because mustRecover will squelch it
132 defer mustRecover(1) // because of panic below
133 defer withoutRecover() // should be no-op, leaving for mustRecover to find
134 defer withoutRecoverRecursive(0) // ditto
138 // Repeat test1 with closures instead of standard function.
139 // Interesting because recover bases its decision
140 // on the frame pointer of its caller, and a closure's
141 // frame pointer is in the middle of its actual arguments
142 // (after the hidden ones for the closed-over variables).
143 func test1WithClosures() {
147 println("spurious recover in closure")
151 defer func(x interface{}) {
155 println("missing recover", x.(int))
159 println("wrong value", v, x)
170 // Recover only sees the panic argument
171 // if it is called from a deferred call.
172 // It does not see the panic when called from a call within a deferred call (too late)
173 // nor does it see the panic when it *is* the deferred call (too early).
175 defer recover() // should be no-op
180 defer mustNotRecover()
182 recover() // should squelch
188 // Equivalent to test3 but using defer to make the call.
189 defer mustNotRecover()
191 defer recover() // should squelch
196 // Check that closures can set output arguments.
197 // Run g(). If it panics, return x; else return deflt.
198 func try(g func(), deflt interface{}) (x interface{}) {
200 if v := recover(); v != nil {
208 // Check that closures can set output arguments.
209 // Run g(). If it panics, return x; else return deflt.
210 func try1(g func(), deflt interface{}) (x interface{}) {
212 if v := recover(); v != nil {
222 v := try(func() { panic(5) }, 55).(int)
224 println("wrong value", v, 5)
228 s := try(func() {}, "hi").(string)
230 println("wrong value", s, "hi")
234 v = try1(func() { panic(5) }, 55).(int)
236 println("try1 wrong value", v, 5)
240 s = try1(func() {}, "hi").(string)
242 println("try1 wrong value", s, "hi")
247 // When a deferred big call starts, it must first
248 // create yet another stack segment to hold the
249 // giant frame for x. Make sure that doesn't
251 func big(mustRecover bool) {
260 println("missing big recover")
265 println("spurious big recover")
277 func test6WithClosures() {
283 if recover() != nil {
284 println("spurious big closure recover")
293 if recover() == nil {
294 println("missing big closure recover")
298 panic("6WithClosures")
304 // should panic, then call mustRecover 7, which stops the panic.
305 // then should keep processing ordinary defers earlier than that one
307 // this test checks that the defer func on the next line actually runs.
308 defer func() { ok = true }()
313 println("did not run ok func")
318 func varargs(s *int, a ...int) {
320 for _, v := range a {
323 if recover() != nil {
328 func test8a() (r int) {
329 defer varargs(&r, 1, 2, 3)
333 func test8b() (r int) {
334 defer varargs(&r, 4, 5, 6)
339 if test8a() != 106 || test8b() != 15 {
340 println("wrong value")
349 // pointer receiver, so no wrapper in i.M()
353 mustRecoverBody(doubleRecover(), recover(), recover(), 9)
362 func test9reflect1() {
363 f := reflect.ValueOf(&T1{}).Method(0).Interface().(func())
368 func test9reflect2() {
369 f := reflect.TypeOf(&T1{}).Method(0).Func.Interface().(func(*T1))
374 // word-sized value receiver, so no wrapper in i.M()
378 mustRecoverBody(doubleRecover(), recover(), recover(), 10)
387 func test10reflect1() {
388 f := reflect.ValueOf(T2(0)).Method(0).Interface().(func())
393 func test10reflect2() {
394 f := reflect.TypeOf(T2(0)).Method(0).Func.Interface().(func(T2))
399 // tiny receiver, so basic wrapper in i.M()
403 mustRecoverBody(doubleRecover(), recover(), recover(), 11)
412 func test11reflect1() {
413 f := reflect.ValueOf(T3{}).Method(0).Interface().(func())
418 func test11reflect2() {
419 f := reflect.TypeOf(T3{}).Method(0).Func.Interface().(func(T3))
424 // tiny receiver, so basic wrapper in i.M()
425 type T3deeper struct{}
427 func (T3deeper) M() {
428 badstate() // difference from T3
429 mustRecoverBody(doubleRecover(), recover(), recover(), 111)
444 // i.M is a wrapper, and i.M panics.
446 // This is a torture test for an old implementation of recover that
447 // tried to deal with wrapper functions by doing some argument
448 // positioning math on both entry and exit. Doing anything on exit
449 // is a problem because sometimes functions exit via panic instead
450 // of an ordinary return, so panic would have to know to do the
451 // same math when unwinding the stack. It gets complicated fast.
452 // This particular test never worked with the old scheme, because
453 // panic never did the right unwinding math.
455 // The new scheme adjusts Panic.argp on entry to a wrapper.
456 // It has no exit work, so if a wrapper is interrupted by a panic,
457 // there's no cleanup that panic itself must do.
458 // This test just works now.
467 // large receiver, so basic wrapper in i.M()
471 mustRecoverBody(doubleRecover(), recover(), recover(), 12)
480 func test12reflect1() {
481 f := reflect.ValueOf(T4{}).Method(0).Interface().(func())
486 func test12reflect2() {
487 f := reflect.TypeOf(T4{}).Method(0).Func.Interface().(func(T4))
492 // enormous receiver, so wrapper splits stack to call M
496 mustRecoverBody(doubleRecover(), recover(), recover(), 13)
505 func test13reflect1() {
506 f := reflect.ValueOf(T5{}).Method(0).Interface().(func())
511 func test13reflect2() {
512 f := reflect.TypeOf(T5{}).Method(0).Func.Interface().(func(T5))
517 // enormous receiver + enormous method frame, so wrapper splits stack to call M,
518 // and then M splits stack to allocate its frame.
519 // recover must look back two frames to find the panic.
531 mustRecoverBody(doubleRecover(), recover(), recover(), 14)
540 func test14reflect1() {
541 f := reflect.ValueOf(T6{}).Method(0).Interface().(func())
546 func test14reflect2() {
547 f := reflect.TypeOf(T6{}).Method(0).Func.Interface().(func(T6))
552 // function created by reflect.MakeFunc
554 func reflectFunc(args []reflect.Value) (results []reflect.Value) {
555 mustRecoverBody(doubleRecover(), recover(), recover(), 15)
560 f := reflect.MakeFunc(reflect.TypeOf((func())(nil)), reflectFunc).Interface().(func())
565 func reflectFunc2(args []reflect.Value) (results []reflect.Value) {
566 // This will call reflectFunc3
567 args[0].Interface().(func())()
571 func reflectFunc3(args []reflect.Value) (results []reflect.Value) {
572 if v := recover(); v != nil {
573 println("spurious recover", v)
580 defer mustRecover(16)
582 f2 := reflect.MakeFunc(reflect.TypeOf((func(func()))(nil)), reflectFunc2).Interface().(func(func()))
583 f3 := reflect.MakeFunc(reflect.TypeOf((func())(nil)), reflectFunc3).Interface().(func())