forked from StalkR/misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
race-slice.go
77 lines (71 loc) · 1.22 KB
/
race-slice.go
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
/*
go run race-slice.go
go run -race race-slice.go
GORACE=halt_on_error=1 go run -race race-slice.go
*/
package main
import (
"fmt"
"os"
"runtime"
"strconv"
)
func address(i interface{}) int {
addr, err := strconv.ParseUint(fmt.Sprintf("%p", i), 0, 0)
if err != nil {
panic(err)
}
return int(addr)
}
type fptr struct {
f func()
}
func win() {
fmt.Println("win", i, j)
os.Exit(1)
}
var i, j int
func main() {
if runtime.NumCPU() < 2 {
fmt.Println("need >= 2 CPUs")
os.Exit(1)
}
runtime.GOMAXPROCS(runtime.NumCPU())
pp := address(win)
long := make([]*int, 2)
short := make([]*int, 1)
target := new(fptr)
if address(short)+8 != address(target) {
fmt.Println("target object isn't next to short object")
os.Exit(0)
}
confused := short
go func() {
for {
confused = long
confused = short
i++
}
}()
// we want confused to point to short but still have the length
// and capacity of long, which allows to write f in target
// if this isn't good, it will panic with index out of range, which
// we can recover from
g := func() {
confused[1] = &pp
if target.f != nil {
target.f()
}
j++
}
f := func() {
defer func() {
if r := recover(); r != nil {
}
}()
g()
}
for {
f()
}
}