Skip to content

Commit

Permalink
unix: add unsafe mmap, munmap, mremap
Browse files Browse the repository at this point in the history
Fixes golang/go#56123

Change-Id: I63a7a6fb3a5b1bb556ac19d76a1e0b04a03ebcfa
GitHub-Last-Rev: 39dbc8e
GitHub-Pull-Request: #197
Reviewed-on: https://go-review.googlesource.com/c/sys/+/592415
Reviewed-by: Dmitri Shuralyov <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Dmitri Shuralyov <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
ncruces authored and gopherbot committed Jun 25, 2024
1 parent 7670087 commit daa2394
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
23 changes: 23 additions & 0 deletions unix/mmap_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package unix_test
import (
"runtime"
"testing"
"unsafe"

"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -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)
}
}
5 changes: 5 additions & 0 deletions unix/mremap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 30 additions & 0 deletions unix/mremap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package unix_test

import (
"testing"
"unsafe"

"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -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)
}
}
9 changes: 9 additions & 0 deletions unix/syscall_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit daa2394

Please sign in to comment.