golang.org/x/sys@v0.9.0/unix/mmap_unix_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
     7  
     8  package unix_test
     9  
    10  import (
    11  	"runtime"
    12  	"testing"
    13  
    14  	"golang.org/x/sys/unix"
    15  )
    16  
    17  func TestMmap(t *testing.T) {
    18  	mmapProt := unix.PROT_NONE
    19  	mprotectProt := unix.PROT_READ | unix.PROT_WRITE
    20  	// On NetBSD PAX mprotect prohibits setting protection bits
    21  	// missing from the original mmap call unless explicitly
    22  	// requested with PROT_MPROTECT.
    23  	if runtime.GOOS == "netbsd" {
    24  		// PROT_MPROTECT(x) is defined as ((x) << 3):
    25  		// https://github.com/NetBSD/src/blob/aba449a55bf91b44bc68f542edd9afa341962b89/sys/sys/mman.h#L73
    26  		mmapProt = mprotectProt << 3
    27  	}
    28  	b, err := unix.Mmap(-1, 0, unix.Getpagesize(), mmapProt, unix.MAP_ANON|unix.MAP_PRIVATE)
    29  	if err != nil {
    30  		t.Fatalf("Mmap: %v", err)
    31  	}
    32  	if err := unix.Mprotect(b, mprotectProt); err != nil {
    33  		t.Fatalf("Mprotect: %v", err)
    34  	}
    35  
    36  	b[0] = 42
    37  
    38  	if runtime.GOOS == "aix" {
    39  		t.Skip("msync returns invalid argument for AIX, skipping msync test")
    40  	} else {
    41  		if err := unix.Msync(b, unix.MS_SYNC); err != nil {
    42  			t.Fatalf("Msync: %v", err)
    43  		}
    44  	}
    45  
    46  	if err := unix.Madvise(b, unix.MADV_DONTNEED); err != nil {
    47  		t.Fatalf("Madvise: %v", err)
    48  	}
    49  	if err := unix.Munmap(b); err != nil {
    50  		t.Fatalf("Munmap: %v", err)
    51  	}
    52  }