github.com/lzhfromustc/gofuzz@v0.0.0-20211116160056-151b3108bbd1/runtime/mem_bsd.go (about)

     1  // Copyright 2010 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  // +build dragonfly freebsd netbsd openbsd solaris
     6  
     7  package runtime
     8  
     9  import (
    10  	"unsafe"
    11  )
    12  
    13  // Don't split the stack as this function may be invoked without a valid G,
    14  // which prevents us from allocating more stack.
    15  //go:nosplit
    16  func sysAlloc(n uintptr, sysStat *sysMemStat) unsafe.Pointer {
    17  	v, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
    18  	if err != 0 {
    19  		return nil
    20  	}
    21  	sysStat.add(int64(n))
    22  	return v
    23  }
    24  
    25  func sysUnused(v unsafe.Pointer, n uintptr) {
    26  	madvise(v, n, _MADV_FREE)
    27  }
    28  
    29  func sysUsed(v unsafe.Pointer, n uintptr) {
    30  }
    31  
    32  func sysHugePage(v unsafe.Pointer, n uintptr) {
    33  }
    34  
    35  // Don't split the stack as this function may be invoked without a valid G,
    36  // which prevents us from allocating more stack.
    37  //go:nosplit
    38  func sysFree(v unsafe.Pointer, n uintptr, sysStat *sysMemStat) {
    39  	sysStat.add(-int64(n))
    40  	munmap(v, n)
    41  }
    42  
    43  func sysFault(v unsafe.Pointer, n uintptr) {
    44  	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
    45  }
    46  
    47  // Indicates not to reserve swap space for the mapping.
    48  const _sunosMAP_NORESERVE = 0x40
    49  
    50  func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
    51  	flags := int32(_MAP_ANON | _MAP_PRIVATE)
    52  	if GOOS == "solaris" || GOOS == "illumos" {
    53  		// Be explicit that we don't want to reserve swap space
    54  		// for PROT_NONE anonymous mappings. This avoids an issue
    55  		// wherein large mappings can cause fork to fail.
    56  		flags |= _sunosMAP_NORESERVE
    57  	}
    58  	p, err := mmap(v, n, _PROT_NONE, flags, -1, 0)
    59  	if err != 0 {
    60  		return nil
    61  	}
    62  	return p
    63  }
    64  
    65  const _sunosEAGAIN = 11
    66  const _ENOMEM = 12
    67  
    68  func sysMap(v unsafe.Pointer, n uintptr, sysStat *sysMemStat) {
    69  	sysStat.add(int64(n))
    70  
    71  	p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
    72  	if err == _ENOMEM || ((GOOS == "solaris" || GOOS == "illumos") && err == _sunosEAGAIN) {
    73  		throw("runtime: out of memory")
    74  	}
    75  	if p != v || err != 0 {
    76  		throw("runtime: cannot map pages in arena address space")
    77  	}
    78  }