summaryrefslogtreecommitdiff
path: root/src/pkg/runtime/race/testdata/regression_test.go
blob: d461269d98225d3bf0471e7979399d3e52cde7cd (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
185
186
187
188
189
190
191
192
193
194
// Copyright 2012 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.

// Code patterns that caused problems in the past.

package race_test

import (
	"testing"
)

type LogImpl struct {
	x int
}

func NewLog() (l LogImpl) {
	c := make(chan bool)
	go func() {
		_ = l
		c <- true
	}()
	l = LogImpl{}
	<-c
	return
}

var _ LogImpl = NewLog()

func MakeMap() map[int]int {
	return make(map[int]int)
}

func InstrumentMapLen() {
	_ = len(MakeMap())
}

func InstrumentMapLen2() {
	m := make(map[int]map[int]int)
	_ = len(m[0])
}

func InstrumentMapLen3() {
	m := make(map[int]*map[int]int)
	_ = len(*m[0])
}

func TestRaceUnaddressableMapLen(t *testing.T) {
	m := make(map[int]map[int]int)
	ch := make(chan int, 1)
	m[0] = make(map[int]int)
	go func() {
		_ = len(m[0])
		ch <- 0
	}()
	m[0][0] = 1
	<-ch
}

type Rect struct {
	x, y int
}

type Image struct {
	min, max Rect
}

func NewImage() Image {
	var pleaseDoNotInlineMe stack
	pleaseDoNotInlineMe.push(1)
	_ = pleaseDoNotInlineMe.pop()
	return Image{}
}

func AddrOfTemp() {
	_ = NewImage().min
}

type TypeID int

func (t *TypeID) encodeType(x int) (tt TypeID, err error) {
	switch x {
	case 0:
		return t.encodeType(x * x)
	}
	return 0, nil
}

type stack []int

func (s *stack) push(x int) {
	*s = append(*s, x)
}

func (s *stack) pop() int {
	i := len(*s)
	n := (*s)[i-1]
	*s = (*s)[:i-1]
	return n
}

func TestNoRaceStackPushPop(t *testing.T) {
	var s stack
	go func(s *stack) {}(&s)
	s.push(1)
	x := s.pop()
	_ = x
}

type RpcChan struct {
	c chan bool
}

var makeChanCalls int

func makeChan() *RpcChan {
	var pleaseDoNotInlineMe stack
	pleaseDoNotInlineMe.push(1)
	_ = pleaseDoNotInlineMe.pop()

	makeChanCalls++
	c := &RpcChan{make(chan bool, 1)}
	c.c <- true
	return c
}

func call() bool {
	x := <-makeChan().c
	return x
}

func TestNoRaceRpcChan(t *testing.T) {
	makeChanCalls = 0
	_ = call()
	if makeChanCalls != 1 {
		t.Fatalf("makeChanCalls %d, expected 1\n", makeChanCalls)
	}
}

func divInSlice() {
	v := make([]int64, 10)
	i := 1
	_ = v[(i*4)/3]
}

func TestNoRaceReturn(t *testing.T) {
	c := make(chan int)
	noRaceReturn(c)
	<-c
}

// Return used to do an implicit a = a, causing a read/write race
// with the goroutine. Compiler has an optimization to avoid that now.
// See issue 4014.
func noRaceReturn(c chan int) (a, b int) {
	a = 42
	go func() {
		_ = a
		c <- 1
	}()
	return a, 10
}

func issue5431() {
	var p **inltype
	if inlinetest(p).x && inlinetest(p).y {
	} else if inlinetest(p).x || inlinetest(p).y {
	}
}

type inltype struct {
	x, y bool
}

func inlinetest(p **inltype) *inltype {
	return *p
}

type iface interface {
	Foo() *struct{ b bool }
}

type Int int

func (i Int) Foo() *struct{ b bool } {
	return &struct{ b bool }{false}
}

func TestNoRaceForInfiniteLoop(t *testing.T) {
	var x Int
	// interface conversion causes nodes to be put on init list
	for iface(x).Foo().b {
	}
}