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
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package safecopy
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"runtime/debug"
"syscall"
"testing"
"unsafe"
)
// Size of a page in bytes. Cloned from usermem.PageSize to avoid a circular
// dependency.
const pageSize = 4096
func initRandom(b []byte) {
for i := range b {
b[i] = byte(rand.Intn(256))
}
}
func randBuf(size int) []byte {
b := make([]byte, size)
initRandom(b)
return b
}
func TestCopyInSuccess(t *testing.T) {
// Test that CopyIn does not return an error when all pages are accessible.
const bufLen = 8192
a := randBuf(bufLen)
b := make([]byte, bufLen)
n, err := CopyIn(b, unsafe.Pointer(&a[0]))
if n != bufLen {
t.Errorf("Unexpected copy length, got %v, want %v", n, bufLen)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !bytes.Equal(a, b) {
t.Errorf("Buffers are not equal when they should be: %v %v", a, b)
}
}
func TestCopyOutSuccess(t *testing.T) {
// Test that CopyOut does not return an error when all pages are
// accessible.
const bufLen = 8192
a := randBuf(bufLen)
b := make([]byte, bufLen)
n, err := CopyOut(unsafe.Pointer(&b[0]), a)
if n != bufLen {
t.Errorf("Unexpected copy length, got %v, want %v", n, bufLen)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !bytes.Equal(a, b) {
t.Errorf("Buffers are not equal when they should be: %v %v", a, b)
}
}
func TestCopySuccess(t *testing.T) {
// Test that Copy does not return an error when all pages are accessible.
const bufLen = 8192
a := randBuf(bufLen)
b := make([]byte, bufLen)
n, err := Copy(unsafe.Pointer(&b[0]), unsafe.Pointer(&a[0]), bufLen)
if n != bufLen {
t.Errorf("Unexpected copy length, got %v, want %v", n, bufLen)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !bytes.Equal(a, b) {
t.Errorf("Buffers are not equal when they should be: %v %v", a, b)
}
}
func TestZeroOutSuccess(t *testing.T) {
// Test that ZeroOut does not return an error when all pages are
// accessible.
const bufLen = 8192
a := make([]byte, bufLen)
b := randBuf(bufLen)
n, err := ZeroOut(unsafe.Pointer(&b[0]), bufLen)
if n != bufLen {
t.Errorf("Unexpected copy length, got %v, want %v", n, bufLen)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !bytes.Equal(a, b) {
t.Errorf("Buffers are not equal when they should be: %v %v", a, b)
}
}
func TestSwapUint32Success(t *testing.T) {
// Test that SwapUint32 does not return an error when the page is
// accessible.
before := uint32(rand.Int31())
after := uint32(rand.Int31())
val := before
old, err := SwapUint32(unsafe.Pointer(&val), after)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if old != before {
t.Errorf("Unexpected old value: got %v, want %v", old, before)
}
if val != after {
t.Errorf("Unexpected new value: got %v, want %v", val, after)
}
}
func TestSwapUint32AlignmentError(t *testing.T) {
// Test that SwapUint32 returns an AlignmentError when passed an unaligned
// address.
data := new(struct{ val uint64 })
addr := uintptr(unsafe.Pointer(&data.val)) + 1
want := AlignmentError{Addr: addr, Alignment: 4}
if _, err := SwapUint32(unsafe.Pointer(addr), 1); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
}
func TestSwapUint64Success(t *testing.T) {
// Test that SwapUint64 does not return an error when the page is
// accessible.
before := uint64(rand.Int63())
after := uint64(rand.Int63())
// "The first word in ... an allocated struct or slice can be relied upon
// to be 64-bit aligned." - sync/atomic docs
data := new(struct{ val uint64 })
data.val = before
old, err := SwapUint64(unsafe.Pointer(&data.val), after)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if old != before {
t.Errorf("Unexpected old value: got %v, want %v", old, before)
}
if data.val != after {
t.Errorf("Unexpected new value: got %v, want %v", data.val, after)
}
}
func TestSwapUint64AlignmentError(t *testing.T) {
// Test that SwapUint64 returns an AlignmentError when passed an unaligned
// address.
data := new(struct{ val1, val2 uint64 })
addr := uintptr(unsafe.Pointer(&data.val1)) + 1
want := AlignmentError{Addr: addr, Alignment: 8}
if _, err := SwapUint64(unsafe.Pointer(addr), 1); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
}
func TestCompareAndSwapUint32Success(t *testing.T) {
// Test that CompareAndSwapUint32 does not return an error when the page is
// accessible.
before := uint32(rand.Int31())
after := uint32(rand.Int31())
val := before
old, err := CompareAndSwapUint32(unsafe.Pointer(&val), before, after)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if old != before {
t.Errorf("Unexpected old value: got %v, want %v", old, before)
}
if val != after {
t.Errorf("Unexpected new value: got %v, want %v", val, after)
}
}
func TestCompareAndSwapUint32AlignmentError(t *testing.T) {
// Test that CompareAndSwapUint32 returns an AlignmentError when passed an
// unaligned address.
data := new(struct{ val uint64 })
addr := uintptr(unsafe.Pointer(&data.val)) + 1
want := AlignmentError{Addr: addr, Alignment: 4}
if _, err := CompareAndSwapUint32(unsafe.Pointer(addr), 0, 1); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
}
// withSegvErrorTestMapping calls fn with a two-page mapping. The first page
// contains random data, and the second page generates SIGSEGV when accessed.
func withSegvErrorTestMapping(t *testing.T, fn func(m []byte)) {
mapping, err := syscall.Mmap(-1, 0, 2*pageSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANONYMOUS|syscall.MAP_PRIVATE)
if err != nil {
t.Fatalf("Mmap failed: %v", err)
}
defer syscall.Munmap(mapping)
if err := syscall.Mprotect(mapping[pageSize:], syscall.PROT_NONE); err != nil {
t.Fatalf("Mprotect failed: %v", err)
}
initRandom(mapping[:pageSize])
fn(mapping)
}
// withBusErrorTestMapping calls fn with a two-page mapping. The first page
// contains random data, and the second page generates SIGBUS when accessed.
func withBusErrorTestMapping(t *testing.T, fn func(m []byte)) {
f, err := ioutil.TempFile("", "sigbus_test")
if err != nil {
t.Fatalf("TempFile failed: %v", err)
}
defer f.Close()
if err := f.Truncate(pageSize); err != nil {
t.Fatalf("Truncate failed: %v", err)
}
mapping, err := syscall.Mmap(int(f.Fd()), 0, 2*pageSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
t.Fatalf("Mmap failed: %v", err)
}
defer syscall.Munmap(mapping)
initRandom(mapping[:pageSize])
fn(mapping)
}
func TestCopyInSegvError(t *testing.T) {
// Test that CopyIn returns a SegvError when reaching a page that signals
// SIGSEGV.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting copy %d bytes before SIGSEGV", bytesBeforeFault), func(t *testing.T) {
withSegvErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
src := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
dst := randBuf(pageSize)
n, err := CopyIn(dst, src)
if n != bytesBeforeFault {
t.Errorf("Unexpected copy length: got %v, want %v", n, bytesBeforeFault)
}
if want := (SegvError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := dst[:bytesBeforeFault], mapping[pageSize-bytesBeforeFault:pageSize]; !bytes.Equal(got, want) {
t.Errorf("Buffers are not equal when they should be: %v %v", got, want)
}
})
})
}
}
func TestCopyInBusError(t *testing.T) {
// Test that CopyIn returns a BusError when reaching a page that signals
// SIGBUS.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting copy %d bytes before SIGBUS", bytesBeforeFault), func(t *testing.T) {
withBusErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
src := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
dst := randBuf(pageSize)
n, err := CopyIn(dst, src)
if n != bytesBeforeFault {
t.Errorf("Unexpected copy length: got %v, want %v", n, bytesBeforeFault)
}
if want := (BusError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := dst[:bytesBeforeFault], mapping[pageSize-bytesBeforeFault:pageSize]; !bytes.Equal(got, want) {
t.Errorf("Buffers are not equal when they should be: %v %v", got, want)
}
})
})
}
}
func TestCopyOutSegvError(t *testing.T) {
// Test that CopyOut returns a SegvError when reaching a page that signals
// SIGSEGV.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting copy %d bytes before SIGSEGV", bytesBeforeFault), func(t *testing.T) {
withSegvErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
dst := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
src := randBuf(pageSize)
n, err := CopyOut(dst, src)
if n != bytesBeforeFault {
t.Errorf("Unexpected copy length: got %v, want %v", n, bytesBeforeFault)
}
if want := (SegvError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := mapping[pageSize-bytesBeforeFault:pageSize], src[:bytesBeforeFault]; !bytes.Equal(got, want) {
t.Errorf("Buffers are not equal when they should be: %v %v", got, want)
}
})
})
}
}
func TestCopyOutBusError(t *testing.T) {
// Test that CopyOut returns a BusError when reaching a page that signals
// SIGBUS.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting copy %d bytes before SIGSEGV", bytesBeforeFault), func(t *testing.T) {
withBusErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
dst := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
src := randBuf(pageSize)
n, err := CopyOut(dst, src)
if n != bytesBeforeFault {
t.Errorf("Unexpected copy length: got %v, want %v", n, bytesBeforeFault)
}
if want := (BusError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := mapping[pageSize-bytesBeforeFault:pageSize], src[:bytesBeforeFault]; !bytes.Equal(got, want) {
t.Errorf("Buffers are not equal when they should be: %v %v", got, want)
}
})
})
}
}
func TestCopySourceSegvError(t *testing.T) {
// Test that Copy returns a SegvError when copying from a page that signals
// SIGSEGV.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting copy %d bytes before SIGSEGV", bytesBeforeFault), func(t *testing.T) {
withSegvErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
src := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
dst := randBuf(pageSize)
n, err := Copy(unsafe.Pointer(&dst[0]), src, pageSize)
if n != uintptr(bytesBeforeFault) {
t.Errorf("Unexpected copy length: got %v, want %v", n, bytesBeforeFault)
}
if want := (SegvError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := dst[:bytesBeforeFault], mapping[pageSize-bytesBeforeFault:pageSize]; !bytes.Equal(got, want) {
t.Errorf("Buffers are not equal when they should be: %v %v", got, want)
}
})
})
}
}
func TestCopySourceBusError(t *testing.T) {
// Test that Copy returns a BusError when copying from a page that signals
// SIGBUS.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting copy %d bytes before SIGBUS", bytesBeforeFault), func(t *testing.T) {
withBusErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
src := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
dst := randBuf(pageSize)
n, err := Copy(unsafe.Pointer(&dst[0]), src, pageSize)
if n != uintptr(bytesBeforeFault) {
t.Errorf("Unexpected copy length: got %v, want %v", n, bytesBeforeFault)
}
if want := (BusError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := dst[:bytesBeforeFault], mapping[pageSize-bytesBeforeFault:pageSize]; !bytes.Equal(got, want) {
t.Errorf("Buffers are not equal when they should be: %v %v", got, want)
}
})
})
}
}
func TestCopyDestinationSegvError(t *testing.T) {
// Test that Copy returns a SegvError when copying to a page that signals
// SIGSEGV.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting copy %d bytes before SIGSEGV", bytesBeforeFault), func(t *testing.T) {
withSegvErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
dst := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
src := randBuf(pageSize)
n, err := Copy(dst, unsafe.Pointer(&src[0]), pageSize)
if n != uintptr(bytesBeforeFault) {
t.Errorf("Unexpected copy length: got %v, want %v", n, bytesBeforeFault)
}
if want := (SegvError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := mapping[pageSize-bytesBeforeFault:pageSize], src[:bytesBeforeFault]; !bytes.Equal(got, want) {
t.Errorf("Buffers are not equal when they should be: %v %v", got, want)
}
})
})
}
}
func TestCopyDestinationBusError(t *testing.T) {
// Test that Copy returns a BusError when copying to a page that signals
// SIGBUS.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting copy %d bytes before SIGBUS", bytesBeforeFault), func(t *testing.T) {
withBusErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
dst := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
src := randBuf(pageSize)
n, err := Copy(dst, unsafe.Pointer(&src[0]), pageSize)
if n != uintptr(bytesBeforeFault) {
t.Errorf("Unexpected copy length: got %v, want %v", n, bytesBeforeFault)
}
if want := (BusError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := mapping[pageSize-bytesBeforeFault:pageSize], src[:bytesBeforeFault]; !bytes.Equal(got, want) {
t.Errorf("Buffers are not equal when they should be: %v %v", got, want)
}
})
})
}
}
func TestZeroOutSegvError(t *testing.T) {
// Test that ZeroOut returns a SegvError when reaching a page that signals
// SIGSEGV.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting write %d bytes before SIGSEGV", bytesBeforeFault), func(t *testing.T) {
withSegvErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
dst := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
n, err := ZeroOut(dst, pageSize)
if n != uintptr(bytesBeforeFault) {
t.Errorf("Unexpected write length: got %v, want %v", n, bytesBeforeFault)
}
if want := (SegvError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := mapping[pageSize-bytesBeforeFault:pageSize], make([]byte, bytesBeforeFault); !bytes.Equal(got, want) {
t.Errorf("Non-zero bytes in written part of mapping: %v", got)
}
})
})
}
}
func TestZeroOutBusError(t *testing.T) {
// Test that ZeroOut returns a BusError when reaching a page that signals
// SIGBUS.
for bytesBeforeFault := 0; bytesBeforeFault <= 2*maxRegisterSize; bytesBeforeFault++ {
t.Run(fmt.Sprintf("starting write %d bytes before SIGBUS", bytesBeforeFault), func(t *testing.T) {
withBusErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
dst := unsafe.Pointer(secondPage - uintptr(bytesBeforeFault))
n, err := ZeroOut(dst, pageSize)
if n != uintptr(bytesBeforeFault) {
t.Errorf("Unexpected write length: got %v, want %v", n, bytesBeforeFault)
}
if want := (BusError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
if got, want := mapping[pageSize-bytesBeforeFault:pageSize], make([]byte, bytesBeforeFault); !bytes.Equal(got, want) {
t.Errorf("Non-zero bytes in written part of mapping: %v", got)
}
})
})
}
}
func TestSwapUint32SegvError(t *testing.T) {
// Test that SwapUint32 returns a SegvError when reaching a page that
// signals SIGSEGV.
withSegvErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
_, err := SwapUint32(unsafe.Pointer(secondPage), 1)
if want := (SegvError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
})
}
func TestSwapUint32BusError(t *testing.T) {
// Test that SwapUint32 returns a BusError when reaching a page that
// signals SIGBUS.
withBusErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
_, err := SwapUint32(unsafe.Pointer(secondPage), 1)
if want := (BusError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
})
}
func TestSwapUint64SegvError(t *testing.T) {
// Test that SwapUint64 returns a SegvError when reaching a page that
// signals SIGSEGV.
withSegvErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
_, err := SwapUint64(unsafe.Pointer(secondPage), 1)
if want := (SegvError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
})
}
func TestSwapUint64BusError(t *testing.T) {
// Test that SwapUint64 returns a BusError when reaching a page that
// signals SIGBUS.
withBusErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
_, err := SwapUint64(unsafe.Pointer(secondPage), 1)
if want := (BusError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
})
}
func TestCompareAndSwapUint32SegvError(t *testing.T) {
// Test that CompareAndSwapUint32 returns a SegvError when reaching a page
// that signals SIGSEGV.
withSegvErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
_, err := CompareAndSwapUint32(unsafe.Pointer(secondPage), 0, 1)
if want := (SegvError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
})
}
func TestCompareAndSwapUint32BusError(t *testing.T) {
// Test that CompareAndSwapUint32 returns a BusError when reaching a page
// that signals SIGBUS.
withBusErrorTestMapping(t, func(mapping []byte) {
secondPage := uintptr(unsafe.Pointer(&mapping[0])) + pageSize
_, err := CompareAndSwapUint32(unsafe.Pointer(secondPage), 0, 1)
if want := (BusError{secondPage}); err != want {
t.Errorf("Unexpected error: got %v, want %v", err, want)
}
})
}
func testCopy(dst, src []byte) (panicked bool) {
defer func() {
if r := recover(); r != nil {
panicked = true
}
}()
debug.SetPanicOnFault(true)
copy(dst, src)
return
}
func TestSegVOnMemmove(t *testing.T) {
// Test that SIGSEGVs received by runtime.memmove when *not* doing
// CopyIn or CopyOut work gets propagated to the runtime.
const bufLen = pageSize
a, err := syscall.Mmap(-1, 0, bufLen, syscall.PROT_NONE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
if err != nil {
t.Fatalf("Mmap failed: %v", err)
}
defer syscall.Munmap(a)
b := randBuf(bufLen)
if !testCopy(b, a) {
t.Fatalf("testCopy didn't panic when it should have")
}
if !testCopy(a, b) {
t.Fatalf("testCopy didn't panic when it should have")
}
}
func TestSigbusOnMemmove(t *testing.T) {
// Test that SIGBUS received by runtime.memmove when *not* doing
// CopyIn or CopyOut work gets propagated to the runtime.
const bufLen = pageSize
f, err := ioutil.TempFile("", "sigbus_test")
if err != nil {
t.Fatalf("TempFile failed: %v", err)
}
os.Remove(f.Name())
defer f.Close()
a, err := syscall.Mmap(int(f.Fd()), 0, bufLen, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
t.Fatalf("Mmap failed: %v", err)
}
defer syscall.Munmap(a)
b := randBuf(bufLen)
if !testCopy(b, a) {
t.Fatalf("testCopy didn't panic when it should have")
}
if !testCopy(a, b) {
t.Fatalf("testCopy didn't panic when it should have")
}
}
|