Source file src/runtime/traceback_test.go

     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  		if !bytes.Contains(got, []byte(test.expect)) {
   175  			t.Errorf("traceback does not contain expected string: want %q, got\n%s", test.expect, got)
   176  		}
   177  	}
   178  }
   179  
   180  //go:noinline
   181  func testTracebackArgs1(a, b, c, d, e int) int {
   182  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   183  	if a < 0 {
   184  		// use in-reg args to keep them alive
   185  		return a + b + c + d + e
   186  	}
   187  	return n
   188  }
   189  
   190  //go:noinline
   191  func testTracebackArgs2(a bool, b struct {
   192  	a, b, c int
   193  	x       [2]int
   194  }, _ [0]int, d [3]byte) int {
   195  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   196  	if a {
   197  		// use in-reg args to keep them alive
   198  		return b.a + b.b + b.c + b.x[0] + b.x[1] + int(d[0]) + int(d[1]) + int(d[2])
   199  	}
   200  	return n
   201  
   202  }
   203  
   204  //go:noinline
   205  //go:registerparams
   206  func testTracebackArgs3(x [3]byte, a, b, c int, y [3]byte) int {
   207  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   208  	if a < 0 {
   209  		// use in-reg args to keep them alive
   210  		return int(x[0]) + int(x[1]) + int(x[2]) + a + b + c + int(y[0]) + int(y[1]) + int(y[2])
   211  	}
   212  	return n
   213  }
   214  
   215  //go:noinline
   216  func testTracebackArgs4(a bool, x [1][1][1][1][1][1][1][1][1][1]int) int {
   217  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   218  	if a {
   219  		panic(x) // use args to keep them alive
   220  	}
   221  	return n
   222  }
   223  
   224  //go:noinline
   225  func testTracebackArgs5(a bool, x struct {
   226  	x int
   227  	y [0]int
   228  	z [2][0]int
   229  }, _, _, _, _, _, _, _, _, _, _, _, _ [0]int) int {
   230  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   231  	if a {
   232  		panic(x) // use args to keep them alive
   233  	}
   234  	return n
   235  }
   236  
   237  //go:noinline
   238  func testTracebackArgs6a(a, b, c, d, e, f, g, h, i, j int) int {
   239  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   240  	if a < 0 {
   241  		// use in-reg args to keep them alive
   242  		return a + b + c + d + e + f + g + h + i + j
   243  	}
   244  	return n
   245  }
   246  
   247  //go:noinline
   248  func testTracebackArgs6b(a, b, c, d, e, f, g, h, i, j, k int) int {
   249  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   250  	if a < 0 {
   251  		// use in-reg args to keep them alive
   252  		return a + b + c + d + e + f + g + h + i + j + k
   253  	}
   254  	return n
   255  }
   256  
   257  //go:noinline
   258  func testTracebackArgs7a(a [10]int) int {
   259  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   260  	if a[0] < 0 {
   261  		// use in-reg args to keep them alive
   262  		return a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9]
   263  	}
   264  	return n
   265  }
   266  
   267  //go:noinline
   268  func testTracebackArgs7b(a [11]int) int {
   269  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   270  	if a[0] < 0 {
   271  		// use in-reg args to keep them alive
   272  		return a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9] + a[10]
   273  	}
   274  	return n
   275  }
   276  
   277  //go:noinline
   278  func testTracebackArgs7c(a [10]int, b int) int {
   279  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   280  	if a[0] < 0 {
   281  		// use in-reg args to keep them alive
   282  		return a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9] + b
   283  	}
   284  	return n
   285  }
   286  
   287  //go:noinline
   288  func testTracebackArgs7d(a [11]int, b int) int {
   289  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   290  	if a[0] < 0 {
   291  		// use in-reg args to keep them alive
   292  		return a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9] + a[10] + b
   293  	}
   294  	return n
   295  }
   296  
   297  type testArgsType8a struct {
   298  	a, b, c, d, e, f, g, h int
   299  	i                      [2]int
   300  }
   301  type testArgsType8b struct {
   302  	a, b, c, d, e, f, g, h int
   303  	i                      [3]int
   304  }
   305  type testArgsType8c struct {
   306  	a, b, c, d, e, f, g, h int
   307  	i                      [2]int
   308  	j                      int
   309  }
   310  type testArgsType8d struct {
   311  	a, b, c, d, e, f, g, h int
   312  	i                      [3]int
   313  	j                      int
   314  }
   315  
   316  //go:noinline
   317  func testTracebackArgs8a(a testArgsType8a) int {
   318  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   319  	if a.a < 0 {
   320  		// use in-reg args to keep them alive
   321  		return a.b + a.c + a.d + a.e + a.f + a.g + a.h + a.i[0] + a.i[1]
   322  	}
   323  	return n
   324  }
   325  
   326  //go:noinline
   327  func testTracebackArgs8b(a testArgsType8b) int {
   328  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   329  	if a.a < 0 {
   330  		// use in-reg args to keep them alive
   331  		return a.b + a.c + a.d + a.e + a.f + a.g + a.h + a.i[0] + a.i[1] + a.i[2]
   332  	}
   333  	return n
   334  }
   335  
   336  //go:noinline
   337  func testTracebackArgs8c(a testArgsType8c) int {
   338  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   339  	if a.a < 0 {
   340  		// use in-reg args to keep them alive
   341  		return a.b + a.c + a.d + a.e + a.f + a.g + a.h + a.i[0] + a.i[1] + a.j
   342  	}
   343  	return n
   344  }
   345  
   346  //go:noinline
   347  func testTracebackArgs8d(a testArgsType8d) int {
   348  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   349  	if a.a < 0 {
   350  		// use in-reg args to keep them alive
   351  		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
   352  	}
   353  	return n
   354  }
   355  
   356  // nosplit to avoid preemption or morestack spilling registers.
   357  //
   358  //go:nosplit
   359  //go:noinline
   360  func testTracebackArgs9(a int64, b int32, c int16, d int8, x [2]int, y int) int {
   361  	if a < 0 {
   362  		println(&y) // take address, make y live, even if no longer used at traceback
   363  	}
   364  	n := runtime.Stack(testTracebackArgsBuf[:], false)
   365  	if a < 0 {
   366  		// use half of in-reg args to keep them alive, the other half are dead
   367  		return int(a) + int(c)
   368  	}
   369  	return n
   370  }
   371  
   372  // nosplit to avoid preemption or morestack spilling registers.
   373  //
   374  //go:nosplit
   375  //go:noinline
   376  func testTracebackArgs10(a, b, c, d, e int32) int {
   377  	// no use of any args
   378  	return runtime.Stack(testTracebackArgsBuf[:], false)
   379  }
   380  
   381  // norace to avoid race instrumentation changing spill locations.
   382  // nosplit to avoid preemption or morestack spilling registers.
   383  //
   384  //go:norace
   385  //go:nosplit
   386  //go:noinline
   387  func testTracebackArgs11a(a, b, c int32) int {
   388  	if a < 0 {
   389  		println(a, b, c) // spill in a conditional, may not execute
   390  	}
   391  	if b < 0 {
   392  		return int(a + b + c)
   393  	}
   394  	return runtime.Stack(testTracebackArgsBuf[:], false)
   395  }
   396  
   397  // norace to avoid race instrumentation changing spill locations.
   398  // nosplit to avoid preemption or morestack spilling registers.
   399  //
   400  //go:norace
   401  //go:nosplit
   402  //go:noinline
   403  func testTracebackArgs11b(a, b, c, d int32) int {
   404  	var x int32
   405  	if a < 0 {
   406  		print() // spill b in a conditional
   407  		x = b
   408  	} else {
   409  		print() // spill c in a conditional
   410  		x = c
   411  	}
   412  	if d < 0 { // d is always needed
   413  		return int(x + d)
   414  	}
   415  	return runtime.Stack(testTracebackArgsBuf[:], false)
   416  }
   417  
   418  // Poison the arg area with deterministic values.
   419  //
   420  //go:noinline
   421  func poisonStack() [20]int {
   422  	return [20]int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
   423  }
   424  

View as plain text