aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/defer_test.go
blob: fc961445975867b219812a9f6846790a5dc1996c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime_test

import (
	"fmt"
	"reflect"
	"runtime"
	"testing"
)

// Make sure open-coded defer exit code is not lost, even when there is an
// unconditional panic (hence no return from the function)
func TestUnconditionalPanic(t *testing.T) {
	defer func() {
		if recover() != "testUnconditional" {
			t.Fatal("expected unconditional panic")
		}
	}()
	panic("testUnconditional")
}

var glob int = 3

// Test an open-coded defer and non-open-coded defer - make sure both defers run
// and call recover()
func TestOpenAndNonOpenDefers(t *testing.T) {
	for {
		// Non-open defer because in a loop
		defer func(n int) {
			if recover() != "testNonOpenDefer" {
				t.Fatal("expected testNonOpen panic")
			}
		}(3)
		if glob > 2 {
			break
		}
	}
	testOpen(t, 47)
	panic("testNonOpenDefer")
}

//go:noinline
func testOpen(t *testing.T, arg int) {
	defer func(n int) {
		if recover() != "testOpenDefer" {
			t.Fatal("expected testOpen panic")
		}
	}(4)
	if arg > 2 {
		panic("testOpenDefer")
	}
}

// Test a non-open-coded defer and an open-coded defer - make sure both defers run
// and call recover()
func TestNonOpenAndOpenDefers(t *testing.T) {
	testOpen(t, 47)
	for {
		// Non-open defer because in a loop
		defer func(n int) {
			if recover() != "testNonOpenDefer" {
				t.Fatal("expected testNonOpen panic")
			}
		}(3)
		if glob > 2 {
			break
		}
	}
	panic("testNonOpenDefer")
}

var list []int

// Make sure that conditional open-coded defers are activated correctly and run in
// the correct order.
func TestConditionalDefers(t *testing.T) {
	list = make([]int, 0, 10)

	defer func() {
		if recover() != "testConditional" {
			t.Fatal("expected panic")
		}
		want := []int{4, 2, 1}
		if !reflect.DeepEqual(want, list) {
			t.Fatal(fmt.Sprintf("wanted %v, got %v", want, list))
		}

	}()
	testConditionalDefers(8)
}

func testConditionalDefers(n int) {
	doappend := func(i int) {
		list = append(list, i)
	}

	defer doappend(1)
	if n > 5 {
		defer doappend(2)
		if n > 8 {
			defer doappend(3)
		} else {
			defer doappend(4)
		}
	}
	panic("testConditional")
}

// Test that there is no compile-time or run-time error if an open-coded defer
// call is removed by constant propagation and dead-code elimination.
func TestDisappearingDefer(t *testing.T) {
	switch runtime.GOOS {
	case "invalidOS":
		defer func() {
			t.Fatal("Defer shouldn't run")
		}()
	}
}

// This tests an extra recursive panic behavior that is only specified in the
// code. Suppose a first panic P1 happens and starts processing defer calls. If a
// second panic P2 happens while processing defer call D in frame F, then defer
// call processing is restarted (with some potentially new defer calls created by
// D or its callees). If the defer processing reaches the started defer call D
// again in the defer stack, then the original panic P1 is aborted and cannot
// continue panic processing or be recovered. If the panic P2 does a recover at
// some point, it will naturally remove the original panic P1 from the stack
// (since the original panic had to be in frame F or a descendant of F).
func TestAbortedPanic(t *testing.T) {
	defer func() {
		r := recover()
		if r != nil {
			t.Fatal(fmt.Sprintf("wanted nil recover, got %v", r))
		}
	}()
	defer func() {
		r := recover()
		if r != "panic2" {
			t.Fatal(fmt.Sprintf("wanted %v, got %v", "panic2", r))
		}
	}()
	defer func() {
		panic("panic2")
	}()
	panic("panic1")
}

// This tests that recover() does not succeed unless it is called directly from a
// defer function that is directly called by the panic.  Here, we first call it
// from a defer function that is created by the defer function called directly by
// the panic.  In
func TestRecoverMatching(t *testing.T) {
	defer func() {
		r := recover()
		if r != "panic1" {
			t.Fatal(fmt.Sprintf("wanted %v, got %v", "panic1", r))
		}
	}()
	defer func() {
		defer func() {
			// Shouldn't succeed, even though it is called directly
			// from a defer function, since this defer function was
			// not directly called by the panic.
			r := recover()
			if r != nil {
				t.Fatal(fmt.Sprintf("wanted nil recover, got %v", r))
			}
		}()
	}()
	panic("panic1")
}

type nonSSAable [128]byte

type bigStruct struct {
	x, y, z, w, p, q int64
}

type containsBigStruct struct {
	element bigStruct
}

func mknonSSAable() nonSSAable {
	globint1++
	return nonSSAable{0, 0, 0, 0, 5}
}

var globint1, globint2, globint3 int

//go:noinline
func sideeffect(n int64) int64 {
	globint2++
	return n
}

func sideeffect2(in containsBigStruct) containsBigStruct {
	globint3++
	return in
}

// Test that nonSSAable arguments to defer are handled correctly and only evaluated once.
func TestNonSSAableArgs(t *testing.T) {
	globint1 = 0
	globint2 = 0
	globint3 = 0
	var save1 byte
	var save2 int64
	var save3 int64
	var save4 int64

	defer func() {
		if globint1 != 1 {
			t.Fatal(fmt.Sprintf("globint1:  wanted: 1, got %v", globint1))
		}
		if save1 != 5 {
			t.Fatal(fmt.Sprintf("save1:  wanted: 5, got %v", save1))
		}
		if globint2 != 1 {
			t.Fatal(fmt.Sprintf("globint2:  wanted: 1, got %v", globint2))
		}
		if save2 != 2 {
			t.Fatal(fmt.Sprintf("save2:  wanted: 2, got %v", save2))
		}
		if save3 != 4 {
			t.Fatal(fmt.Sprintf("save3:  wanted: 4, got %v", save3))
		}
		if globint3 != 1 {
			t.Fatal(fmt.Sprintf("globint3:  wanted: 1, got %v", globint3))
		}
		if save4 != 4 {
			t.Fatal(fmt.Sprintf("save1:  wanted: 4, got %v", save4))
		}
	}()

	// Test function returning a non-SSAable arg
	defer func(n nonSSAable) {
		save1 = n[4]
	}(mknonSSAable())
	// Test composite literal that is not SSAable
	defer func(b bigStruct) {
		save2 = b.y
	}(bigStruct{1, 2, 3, 4, 5, sideeffect(6)})

	// Test struct field reference that is non-SSAable
	foo := containsBigStruct{}
	foo.element.z = 4
	defer func(element bigStruct) {
		save3 = element.z
	}(foo.element)
	defer func(element bigStruct) {
		save4 = element.z
	}(sideeffect2(foo).element)
}

//go:noinline
func doPanic() {
	panic("Test panic")
}

func TestDeferForFuncWithNoExit(t *testing.T<