Skip to content

unix: add unsafe mmap, munmap, mremap #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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