From daa239428c2d61fe52eab0fce9e8d0921c9dbbed Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 25 Jun 2024 00:35:40 +0000 Subject: [PATCH] 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 LUCI-TryBot-Result: Go LUCI Auto-Submit: Dmitri Shuralyov Reviewed-by: Ian Lance Taylor --- unix/mmap_unix_test.go | 23 +++++++++++++++++++++++ unix/mremap.go | 5 +++++ unix/mremap_test.go | 30 ++++++++++++++++++++++++++++++ unix/syscall_unix.go | 9 +++++++++ 4 files changed, 67 insertions(+) diff --git a/unix/mmap_unix_test.go b/unix/mmap_unix_test.go index a60a0f62b..eb146059c 100644 --- a/unix/mmap_unix_test.go +++ b/unix/mmap_unix_test.go @@ -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) + } +} diff --git a/unix/mremap.go b/unix/mremap.go index fd45fe529..3a5e776f8 100644 --- a/unix/mremap.go +++ b/unix/mremap.go @@ -50,3 +50,8 @@ func (m *mremapMmapper) Mremap(oldData []byte, newLength int, flags int) (data [ func Mremap(oldData []byte, newLength int, flags int) (data []byte, err error) { return mapper.Mremap(oldData, newLength, flags) } + +func MremapPtr(oldAddr unsafe.Pointer, oldSize uintptr, newAddr unsafe.Pointer, newSize uintptr, flags int) (ret unsafe.Pointer, err error) { + xaddr, err := mapper.mremap(uintptr(oldAddr), oldSize, newSize, flags, uintptr(newAddr)) + return unsafe.Pointer(xaddr), err +} diff --git a/unix/mremap_test.go b/unix/mremap_test.go index e84104fc6..5019dd789 100644 --- a/unix/mremap_test.go +++ b/unix/mremap_test.go @@ -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) + } +} diff --git a/unix/syscall_unix.go b/unix/syscall_unix.go index 77081de8c..4e92e5aa4 100644 --- a/unix/syscall_unix.go +++ b/unix/syscall_unix.go @@ -154,6 +154,15 @@ func Munmap(b []byte) (err error) { return mapper.Munmap(b) } +func MmapPtr(fd int, offset int64, addr unsafe.Pointer, length uintptr, prot int, flags int) (ret unsafe.Pointer, err error) { + xaddr, err := mapper.mmap(uintptr(addr), length, prot, flags, fd, offset) + return unsafe.Pointer(xaddr), err +} + +func MunmapPtr(addr unsafe.Pointer, length uintptr) (err error) { + return mapper.munmap(uintptr(addr), length) +} + func Read(fd int, p []byte) (n int, err error) { n, err = read(fd, p) if raceenabled {