...
Run Format

Source file src/runtime/race/testdata/mop_test.go

Documentation: runtime/race/testdata

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

View as plain text