forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmop_test.go
More file actions
2094 lines (1921 loc) · 28 KB
/
mop_test.go
File metadata and controls
2094 lines (1921 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package race_test
import (
"bytes"
"crypto/sha1"
"errors"
"fmt"
"io"
"os"
"runtime"
"sync"
"testing"
"time"
"unsafe"
)
type Point struct {
x, y int
}
type NamedPoint struct {
name string
p Point
}
type DummyWriter struct {
state int
}
type Writer interface {
Write(p []byte) (n int)
}
func (d DummyWriter) Write(p []byte) (n int) {
return 0
}
var GlobalX, GlobalY int = 0, 0
var GlobalCh chan int = make(chan int, 2)
func GlobalFunc1() {
GlobalY = GlobalX
GlobalCh <- 1
}
func GlobalFunc2() {
GlobalX = 1
GlobalCh <- 1
}
func TestRaceIntRWGlobalFuncs(t *testing.T) {
go GlobalFunc1()
go GlobalFunc2()
<-GlobalCh
<-GlobalCh
}
func TestRaceIntRWClosures(t *testing.T) {
var x, y int
_ = y
ch := make(chan int, 2)
go func() {
y = x
ch <- 1
}()
go func() {
x = 1
ch <- 1
}()
<-ch
<-ch
}
func TestNoRaceIntRWClosures(t *testing.T) {
var x, y int
_ = y
ch := make(chan int, 1)
go func() {
y = x
ch <- 1
}()
<-ch
go func() {
x = 1
ch <- 1
}()
<-ch
}
func TestRaceInt32RWClosures(t *testing.T) {
var x, y int32
_ = y
ch := make(chan bool, 2)
go func() {
y = x
ch <- true
}()
go func() {
x = 1
ch <- true
}()
<-ch
<-ch
}
func TestNoRaceCase(t *testing.T) {
var y int
for x := -1; x <= 1; x++ {
switch {
case x < 0:
y = -1
case x == 0:
y = 0
case x > 0:
y = 1
}
}
y++
}
func TestRaceCaseCondition(t *testing.T) {
var x int = 0
ch := make(chan int, 2)
go func() {
x = 2
ch <- 1
}()
go func() {
switch x < 2 {
case true:
x = 1
//case false:
// x = 5
}
ch <- 1
}()
<-ch
<-ch
}
func TestRaceCaseCondition2(t *testing.T) {
// switch body is rearranged by the compiler so the tests
// passes even if we don't instrument '<'
var x int = 0
ch := make(chan int, 2)
go func() {
x = 2
ch <- 1
}()
go func() {
switch x < 2 {
case true:
x = 1
case false:
x = 5
}
ch <- 1
}()
<-ch
<-ch
}
func TestRaceCaseBody(t *testing.T) {
var x, y int
_ = y
ch := make(chan int, 2)
go func() {
y = x
ch <- 1
}()
go func() {
switch {
default:
x = 1
case x == 100:
x = -x
}
ch <- 1
}()
<-ch
<-ch
}
func TestNoRaceCaseFallthrough(t *testing.T) {
var x, y, z int
_ = y
ch := make(chan int, 2)
z = 1
go func() {
y = x
ch <- 1
}()
go func() {
switch {
case z == 1:
case z == 2:
x = 2
}
ch <- 1
}()
<-ch
<-ch
}
func TestRaceCaseFallthrough(t *testing.T) {
var x, y, z int
_ = y
ch := make(chan int, 2)
z = 1
go func() {
y = x
ch <- 1
}()
go func() {
switch {
case z == 1:
fallthrough
case z == 2:
x = 2
}
ch <- 1
}()
<-ch
<-ch
}
func TestRaceCaseIssue6418(t *testing.T) {
m := map[string]map[string]string{
"a": {
"b": "c",
},
}
ch := make(chan int)
go func() {
m["a"]["x"] = "y"
ch <- 1
}()
switch m["a"]["b"] {
}
<-ch
}
func TestRaceCaseType(t *testing.T) {
var x, y int
var i any = x
c := make(chan int, 1)
go func() {
switch i.(type) {
case nil:
case int:
}
c <- 1
}()
i = y
<-c
}
func TestRaceCaseTypeBody(t *testing.T) {
var x, y int
var i any = &x
c := make(chan int, 1)
go func() {
switch i := i.(type) {
case nil:
case *int:
*i = y
}
c <- 1
}()
x = y
<-c
}
func TestRaceCaseTypeIssue5890(t *testing.T) {
// spurious extra instrumentation of the initial interface
// value.
var x, y int
m := make(map[int]map[int]any)
m[0] = make(map[int]any)
c := make(chan int, 1)
go func() {
switch i := m[0][1].(type) {
case nil:
case *int:
*i = x
}
c <- 1
}()
m[0][1] = y
<-c
}
func TestNoRaceRange(t *testing.T) {
ch := make(chan int, 3)
a := [...]int{1, 2, 3}
for _, v := range a {
ch <- v
}
close(ch)
}
func TestNoRaceRangeIssue5446(t *testing.T) {
ch := make(chan int, 3)
a := []int{1, 2, 3}
b := []int{4}
// used to insert a spurious instrumentation of a[i]
// and crash.
i := 1
for i, a[i] = range b {
ch <- i
}
close(ch)
}
func TestRaceRange(t *testing.T) {
const N = 2
var a [N]int
var x, y int
_ = x + y
done := make(chan bool, N)
for i, v := range a {
go func(i int) {
// we don't want a write-vs-write race
// so there is no array b here
if i == 0 {
x = v
} else {
y = v
}
done <- true
}(i)
// Ensure the goroutine runs before we continue the loop.
runtime.Gosched()
}
for i := 0; i < N; i++ {
<-done
}
}
func TestRaceForInit(t *testing.T) {
c := make(chan int)
x := 0
go func() {
c <- x
}()
for x = 42; false; {
}
<-c
}
func TestNoRaceForInit(t *testing.T) {
done := make(chan bool)
c := make(chan bool)
x := 0
go func() {
for {
_, ok := <-c
if !ok {
done <- true
return
}
x++
}
}()
i := 0
for x = 42; i < 10; i++ {
c <- true
}
close(c)
<-done
}
func TestRaceForTest(t *testing.T) {
done := make(chan bool)
c := make(chan bool)
stop := false
go func() {
for {
_, ok := <-c
if !ok {
done <- true
return
}
stop = true
}
}()
for !stop {
c <- true
}
close(c)
<-done
}
func TestRaceForIncr(t *testing.T) {
done := make(chan bool)
c := make(chan bool)
x := 0
go func() {
for {
_, ok := <-c
if !ok {
done <- true
return
}
x++
}
}()
for i := 0; i < 10; x++ {
i++
c <- true
}
close(c)
<-done
}
func TestNoRaceForIncr(t *testing.T) {
done := make(chan bool)
x := 0
go func() {
x++
done <- true
}()
for i := 0; i < 0; x++ {
}
<-done
}
func TestRacePlus(t *testing.T) {
var x, y, z int
_ = y
ch := make(chan int, 2)
go func() {
y = x + z
ch <- 1
}()
go func() {
y = x + z + z
ch <- 1
}()
<-ch
<-ch
}
func TestRacePlus2(t *testing.T) {
var x, y, z int
_ = y
ch := make(chan int, 2)
go func() {
x = 1
ch <- 1
}()
go func() {
y = +x + z
ch <- 1
}()
<-ch
<-ch
}
func TestNoRacePlus(t *testing.T) {
var x, y, z, f int
_ = x + y + f
ch := make(chan int, 2)
go func() {
y = x + z
ch <- 1
}()
go func() {
f = z + x
ch <- 1
}()
<-ch
<-ch
}
func TestRaceComplement(t *testing.T) {
var x, y, z int
_ = x
ch := make(chan int, 2)
go func() {
x = ^y
ch <- 1
}()
go func() {
y = ^z
ch <- 1
}()
<-ch
<-ch
}
func TestRaceDiv(t *testing.T) {
var x, y, z int
_ = x
ch := make(chan int, 2)
go func() {
x = y / (z + 1)
ch <- 1
}()
go func() {
y = z
ch <- 1
}()
<-ch
<-ch
}
func TestRaceDivConst(t *testing.T) {
var x, y, z uint32
_ = x
ch := make(chan int, 2)
go func() {
x = y / 3 // involves only a HMUL node
ch <- 1
}()
go func() {
y = z
ch <- 1
}()
<-ch
<-ch
}
func TestRaceMod(t *testing.T) {
var x, y, z int
_ = x
ch := make(chan int, 2)
go func() {
x = y % (z + 1)
ch <- 1
}()
go func() {
y = z
ch <- 1
}()
<-ch
<-ch
}
func TestRaceModConst(t *testing.T) {
var x, y, z int
_ = x
ch := make(chan int, 2)
go func() {
x = y % 3
ch <- 1
}()
go func() {
y = z
ch <- 1
}()
<-ch
<-ch
}
func TestRaceRotate(t *testing.T) {
var x, y, z uint32
_ = x
ch := make(chan int, 2)
go func() {
x = y<<12 | y>>20
ch <- 1
}()
go func() {
y = z
ch <- 1
}()
<-ch
<-ch
}
// May crash if the instrumentation is reckless.
func TestNoRaceEnoughRegisters(t *testing.T) {
// from erf.go
const (
sa1 = 1
sa2 = 2
sa3 = 3
sa4 = 4
sa5 = 5
sa6 = 6
sa7 = 7
sa8 = 8
)
var s, S float64
s = 3.1415
S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
s = S
}
// emptyFunc should not be inlined.
func emptyFunc(x int) {
if false {
fmt.Println(x)
}
}
func TestRaceFuncArgument(t *testing.T) {
var x int
ch := make(chan bool, 1)
go func() {
emptyFunc(x)
ch <- true
}()
x = 1
<-ch
}
func TestRaceFuncArgument2(t *testing.T) {
var x int
ch := make(chan bool, 2)
go func() {
x = 42
ch <- true
}()
go func(y int) {
ch <- true
}(x)
<-ch
<-ch
}
func TestRaceSprint(t *testing.T) {
var x int
ch := make(chan bool, 1)
go func() {
fmt.Sprint(x)
ch <- true
}()
x = 1
<-ch
}
func TestRaceArrayCopy(t *testing.T) {
ch := make(chan bool, 1)
var a [5]int
go func() {
a[3] = 1
ch <- true
}()
a = [5]int{1, 2, 3, 4, 5}
<-ch
}
// Blows up a naive compiler.
func TestRaceNestedArrayCopy(t *testing.T) {
ch := make(chan bool, 1)
type (
Point32 [2][2][2][2][2]Point
Point1024 [2][2][2][2][2]Point32
Point32k [2][2][2][2][2]Point1024
Point1M [2][2][2][2][2]Point32k
)
var a, b Point1M
go func() {
a[0][1][0][1][0][1][0][1][0][1][0][1][0][1][0][1][0][1][0][1].y = 1
ch <- true
}()
a = b
<-ch
}
func TestRaceStructRW(t *testing.T) {
p := Point{0, 0}
ch := make(chan bool, 1)
go func() {
p = Point{1, 1}
ch <- true
}()
q := p
<-ch
p = q
}
func TestRaceStructFieldRW1(t *testing.T) {
p := Point{0, 0}
ch := make(chan bool, 1)
go func() {
p.x = 1
ch <- true
}()
_ = p.x
<-ch
}
func TestNoRaceStructFieldRW1(t *testing.T) {
// Same struct, different variables, no
// pointers. The layout is known (at compile time?) ->
// no read on p
// writes on x and y
p := Point{0, 0}
ch := make(chan bool, 1)
go func() {
p.x = 1
ch <- true
}()
p.y = 1
<-ch
_ = p
}
func TestNoRaceStructFieldRW2(t *testing.T) {
// Same as NoRaceStructFieldRW1
// but p is a pointer, so there is a read on p
p := Point{0, 0}
ch := make(chan bool, 1)
go func() {
p.x = 1
ch <- true
}()
p.y = 1
<-ch
_ = p
}
func TestRaceStructFieldRW2(t *testing.T) {
p := &Point{0, 0}
ch := make(chan bool, 1)
go func() {
p.x = 1
ch <- true
}()
_ = p.x
<-ch
}
func TestRaceStructFieldRW3(t *testing.T) {
p := NamedPoint{name: "a", p: Point{0, 0}}
ch := make(chan bool, 1)
go func() {
p.p.x = 1
ch <- true
}()
_ = p.p.x
<-ch
}
func TestRaceEfaceWW(t *testing.T) {
var a, b any
ch := make(chan bool, 1)
go func() {
a = 1
ch <- true
}()
a = 2
<-ch
_, _ = a, b
}
func TestRaceIfaceWW(t *testing.T) {
var a, b Writer
ch := make(chan bool, 1)
go func() {
a = DummyWriter{1}
ch <- true
}()
a = DummyWriter{2}
<-ch
b = a
a = b
}
func TestRaceIfaceCmp(t *testing.T) {
var a, b Writer
a = DummyWriter{1}
ch := make(chan bool, 1)
go func() {
a = DummyWriter{1}
ch <- true
}()
_ = a == b
<-ch
}
func TestRaceIfaceCmpNil(t *testing.T) {
var a Writer
a = DummyWriter{1}
ch := make(chan bool, 1)
go func() {
a = DummyWriter{1}
ch <- true
}()
_ = a == nil
<-ch
}
func TestRaceEfaceConv(t *testing.T) {
c := make(chan bool)
v := 0
go func() {
go func(x any) {
}(v)
c <- true
}()
v = 42
<-c
}
type OsFile struct{}
func (*OsFile) Read() {
}
type IoReader interface {
Read()
}
func TestRaceIfaceConv(t *testing.T) {
c := make(chan bool)
f := &OsFile{}
go func() {
go func(x IoReader) {
}(f)
c <- true
}()
f = &OsFile{}
<-c
}
func TestRaceError(t *testing.T) {
ch := make(chan bool, 1)
var err error
go func() {
err = nil
ch <- true
}()
_ = err
<-ch
}
func TestRaceIntptrRW(t *testing.T) {
var x, y int
var p *int = &x
ch := make(chan bool, 1)
go func() {
*p = 5
ch <- true
}()
y = *p
x = y
<-ch
}
func TestRaceStringRW(t *testing.T) {
ch := make(chan bool, 1)
s := ""
go func() {
s = "abacaba"
ch <- true
}()
_ = s
<-ch
}
func TestRaceStringPtrRW(t *testing.T) {
ch := make(chan bool, 1)
var x string
p := &x
go func() {
*p = "a"
ch <- true
}()
_ = *p
<-ch
}
func TestRaceFloat64WW(t *testing.T) {
var x, y float64
ch := make(chan bool, 1)
go func() {
x = 1.0
ch <- true
}()
x = 2.0
<-ch
y = x
x = y
}
func TestRaceComplex128WW(t *testing.T) {
var x, y complex128
ch := make(chan bool, 1)
go func() {
x = 2 + 2i
ch <- true
}()
x = 4 + 4i
<-ch
y = x
x = y
}
func TestRaceUnsafePtrRW(t *testing.T) {
var x, y, z int
x, y, z = 1, 2, 3
var p unsafe.Pointer = unsafe.Pointer(&x)
ch := make(chan bool, 1)
go func() {
p = (unsafe.Pointer)(&z)
ch <- true
}()
y = *(*int)(p)
x = y
<-ch
}
func TestRaceFuncVariableRW(t *testing.T) {
var f func(x int) int
f = func(x int) int {
return x * x
}
ch := make(chan bool, 1)
go func() {
f = func(x int) int {
return x
}
ch <- true
}()
y := f(1)
<-ch
x := y
y = x
}
func TestRaceFuncVariableWW(t *testing.T) {
var f func(x int) int
_ = f
ch := make(chan bool, 1)
go func() {
f = func(x int) int {
return x
}
ch <- true
}()
f = func(x int) int {
return x * x
}
<-ch
}
// This one should not belong to mop_test
func TestRacePanic(t *testing.T) {
var x int
_ = x
var zero int = 0
ch := make(chan bool, 2)
go func() {
defer func() {
err := recover()
if err == nil {
panic("should be panicking")
}
x = 1
ch <- true
}()
var y int = 1 / zero
zero = y
}()
go func() {
defer func() {
err := recover()
if err == nil {
panic("should be panicking")
}
x = 2
ch <- true
}()
var y int = 1 / zero
zero = y
}()
<-ch
<-ch
if zero != 0 {
panic("zero has changed")
}