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