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
+23
View File
@@ -9,6 +9,7 @@ package unix_test
import (
"runtime"
"testing"
"unsafe"
"golang.org/x/sys/unix"
)
@@ -49,3 +50,25 @@ func TestMmap(t *testing.T) {
t.Fatalf("Munmap: %v", err)
}
}
func TestMmapPtr(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.MunmapPtr(unsafe.Pointer(&b[0]), uintptr(unix.Getpagesize())); err != nil {
t.Fatalf("MunmapPtr: %v", err)
}
if err := unix.Munmap(b); err != nil {
t.Fatalf("Munmap: %v", err)
}
}