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