unix: add unsafe mmap, munmap, mremap

Fixes golang/go#56123

Change-Id: I63a7a6fb3a5b1bb556ac19d76a1e0b04a03ebcfa
GitHub-Last-Rev: 39dbc8e308807bd07863b6f5a49bcdcf506bc0e7
GitHub-Pull-Request: golang/sys#197
Reviewed-on: https://go-review.googlesource.com/c/sys/+/592415
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Nuno Cruces
2024-06-25 00:37:56 +00:00
committed by Gopher Robot
parent 76700875df
commit daa239428c
4 changed files with 67 additions and 0 deletions
+30
View File
@@ -8,6 +8,7 @@ package unix_test
import (
"testing"
"unsafe"
"golang.org/x/sys/unix"
)
@@ -44,3 +45,32 @@ func TestMremap(t *testing.T) {
t.Fatalf("remapping to a fixed address; got %v, want %v", err, unix.EINVAL)
}
}
func TestMremapPtr(t *testing.T) {
mmapProt := unix.PROT_NONE
mmapPtrProt := unix.PROT_READ | unix.PROT_WRITE
b, err := unix.Mmap(-1, 0, 2*unix.Getpagesize(), mmapProt, unix.MAP_ANON|unix.MAP_PRIVATE)
if err != nil {
t.Fatalf("Mmap: %v", err)
}
if _, err := unix.MmapPtr(-1, 0, unsafe.Pointer(&b[0]), uintptr(unix.Getpagesize()),
mmapPtrProt, unix.MAP_ANON|unix.MAP_PRIVATE|unix.MAP_FIXED); err != nil {
t.Fatalf("MmapPtr: %v", err)
}
b[0] = 42
if _, err := unix.MremapPtr(
unsafe.Pointer(&b[0]), uintptr(unix.Getpagesize()),
unsafe.Pointer(&b[unix.Getpagesize()]), uintptr(unix.Getpagesize()),
unix.MremapFixed|unix.MremapMaymove); err != nil {
t.Fatalf("MremapPtr: %v", err)
}
if got := b[unix.Getpagesize()]; got != 42 {
t.Errorf("got %d, want 42", got)
}
if err := unix.Munmap(b); err != nil {
t.Fatalf("Munmap: %v", err)
}
}