| 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. |
| 4 |
|
| 5 |
package runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"internal/goexperiment" |
| 10 |
"internal/testenv" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
var testTracebackArgsBuf [1000]byte |
| 17 |
|
| 18 |
func TestTracebackArgs(t *testing.T) { |
| 19 |
if *flagQuick { |
| 20 |
t.Skip("-quick") |
| 21 |
} |
| 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 |
| 28 |
} |
| 29 |
return y |
| 30 |
} |
| 31 |
|
| 32 |
tests := []struct { |
| 33 |
fn func() int |
| 34 |
expect string |
| 35 |
}{ |
| 36 |
// simple ints |
| 37 |
{ |
| 38 |
func() int { return testTracebackArgs1(1, 2, 3, 4, 5) }, |
| 39 |
"testTracebackArgs1(0x1, 0x2, 0x3, 0x4, 0x5)", |
| 40 |
}, |
| 41 |
// some aggregates |
| 42 |
{ |
| 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}) |
| 48 |
}, |
| 49 |
"testTracebackArgs2(0x0, {0x1, 0x2, 0x3, {0x4, 0x5}}, {}, {0x6, 0x7, 0x8})", |
| 50 |
}, |
| 51 |
{ |
| 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})", |
| 54 |
}, |
| 55 |
// too deeply nested type |
| 56 |
{ |
| 57 |
func() int { return testTracebackArgs4(false, [1][1][1][1][1][1][1][1][1][1]int{}) }, |
| 58 |
"testTracebackArgs4(0x0, {{{{{...}}}}})", |
| 59 |
}, |
| 60 |
// a lot of zero-sized type |
| 61 |
{ |
| 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) |
| 69 |
}, |
| 70 |
"testTracebackArgs5(0x0, {0x1, {}, {{}, {}}}, {}, {}, {}, {}, {}, ...)", |
| 71 |
}, |
| 72 |
|
| 73 |
// edge cases for ... |
| 74 |
// no ... for 10 args |
| 75 |
{ |
| 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)", |
| 78 |
}, |
| 79 |
// has ... for 11 args |
| 80 |
{ |
| 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, ...)", |
| 83 |
}, |
| 84 |
// no ... for aggregates with 10 words |
| 85 |
{ |
| 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})", |
| 88 |
}, |
| 89 |
// has ... for aggregates with 11 words |
| 90 |
{ |
| 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, ...})", |
| 93 |
}, |
| 94 |
// no ... for aggregates, but with more args |
| 95 |
{ |
| 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}, ...)", |
| 98 |
}, |
| 99 |
// has ... for aggregates and also for more args |
| 100 |
{ |
| 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, ...}, ...)", |
| 103 |
}, |
| 104 |
// nested aggregates, no ... |
| 105 |
{ |
| 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}})", |
| 108 |
}, |
| 109 |
// nested aggregates, ... in inner but not outer |
| 110 |
{ |
| 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, ...}})", |
| 113 |
}, |
| 114 |
// nested aggregates, ... in outer but not inner |
| 115 |
{ |
| 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}, ...})", |
| 118 |
}, |
| 119 |
// nested aggregates, ... in both inner and outer |
| 120 |
{ |
| 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, ...}, ...})", |
| 123 |
}, |
| 124 |
|
| 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. |
| 128 |
{ |
| 129 |
func() int { |
| 130 |
poisonStack() // poison arg area to make output deterministic |
| 131 |
return testTracebackArgs9(1, 2, 3, 4, [2]int{5, 6}, 7) |
| 132 |
}, |
| 133 |
abiSel( |
| 134 |
"testTracebackArgs9(0x1, 0xffffffff?, 0x3, 0xff?, {0x5, 0x6}, 0x7)", |
| 135 |
"testTracebackArgs9(0x1, 0x2, 0x3, 0x4, {0x5, 0x6}, 0x7)"), |
| 136 |
}, |
| 137 |
// No live. |
| 138 |
// (Note: this assume at least 5 int registers if register ABI is used.) |
| 139 |
{ |
| 140 |
func() int { |
| 141 |
poisonStack() // poison arg area to make output deterministic |
| 142 |
return testTracebackArgs10(1, 2, 3, 4, 5) |
| 143 |
}, |
| 144 |
abiSel( |
| 145 |
"testTracebackArgs10(0xffffffff?, 0xffffffff?, 0xffffffff?, 0xffffffff?, 0xffffffff?)", |
| 146 |
"testTracebackArgs10(0x1, 0x2, 0x3, 0x4, 0x5)"), |
| 147 |
}, |
| 148 |
// Conditional spills. |
| 149 |
// Spill in conditional, not executed. |
| 150 |
{ |
| 151 |
func() int { |
| 152 |
poisonStack() // poison arg area to make output deterministic |
| 153 |
return testTracebackArgs11a(1, 2, 3) |
| 154 |
}, |
| 155 |
abiSel( |
| 156 |
"testTracebackArgs11a(0xffffffff?, 0xffffffff?, 0xffffffff?)", |
| 157 |
"testTracebackArgs11a(0x1, 0x2, 0x3)"), |
| 158 |
}, |
| 159 |
// 2 spills in conditional, not executed; 3 spills in conditional, executed, but not statically known. |
| 160 |
// So print 0x3?. |
| 161 |
{ |
| 162 |
func() int { |
| 163 |
poisonStack() // poison arg area to make output deterministic |
| 164 |
return testTracebackArgs11b(1, 2, 3, 4) |
| 165 |
}, |
| 166 |
abiSel( |
| 167 |
"testTracebackArgs11b(0xffffffff?, 0xffffffff?, 0x3?, 0x4)", |
| 168 |
"testTracebackArgs11b(0x1, 0x2, 0x3, 0x4)"), |
| 169 |
}, |
| 170 |
} |
| 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, "(")] |
| 177 |
} |
| 178 |
if !bytes.Contains(got, []byte(expect)) { |
| 179 |
t.Errorf("traceback does not contain expected string: want %q, got\n%s", expect, got) |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 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 |
| 190 |
} |
| 191 |
return n |
| 192 |
} |
| 193 |
|
| 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]) |
| 203 |
} |
| 204 |
return n |
| 205 |
|
| 206 |
} |
| 207 |
|
| 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]) |
| 215 |
} |
| 216 |
return n |
| 217 |
} |
| 218 |
|
| 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 |
| 224 |
} |
| 225 |
return n |
| 226 |
} |
| 227 |
|
| 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 |
| 237 |
} |
| 238 |
return n |
| 239 |
} |
| 240 |
|
| 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 |
| 247 |
} |
| 248 |
return n |
| 249 |
} |
| 250 |
|
| 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 |
| 257 |
} |
| 258 |
return n |
| 259 |
} |
| 260 |
|
| 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] |
| 267 |
} |
| 268 |
return n |
| 269 |
} |
| 270 |
|
| 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] |
| 277 |
} |
| 278 |
return n |
| 279 |
} |
| 280 |
|
| 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 |
| 287 |
} |
| 288 |
return n |
| 289 |
} |
| 290 |
|
| 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 |
| 297 |
} |
| 298 |
return n |
| 299 |
} |
| 300 |
|
| 301 |
type testArgsType8a struct { |
| 302 |
a, b, c, d, e, f, g, h int |
| 303 |
i [2]int |
| 304 |
} |
| 305 |
type testArgsType8b struct { |
| 306 |
a, b, c, d, e, f, g, h int |
| 307 |
i [3]int |
| 308 |
} |
| 309 |
type testArgsType8c struct { |
| 310 |
a, b, c, d, e, f, g, h int |
| 311 |
i [2]int |
| 312 |
j int |
| 313 |
} |
| 314 |
type testArgsType8d struct { |
| 315 |
a, b, c, d, e, f, g, h int |
| 316 |
i [3]int |
| 317 |
j int |
| 318 |
} |
| 319 |
|
| 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] |
| 326 |
} |
| 327 |
return n |
| 328 |
} |
| 329 |
|
| 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] |
| 336 |
} |
| 337 |
return n |
| 338 |
} |
| 339 |
|
| 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 |
| 346 |
} |
| 347 |
return n |
| 348 |
} |
| 349 |
|
| 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 |
| 356 |
} |
| 357 |
return n |
| 358 |
} |
| 359 |
|
| 360 |
// nosplit to avoid preemption or morestack spilling registers. |
| 361 |
// |
| 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 |
| 367 |
} |
| 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) |
| 372 |
} |
| 373 |
return n |
| 374 |
} |
| 375 |
|
| 376 |
// nosplit to avoid preemption or morestack spilling registers. |
| 377 |
// |
| 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) |
| 383 |
} |
| 384 |
|
| 385 |
// norace to avoid race instrumentation changing spill locations. |
| 386 |
// nosplit to avoid preemption or morestack spilling registers. |
| 387 |
// |
| 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 |
| 394 |
} |
| 395 |
if b < 0 { |
| 396 |
return int(a + b + c) |
| 397 |
} |
| 398 |
return runtime.Stack(testTracebackArgsBuf[:], false) |
| 399 |
} |
| 400 |
|
| 401 |
// norace to avoid race instrumentation changing spill locations. |
| 402 |
// nosplit to avoid preemption or morestack spilling registers. |
| 403 |
// |
| 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 |
| 415 |
} |
| 416 |
if d < 0 { // d is always needed |
| 417 |
return int(x + d) |
| 418 |
} |
| 419 |
return runtime.Stack(testTracebackArgsBuf[:], false) |
| 420 |
} |
| 421 |
|
| 422 |
// Poison the arg area with deterministic values. |
| 423 |
// |
| 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} |
| 427 |
} |