Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/syscall/mmap_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.

//go:build darwin || linux
// +build darwin linux

package syscall_test

import (
"syscall"
"testing"
)

func TestMmap(t *testing.T) {
b, err := syscall.Mmap(-1, 0, syscall.Getpagesize(), syscall.PROT_NONE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
if err != nil {
t.Fatalf("Mmap: %v", err)
}
if err := syscall.Munmap(b); err != nil {
t.Fatalf("Munmap: %v", err)
}
}
20 changes: 20 additions & 0 deletions src/syscall/syscall_libc.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, e
return (*[1 << 30]byte)(addr)[:length:length], nil
}

func Munmap(b []byte) (err error) {
errCode := libc_munmap(unsafe.Pointer(&b[0]), uintptr(len(b)))
if errCode != 0 {
err = getErrno()
}
return err
}

func Mprotect(b []byte, prot int) (err error) {
errCode := libc_mprotect(unsafe.Pointer(&b[0]), uintptr(len(b)), int32(prot))
if errCode != 0 {
Expand All @@ -234,6 +242,10 @@ func Mprotect(b []byte, prot int) (err error) {
return
}

func Getpagesize() int {
return int(libc_getpagesize())
}

func Environ() []string {

// This function combines all the environment into a single allocation.
Expand Down Expand Up @@ -343,10 +355,18 @@ func libc_dup(fd int32) int32
//export mmap
func libc_mmap(addr unsafe.Pointer, length uintptr, prot, flags, fd int32, offset uintptr) unsafe.Pointer

// int munmap(void *addr, size_t length);
//export munmap
func libc_munmap(addr unsafe.Pointer, length uintptr) int32

// int mprotect(void *addr, size_t len, int prot);
//export mprotect
func libc_mprotect(addr unsafe.Pointer, len uintptr, prot int32) int32

// int getpagesize();
//export getpagesize
func libc_getpagesize() int32

// int chdir(const char *pathname, mode_t mode);
//export chdir
func libc_chdir(pathname *byte) int32
Expand Down