-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathptrguard_test.go
241 lines (227 loc) · 4.23 KB
/
ptrguard_test.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
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
package ptrguard_test
import (
"runtime"
"testing"
"time"
"unsafe"
"github.com/ansiwen/ptrguard"
. "github.com/ansiwen/ptrguard/internal/testhelper"
"github.com/stretchr/testify/assert"
)
const (
ptrSize = unsafe.Sizeof(unsafe.Pointer(nil))
fooBar = "fooBar"
)
type tracer struct {
p *string
b *bool
}
func newTracer() tracer {
var b bool
s := "foobar"
runtime.SetFinalizer(&s, func(interface{}) { b = true })
return tracer{&s, &b}
}
func TestPin(t *testing.T) {
tr1 := newTracer()
tr2 := newTracer()
cPtr := (*unsafe.Pointer)(Malloc(ptrSize))
defer Free(unsafe.Pointer(cPtr))
func() {
var p ptrguard.Pinner
defer p.Unpin()
p.Pin(tr1.p).Store(cPtr)
assert.Equal(t, unsafe.Pointer(tr1.p), *cPtr)
tr1.p = nil
tr2.p = nil
runtime.GC()
runtime.GC()
assert.False(t, *tr1.b)
assert.True(t, *tr2.b)
}()
runtime.GC()
runtime.GC()
assert.Eventually(t, func() bool { return *tr1.b == true },
5*time.Second, 10*time.Millisecond)
assert.Zero(t, *cPtr)
}
func TestReusePinner(t *testing.T) {
tr1 := newTracer()
tr2 := newTracer()
cPtr := (*unsafe.Pointer)(Malloc(ptrSize))
defer Free(unsafe.Pointer(cPtr))
var p ptrguard.Pinner
p.Pin(tr1.p).Store(cPtr)
assert.Equal(t, unsafe.Pointer(tr1.p), *cPtr)
tr1.p = nil
runtime.GC()
runtime.GC()
assert.False(t, *tr1.b)
p.Unpin()
runtime.GC()
runtime.GC()
assert.Eventually(t, func() bool { return *tr1.b == true },
5*time.Second, 10*time.Millisecond)
assert.Zero(t, *cPtr)
p.Pin(tr2.p).Store(cPtr)
assert.Equal(t, unsafe.Pointer(tr2.p), *cPtr)
tr2.p = nil
runtime.GC()
runtime.GC()
assert.False(t, *tr2.b)
p.Unpin()
runtime.GC()
runtime.GC()
assert.Eventually(t, func() bool { return *tr2.b == true },
5*time.Second, 10*time.Millisecond)
assert.Zero(t, *cPtr)
}
func TestMultiStore(t *testing.T) {
goPtr := &[1]byte{}
cPtrArr := (*[1024]unsafe.Pointer)(Malloc(ptrSize * 1024))
defer Free(unsafe.Pointer(&cPtrArr[0]))
func() {
var pg ptrguard.Pinner
defer pg.Unpin()
pp := pg.Pin(goPtr)
for i := range cPtrArr {
pp.Store(&cPtrArr[i])
}
for i := range cPtrArr {
assert.Equal(t, cPtrArr[i], unsafe.Pointer(goPtr))
}
}()
for i := range cPtrArr {
assert.Zero(t, cPtrArr[i])
}
}
func TestMultiPin(t *testing.T) {
var trs [1024]tracer
for i := range trs {
trs[i] = newTracer()
}
func() {
var pg ptrguard.Pinner
defer pg.Unpin()
for i := range trs {
pg.Pin(trs[i].p)
trs[i].p = nil
}
runtime.GC()
runtime.GC()
for i := range trs {
assert.False(t, *trs[i].b)
}
}()
runtime.GC()
runtime.GC()
assert.Eventually(t, func() bool { return *trs[len(trs)-1].b == true },
5*time.Second, 10*time.Millisecond)
for i := range trs {
assert.True(t, *trs[i].b)
}
}
func TestNoCheck(t *testing.T) {
s := fooBar
goPtr := (unsafe.Pointer)(&s)
goPtrPtr := (unsafe.Pointer)(&goPtr)
assert.Panics(t,
func() {
DummyCCall(goPtrPtr)
},
"Please run tests with GODEBUG=cgocheck=2",
)
assert.NotPanics(t,
func() {
ptrguard.NoCheck(func() {
DummyCCall(goPtrPtr)
})
},
)
assert.Panics(t,
func() {
DummyCCall(goPtrPtr)
},
"Please run tests with GODEBUG=cgocheck=2",
)
assert.NotPanics(t,
func() {
ptrguard.NoCheck(func() {
DummyCCall(goPtrPtr)
})
},
)
}
func TestUnintialized(t *testing.T) {
var pp ptrguard.Pinner
assert.NotPanics(t,
func() {
pp.Unpin()
pp.Unpin()
},
)
}
func TestDoubleUnpin(t *testing.T) {
s := fooBar
var pp ptrguard.Pinner
pp.Pin(&s)
assert.NotPanics(t,
func() {
pp.Unpin()
pp.Unpin()
},
)
}
func TestPinNonPointerPanics(t *testing.T) {
s := []byte("string")
var pg ptrguard.Pinner
defer pg.Unpin()
assert.NotPanics(t,
func() {
pg.Pin(&s)
},
)
assert.NotPanics(t,
func() {
pg.Pin(unsafe.Pointer(&s))
},
)
assert.Panics(t,
func() {
pg.Pin(s)
},
)
}
func TestStoreToNonPtrPtrPanics(t *testing.T) {
s := []byte("string")
var p1 unsafe.Pointer
var p2 *byte
var i uintptr
var pg ptrguard.Pinner
defer pg.Unpin()
assert.NotPanics(t,
func() {
pg.Pin(&s).Store(&p1)
},
)
assert.NotPanics(t,
func() {
pg.Pin(&s).Store(&p2)
},
)
assert.Panics(t,
func() {
pg.Pin(&s).Store(&i)
},
)
assert.Panics(t,
func() {
pg.Pin(&s).Store(unsafe.Pointer(&i))
},
)
assert.Panics(t,
func() {
pg.Pin(&s).Store(i)
},
)
}