1 // Copyright 2021 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.
9 "internal/goexperiment"
16 var testTracebackArgsBuf
[1000]byte
18 func TestTracebackArgs(t
*testing
.T
) {
22 optimized
:= !strings
.HasSuffix(testenv
.Builder(), "-noopt")
23 abiSel
:= func(x
, y
string) string {
24 // select expected output based on ABI
25 // In noopt build we always spill arguments so the output is the same as stack ABI.
26 if optimized
&& goexperiment
.RegabiArgs
{
38 func() int { return testTracebackArgs1(1, 2, 3, 4, 5) },
39 "testTracebackArgs1(0x1, 0x2, 0x3, 0x4, 0x5)",
44 return testTracebackArgs2(false, struct {
47 }{1, 2, 3, [2]int{4, 5}}, [0]int{}, [3]byte{6, 7, 8})
49 "testTracebackArgs2(0x0, {0x1, 0x2, 0x3, {0x4, 0x5}}, {}, {0x6, 0x7, 0x8})",
52 func() int { return testTracebackArgs3([3]byte{1, 2, 3}, 4, 5, 6, [3]byte{7, 8, 9}) },
53 "testTracebackArgs3({0x1, 0x2, 0x3}, 0x4, 0x5, 0x6, {0x7, 0x8, 0x9})",
55 // too deeply nested type
57 func() int { return testTracebackArgs4(false, [1][1][1][1][1][1][1][1][1][1]int{}) },
58 "testTracebackArgs4(0x0, {{{{{...}}}}})",
60 // a lot of zero-sized type
64 return testTracebackArgs5(false, struct {
68 }{1, z
, [2][0]int{}}, z
, z
, z
, z
, z
, z
, z
, z
, z
, z
, z
, z
)
70 "testTracebackArgs5(0x0, {0x1, {}, {{}, {}}}, {}, {}, {}, {}, {}, ...)",
76 func() int { return testTracebackArgs6a(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) },
77 "testTracebackArgs6a(0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa)",
79 // has ... for 11 args
81 func() int { return testTracebackArgs6b(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) },
82 "testTracebackArgs6b(0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...)",
84 // no ... for aggregates with 10 words
86 func() int { return testTracebackArgs7a([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) },
87 "testTracebackArgs7a({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa})",
89 // has ... for aggregates with 11 words
91 func() int { return testTracebackArgs7b([11]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}) },
92 "testTracebackArgs7b({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...})",
94 // no ... for aggregates, but with more args
96 func() int { return testTracebackArgs7c([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 11) },
97 "testTracebackArgs7c({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}, ...)",
99 // has ... for aggregates and also for more args
101 func() int { return testTracebackArgs7d([11]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 12) },
102 "testTracebackArgs7d({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...}, ...)",
104 // nested aggregates, no ...
106 func() int { return testTracebackArgs8a(testArgsType8a
{1, 2, 3, 4, 5, 6, 7, 8, [2]int{9, 10}}) },
107 "testTracebackArgs8a({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, {0x9, 0xa}})",
109 // nested aggregates, ... in inner but not outer
111 func() int { return testTracebackArgs8b(testArgsType8b
{1, 2, 3, 4, 5, 6, 7, 8, [3]int{9, 10, 11}}) },
112 "testTracebackArgs8b({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, {0x9, 0xa, ...}})",
114 // nested aggregates, ... in outer but not inner
116 func() int { return testTracebackArgs8c(testArgsType8c
{1, 2, 3, 4, 5, 6, 7, 8, [2]int{9, 10}, 11}) },
117 "testTracebackArgs8c({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, {0x9, 0xa}, ...})",
119 // nested aggregates, ... in both inner and outer
121 func() int { return testTracebackArgs8d(testArgsType8d
{1, 2, 3, 4, 5, 6, 7, 8, [3]int{9, 10, 11}, 12}) },
122 "testTracebackArgs8d({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, {0x9, 0xa, ...}, ...})",
125 // Register argument liveness.
126 // 1, 3 are used and live, 2, 4 are dead (in register ABI).
127 // Address-taken (7) and stack ({5, 6}) args are always live.
130 poisonStack() // poison arg area to make output deterministic
131 return testTracebackArgs9(1, 2, 3, 4, [2]int{5, 6}, 7)
134 "testTracebackArgs9(0x1, 0xffffffff?, 0x3, 0xff?, {0x5, 0x6}, 0x7)",
135 "testTracebackArgs9(0x1, 0x2, 0x3, 0x4, {0x5, 0x6}, 0x7)"),
138 // (Note: this assume at least 5 int registers if register ABI is used.)
141 poisonStack() // poison arg area to make output deterministic
142 return testTracebackArgs10(1, 2, 3, 4, 5)
145 "testTracebackArgs10(0xffffffff?, 0xffffffff?, 0xffffffff?, 0xffffffff?, 0xffffffff?)",
146 "testTracebackArgs10(0x1, 0x2, 0x3, 0x4, 0x5)"),
148 // Conditional spills.
149 // Spill in conditional, not executed.
152 poisonStack() // poison arg area to make output deterministic
153 return testTracebackArgs11a(1, 2, 3)
156 "testTracebackArgs11a(0xffffffff?, 0xffffffff?, 0xffffffff?)",
157 "testTracebackArgs11a(0x1, 0x2, 0x3)"),
159 // 2 spills in conditional, not executed; 3 spills in conditional, executed, but not statically known.
163 poisonStack() // poison arg area to make output deterministic
164 return testTracebackArgs11b(1, 2, 3, 4)
167 "testTracebackArgs11b(0xffffffff?, 0xffffffff?, 0x3?, 0x4)",
168 "testTracebackArgs11b(0x1, 0x2, 0x3, 0x4)"),
171 for _
, test
:= range tests
{
173 got
:= testTracebackArgsBuf
[:n
]
174 expect
:= test
.expect
175 if runtime
.Compiler
== "gccgo" {
176 expect
= expect
[:strings
.Index(expect
, "(")]
178 if !bytes
.Contains(got
, []byte(expect
)) {
179 t
.Errorf("traceback does not contain expected string: want %q, got\n%s", expect
, got
)
185 func testTracebackArgs1(a
, b
, c
, d
, e
int) int {
186 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
188 // use in-reg args to keep them alive
189 return a
+ b
+ c
+ d
+ e
195 func testTracebackArgs2(a
bool, b
struct {
198 }, _
[0]int, d
[3]byte) int {
199 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
201 // use in-reg args to keep them alive
202 return b
.a
+ b
.b
+ b
.c
+ b
.x
[0] + b
.x
[1] + int(d
[0]) + int(d
[1]) + int(d
[2])
210 func testTracebackArgs3(x
[3]byte, a
, b
, c
int, y
[3]byte) int {
211 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
213 // use in-reg args to keep them alive
214 return int(x
[0]) + int(x
[1]) + int(x
[2]) + a
+ b
+ c
+ int(y
[0]) + int(y
[1]) + int(y
[2])
220 func testTracebackArgs4(a
bool, x
[1][1][1][1][1][1][1][1][1][1]int) int {
221 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
223 panic(x
) // use args to keep them alive
229 func testTracebackArgs5(a
bool, x
struct {
233 }, _
, _
, _
, _
, _
, _
, _
, _
, _
, _
, _
, _
[0]int) int {
234 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
236 panic(x
) // use args to keep them alive
242 func testTracebackArgs6a(a
, b
, c
, d
, e
, f
, g
, h
, i
, j
int) int {
243 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
245 // use in-reg args to keep them alive
246 return a
+ b
+ c
+ d
+ e
+ f
+ g
+ h
+ i
+ j
252 func testTracebackArgs6b(a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
int) int {
253 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
255 // use in-reg args to keep them alive
256 return a
+ b
+ c
+ d
+ e
+ f
+ g
+ h
+ i
+ j
+ k
262 func testTracebackArgs7a(a
[10]int) int {
263 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
265 // use in-reg args to keep them alive
266 return a
[1] + a
[2] + a
[3] + a
[4] + a
[5] + a
[6] + a
[7] + a
[8] + a
[9]
272 func testTracebackArgs7b(a
[11]int) int {
273 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
275 // use in-reg args to keep them alive
276 return a
[1] + a
[2] + a
[3] + a
[4] + a
[5] + a
[6] + a
[7] + a
[8] + a
[9] + a
[10]
282 func testTracebackArgs7c(a
[10]int, b
int) int {
283 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
285 // use in-reg args to keep them alive
286 return a
[1] + a
[2] + a
[3] + a
[4] + a
[5] + a
[6] + a
[7] + a
[8] + a
[9] + b
292 func testTracebackArgs7d(a
[11]int, b
int) int {
293 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
295 // use in-reg args to keep them alive
296 return a
[1] + a
[2] + a
[3] + a
[4] + a
[5] + a
[6] + a
[7] + a
[8] + a
[9] + a
[10] + b
301 type testArgsType8a
struct {
302 a
, b
, c
, d
, e
, f
, g
, h
int
305 type testArgsType8b
struct {
306 a
, b
, c
, d
, e
, f
, g
, h
int
309 type testArgsType8c
struct {
310 a
, b
, c
, d
, e
, f
, g
, h
int
314 type testArgsType8d
struct {
315 a
, b
, c
, d
, e
, f
, g
, h
int
321 func testTracebackArgs8a(a testArgsType8a
) int {
322 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
324 // use in-reg args to keep them alive
325 return a
.b
+ a
.c
+ a
.d
+ a
.e
+ a
.f
+ a
.g
+ a
.h
+ a
.i
[0] + a
.i
[1]
331 func testTracebackArgs8b(a testArgsType8b
) int {
332 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
334 // use in-reg args to keep them alive
335 return a
.b
+ a
.c
+ a
.d
+ a
.e
+ a
.f
+ a
.g
+ a
.h
+ a
.i
[0] + a
.i
[1] + a
.i
[2]
341 func testTracebackArgs8c(a testArgsType8c
) int {
342 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
344 // use in-reg args to keep them alive
345 return a
.b
+ a
.c
+ a
.d
+ a
.e
+ a
.f
+ a
.g
+ a
.h
+ a
.i
[0] + a
.i
[1] + a
.j
351 func testTracebackArgs8d(a testArgsType8d
) int {
352 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
354 // use in-reg args to keep them alive
355 return a
.b
+ a
.c
+ a
.d
+ a
.e
+ a
.f
+ a
.g
+ a
.h
+ a
.i
[0] + a
.i
[1] + a
.i
[2] + a
.j
360 // nosplit to avoid preemption or morestack spilling registers.
364 func testTracebackArgs9(a
int64, b
int32, c
int16, d
int8, x
[2]int, y
int) int {
366 println(&y
) // take address, make y live, even if no longer used at traceback
368 n
:= runtime
.Stack(testTracebackArgsBuf
[:], false)
370 // use half of in-reg args to keep them alive, the other half are dead
371 return int(a
) + int(c
)
376 // nosplit to avoid preemption or morestack spilling registers.
380 func testTracebackArgs10(a
, b
, c
, d
, e
int32) int {
381 // no use of any args
382 return runtime
.Stack(testTracebackArgsBuf
[:], false)
385 // norace to avoid race instrumentation changing spill locations.
386 // nosplit to avoid preemption or morestack spilling registers.
391 func testTracebackArgs11a(a
, b
, c
int32) int {
393 println(a
, b
, c
) // spill in a conditional, may not execute
396 return int(a
+ b
+ c
)
398 return runtime
.Stack(testTracebackArgsBuf
[:], false)
401 // norace to avoid race instrumentation changing spill locations.
402 // nosplit to avoid preemption or morestack spilling registers.
407 func testTracebackArgs11b(a
, b
, c
, d
int32) int {
410 print() // spill b in a conditional
413 print() // spill c in a conditional
416 if d
< 0 { // d is always needed
419 return runtime
.Stack(testTracebackArgsBuf
[:], false)
422 // Poison the arg area with deterministic values.
425 func poisonStack() [20]int {
426 return [20]int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}