Skip to content

Commit

Permalink
unix: fix MmapPtr test failing on OpenBSD
Browse files Browse the repository at this point in the history
OpenBSD apparently doesn't allow unmapping address space
if part of the region is already unmapped.
This tweaks the test so that munmapping twice no longer happens.

Cq-Include-Trybots: luci.golang.try:gotip-openbsd-amd64
  • Loading branch information
ncruces committed Jun 26, 2024
1 parent daa2394 commit 2535abd
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions unix/mmap_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package unix_test
import (
"runtime"
"testing"
"unsafe"

"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -52,23 +51,20 @@ func TestMmap(t *testing.T) {
}

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)
p, err := unix.MmapPtr(-1, 0, nil, uintptr(2*unix.Getpagesize()),
unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
if err != nil {
t.Fatalf("Mmap: %v", err)
t.Fatalf("MmapPtr: %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 {

if _, err := unix.MmapPtr(-1, 0, p, uintptr(unix.Getpagesize()),
unix.PROT_READ|unix.PROT_WRITE, unix.MAP_ANON|unix.MAP_PRIVATE|unix.MAP_FIXED); err != nil {
t.Fatalf("MmapPtr: %v", err)
}

b[0] = 42
*(*byte)(p) = 42

if err := unix.MunmapPtr(unsafe.Pointer(&b[0]), uintptr(unix.Getpagesize())); err != nil {
if err := unix.MunmapPtr(p, uintptr(2*unix.Getpagesize())); err != nil {
t.Fatalf("MunmapPtr: %v", err)
}
if err := unix.Munmap(b); err != nil {
t.Fatalf("Munmap: %v", err)
}
}

0 comments on commit 2535abd

Please sign in to comment.