hppa: Fix ICE caused by mismatched predicate and constraint in xmpyu patterns
[official-gcc.git] / libgo / go / runtime / traceback_test.go
blob259773141fa804fc79e44b03f91a7b2aac4b9492
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.
5 package runtime_test
7 import (
8 "bytes"
9 "internal/goexperiment"
10 "internal/testenv"
11 "runtime"
12 "strings"
13 "testing"
16 var testTracebackArgsBuf [1000]byte
18 func TestTracebackArgs(t *testing.T) {
19 if *flagQuick {
20 t.Skip("-quick")
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 {
27 return x
29 return y
32 tests := []struct {
33 fn func() int
34 expect string
36 // simple ints
38 func() int { return testTracebackArgs1(1, 2, 3, 4, 5) },
39 "testTracebackArgs1(0x1, 0x2, 0x3, 0x4, 0x5)",
41 // some aggregates
43 func() int {
44 return testTracebackArgs2(false, struct {
45 a, b, c int
46 x [2]int
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
62 func() int {
63 z := [0]int{}
64 return testTracebackArgs5(false, struct {
65 x int
66 y [0]int
67 z [2][0]int
68 }{1, z, [2][0]int{}}, z, z, z, z, z, z, z, z, z, z, z, z)
70 "testTracebackArgs5(0x0, {0x1, {}, {{}, {}}}, {}, {}, {}, {}, {}, ...)",
73 // edge cases for ...
74 // no ... for 10 args
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.
129 func() int {
130 poisonStack() // poison arg area to make output deterministic
131 return testTracebackArgs9(1, 2, 3, 4, [2]int{5, 6}, 7)
133 abiSel(
134 "testTracebackArgs9(0x1, 0xffffffff?, 0x3, 0xff?, {0x5, 0x6}, 0x7)",
135 "testTracebackArgs9(0x1, 0x2, 0x3, 0x4, {0x5, 0x6}, 0x7)"),
137 // No live.
138 // (Note: this assume at least 5 int registers if register ABI is used.)
140 func() int {
141 poisonStack() // poison arg area to make output deterministic
142 return testTracebackArgs10(1, 2, 3, 4, 5)
144 abiSel(
145 "testTracebackArgs10(0xffffffff?, 0xffffffff?, 0xffffffff?, 0xffffffff?, 0xffffffff?)",
146 "testTracebackArgs10(0x1, 0x2, 0x3, 0x4, 0x5)"),
148 // Conditional spills.
149 // Spill in conditional, not executed.
151 func() int {
152 poisonStack() // poison arg area to make output deterministic
153 return testTracebackArgs11a(1, 2, 3)
155 abiSel(
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.
160 // So print 0x3?.
162 func() int {
163 poisonStack() // poison arg area to make output deterministic
164 return testTracebackArgs11b(1, 2, 3, 4)
166 abiSel(
167 "testTracebackArgs11b(0xffffffff?, 0xffffffff?, 0x3?, 0x4)",
168 "testTracebackArgs11b(0x1, 0x2, 0x3, 0x4)"),
171 for _, test := range tests {
172 n := test.fn()
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)
184 //go:noinline
185 func testTracebackArgs1(a, b, c, d, e int) int {
186 n := runtime.Stack(testTracebackArgsBuf[:], false)
187 if a < 0 {
188 // use in-reg args to keep them alive
189 return a + b + c + d + e
191 return n
194 //go:noinline
195 func testTracebackArgs2(a bool, b struct {
196 a, b, c int
197 x [2]int
198 }, _ [0]int, d [3]byte) int {
199 n := runtime.Stack(testTracebackArgsBuf[:], false)
200 if a {
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])
204 return n
208 //go:noinline
209 //go:registerparams
210 func testTracebackArgs3(x [3]byte, a, b, c int, y [3]byte) int {
211 n := runtime.Stack(testTracebackArgsBuf[:], false)
212 if a < 0 {
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])
216 return n
219 //go:noinline
220 func testTracebackArgs4(a bool, x [1][1][1][1][1][1][1][1][1][1]int) int {
221 n := runtime.Stack(testTracebackArgsBuf[:], false)
222 if a {
223 panic(x) // use args to keep them alive
225 return n
228 //go:noinline
229 func testTracebackArgs5(a bool, x struct {
230 x int
231 y [0]int
232 z [2][0]int
233 }, _, _, _, _, _, _, _, _, _, _, _, _ [0]int) int {
234 n := runtime.Stack(testTracebackArgsBuf[:], false)
235 if a {
236 panic(x) // use args to keep them alive
238 return n
241 //go:noinline
242 func testTracebackArgs6a(a, b, c, d, e, f, g, h, i, j int) int {
243 n := runtime.Stack(testTracebackArgsBuf[:], false)
244 if a < 0 {
245 // use in-reg args to keep them alive
246 return a + b + c + d + e + f + g + h + i + j
248 return n
251 //go:noinline
252 func testTracebackArgs6b(a, b, c, d, e, f, g, h, i, j, k int) int {
253 n := runtime.Stack(testTracebackArgsBuf[:], false)
254 if a < 0 {
255 // use in-reg args to keep them alive
256 return a + b + c + d + e + f + g + h + i + j + k
258 return n
261 //go:noinline
262 func testTracebackArgs7a(a [10]int) int {
263 n := runtime.Stack(testTracebackArgsBuf[:], false)
264 if a[0] < 0 {
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]
268 return n
271 //go:noinline
272 func testTracebackArgs7b(a [11]int) int {
273 n := runtime.Stack(testTracebackArgsBuf[:], false)
274 if a[0] < 0 {
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]
278 return n
281 //go:noinline
282 func testTracebackArgs7c(a [10]int, b int) int {
283 n := runtime.Stack(testTracebackArgsBuf[:], false)
284 if a[0] < 0 {
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
288 return n
291 //go:noinline
292 func testTracebackArgs7d(a [11]int, b int) int {
293 n := runtime.Stack(testTracebackArgsBuf[:], false)
294 if a[0] < 0 {
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
298 return n
301 type testArgsType8a struct {
302 a, b, c, d, e, f, g, h int
303 i [2]int
305 type testArgsType8b struct {
306 a, b, c, d, e, f, g, h int
307 i [3]int
309 type testArgsType8c struct {
310 a, b, c, d, e, f, g, h int
311 i [2]int
312 j int
314 type testArgsType8d struct {
315 a, b, c, d, e, f, g, h int
316 i [3]int
317 j int
320 //go:noinline
321 func testTracebackArgs8a(a testArgsType8a) int {
322 n := runtime.Stack(testTracebackArgsBuf[:], false)
323 if a.a < 0 {
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]
327 return n
330 //go:noinline
331 func testTracebackArgs8b(a testArgsType8b) int {
332 n := runtime.Stack(testTracebackArgsBuf[:], false)
333 if a.a < 0 {
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]
337 return n
340 //go:noinline
341 func testTracebackArgs8c(a testArgsType8c) int {
342 n := runtime.Stack(testTracebackArgsBuf[:], false)
343 if a.a < 0 {
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
347 return n
350 //go:noinline
351 func testTracebackArgs8d(a testArgsType8d) int {
352 n := runtime.Stack(testTracebackArgsBuf[:], false)
353 if a.a < 0 {
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
357 return n
360 // nosplit to avoid preemption or morestack spilling registers.
362 //go:nosplit
363 //go:noinline
364 func testTracebackArgs9(a int64, b int32, c int16, d int8, x [2]int, y int) int {
365 if a < 0 {
366 println(&y) // take address, make y live, even if no longer used at traceback
368 n := runtime.Stack(testTracebackArgsBuf[:], false)
369 if a < 0 {
370 // use half of in-reg args to keep them alive, the other half are dead
371 return int(a) + int(c)
373 return n
376 // nosplit to avoid preemption or morestack spilling registers.
378 //go:nosplit
379 //go:noinline
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.
388 //go:norace
389 //go:nosplit
390 //go:noinline
391 func testTracebackArgs11a(a, b, c int32) int {
392 if a < 0 {
393 println(a, b, c) // spill in a conditional, may not execute
395 if b < 0 {
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.
404 //go:norace
405 //go:nosplit
406 //go:noinline
407 func testTracebackArgs11b(a, b, c, d int32) int {
408 var x int32
409 if a < 0 {
410 print() // spill b in a conditional
411 x = b
412 } else {
413 print() // spill c in a conditional
414 x = c
416 if d < 0 { // d is always needed
417 return int(x + d)
419 return runtime.Stack(testTracebackArgsBuf[:], false)
422 // Poison the arg area with deterministic values.
424 //go:noinline
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}