golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/unix/mremap_test.go (about) 1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build linux || netbsd 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 // checks 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 }