-
Notifications
You must be signed in to change notification settings - Fork 17.6k
/
callers_test.go
489 lines (444 loc) · 12.1 KB
/
callers_test.go
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2016 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 (
"runtime"
"slices"
"strings"
"testing"
)
func f1(pan bool) []uintptr {
return f2(pan) // line 15
}
func f2(pan bool) []uintptr {
return f3(pan) // line 19
}
func f3(pan bool) []uintptr {
if pan {
panic("f3") // line 24
}
ret := make([]uintptr, 20)
return ret[:runtime.Callers(0, ret)] // line 27
}
func testCallers(t *testing.T, pcs []uintptr, pan bool) {
m := make(map[string]int, len(pcs))
frames := runtime.CallersFrames(pcs)
for {
frame, more := frames.Next()
if frame.Function != "" {
m[frame.Function] = frame.Line
}
if !more {
break
}
}
var seen []string
for k := range m {
seen = append(seen, k)
}
t.Logf("functions seen: %s", strings.Join(seen, " "))
var f3Line int
if pan {
f3Line = 24
} else {
f3Line = 27
}
want := []struct {
name string
line int
}{
{"f1", 15},
{"f2", 19},
{"f3", f3Line},
}
for _, w := range want {
if got := m["runtime_test."+w.name]; got != w.line {
t.Errorf("%s is line %d, want %d", w.name, got, w.line)
}
}
}
func testCallersEqual(t *testing.T, pcs []uintptr, want []string) {
t.Helper()
got := make([]string, 0, len(want))
frames := runtime.CallersFrames(pcs)
for {
frame, more := frames.Next()
if !more || len(got) >= len(want) {
break
}
got = append(got, frame.Function)
}
if !slices.Equal(want, got) {
t.Fatalf("wanted %v, got %v", want, got)
}
}
func TestCallers(t *testing.T) {
testCallers(t, f1(false), false)
}
func TestCallersPanic(t *testing.T) {
// Make sure we don't have any extra frames on the stack (due to
// open-coded defer processing)
want := []string{"runtime.Callers", "runtime_test.TestCallersPanic.func1",
"runtime.gopanic", "runtime_test.f3", "runtime_test.f2", "runtime_test.f1",
"runtime_test.TestCallersPanic"}
defer func() {
if r := recover(); r == nil {
t.Fatal("did not panic")
}
pcs := make([]uintptr, 20)
pcs = pcs[:runtime.Callers(0, pcs)]
testCallers(t, pcs, true)
testCallersEqual(t, pcs, want)
}()
f1(true)
}
func TestCallersDoublePanic(t *testing.T) {
// Make sure we don't have any extra frames on the stack (due to
// open-coded defer processing)
want := []string{"runtime.Callers", "runtime_test.TestCallersDoublePanic.func1.1",
"runtime.gopanic", "runtime_test.TestCallersDoublePanic.func1", "runtime.gopanic", "runtime_test.TestCallersDoublePanic"}
defer func() {
defer func() {
pcs := make([]uintptr, 20)
pcs = pcs[:runtime.Callers(0, pcs)]
if recover() == nil {
t.Fatal("did not panic")
}
testCallersEqual(t, pcs, want)
}()
if recover() == nil {
t.Fatal("did not panic")
}
panic(2)
}()
panic(1)
}
// Test that a defer after a successful recovery looks like it is called directly
// from the function with the defers.
func TestCallersAfterRecovery(t *testing.T) {
want := []string{"runtime.Callers", "runtime_test.TestCallersAfterRecovery.func1", "runtime_test.TestCallersAfterRecovery"}
defer func() {
pcs := make([]uintptr, 20)
pcs = pcs[:runtime.Callers(0, pcs)]
testCallersEqual(t, pcs, want)
}()
defer func() {
if recover() == nil {
t.Fatal("did not recover from panic")
}
}()
panic(1)
}
func TestCallersAbortedPanic(t *testing.T) {
want := []string{"runtime.Callers", "runtime_test.TestCallersAbortedPanic.func2", "runtime_test.TestCallersAbortedPanic"}
defer func() {
r := recover()
if r != nil {
t.Fatalf("should be no panic remaining to recover")
}
}()
defer func() {
// panic2 was aborted/replaced by panic1, so when panic2 was
// recovered, there is no remaining panic on the stack.
pcs := make([]uintptr, 20)
pcs = pcs[:runtime.Callers(0, pcs)]
testCallersEqual(t, pcs, want)
}()
defer func() {
r := recover()
if r != "panic2" {
t.Fatalf("got %v, wanted %v", r, "panic2")
}
}()
defer func() {
// panic2 aborts/replaces panic1, because it is a recursive panic
// that is not recovered within the defer function called by
// panic1 panicking sequence
panic("panic2")
}()
panic("panic1")
}
func TestCallersAbortedPanic2(t *testing.T) {
want := []string{"runtime.Callers", "runtime_test.TestCallersAbortedPanic2.func2", "runtime_test.TestCallersAbortedPanic2"}
defer func() {
r := recover()
if r != nil {
t.Fatalf("should be no panic remaining to recover")
}
}()
defer func() {
pcs := make([]uintptr, 20)
pcs = pcs[:runtime.Callers(0, pcs)]
testCallersEqual(t, pcs, want)
}()
func() {
defer func() {
r := recover()
if r != "panic2" {
t.Fatalf("got %v, wanted %v", r, "panic2")
}
}()
func() {
defer func() {
// Again, panic2 aborts/replaces panic1
panic("panic2")
}()
panic("panic1")
}()
}()
}
func TestCallersNilPointerPanic(t *testing.T) {
// Make sure we don't have any extra frames on the stack (due to
// open-coded defer processing)
want := []string{"runtime.Callers", "runtime_test.TestCallersNilPointerPanic.func1",
"runtime.gopanic", "runtime.panicmem", "runtime.sigpanic",
"runtime_test.TestCallersNilPointerPanic"}
defer func() {
if r := recover(); r == nil {
t.Fatal("did not panic")
}
pcs := make([]uintptr, 20)
pcs = pcs[:runtime.Callers(0, pcs)]
testCallersEqual(t, pcs, want)
}()
var p *int
if *p == 3 {
t.Fatal("did not see nil pointer panic")
}
}
func TestCallersDivZeroPanic(t *testing.T) {
// Make sure we don't have any extra frames on the stack (due to
// open-coded defer processing)
want := []string{"runtime.Callers", "runtime_test.TestCallersDivZeroPanic.func1",
"runtime.gopanic", "runtime.panicdivide",
"runtime_test.TestCallersDivZeroPanic"}
defer func() {
if r := recover(); r == nil {
t.Fatal("did not panic")
}
pcs := make([]uintptr, 20)
pcs = pcs[:runtime.Callers(0, pcs)]
testCallersEqual(t, pcs, want)
}()
var n int
if 5/n == 1 {
t.Fatal("did not see divide-by-sizer panic")
}
}
func TestCallersDeferNilFuncPanic(t *testing.T) {
// Make sure we don't have any extra frames on the stack. We cut off the check
// at runtime.sigpanic, because non-open-coded defers (which may be used in
// non-opt or race checker mode) include an extra 'deferreturn' frame (which is
// where the nil pointer deref happens).
state := 1
want := []string{"runtime.Callers", "runtime_test.TestCallersDeferNilFuncPanic.func1",
"runtime.gopanic", "runtime.panicmem", "runtime.sigpanic"}
defer func() {
if r := recover(); r == nil {
t.Fatal("did not panic")
}
pcs := make([]uintptr, 20)
pcs = pcs[:runtime.Callers(0, pcs)]
testCallersEqual(t, pcs, want)
if state == 1 {
t.Fatal("nil defer func panicked at defer time rather than function exit time")
}
}()
var f func()
defer f()
// Use the value of 'state' to make sure nil defer func f causes panic at
// function exit, rather than at the defer statement.
state = 2
}
// Same test, but forcing non-open-coded defer by putting the defer in a loop. See
// issue #36050
func TestCallersDeferNilFuncPanicWithLoop(t *testing.T) {
state := 1
want := []string{"runtime.Callers", "runtime_test.TestCallersDeferNilFuncPanicWithLoop.func1",
"runtime.gopanic", "runtime.panicmem", "runtime.sigpanic", "runtime.deferreturn", "runtime_test.TestCallersDeferNilFuncPanicWithLoop"}
defer func() {
if r := recover(); r == nil {
t.Fatal("did not panic")
}
pcs := make([]uintptr, 20)
pcs = pcs[:runtime.Callers(0, pcs)]
testCallersEqual(t, pcs, want)
if state == 1 {
t.Fatal("nil defer func panicked at defer time rather than function exit time")
}
}()
for i := 0; i < 1; i++ {
var f func()
defer f()
}
// Use the value of 'state' to make sure nil defer func f causes panic at
// function exit, rather than at the defer statement.
state = 2
}
// issue #51988
// Func.Endlineno was lost when instantiating generic functions, leading to incorrect
// stack trace positions.
func TestCallersEndlineno(t *testing.T) {
testNormalEndlineno(t)
testGenericEndlineno[int](t)
}
func testNormalEndlineno(t *testing.T) {
defer testCallerLine(t, callerLine(t, 0)+1)
}
func testGenericEndlineno[_ any](t *testing.T) {
defer testCallerLine(t, callerLine(t, 0)+1)
}
func testCallerLine(t *testing.T, want int) {
if have := callerLine(t, 1); have != want {
t.Errorf("callerLine(1) returned %d, but want %d\n", have, want)
}
}
func callerLine(t *testing.T, skip int) int {
_, _, line, ok := runtime.Caller(skip + 1)
if !ok {
t.Fatalf("runtime.Caller(%d) failed", skip+1)
}
return line
}
func BenchmarkCallers(b *testing.B) {
b.Run("cached", func(b *testing.B) {
// Very pcvalueCache-friendly, no inlining.
callersCached(b, 100)
})
b.Run("inlined", func(b *testing.B) {
// Some inlining, still pretty cache-friendly.
callersInlined(b, 100)
})
b.Run("no-cache", func(b *testing.B) {
// Cache-hostile
callersNoCache(b, 100)
})
}
func callersCached(b *testing.B, n int) int {
if n <= 0 {
pcs := make([]uintptr, 32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
runtime.Callers(0, pcs)
}
b.StopTimer()
return 0
}
return 1 + callersCached(b, n-1)
}
func callersInlined(b *testing.B, n int) int {
if n <= 0 {
pcs := make([]uintptr, 32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
runtime.Callers(0, pcs)
}
b.StopTimer()
return 0
}
return 1 + callersInlined1(b, n-1)
}
func callersInlined1(b *testing.B, n int) int { return callersInlined2(b, n) }
func callersInlined2(b *testing.B, n int) int { return callersInlined3(b, n) }
func callersInlined3(b *testing.B, n int) int { return callersInlined4(b, n) }
func callersInlined4(b *testing.B, n int) int { return callersInlined(b, n) }
func callersNoCache(b *testing.B, n int) int {
if n <= 0 {
pcs := make([]uintptr, 32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
runtime.Callers(0, pcs)
}
b.StopTimer()
return 0
}
switch n % 16 {
case 0:
return 1 + callersNoCache(b, n-1)
case 1:
return 1 + callersNoCache(b, n-1)
case 2:
return 1 + callersNoCache(b, n-1)
case 3:
return 1 + callersNoCache(b, n-1)
case 4:
return 1 + callersNoCache(b, n-1)
case 5:
return 1 + callersNoCache(b, n-1)
case 6:
return 1 + callersNoCache(b, n-1)
case 7:
return 1 + callersNoCache(b, n-1)
case 8:
return 1 + callersNoCache(b, n-1)
case 9:
return 1 + callersNoCache(b, n-1)
case 10:
return 1 + callersNoCache(b, n-1)
case 11:
return 1 + callersNoCache(b, n-1)
case 12:
return 1 + callersNoCache(b, n-1)
case 13:
return 1 + callersNoCache(b, n-1)
case 14:
return 1 + callersNoCache(b, n-1)
default:
return 1 + callersNoCache(b, n-1)
}
}
func BenchmarkFPCallers(b *testing.B) {
b.Run("cached", func(b *testing.B) {
// Very pcvalueCache-friendly, no inlining.
fpCallersCached(b, 100)
})
}
func fpCallersCached(b *testing.B, n int) int {
if n <= 0 {
pcs := make([]uintptr, 32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
runtime.FPCallers(pcs)
}
b.StopTimer()
return 0
}
return 1 + fpCallersCached(b, n-1)
}
func TestFPUnwindAfterRecovery(t *testing.T) {
if !runtime.FramePointerEnabled {
t.Skip("frame pointers not supported for this architecture")
}
// Make sure that frame pointer unwinding succeeds from a deferred
// function run after recovering from a panic. It can fail if the
// recovery does not properly restore the caller's frame pointer before
// running the remaining deferred functions.
//
// This test does not verify the accuracy of the call stack (it
// currently includes a frame from runtime.deferreturn which would
// normally be omitted). It is only intended to check that producing the
// call stack won't crash.
defer func() {
pcs := make([]uintptr, 32)
for i := range pcs {
// If runtime.recovery doesn't properly restore the
// frame pointer before returning control to this
// function, it will point somewhere lower in the stack
// from one of the frames of runtime.gopanic() or one of
// it's callees prior to recovery. So, we put some
// non-zero values on the stack to ensure that frame
// pointer unwinding will crash if it sees the old,
// invalid frame pointer.
pcs[i] = 10
}
runtime.FPCallers(pcs)
t.Logf("%v", pcs)
}()
defer func() {
if recover() == nil {
t.Fatal("did not recover from panic")
}
}()
panic(1)
}