Source file src/cmd/compile/internal/test/zerorange_test.go

     1  // Copyright 2019 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 test
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  var glob = 3
    12  var globp *int64
    13  
    14  // Testing compilation of arch.ZeroRange of various sizes.
    15  
    16  // By storing a pointer to an int64 output param in a global, the compiler must
    17  // ensure that output param is allocated on the heap. Also, since there is a
    18  // defer, the pointer to each output param must be zeroed in the prologue (see
    19  // plive.go:epilogue()). So, we will get a block of one or more stack slots that
    20  // need to be zeroed. Hence, we are testing compilation completes successfully when
    21  // zerorange calls of various sizes (8-136 bytes) are generated. We are not
    22  // testing runtime correctness (which is hard to do for the current uses of
    23  // ZeroRange).
    24  
    25  func TestZeroRange(t *testing.T) {
    26  	testZeroRange8(t)
    27  	testZeroRange16(t)
    28  	testZeroRange32(t)
    29  	testZeroRange64(t)
    30  	testZeroRange136(t)
    31  }
    32  
    33  func testZeroRange8(t *testing.T) (r int64) {
    34  	defer func() {
    35  		glob = 4
    36  	}()
    37  	globp = &r
    38  	return
    39  }
    40  
    41  func testZeroRange16(t *testing.T) (r, s int64) {
    42  	defer func() {
    43  		glob = 4
    44  	}()
    45  	globp = &r
    46  	globp = &s
    47  	return
    48  }
    49  
    50  func testZeroRange32(t *testing.T) (r, s, t2, u int64) {
    51  	defer func() {
    52  		glob = 4
    53  	}()
    54  	globp = &r
    55  	globp = &s
    56  	globp = &t2
    57  	globp = &u
    58  	return
    59  }
    60  
    61  func testZeroRange64(t *testing.T) (r, s, t2, u, v, w, x, y int64) {
    62  	defer func() {
    63  		glob = 4
    64  	}()
    65  	globp = &r
    66  	globp = &s
    67  	globp = &t2
    68  	globp = &u
    69  	globp = &v
    70  	globp = &w
    71  	globp = &x
    72  	globp = &y
    73  	return
    74  }
    75  
    76  func testZeroRange136(t *testing.T) (r, s, t2, u, v, w, x, y, r1, s1, t1, u1, v1, w1, x1, y1, z1 int64) {
    77  	defer func() {
    78  		glob = 4
    79  	}()
    80  	globp = &r
    81  	globp = &s
    82  	globp = &t2
    83  	globp = &u
    84  	globp = &v
    85  	globp = &w
    86  	globp = &x
    87  	globp = &y
    88  	globp = &r1
    89  	globp = &s1
    90  	globp = &t1
    91  	globp = &u1
    92  	globp = &v1
    93  	globp = &w1
    94  	globp = &x1
    95  	globp = &y1
    96  	globp = &z1
    97  	return
    98  }
    99  
   100  type S struct {
   101  	x [2]uint64
   102  	p *uint64
   103  	y [2]uint64
   104  	q uint64
   105  }
   106  
   107  type M struct {
   108  	x [8]uint64
   109  	p *uint64
   110  	y [8]uint64
   111  	q uint64
   112  }
   113  
   114  type L struct {
   115  	x [4096]uint64
   116  	p *uint64
   117  	y [4096]uint64
   118  	q uint64
   119  }
   120  
   121  //go:noinline
   122  func triggerZerorangeLarge(f, g, h uint64) (rv0 uint64) {
   123  	ll := L{p: &f}
   124  	da := f
   125  	rv0 = f + g + h
   126  	defer func(dl L, i uint64) {
   127  		rv0 += dl.q + i
   128  	}(ll, da)
   129  	return rv0
   130  }
   131  
   132  //go:noinline
   133  func triggerZerorangeMedium(f, g, h uint64) (rv0 uint64) {
   134  	ll := M{p: &f}
   135  	rv0 = f + g + h
   136  	defer func(dm M, i uint64) {
   137  		rv0 += dm.q + i
   138  	}(ll, f)
   139  	return rv0
   140  }
   141  
   142  //go:noinline
   143  func triggerZerorangeSmall(f, g, h uint64) (rv0 uint64) {
   144  	ll := S{p: &f}
   145  	rv0 = f + g + h
   146  	defer func(ds S, i uint64) {
   147  		rv0 += ds.q + i
   148  	}(ll, f)
   149  	return rv0
   150  }
   151  
   152  // This test was created as a follow up to issue #45372, to help
   153  // improve coverage of the compiler's arch-specific "zerorange"
   154  // function, which is invoked to zero out ambiguously live portions of
   155  // the stack frame in certain specific circumstances.
   156  //
   157  // In the current compiler implementation, for zerorange to be
   158  // invoked, we need to have an ambiguously live variable that needs
   159  // zeroing. One way to trigger this is to have a function with an
   160  // open-coded defer, where the opendefer function has an argument that
   161  // contains a pointer (this is what's used below).
   162  //
   163  // At the moment this test doesn't do any specific checking for
   164  // code sequence, or verification that things were properly set to zero,
   165  // this seems as though it would be too tricky and would result
   166  // in a "brittle" test.
   167  //
   168  // The small/medium/large scenarios below are inspired by the amd64
   169  // implementation of zerorange, which generates different code
   170  // depending on the size of the thing that needs to be zeroed out
   171  // (I've verified at the time of the writing of this test that it
   172  // exercises the various cases).
   173  //
   174  func TestZerorange45372(t *testing.T) {
   175  	if r := triggerZerorangeLarge(101, 303, 505); r != 1010 {
   176  		t.Errorf("large: wanted %d got %d", 1010, r)
   177  	}
   178  	if r := triggerZerorangeMedium(101, 303, 505); r != 1010 {
   179  		t.Errorf("medium: wanted %d got %d", 1010, r)
   180  	}
   181  	if r := triggerZerorangeSmall(101, 303, 505); r != 1010 {
   182  		t.Errorf("small: wanted %d got %d", 1010, r)
   183  	}
   184  
   185  }
   186  

View as plain text