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

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

View as plain text