summaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/test/zerorange_test.go
blob: e92b5d342fe136348e2c82d9c33eeb27702c3869 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package test

import (
	"testing"
)

var glob = 3
var globp *int64

// Testing compilation of arch.ZeroRange of various sizes.

// By storing a pointer to an int64 output param in a global, the compiler must
// ensure that output param is allocated on the heap. Also, since there is a
// defer, the pointer to each output param must be zeroed in the prologue (see
// plive.go:epilogue()). So, we will get a block of one or more stack slots that
// need to be zeroed. Hence, we are testing compilation completes successfully when
// zerorange calls of various sizes (8-136 bytes) are generated. We are not
// testing runtime correctness (which is hard to do for the current uses of
// ZeroRange).

func TestZeroRange(t *testing.T) {
	testZeroRange8(t)
	testZeroRange16(t)
	testZeroRange32(t)
	testZeroRange64(t)
	testZeroRange136(t)
}

func testZeroRange8(t *testing.T) (r int64) {
	defer func() {
		glob = 4
	}()
	globp = &r
	return
}

func testZeroRange16(t *testing.T) (r, s int64) {
	defer func() {
		glob = 4
	}()
	globp = &r
	globp = &s
	return
}

func testZeroRange32(t *testing.T) (r, s, t2, u int64) {
	defer func() {
		glob = 4
	}()
	globp = &r
	globp = &s
	globp = &t2
	globp = &u
	return
}

func testZeroRange64(t *testing.T) (r, s, t2, u, v, w, x, y int64) {
	defer func() {
		glob = 4
	}()
	globp = &r
	globp = &s
	globp = &t2
	globp = &u
	globp = &v
	globp = &w
	globp = &x
	globp = &y
	return
}

func testZeroRange136(t *testing.T) (r, s, t2, u, v, w, x, y, r1, s1, t1, u1, v1, w1, x1, y1, z1 int64) {
	defer func() {
		glob = 4
	}()
	globp = &r
	globp = &s
	globp = &t2
	globp = &u
	globp = &v
	globp = &w
	globp = &x
	globp = &y
	globp = &r1
	globp = &s1
	globp = &t1
	globp = &u1
	globp = &v1
	globp = &w1
	globp = &x1
	globp = &y1
	globp = &z1
	return
}

type S struct {
	x [2]uint64
	p *uint64
	y [2]uint64
	q uint64
}

type M struct {
	x [8]uint64
	p *uint64
	y [8]uint64
	q uint64
}

type L struct {
	x [4096]uint64
	p *uint64
	y [4096]uint64
	q uint64
}

//go:noinline
func triggerZerorangeLarge(f, g, h uint64) (rv0 uint64) {
	ll := L{p: &f}
	da := f
	rv0 = f + g + h
	defer func(dl L, i uint64) {
		rv0 += dl.q + i
	}(ll, da)
	return rv0
}

//go:noinline
func triggerZerorangeMedium(f, g, h uint64) (rv0 uint64) {
	ll := M{p: &f}
	rv0 = f + g + h
	defer func(dm M, i uint64) {
		rv0 += dm.q + i
	}(ll, f)
	return rv0
}

//go:noinline
func triggerZerorangeSmall(f, g, h uint64) (rv0 uint64) {
	ll := S{p: &f}
	rv0 = f + g + h
	defer func(ds S, i uint64) {
		rv0 += ds.q + i
	}(ll, f)
	return rv0
}

// This test was created as a follow up to issue #45372, to help
// improve coverage of the compiler's arch-specific "zerorange"
// function, which is invoked to zero out ambiguously live portions of
// the stack frame in certain specific circumstances.
//
// In the current compiler implementation, for zerorange to be
// invoked, we need to have an ambiguously live variable that needs
// zeroing. One way to trigger this is to have a function with an
// open-coded defer, where the opendefer function has an argument that
// contains a pointer (this is what's used below).
//
// At the moment this test doesn't do any specific checking for
// code sequence, or verification that things were properly set to zero,
// this seems as though it would be too tricky and would result
// in a "brittle" test.
//
// The small/medium/large scenarios below are inspired by the amd64
// implementation of zerorange, which generates different code
// depending on the size of the thing that needs to be zeroed out
// (I've verified at the time of the writing of this test that it
// exercises the various cases).
func TestZerorange45372(t *testing.T) {
	if r := triggerZerorangeLarge(101, 303, 505); r != 1010 {
		t.Errorf("large: wanted %d got %d", 1010, r)
	}
	if r := triggerZerorangeMedium(101, 303, 505); r != 1010 {
		t.Errorf("medium: wanted %d got %d", 1010, r)
	}
	if r := triggerZerorangeSmall(101, 303, 505); r != 1010 {
		t.Errorf("small: wanted %d got %d", 1010, r)
	}

}