...
1
2
3
4
5
6
7 package unix_test
8
9 import (
10 "testing"
11
12 "golang.org/x/sys/unix"
13 )
14
15 func TestMremap(t *testing.T) {
16 b, err := unix.Mmap(-1, 0, unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
17 if err != nil {
18 t.Fatalf("Mmap: %v", err)
19 }
20 if err := unix.Mprotect(b, unix.PROT_READ|unix.PROT_WRITE); err != nil {
21 t.Fatalf("Mprotect: %v", err)
22 }
23
24 b[0] = 42
25
26 bNew, err := unix.Mremap(b, unix.Getpagesize()*2, unix.MremapMaymove)
27 if err != nil {
28 t.Fatalf("Mremap2: %v", err)
29 }
30 bNew[unix.Getpagesize()+1] = 84
31
32 if bNew[0] != 42 {
33 t.Fatal("first element value was changed")
34 }
35 if len(bNew) != unix.Getpagesize()*2 {
36 t.Fatal("new memory len not equal to specified len")
37 }
38 if cap(bNew) != unix.Getpagesize()*2 {
39 t.Fatal("new memory cap not equal to specified len")
40 }
41
42 _, err = unix.Mremap(b, unix.Getpagesize(), unix.MremapFixed)
43 if err != unix.EINVAL {
44 t.Fatalf("remapping to a fixed address; got %v, want %v", err, unix.EINVAL)
45 }
46 }
47
View as plain text