| 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 |
|
| 7 |
package unix_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"runtime" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"golang.org/x/sys/unix" |
| 14 |
) |
| 15 |
|
| 16 |
func TestMmap(t *testing.T) { |
| 17 |
mmapProt := unix.PROT_NONE |
| 18 |
mprotectProt := unix.PROT_READ | unix.PROT_WRITE |
| 19 |
// On NetBSD PAX mprotect prohibits setting protection bits |
| 20 |
// missing from the original mmap call unless explicitly |
| 21 |
// requested with PROT_MPROTECT. |
| 22 |
if runtime.GOOS == "netbsd" { |
| 23 |
// PROT_MPROTECT(x) is defined as ((x) << 3): |
| 24 |
// https://github.com/NetBSD/src/blob/aba449a55bf91b44bc68f542edd9afa341962b89/sys/sys/mman.h#L73 |
| 25 |
mmapProt = mprotectProt << 3 |
| 26 |
} |
| 27 |
b, err := unix.Mmap(-1, 0, unix.Getpagesize(), mmapProt, unix.MAP_ANON|unix.MAP_PRIVATE) |
| 28 |
if err != nil { |
| 29 |
t.Fatalf("Mmap: %v", err) |
| 30 |
} |
| 31 |
if err := unix.Mprotect(b, mprotectProt); err != nil { |
| 32 |
t.Fatalf("Mprotect: %v", err) |
| 33 |
} |
| 34 |
|
| 35 |
b[0] = 42 |
| 36 |
|
| 37 |
if runtime.GOOS == "aix" { |
| 38 |
t.Skip("msync returns invalid argument for AIX, skipping msync test") |
| 39 |
} else { |
| 40 |
if err := unix.Msync(b, unix.MS_SYNC); err != nil { |
| 41 |
t.Fatalf("Msync: %v", err) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if err := unix.Madvise(b, unix.MADV_DONTNEED); err != nil { |
| 46 |
t.Fatalf("Madvise: %v", err) |
| 47 |
} |
| 48 |
if err := unix.Munmap(b); err != nil { |
| 49 |
t.Fatalf("Munmap: %v", err) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestMmapPtr(t *testing.T) { |
| 54 |
p, err := unix.MmapPtr(-1, 0, nil, uintptr(2*unix.Getpagesize()), |
| 55 |
unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE) |
| 56 |
if err != nil { |
| 57 |
t.Fatalf("MmapPtr: %v", err) |
| 58 |
} |
| 59 |
|
| 60 |
if _, err := unix.MmapPtr(-1, 0, p, uintptr(unix.Getpagesize()), |
| 61 |
unix.PROT_READ|unix.PROT_WRITE, unix.MAP_ANON|unix.MAP_PRIVATE|unix.MAP_FIXED); err != nil { |
| 62 |
t.Fatalf("MmapPtr: %v", err) |
| 63 |
} |
| 64 |
|
| 65 |
*(*byte)(p) = 42 |
| 66 |
|
| 67 |
if err := unix.MunmapPtr(p, uintptr(2*unix.Getpagesize())); err != nil { |
| 68 |
t.Fatalf("MunmapPtr: %v", err) |
| 69 |
} |
| 70 |
} |