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