github.com/euank/go@v0.0.0-20160829210321-495514729181/src/runtime/race/testdata/mop_test.go (about)

     1  // Copyright 2011 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 race_test
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/sha1"
    10  	"errors"
    11  	"fmt"
    12  	"io"
    13  	"os"
    14  	"runtime"
    15  	"sync"
    16  	"testing"
    17  	"time"
    18  	"unsafe"
    19  )
    20  
    21  type Point struct {
    22  	x, y int
    23  }
    24  
    25  type NamedPoint struct {
    26  	name string
    27  	p    Point
    28  }
    29  
    30  type DummyWriter struct {
    31  	state int
    32  }
    33  type Writer interface {
    34  	Write(p []byte) (n int)
    35  }
    36  
    37  func (d DummyWriter) Write(p []byte) (n int) {
    38  	return 0
    39  }
    40  
    41  var GlobalX, GlobalY int = 0, 0
    42  var GlobalCh chan int = make(chan int, 2)
    43  
    44  func GlobalFunc1() {
    45  	GlobalY = GlobalX
    46  	GlobalCh <- 1
    47  }
    48  
    49  func GlobalFunc2() {
    50  	GlobalX = 1
    51  	GlobalCh <- 1
    52  }
    53  
    54  func TestRaceIntRWGlobalFuncs(t *testing.T) {
    55  	go GlobalFunc1()
    56  	go GlobalFunc2()
    57  	<-GlobalCh
    58  	<-GlobalCh
    59  }
    60  
    61  func TestRaceIntRWClosures(t *testing.T) {
    62  	var x, y int
    63  	ch := make(chan int, 2)
    64  
    65  	go func() {
    66  		y = x
    67  		ch <- 1
    68  	}()
    69  	go func() {
    70  		x = 1
    71  		ch <- 1
    72  	}()
    73  	<-ch
    74  	<-ch
    75  }
    76  
    77  func TestNoRaceIntRWClosures(t *testing.T) {
    78  	var x, y int
    79  	ch := make(chan int, 1)
    80  
    81  	go func() {
    82  		y = x
    83  		ch <- 1
    84  	}()
    85  	<-ch
    86  	go func() {
    87  		x = 1
    88  		ch <- 1
    89  	}()
    90  	<-ch
    91  
    92  }
    93  
    94  func TestRaceInt32RWClosures(t *testing.T) {
    95  	var x, y int32
    96  	ch := make(chan bool, 2)
    97  
    98  	go func() {
    99  		y = x
   100  		ch <- true
   101  	}()
   102  	go func() {
   103  		x = 1
   104  		ch <- true
   105  	}()
   106  	<-ch
   107  	<-ch
   108  }
   109  
   110  func TestNoRaceCase(t *testing.T) {
   111  	var y int
   112  	for x := -1; x <= 1; x++ {
   113  		switch {
   114  		case x < 0:
   115  			y = -1
   116  		case x == 0:
   117  			y = 0
   118  		case x > 0:
   119  			y = 1
   120  		}
   121  	}
   122  	y++
   123  }
   124  
   125  func TestRaceCaseCondition(t *testing.T) {
   126  	var x int = 0
   127  	ch := make(chan int, 2)
   128  
   129  	go func() {
   130  		x = 2
   131  		ch <- 1
   132  	}()
   133  	go func() {
   134  		switch x < 2 {
   135  		case true:
   136  			x = 1
   137  			//case false:
   138  			//	x = 5
   139  		}
   140  		ch <- 1
   141  	}()
   142  	<-ch
   143  	<-ch
   144  }
   145  
   146  func TestRaceCaseCondition2(t *testing.T) {
   147  	// switch body is rearranged by the compiler so the tests
   148  	// passes even if we don't instrument '<'
   149  	var x int = 0
   150  	ch := make(chan int, 2)
   151  
   152  	go func() {
   153  		x = 2
   154  		ch <- 1
   155  	}()
   156  	go func() {
   157  		switch x < 2 {
   158  		case true:
   159  			x = 1
   160  		case false:
   161  			x = 5
   162  		}
   163  		ch <- 1
   164  	}()
   165  	<-ch
   166  	<-ch
   167  }
   168  
   169  func TestRaceCaseBody(t *testing.T) {
   170  	var x, y int
   171  	ch := make(chan int, 2)
   172  
   173  	go func() {
   174  		y = x
   175  		ch <- 1
   176  	}()
   177  	go func() {
   178  		switch {
   179  		default:
   180  			x = 1
   181  		case x == 100:
   182  			x = -x
   183  		}
   184  		ch <- 1
   185  	}()
   186  	<-ch
   187  	<-ch
   188  }
   189  
   190  func TestNoRaceCaseFallthrough(t *testing.T) {
   191  	var x, y, z int
   192  	ch := make(chan int, 2)
   193  	z = 1
   194  
   195  	go func() {
   196  		y = x
   197  		ch <- 1
   198  	}()
   199  	go func() {
   200  		switch {
   201  		case z == 1:
   202  		case z == 2:
   203  			x = 2
   204  		}
   205  		ch <- 1
   206  	}()
   207  	<-ch
   208  	<-ch
   209  }
   210  
   211  func TestRaceCaseFallthrough(t *testing.T) {
   212  	var x, y, z int
   213  	ch := make(chan int, 2)
   214  	z = 1
   215  
   216  	go func() {
   217  		y = x
   218  		ch <- 1
   219  	}()
   220  	go func() {
   221  		switch {
   222  		case z == 1:
   223  			fallthrough
   224  		case z == 2:
   225  			x = 2
   226  		}
   227  		ch <- 1
   228  	}()
   229  
   230  	<-ch
   231  	<-ch
   232  }
   233  
   234  func TestRaceCaseIssue6418(t *testing.T) {
   235  	m := map[string]map[string]string{
   236  		"a": {
   237  			"b": "c",
   238  		},
   239  	}
   240  	ch := make(chan int)
   241  	go func() {
   242  		m["a"]["x"] = "y"
   243  		ch <- 1
   244  	}()
   245  	switch m["a"]["b"] {
   246  	}
   247  	<-ch
   248  }
   249  
   250  func TestRaceCaseType(t *testing.T) {
   251  	var x, y int
   252  	var i interface{} = x
   253  	c := make(chan int, 1)
   254  	go func() {
   255  		switch i.(type) {
   256  		case nil:
   257  		case int:
   258  		}
   259  		c <- 1
   260  	}()
   261  	i = y
   262  	<-c
   263  }
   264  
   265  func TestRaceCaseTypeBody(t *testing.T) {
   266  	var x, y int
   267  	var i interface{} = &x
   268  	c := make(chan int, 1)
   269  	go func() {
   270  		switch i := i.(type) {
   271  		case nil:
   272  		case *int:
   273  			*i = y
   274  		}
   275  		c <- 1
   276  	}()
   277  	x = y
   278  	<-c
   279  }
   280  
   281  func TestRaceCaseTypeIssue5890(t *testing.T) {
   282  	// spurious extra instrumentation of the initial interface
   283  	// value.
   284  	var x, y int
   285  	m := make(map[int]map[int]interface{})
   286  	m[0] = make(map[int]interface{})
   287  	c := make(chan int, 1)
   288  	go func() {
   289  		switch i := m[0][1].(type) {
   290  		case nil:
   291  		case *int:
   292  			*i = x
   293  		}
   294  		c <- 1
   295  	}()
   296  	m[0][1] = y
   297  	<-c
   298  }
   299  
   300  func TestNoRaceRange(t *testing.T) {
   301  	ch := make(chan int, 3)
   302  	a := [...]int{1, 2, 3}
   303  	for _, v := range a {
   304  		ch <- v
   305  	}
   306  	close(ch)
   307  }
   308  
   309  func TestNoRaceRangeIssue5446(t *testing.T) {
   310  	ch := make(chan int, 3)
   311  	a := []int{1, 2, 3}
   312  	b := []int{4}
   313  	// used to insert a spurious instrumentation of a[i]
   314  	// and crash.
   315  	i := 1
   316  	for i, a[i] = range b {
   317  		ch <- i
   318  	}
   319  	close(ch)
   320  }
   321  
   322  func TestRaceRange(t *testing.T) {
   323  	const N = 2
   324  	var a [N]int
   325  	var x, y int
   326  	done := make(chan bool, N)
   327  	for i, v := range a {
   328  		go func(i int) {
   329  			// we don't want a write-vs-write race
   330  			// so there is no array b here
   331  			if i == 0 {
   332  				x = v
   333  			} else {
   334  				y = v
   335  			}
   336  			done <- true
   337  		}(i)
   338  		// Ensure the goroutine runs before we continue the loop.
   339  		runtime.Gosched()
   340  	}
   341  	for i := 0; i < N; i++ {
   342  		<-done
   343  	}
   344  }
   345  
   346  func TestRaceForInit(t *testing.T) {
   347  	c := make(chan int)
   348  	x := 0
   349  	go func() {
   350  		c <- x
   351  	}()
   352  	for x = 42; false; {
   353  	}
   354  	<-c
   355  }
   356  
   357  func TestNoRaceForInit(t *testing.T) {
   358  	done := make(chan bool)
   359  	c := make(chan bool)
   360  	x := 0
   361  	go func() {
   362  		for {
   363  			_, ok := <-c
   364  			if !ok {
   365  				done <- true
   366  				return
   367  			}
   368  			x++
   369  		}
   370  	}()
   371  	i := 0
   372  	for x = 42; i < 10; i++ {
   373  		c <- true
   374  	}
   375  	close(c)
   376  	<-done
   377  }
   378  
   379  func TestRaceForTest(t *testing.T) {
   380  	done := make(chan bool)
   381  	c := make(chan bool)
   382  	stop := false
   383  	go func() {
   384  		for {
   385  			_, ok := <-c
   386  			if !ok {
   387  				done <- true
   388  				return
   389  			}
   390  			stop = true
   391  		}
   392  	}()
   393  	for !stop {
   394  		c <- true
   395  	}
   396  	close(c)
   397  	<-done
   398  }
   399  
   400  func TestRaceForIncr(t *testing.T) {
   401  	done := make(chan bool)
   402  	c := make(chan bool)
   403  	x := 0
   404  	go func() {
   405  		for {
   406  			_, ok := <-c
   407  			if !ok {
   408  				done <- true
   409  				return
   410  			}
   411  			x++
   412  		}
   413  	}()
   414  	for i := 0; i < 10; x++ {
   415  		i++
   416  		c <- true
   417  	}
   418  	close(c)
   419  	<-done
   420  }
   421  
   422  func TestNoRaceForIncr(t *testing.T) {
   423  	done := make(chan bool)
   424  	x := 0
   425  	go func() {
   426  		x++
   427  		done <- true
   428  	}()
   429  	for i := 0; i < 0; x++ {
   430  	}
   431  	<-done
   432  }
   433  
   434  func TestRacePlus(t *testing.T) {
   435  	var x, y, z int
   436  	ch := make(chan int, 2)
   437  
   438  	go func() {
   439  		y = x + z
   440  		ch <- 1
   441  	}()
   442  	go func() {
   443  		y = x + z + z
   444  		ch <- 1
   445  	}()
   446  	<-ch
   447  	<-ch
   448  }
   449  
   450  func TestRacePlus2(t *testing.T) {
   451  	var x, y, z int
   452  	ch := make(chan int, 2)
   453  
   454  	go func() {
   455  		x = 1
   456  		ch <- 1
   457  	}()
   458  	go func() {
   459  		y = +x + z
   460  		ch <- 1
   461  	}()
   462  	<-ch
   463  	<-ch
   464  }
   465  
   466  func TestNoRacePlus(t *testing.T) {
   467  	var x, y, z, f int
   468  	ch := make(chan int, 2)
   469  
   470  	go func() {
   471  		y = x + z
   472  		ch <- 1
   473  	}()
   474  	go func() {
   475  		f = z + x
   476  		ch <- 1
   477  	}()
   478  	<-ch
   479  	<-ch
   480  }
   481  
   482  func TestRaceComplement(t *testing.T) {
   483  	var x, y, z int
   484  	ch := make(chan int, 2)
   485  
   486  	go func() {
   487  		x = ^y
   488  		ch <- 1
   489  	}()
   490  	go func() {
   491  		y = ^z
   492  		ch <- 1
   493  	}()
   494  	<-ch
   495  	<-ch
   496  }
   497  
   498  func TestRaceDiv(t *testing.T) {
   499  	var x, y, z int
   500  	ch := make(chan int, 2)
   501  
   502  	go func() {
   503  		x = y / (z + 1)
   504  		ch <- 1
   505  	}()
   506  	go func() {
   507  		y = z
   508  		ch <- 1
   509  	}()
   510  	<-ch
   511  	<-ch
   512  }
   513  
   514  func TestRaceDivConst(t *testing.T) {
   515  	var x, y, z uint32
   516  	ch := make(chan int, 2)
   517  
   518  	go func() {
   519  		x = y / 3 // involves only a HMUL node
   520  		ch <- 1
   521  	}()
   522  	go func() {
   523  		y = z
   524  		ch <- 1
   525  	}()
   526  	<-ch
   527  	<-ch
   528  }
   529  
   530  func TestRaceMod(t *testing.T) {
   531  	var x, y, z int
   532  	ch := make(chan int, 2)
   533  
   534  	go func() {
   535  		x = y % (z + 1)
   536  		ch <- 1
   537  	}()
   538  	go func() {
   539  		y = z
   540  		ch <- 1
   541  	}()
   542  	<-ch
   543  	<-ch
   544  }
   545  
   546  func TestRaceModConst(t *testing.T) {
   547  	var x, y, z int
   548  	ch := make(chan int, 2)
   549  
   550  	go func() {
   551  		x = y % 3
   552  		ch <- 1
   553  	}()
   554  	go func() {
   555  		y = z
   556  		ch <- 1
   557  	}()
   558  	<-ch
   559  	<-ch
   560  }
   561  
   562  func TestRaceRotate(t *testing.T) {
   563  	var x, y, z uint32
   564  	ch := make(chan int, 2)
   565  
   566  	go func() {
   567  		x = y<<12 | y>>20
   568  		ch <- 1
   569  	}()
   570  	go func() {
   571  		y = z
   572  		ch <- 1
   573  	}()
   574  	<-ch
   575  	<-ch
   576  }
   577  
   578  // May crash if the instrumentation is reckless.
   579  func TestNoRaceEnoughRegisters(t *testing.T) {
   580  	// from erf.go
   581  	const (
   582  		sa1 = 1
   583  		sa2 = 2
   584  		sa3 = 3
   585  		sa4 = 4
   586  		sa5 = 5
   587  		sa6 = 6
   588  		sa7 = 7
   589  		sa8 = 8
   590  	)
   591  	var s, S float64
   592  	s = 3.1415
   593  	S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
   594  	s = S
   595  }
   596  
   597  // emptyFunc should not be inlined.
   598  func emptyFunc(x int) {
   599  	if false {
   600  		fmt.Println(x)
   601  	}
   602  }
   603  
   604  func TestRaceFuncArgument(t *testing.T) {
   605  	var x int
   606  	ch := make(chan bool, 1)
   607  	go func() {
   608  		emptyFunc(x)
   609  		ch <- true
   610  	}()
   611  	x = 1
   612  	<-ch
   613  }
   614  
   615  func TestRaceFuncArgument2(t *testing.T) {
   616  	var x int
   617  	ch := make(chan bool, 2)
   618  	go func() {
   619  		x = 42
   620  		ch <- true
   621  	}()
   622  	go func(y int) {
   623  		ch <- true
   624  	}(x)
   625  	<-ch
   626  	<-ch
   627  }
   628  
   629  func TestRaceSprint(t *testing.T) {
   630  	var x int
   631  	ch := make(chan bool, 1)
   632  	go func() {
   633  		fmt.Sprint(x)
   634  		ch <- true
   635  	}()
   636  	x = 1
   637  	<-ch
   638  }
   639  
   640  func TestRaceArrayCopy(t *testing.T) {
   641  	ch := make(chan bool, 1)
   642  	var a [5]int
   643  	go func() {
   644  		a[3] = 1
   645  		ch <- true
   646  	}()
   647  	a = [5]int{1, 2, 3, 4, 5}
   648  	<-ch
   649  }
   650  
   651  // Blows up a naive compiler.
   652  func TestRaceNestedArrayCopy(t *testing.T) {
   653  	ch := make(chan bool, 1)
   654  	type (
   655  		Point32   [2][2][2][2][2]Point
   656  		Point1024 [2][2][2][2][2]Point32
   657  		Point32k  [2][2][2][2][2]Point1024
   658  		Point1M   [2][2][2][2][2]Point32k
   659  	)
   660  	var a, b Point1M
   661  	go func() {
   662  		a[0][1][0][1][0][1][0][1][0][1][0][1][0][1][0][1][0][1][0][1].y = 1
   663  		ch <- true
   664  	}()
   665  	a = b
   666  	<-ch
   667  }
   668  
   669  func TestRaceStructRW(t *testing.T) {
   670  	p := Point{0, 0}
   671  	ch := make(chan bool, 1)
   672  	go func() {
   673  		p = Point{1, 1}
   674  		ch <- true
   675  	}()
   676  	q := p
   677  	<-ch
   678  	p = q
   679  }
   680  
   681  func TestRaceStructFieldRW1(t *testing.T) {
   682  	p := Point{0, 0}
   683  	ch := make(chan bool, 1)
   684  	go func() {
   685  		p.x = 1
   686  		ch <- true
   687  	}()
   688  	_ = p.x
   689  	<-ch
   690  }
   691  
   692  func TestNoRaceStructFieldRW1(t *testing.T) {
   693  	// Same struct, different variables, no
   694  	// pointers. The layout is known (at compile time?) ->
   695  	// no read on p
   696  	// writes on x and y
   697  	p := Point{0, 0}
   698  	ch := make(chan bool, 1)
   699  	go func() {
   700  		p.x = 1
   701  		ch <- true
   702  	}()
   703  	p.y = 1
   704  	<-ch
   705  	_ = p
   706  }
   707  
   708  func TestNoRaceStructFieldRW2(t *testing.T) {
   709  	// Same as NoRaceStructFieldRW1
   710  	// but p is a pointer, so there is a read on p
   711  	p := Point{0, 0}
   712  	ch := make(chan bool, 1)
   713  	go func() {
   714  		p.x = 1
   715  		ch <- true
   716  	}()
   717  	p.y = 1
   718  	<-ch
   719  	_ = p
   720  }
   721  
   722  func TestRaceStructFieldRW2(t *testing.T) {
   723  	p := &Point{0, 0}
   724  	ch := make(chan bool, 1)
   725  	go func() {
   726  		p.x = 1
   727  		ch <- true
   728  	}()
   729  	_ = p.x
   730  	<-ch
   731  }
   732  
   733  func TestRaceStructFieldRW3(t *testing.T) {
   734  	p := NamedPoint{name: "a", p: Point{0, 0}}
   735  	ch := make(chan bool, 1)
   736  	go func() {
   737  		p.p.x = 1
   738  		ch <- true
   739  	}()
   740  	_ = p.p.x
   741  	<-ch
   742  }
   743  
   744  func TestRaceEfaceWW(t *testing.T) {
   745  	var a, b interface{}
   746  	ch := make(chan bool, 1)
   747  	go func() {
   748  		a = 1
   749  		ch <- true
   750  	}()
   751  	a = 2
   752  	<-ch
   753  	_, _ = a, b
   754  }
   755  
   756  func TestRaceIfaceWW(t *testing.T) {
   757  	var a, b Writer
   758  	ch := make(chan bool, 1)
   759  	go func() {
   760  		a = DummyWriter{1}
   761  		ch <- true
   762  	}()
   763  	a = DummyWriter{2}
   764  	<-ch
   765  	b = a
   766  	a = b
   767  }
   768  
   769  func TestRaceIfaceCmp(t *testing.T) {
   770  	var a, b Writer
   771  	a = DummyWriter{1}
   772  	ch := make(chan bool, 1)
   773  	go func() {
   774  		a = DummyWriter{1}
   775  		ch <- true
   776  	}()
   777  	_ = a == b
   778  	<-ch
   779  }
   780  
   781  func TestRaceIfaceCmpNil(t *testing.T) {
   782  	var a Writer
   783  	a = DummyWriter{1}
   784  	ch := make(chan bool, 1)
   785  	go func() {
   786  		a = DummyWriter{1}
   787  		ch <- true
   788  	}()
   789  	_ = a == nil
   790  	<-ch
   791  }
   792  
   793  func TestRaceEfaceConv(t *testing.T) {
   794  	c := make(chan bool)
   795  	v := 0
   796  	go func() {
   797  		go func(x interface{}) {
   798  		}(v)
   799  		c <- true
   800  	}()
   801  	v = 42
   802  	<-c
   803  }
   804  
   805  type OsFile struct{}
   806  
   807  func (*OsFile) Read() {
   808  }
   809  
   810  type IoReader interface {
   811  	Read()
   812  }
   813  
   814  func TestRaceIfaceConv(t *testing.T) {
   815  	c := make(chan bool)
   816  	f := &OsFile{}
   817  	go func() {
   818  		go func(x IoReader) {
   819  		}(f)
   820  		c <- true
   821  	}()
   822  	f = &OsFile{}
   823  	<-c
   824  }
   825  
   826  func TestRaceError(t *testing.T) {
   827  	ch := make(chan bool, 1)
   828  	var err error
   829  	go func() {
   830  		err = nil
   831  		ch <- true
   832  	}()
   833  	_ = err
   834  	<-ch
   835  }
   836  
   837  func TestRaceIntptrRW(t *testing.T) {
   838  	var x, y int
   839  	var p *int = &x
   840  	ch := make(chan bool, 1)
   841  	go func() {
   842  		*p = 5
   843  		ch <- true
   844  	}()
   845  	y = *p
   846  	x = y
   847  	<-ch
   848  }
   849  
   850  func TestRaceStringRW(t *testing.T) {
   851  	ch := make(chan bool, 1)
   852  	s := ""
   853  	go func() {
   854  		s = "abacaba"
   855  		ch <- true
   856  	}()
   857  	_ = s
   858  	<-ch
   859  }
   860  
   861  func TestRaceStringPtrRW(t *testing.T) {
   862  	ch := make(chan bool, 1)
   863  	var x string
   864  	p := &x
   865  	go func() {
   866  		*p = "a"
   867  		ch <- true
   868  	}()
   869  	_ = *p
   870  	<-ch
   871  }
   872  
   873  func TestRaceFloat64WW(t *testing.T) {
   874  	var x, y float64
   875  	ch := make(chan bool, 1)
   876  	go func() {
   877  		x = 1.0
   878  		ch <- true
   879  	}()
   880  	x = 2.0
   881  	<-ch
   882  
   883  	y = x
   884  	x = y
   885  }
   886  
   887  func TestRaceComplex128WW(t *testing.T) {
   888  	var x, y complex128
   889  	ch := make(chan bool, 1)
   890  	go func() {
   891  		x = 2 + 2i
   892  		ch <- true
   893  	}()
   894  	x = 4 + 4i
   895  	<-ch
   896  
   897  	y = x
   898  	x = y
   899  }
   900  
   901  func TestRaceUnsafePtrRW(t *testing.T) {
   902  	var x, y, z int
   903  	x, y, z = 1, 2, 3
   904  	var p unsafe.Pointer = unsafe.Pointer(&x)
   905  	ch := make(chan bool, 1)
   906  	go func() {
   907  		p = (unsafe.Pointer)(&z)
   908  		ch <- true
   909  	}()
   910  	y = *(*int)(p)
   911  	x = y
   912  	<-ch
   913  }
   914  
   915  func TestRaceFuncVariableRW(t *testing.T) {
   916  	var f func(x int) int
   917  	f = func(x int) int {
   918  		return x * x
   919  	}
   920  	ch := make(chan bool, 1)
   921  	go func() {
   922  		f = func(x int) int {
   923  			return x
   924  		}
   925  		ch <- true
   926  	}()
   927  	y := f(1)
   928  	<-ch
   929  	x := y
   930  	y = x
   931  }
   932  
   933  func TestRaceFuncVariableWW(t *testing.T) {
   934  	var f func(x int) int
   935  	ch := make(chan bool, 1)
   936  	go func() {
   937  		f = func(x int) int {
   938  			return x
   939  		}
   940  		ch <- true
   941  	}()
   942  	f = func(x int) int {
   943  		return x * x
   944  	}
   945  	<-ch
   946  }
   947  
   948  // This one should not belong to mop_test
   949  func TestRacePanic(t *testing.T) {
   950  	var x int
   951  	var zero int = 0
   952  	ch := make(chan bool, 2)
   953  	go func() {
   954  		defer func() {
   955  			err := recover()
   956  			if err == nil {
   957  				panic("should be panicking")
   958  			}
   959  			x = 1
   960  			ch <- true
   961  		}()
   962  		var y int = 1 / zero
   963  		zero = y
   964  	}()
   965  	go func() {
   966  		defer func() {
   967  			err := recover()
   968  			if err == nil {
   969  				panic("should be panicking")
   970  			}
   971  			x = 2
   972  			ch <- true
   973  		}()
   974  		var y int = 1 / zero
   975  		zero = y
   976  	}()
   977  
   978  	<-ch
   979  	<-ch
   980  	if zero != 0 {
   981  		panic("zero has changed")
   982  	}
   983  }
   984  
   985  func TestNoRaceBlank(t *testing.T) {
   986  	var a [5]int
   987  	ch := make(chan bool, 1)
   988  	go func() {
   989  		_, _ = a[0], a[1]
   990  		ch <- true
   991  	}()
   992  	_, _ = a[2], a[3]
   993  	<-ch
   994  	a[1] = a[0]
   995  }
   996  
   997  func TestRaceAppendRW(t *testing.T) {
   998  	a := make([]int, 10)
   999  	ch := make(chan bool)
  1000  	go func() {
  1001  		_ = append(a, 1)
  1002  		ch <- true
  1003  	}()
  1004  	a[0] = 1
  1005  	<-ch
  1006  }
  1007  
  1008  func TestRaceAppendLenRW(t *testing.T) {
  1009  	a := make([]int, 0)
  1010  	ch := make(chan bool)
  1011  	go func() {
  1012  		a = append(a, 1)
  1013  		ch <- true
  1014  	}()
  1015  	_ = len(a)
  1016  	<-ch
  1017  }
  1018  
  1019  func TestRaceAppendCapRW(t *testing.T) {
  1020  	a := make([]int, 0)
  1021  	ch := make(chan string)
  1022  	go func() {
  1023  		a = append(a, 1)
  1024  		ch <- ""
  1025  	}()
  1026  	_ = cap(a)
  1027  	<-ch
  1028  }
  1029  
  1030  func TestNoRaceFuncArgsRW(t *testing.T) {
  1031  	ch := make(chan byte, 1)
  1032  	var x byte
  1033  	go func(y byte) {
  1034  		_ = y
  1035  		ch <- 0
  1036  	}(x)
  1037  	x = 1
  1038  	<-ch
  1039  }
  1040  
  1041  func TestRaceFuncArgsRW(t *testing.T) {
  1042  	ch := make(chan byte, 1)
  1043  	var x byte
  1044  	go func(y *byte) {
  1045  		_ = *y
  1046  		ch <- 0
  1047  	}(&x)
  1048  	x = 1
  1049  	<-ch
  1050  }
  1051  
  1052  // from the mailing list, slightly modified
  1053  // unprotected concurrent access to seen[]
  1054  func TestRaceCrawl(t *testing.T) {
  1055  	url := "dummyurl"
  1056  	depth := 3
  1057  	seen := make(map[string]bool)
  1058  	ch := make(chan int, 100)
  1059  	var wg sync.WaitGroup
  1060  	var crawl func(string, int)
  1061  	crawl = func(u string, d int) {
  1062  		nurl := 0
  1063  		defer func() {
  1064  			ch <- nurl
  1065  		}()
  1066  		seen[u] = true
  1067  		if d <= 0 {
  1068  			wg.Done()
  1069  			return
  1070  		}
  1071  		urls := [...]string{"a", "b", "c"}
  1072  		for _, uu := range urls {
  1073  			if _, ok := seen[uu]; !ok {
  1074  				wg.Add(1)
  1075  				go crawl(uu, d-1)
  1076  				nurl++
  1077  			}
  1078  		}
  1079  		wg.Done()
  1080  	}
  1081  	wg.Add(1)
  1082  	go crawl(url, depth)
  1083  	wg.Wait()
  1084  }
  1085  
  1086  func TestRaceIndirection(t *testing.T) {
  1087  	ch := make(chan struct{}, 1)
  1088  	var y int
  1089  	var x *int = &y
  1090  	go func() {
  1091  		*x = 1
  1092  		ch <- struct{}{}
  1093  	}()
  1094  	*x = 2
  1095  	<-ch
  1096  	_ = *x
  1097  }
  1098  
  1099  func TestRaceRune(t *testing.T) {
  1100  	c := make(chan bool)
  1101  	var x rune
  1102  	go func() {
  1103  		x = 1
  1104  		c <- true
  1105  	}()
  1106  	_ = x
  1107  	<-c
  1108  }
  1109  
  1110  func TestRaceEmptyInterface1(t *testing.T) {
  1111  	c := make(chan bool)
  1112  	var x interface{}
  1113  	go func() {
  1114  		x = nil
  1115  		c <- true
  1116  	}()
  1117  	_ = x
  1118  	<-c
  1119  }
  1120  
  1121  func TestRaceEmptyInterface2(t *testing.T) {
  1122  	c := make(chan bool)
  1123  	var x interface{}
  1124  	go func() {
  1125  		x = &Point{}
  1126  		c <- true
  1127  	}()
  1128  	_ = x
  1129  	<-c
  1130  }
  1131  
  1132  func TestRaceTLS(t *testing.T) {
  1133  	comm := make(chan *int)
  1134  	done := make(chan bool, 2)
  1135  	go func() {
  1136  		var x int
  1137  		comm <- &x
  1138  		x = 1
  1139  		x = *(<-comm)
  1140  		done <- true
  1141  	}()
  1142  	go func() {
  1143  		p := <-comm
  1144  		*p = 2
  1145  		comm <- p
  1146  		done <- true
  1147  	}()
  1148  	<-done
  1149  	<-done
  1150  }
  1151  
  1152  func TestNoRaceHeapReallocation(t *testing.T) {
  1153  	// It is possible that a future implementation
  1154  	// of memory allocation will ruin this test.
  1155  	// Increasing n might help in this case, so
  1156  	// this test is a bit more generic than most of the
  1157  	// others.
  1158  	const n = 2
  1159  	done := make(chan bool, n)
  1160  	empty := func(p *int) {}
  1161  	for i := 0; i < n; i++ {
  1162  		ms := i
  1163  		go func() {
  1164  			<-time.After(time.Duration(ms) * time.Millisecond)
  1165  			runtime.GC()
  1166  			var x int
  1167  			empty(&x) // x goes to the heap
  1168  			done <- true
  1169  		}()
  1170  	}
  1171  	for i := 0; i < n; i++ {
  1172  		<-done
  1173  	}
  1174  }
  1175  
  1176  func TestRaceAnd(t *testing.T) {
  1177  	c := make(chan bool)
  1178  	x, y := 0, 0
  1179  	go func() {
  1180  		x = 1
  1181  		c <- true
  1182  	}()
  1183  	if x == 1 && y == 1 {
  1184  	}
  1185  	<-c
  1186  }
  1187  
  1188  func TestRaceAnd2(t *testing.T) {
  1189  	c := make(chan bool)
  1190  	x, y := 0, 0
  1191  	go func() {
  1192  		x = 1
  1193  		c <- true
  1194  	}()
  1195  	if y == 0 && x == 1 {
  1196  	}
  1197  	<-c
  1198  }
  1199  
  1200  func TestNoRaceAnd(t *testing.T) {
  1201  	c := make(chan bool)
  1202  	x, y := 0, 0
  1203  	go func() {
  1204  		x = 1
  1205  		c <- true
  1206  	}()
  1207  	if y == 1 && x == 1 {
  1208  	}
  1209  	<-c
  1210  }
  1211  
  1212  func TestRaceOr(t *testing.T) {
  1213  	c := make(chan bool)
  1214  	x, y := 0, 0
  1215  	go func() {
  1216  		x = 1
  1217  		c <- true
  1218  	}()
  1219  	if x == 1 || y == 1 {
  1220  	}
  1221  	<-c
  1222  }
  1223  
  1224  func TestRaceOr2(t *testing.T) {
  1225  	c := make(chan bool)
  1226  	x, y := 0, 0
  1227  	go func() {
  1228  		x = 1
  1229  		c <- true
  1230  	}()
  1231  	if y == 1 || x == 1 {
  1232  	}
  1233  	<-c
  1234  }
  1235  
  1236  func TestNoRaceOr(t *testing.T) {
  1237  	c := make(chan bool)
  1238  	x, y := 0, 0
  1239  	go func() {
  1240  		x = 1
  1241  		c <- true
  1242  	}()
  1243  	if y == 0 || x == 1 {
  1244  	}
  1245  	<-c
  1246  }
  1247  
  1248  func TestNoRaceShortCalc(t *testing.T) {
  1249  	c := make(chan bool)
  1250  	x, y := 0, 0
  1251  	go func() {
  1252  		y = 1
  1253  		c <- true
  1254  	}()
  1255  	if x == 0 || y == 0 {
  1256  	}
  1257  	<-c
  1258  }
  1259  
  1260  func TestNoRaceShortCalc2(t *testing.T) {
  1261  	c := make(chan bool)
  1262  	x, y := 0, 0
  1263  	go func() {
  1264  		y = 1
  1265  		c <- true
  1266  	}()
  1267  	if x == 1 && y == 0 {
  1268  	}
  1269  	<-c
  1270  }
  1271  
  1272  func TestRaceFuncItself(t *testing.T) {
  1273  	c := make(chan bool)
  1274  	f := func() {}
  1275  	go func() {
  1276  		f()
  1277  		c <- true
  1278  	}()
  1279  	f = func() {}
  1280  	<-c
  1281  }
  1282  
  1283  func TestNoRaceFuncUnlock(t *testing.T) {
  1284  	ch := make(chan bool, 1)
  1285  	var mu sync.Mutex
  1286  	x := 0
  1287  	go func() {
  1288  		mu.Lock()
  1289  		x = 42
  1290  		mu.Unlock()
  1291  		ch <- true
  1292  	}()
  1293  	x = func(mu *sync.Mutex) int {
  1294  		mu.Lock()
  1295  		return 43
  1296  	}(&mu)
  1297  	mu.Unlock()
  1298  	<-ch
  1299  }
  1300  
  1301  func TestRaceStructInit(t *testing.T) {
  1302  	type X struct {
  1303  		x, y int
  1304  	}
  1305  	c := make(chan bool, 1)
  1306  	y := 0
  1307  	go func() {
  1308  		y = 42
  1309  		c <- true
  1310  	}()
  1311  	x := X{x: y}
  1312  	_ = x
  1313  	<-c
  1314  }
  1315  
  1316  func TestRaceArrayInit(t *testing.T) {
  1317  	c := make(chan bool, 1)
  1318  	y := 0
  1319  	go func() {
  1320  		y = 42
  1321  		c <- true
  1322  	}()
  1323  	x := []int{0, y, 42}
  1324  	_ = x
  1325  	<-c
  1326  }
  1327  
  1328  func TestRaceMapInit(t *testing.T) {
  1329  	c := make(chan bool, 1)
  1330  	y := 0
  1331  	go func() {
  1332  		y = 42
  1333  		c <- true
  1334  	}()
  1335  	x := map[int]int{0: 42, y: 42}
  1336  	_ = x
  1337  	<-c
  1338  }
  1339  
  1340  func TestRaceMapInit2(t *testing.T) {
  1341  	c := make(chan bool, 1)
  1342  	y := 0
  1343  	go func() {
  1344  		y = 42
  1345  		c <- true
  1346  	}()
  1347  	x := map[int]int{0: 42, 42: y}
  1348  	_ = x
  1349  	<-c
  1350  }
  1351  
  1352  type Inter interface {
  1353  	Foo(x int)
  1354  }
  1355  type InterImpl struct {
  1356  	x, y int
  1357  }
  1358  
  1359  //go:noinline
  1360  func (p InterImpl) Foo(x int) {
  1361  }
  1362  
  1363  type InterImpl2 InterImpl
  1364  
  1365  func (p *InterImpl2) Foo(x int) {
  1366  	if p == nil {
  1367  		InterImpl{}.Foo(x)
  1368  	}
  1369  	InterImpl(*p).Foo(x)
  1370  }
  1371  
  1372  func TestRaceInterCall(t *testing.T) {
  1373  	c := make(chan bool, 1)
  1374  	p := InterImpl{}
  1375  	var x Inter = p
  1376  	go func() {
  1377  		p2 := InterImpl{}
  1378  		x = p2
  1379  		c <- true
  1380  	}()
  1381  	x.Foo(0)
  1382  	<-c
  1383  }
  1384  
  1385  func TestRaceInterCall2(t *testing.T) {
  1386  	c := make(chan bool, 1)
  1387  	p := InterImpl{}
  1388  	var x Inter = p
  1389  	z := 0
  1390  	go func() {
  1391  		z = 42
  1392  		c <- true
  1393  	}()
  1394  	x.Foo(z)
  1395  	<-c
  1396  }
  1397  
  1398  func TestRaceFuncCall(t *testing.T) {
  1399  	c := make(chan bool, 1)
  1400  	f := func(x, y int) {}
  1401  	x, y := 0, 0
  1402  	go func() {
  1403  		y = 42
  1404  		c <- true
  1405  	}()
  1406  	f(x, y)
  1407  	<-c
  1408  }
  1409  
  1410  func TestRaceMethodCall(t *testing.T) {
  1411  	c := make(chan bool, 1)
  1412  	i := InterImpl{}
  1413  	x := 0
  1414  	go func() {
  1415  		x = 42
  1416  		c <- true
  1417  	}()
  1418  	i.Foo(x)
  1419  	<-c
  1420  }
  1421  
  1422  func TestRaceMethodCall2(t *testing.T) {
  1423  	c := make(chan bool, 1)
  1424  	i := &InterImpl{}
  1425  	go func() {
  1426  		i = &InterImpl{}
  1427  		c <- true
  1428  	}()
  1429  	i.Foo(0)
  1430  	<-c
  1431  }
  1432  
  1433  // Method value with concrete value receiver.
  1434  func TestRaceMethodValue(t *testing.T) {
  1435  	c := make(chan bool, 1)
  1436  	i := InterImpl{}
  1437  	go func() {
  1438  		i = InterImpl{}
  1439  		c <- true
  1440  	}()
  1441  	_ = i.Foo
  1442  	<-c
  1443  }
  1444  
  1445  // Method value with interface receiver.
  1446  func TestRaceMethodValue2(t *testing.T) {
  1447  	c := make(chan bool, 1)
  1448  	var i Inter = InterImpl{}
  1449  	go func() {
  1450  		i = InterImpl{}
  1451  		c <- true
  1452  	}()
  1453  	_ = i.Foo
  1454  	<-c
  1455  }
  1456  
  1457  // Method value with implicit dereference.
  1458  func TestRaceMethodValue3(t *testing.T) {
  1459  	c := make(chan bool, 1)
  1460  	i := &InterImpl{}
  1461  	go func() {
  1462  		*i = InterImpl{}
  1463  		c <- true
  1464  	}()
  1465  	_ = i.Foo // dereferences i.
  1466  	<-c
  1467  }
  1468  
  1469  // Method value implicitly taking receiver address.
  1470  func TestNoRaceMethodValue(t *testing.T) {
  1471  	c := make(chan bool, 1)
  1472  	i := InterImpl2{}
  1473  	go func() {
  1474  		i = InterImpl2{}
  1475  		c <- true
  1476  	}()
  1477  	_ = i.Foo // takes the address of i only.
  1478  	<-c
  1479  }
  1480  
  1481  func TestRacePanicArg(t *testing.T) {
  1482  	c := make(chan bool, 1)
  1483  	err := errors.New("err")
  1484  	go func() {
  1485  		err = errors.New("err2")
  1486  		c <- true
  1487  	}()
  1488  	defer func() {
  1489  		recover()
  1490  		<-c
  1491  	}()
  1492  	panic(err)
  1493  }
  1494  
  1495  func TestRaceDeferArg(t *testing.T) {
  1496  	c := make(chan bool, 1)
  1497  	x := 0
  1498  	go func() {
  1499  		x = 42
  1500  		c <- true
  1501  	}()
  1502  	func() {
  1503  		defer func(x int) {
  1504  		}(x)
  1505  	}()
  1506  	<-c
  1507  }
  1508  
  1509  type DeferT int
  1510  
  1511  func (d DeferT) Foo() {
  1512  }
  1513  
  1514  func TestRaceDeferArg2(t *testing.T) {
  1515  	c := make(chan bool, 1)
  1516  	var x DeferT
  1517  	go func() {
  1518  		var y DeferT
  1519  		x = y
  1520  		c <- true
  1521  	}()
  1522  	func() {
  1523  		defer x.Foo()
  1524  	}()
  1525  	<-c
  1526  }
  1527  
  1528  func TestNoRaceAddrExpr(t *testing.T) {
  1529  	c := make(chan bool, 1)
  1530  	x := 0
  1531  	go func() {
  1532  		x = 42
  1533  		c <- true
  1534  	}()
  1535  	_ = &x
  1536  	<-c
  1537  }
  1538  
  1539  type AddrT struct {
  1540  	_ [256]byte
  1541  	x int
  1542  }
  1543  
  1544  type AddrT2 struct {
  1545  	_ [512]byte
  1546  	p *AddrT
  1547  }
  1548  
  1549  func TestRaceAddrExpr(t *testing.T) {
  1550  	c := make(chan bool, 1)
  1551  	a := AddrT2{p: &AddrT{x: 42}}
  1552  	go func() {
  1553  		a.p = &AddrT{x: 43}
  1554  		c <- true
  1555  	}()
  1556  	_ = &a.p.x
  1557  	<-c
  1558  }
  1559  
  1560  func TestRaceTypeAssert(t *testing.T) {
  1561  	c := make(chan bool, 1)
  1562  	x := 0
  1563  	var i interface{} = x
  1564  	go func() {
  1565  		y := 0
  1566  		i = y
  1567  		c <- true
  1568  	}()
  1569  	_ = i.(int)
  1570  	<-c
  1571  }
  1572  
  1573  func TestRaceBlockAs(t *testing.T) {
  1574  	c := make(chan bool, 1)
  1575  	var x, y int
  1576  	go func() {
  1577  		x = 42
  1578  		c <- true
  1579  	}()
  1580  	x, y = y, x
  1581  	<-c
  1582  }
  1583  
  1584  func TestRaceBlockCall1(t *testing.T) {
  1585  	done := make(chan bool)
  1586  	x, y := 0, 0
  1587  	go func() {
  1588  		f := func() (int, int) {
  1589  			return 42, 43
  1590  		}
  1591  		x, y = f()
  1592  		done <- true
  1593  	}()
  1594  	_ = x
  1595  	<-done
  1596  	if x != 42 || y != 43 {
  1597  		panic("corrupted data")
  1598  	}
  1599  }
  1600  func TestRaceBlockCall2(t *testing.T) {
  1601  	done := make(chan bool)
  1602  	x, y := 0, 0
  1603  	go func() {
  1604  		f := func() (int, int) {
  1605  			return 42, 43
  1606  		}
  1607  		x, y = f()
  1608  		done <- true
  1609  	}()
  1610  	_ = y
  1611  	<-done
  1612  	if x != 42 || y != 43 {
  1613  		panic("corrupted data")
  1614  	}
  1615  }
  1616  func TestRaceBlockCall3(t *testing.T) {
  1617  	done := make(chan bool)
  1618  	var x *int
  1619  	y := 0
  1620  	go func() {
  1621  		f := func() (*int, int) {
  1622  			i := 42
  1623  			return &i, 43
  1624  		}
  1625  		x, y = f()
  1626  		done <- true
  1627  	}()
  1628  	_ = x
  1629  	<-done
  1630  	if *x != 42 || y != 43 {
  1631  		panic("corrupted data")
  1632  	}
  1633  }
  1634  func TestRaceBlockCall4(t *testing.T) {
  1635  	done := make(chan bool)
  1636  	x := 0
  1637  	var y *int
  1638  	go func() {
  1639  		f := func() (int, *int) {
  1640  			i := 43
  1641  			return 42, &i
  1642  		}
  1643  		x, y = f()
  1644  		done <- true
  1645  	}()
  1646  	_ = y
  1647  	<-done
  1648  	if x != 42 || *y != 43 {
  1649  		panic("corrupted data")
  1650  	}
  1651  }
  1652  func TestRaceBlockCall5(t *testing.T) {
  1653  	done := make(chan bool)
  1654  	var x *int
  1655  	y := 0
  1656  	go func() {
  1657  		f := func() (*int, int) {
  1658  			i := 42
  1659  			return &i, 43
  1660  		}
  1661  		x, y = f()
  1662  		done <- true
  1663  	}()
  1664  	_ = y
  1665  	<-done
  1666  	if *x != 42 || y != 43 {
  1667  		panic("corrupted data")
  1668  	}
  1669  }
  1670  func TestRaceBlockCall6(t *testing.T) {
  1671  	done := make(chan bool)
  1672  	x := 0
  1673  	var y *int
  1674  	go func() {
  1675  		f := func() (int, *int) {
  1676  			i := 43
  1677  			return 42, &i
  1678  		}
  1679  		x, y = f()
  1680  		done <- true
  1681  	}()
  1682  	_ = x
  1683  	<-done
  1684  	if x != 42 || *y != 43 {
  1685  		panic("corrupted data")
  1686  	}
  1687  }
  1688  func TestRaceSliceSlice(t *testing.T) {
  1689  	c := make(chan bool, 1)
  1690  	x := make([]int, 10)
  1691  	go func() {
  1692  		x = make([]int, 20)
  1693  		c <- true
  1694  	}()
  1695  	_ = x[2:3]
  1696  	<-c
  1697  }
  1698  
  1699  func TestRaceSliceSlice2(t *testing.T) {
  1700  	c := make(chan bool, 1)
  1701  	x := make([]int, 10)
  1702  	i := 2
  1703  	go func() {
  1704  		i = 3
  1705  		c <- true
  1706  	}()
  1707  	_ = x[i:4]
  1708  	<-c
  1709  }
  1710  
  1711  func TestRaceSliceString(t *testing.T) {
  1712  	c := make(chan bool, 1)
  1713  	x := "hello"
  1714  	go func() {
  1715  		x = "world"
  1716  		c <- true
  1717  	}()
  1718  	_ = x[2:3]
  1719  	<-c
  1720  }
  1721  
  1722  func TestRaceSliceStruct(t *testing.T) {
  1723  	type X struct {
  1724  		x, y int
  1725  	}
  1726  	c := make(chan bool, 1)
  1727  	x := make([]X, 10)
  1728  	go func() {
  1729  		y := make([]X, 10)
  1730  		copy(y, x)
  1731  		c <- true
  1732  	}()
  1733  	x[1].y = 42
  1734  	<-c
  1735  }
  1736  
  1737  func TestRaceAppendSliceStruct(t *testing.T) {
  1738  	type X struct {
  1739  		x, y int
  1740  	}
  1741  	c := make(chan bool, 1)
  1742  	x := make([]X, 10)
  1743  	go func() {
  1744  		y := make([]X, 0, 10)
  1745  		y = append(y, x...)
  1746  		c <- true
  1747  	}()
  1748  	x[1].y = 42
  1749  	<-c
  1750  }
  1751  
  1752  func TestRaceStructInd(t *testing.T) {
  1753  	c := make(chan bool, 1)
  1754  	type Item struct {
  1755  		x, y int
  1756  	}
  1757  	i := Item{}
  1758  	go func(p *Item) {
  1759  		*p = Item{}
  1760  		c <- true
  1761  	}(&i)
  1762  	i.y = 42
  1763  	<-c
  1764  }
  1765  
  1766  func TestRaceAsFunc1(t *testing.T) {
  1767  	var s []byte
  1768  	c := make(chan bool, 1)
  1769  	go func() {
  1770  		var err error
  1771  		s, err = func() ([]byte, error) {
  1772  			t := []byte("hello world")
  1773  			return t, nil
  1774  		}()
  1775  		c <- true
  1776  		_ = err
  1777  	}()
  1778  	_ = string(s)
  1779  	<-c
  1780  }
  1781  
  1782  func TestRaceAsFunc2(t *testing.T) {
  1783  	c := make(chan bool, 1)
  1784  	x := 0
  1785  	go func() {
  1786  		func(x int) {
  1787  		}(x)
  1788  		c <- true
  1789  	}()
  1790  	x = 42
  1791  	<-c
  1792  }
  1793  
  1794  func TestRaceAsFunc3(t *testing.T) {
  1795  	c := make(chan bool, 1)
  1796  	var mu sync.Mutex
  1797  	x := 0
  1798  	go func() {
  1799  		func(x int) {
  1800  			mu.Lock()
  1801  		}(x) // Read of x must be outside of the mutex.
  1802  		mu.Unlock()
  1803  		c <- true
  1804  	}()
  1805  	mu.Lock()
  1806  	x = 42
  1807  	mu.Unlock()
  1808  	<-c
  1809  }
  1810  
  1811  func TestNoRaceAsFunc4(t *testing.T) {
  1812  	c := make(chan bool, 1)
  1813  	var mu sync.Mutex
  1814  	x := 0
  1815  	go func() {
  1816  		x = func() int { // Write of x must be under the mutex.
  1817  			mu.Lock()
  1818  			return 42
  1819  		}()
  1820  		mu.Unlock()
  1821  		c <- true
  1822  	}()
  1823  	mu.Lock()
  1824  	x = 42
  1825  	mu.Unlock()
  1826  	<-c
  1827  }
  1828  
  1829  func TestRaceHeapParam(t *testing.T) {
  1830  	done := make(chan bool)
  1831  	x := func() (x int) {
  1832  		go func() {
  1833  			x = 42
  1834  			done <- true
  1835  		}()
  1836  		return
  1837  	}()
  1838  	_ = x
  1839  	<-done
  1840  }
  1841  
  1842  func TestNoRaceEmptyStruct(t *testing.T) {
  1843  	type Empty struct{}
  1844  	type X struct {
  1845  		y int64
  1846  		Empty
  1847  	}
  1848  	type Y struct {
  1849  		x X
  1850  		y int64
  1851  	}
  1852  	c := make(chan X)
  1853  	var y Y
  1854  	go func() {
  1855  		x := y.x
  1856  		c <- x
  1857  	}()
  1858  	y.y = 42
  1859  	<-c
  1860  }
  1861  
  1862  func TestRaceNestedStruct(t *testing.T) {
  1863  	type X struct {
  1864  		x, y int
  1865  	}
  1866  	type Y struct {
  1867  		x X
  1868  	}
  1869  	c := make(chan Y)
  1870  	var y Y
  1871  	go func() {
  1872  		c <- y
  1873  	}()
  1874  	y.x.y = 42
  1875  	<-c
  1876  }
  1877  
  1878  func TestRaceIssue5567(t *testing.T) {
  1879  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(4))
  1880  	in := make(chan []byte)
  1881  	res := make(chan error)
  1882  	go func() {
  1883  		var err error
  1884  		defer func() {
  1885  			close(in)
  1886  			res <- err
  1887  		}()
  1888  		path := "mop_test.go"
  1889  		f, err := os.Open(path)
  1890  		if err != nil {
  1891  			return
  1892  		}
  1893  		defer f.Close()
  1894  		var n, total int
  1895  		b := make([]byte, 17) // the race is on b buffer
  1896  		for err == nil {
  1897  			n, err = f.Read(b)
  1898  			total += n
  1899  			if n > 0 {
  1900  				in <- b[:n]
  1901  			}
  1902  		}
  1903  		if err == io.EOF {
  1904  			err = nil
  1905  		}
  1906  	}()
  1907  	h := sha1.New()
  1908  	for b := range in {
  1909  		h.Write(b)
  1910  	}
  1911  	_ = h.Sum(nil)
  1912  	err := <-res
  1913  	if err != nil {
  1914  		t.Fatal(err)
  1915  	}
  1916  }
  1917  
  1918  func TestRaceIssue5654(t *testing.T) {
  1919  	text := `Friends, Romans, countrymen, lend me your ears;
  1920  I come to bury Caesar, not to praise him.
  1921  The evil that men do lives after them;
  1922  The good is oft interred with their bones;
  1923  So let it be with Caesar. The noble Brutus
  1924  Hath told you Caesar was ambitious:
  1925  If it were so, it was a grievous fault,
  1926  And grievously hath Caesar answer'd it.
  1927  Here, under leave of Brutus and the rest -
  1928  For Brutus is an honourable man;
  1929  So are they all, all honourable men -
  1930  Come I to speak in Caesar's funeral.
  1931  He was my friend, faithful and just to me:
  1932  But Brutus says he was ambitious;
  1933  And Brutus is an honourable man.`
  1934  
  1935  	data := bytes.NewBufferString(text)
  1936  	in := make(chan []byte)
  1937  
  1938  	go func() {
  1939  		buf := make([]byte, 16)
  1940  		var n int
  1941  		var err error
  1942  		for ; err == nil; n, err = data.Read(buf) {
  1943  			in <- buf[:n]
  1944  		}
  1945  		close(in)
  1946  	}()
  1947  	res := ""
  1948  	for s := range in {
  1949  		res += string(s)
  1950  	}
  1951  	_ = res
  1952  }
  1953  
  1954  type Base int
  1955  
  1956  func (b *Base) Foo() int {
  1957  	return 42
  1958  }
  1959  
  1960  func (b Base) Bar() int {
  1961  	return int(b)
  1962  }
  1963  
  1964  func TestNoRaceMethodThunk(t *testing.T) {
  1965  	type Derived struct {
  1966  		pad int
  1967  		Base
  1968  	}
  1969  	var d Derived
  1970  	done := make(chan bool)
  1971  	go func() {
  1972  		_ = d.Foo()
  1973  		done <- true
  1974  	}()
  1975  	d = Derived{}
  1976  	<-done
  1977  }
  1978  
  1979  func TestRaceMethodThunk(t *testing.T) {
  1980  	type Derived struct {
  1981  		pad int
  1982  		*Base
  1983  	}
  1984  	var d Derived
  1985  	done := make(chan bool)
  1986  	go func() {
  1987  		_ = d.Foo()
  1988  		done <- true
  1989  	}()
  1990  	d = Derived{}
  1991  	<-done
  1992  }
  1993  
  1994  func TestRaceMethodThunk2(t *testing.T) {
  1995  	type Derived struct {
  1996  		pad int
  1997  		Base
  1998  	}
  1999  	var d Derived
  2000  	done := make(chan bool)
  2001  	go func() {
  2002  		_ = d.Bar()
  2003  		done <- true
  2004  	}()
  2005  	d = Derived{}
  2006  	<-done
  2007  }
  2008  
  2009  func TestRaceMethodThunk3(t *testing.T) {
  2010  	type Derived struct {
  2011  		pad int
  2012  		*Base
  2013  	}
  2014  	var d Derived
  2015  	d.Base = new(Base)
  2016  	done := make(chan bool)
  2017  	go func() {
  2018  		_ = d.Bar()
  2019  		done <- true
  2020  	}()
  2021  	d.Base = new(Base)
  2022  	<-done
  2023  }
  2024  
  2025  func TestRaceMethodThunk4(t *testing.T) {
  2026  	type Derived struct {
  2027  		pad int
  2028  		*Base
  2029  	}
  2030  	var d Derived
  2031  	d.Base = new(Base)
  2032  	done := make(chan bool)
  2033  	go func() {
  2034  		_ = d.Bar()
  2035  		done <- true
  2036  	}()
  2037  	*(*int)(d.Base) = 42
  2038  	<-done
  2039  }
  2040  
  2041  func TestNoRaceTinyAlloc(t *testing.T) {
  2042  	const P = 4
  2043  	const N = 1e6
  2044  	var tinySink *byte
  2045  	done := make(chan bool)
  2046  	for p := 0; p < P; p++ {
  2047  		go func() {
  2048  			for i := 0; i < N; i++ {
  2049  				var b byte
  2050  				if b != 0 {
  2051  					tinySink = &b // make it heap allocated
  2052  				}
  2053  				b = 42
  2054  			}
  2055  			done <- true
  2056  		}()
  2057  	}
  2058  	for p := 0; p < P; p++ {
  2059  		<-done
  2060  	}
  2061  }