2014-08-12 01:48:46 +04:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-02-20 06:57:36 +03:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2014-08-12 01:48:46 +04:00
|
|
|
|
2014-08-12 02:58:26 +04:00
|
|
|
package unix_test
|
2014-08-12 01:48:46 +04:00
|
|
|
|
|
|
|
import (
|
2018-08-17 11:25:03 +03:00
|
|
|
"runtime"
|
2014-08-12 01:48:46 +04:00
|
|
|
"testing"
|
2014-08-13 09:59:00 +04:00
|
|
|
|
2014-11-10 00:56:17 +03:00
|
|
|
"golang.org/x/sys/unix"
|
2014-08-12 01:48:46 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMmap(t *testing.T) {
|
2023-02-23 19:24:01 +03:00
|
|
|
mmapProt := unix.PROT_NONE
|
|
|
|
mprotectProt := unix.PROT_READ | unix.PROT_WRITE
|
|
|
|
// On NetBSD PAX mprotect prohibits setting protection bits
|
|
|
|
// missing from the original mmap call unless explicitly
|
|
|
|
// requested with PROT_MPROTECT.
|
|
|
|
if runtime.GOOS == "netbsd" {
|
|
|
|
// PROT_MPROTECT(x) is defined as ((x) << 3):
|
|
|
|
// https://github.com/NetBSD/src/blob/aba449a55bf91b44bc68f542edd9afa341962b89/sys/sys/mman.h#L73
|
|
|
|
mmapProt = mprotectProt << 3
|
|
|
|
}
|
|
|
|
b, err := unix.Mmap(-1, 0, unix.Getpagesize(), mmapProt, unix.MAP_ANON|unix.MAP_PRIVATE)
|
2014-08-12 01:48:46 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Mmap: %v", err)
|
|
|
|
}
|
2023-02-23 19:24:01 +03:00
|
|
|
if err := unix.Mprotect(b, mprotectProt); err != nil {
|
2017-08-16 18:16:36 +03:00
|
|
|
t.Fatalf("Mprotect: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b[0] = 42
|
|
|
|
|
2018-08-17 11:25:03 +03:00
|
|
|
if runtime.GOOS == "aix" {
|
|
|
|
t.Skip("msync returns invalid argument for AIX, skipping msync test")
|
|
|
|
} else {
|
|
|
|
if err := unix.Msync(b, unix.MS_SYNC); err != nil {
|
|
|
|
t.Fatalf("Msync: %v", err)
|
|
|
|
}
|
2017-08-16 18:16:36 +03:00
|
|
|
}
|
2018-08-17 11:25:03 +03:00
|
|
|
|
2017-08-16 18:16:36 +03:00
|
|
|
if err := unix.Madvise(b, unix.MADV_DONTNEED); err != nil {
|
|
|
|
t.Fatalf("Madvise: %v", err)
|
|
|
|
}
|
2014-08-12 02:58:26 +04:00
|
|
|
if err := unix.Munmap(b); err != nil {
|
2014-08-12 01:48:46 +04:00
|
|
|
t.Fatalf("Munmap: %v", err)
|
|
|
|
}
|
|
|
|
}
|