Skip to content

Commit a8b3fa1

Browse files
dkegel-fastlydeadprogram
authored andcommitted
Implement getpagesize and munmap. For go 1.18.
1 parent 352d017 commit a8b3fa1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/syscall/mmap_unix_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 darwin || linux
6+
// +build darwin linux
7+
8+
package syscall_test
9+
10+
import (
11+
"syscall"
12+
"testing"
13+
)
14+
15+
func TestMmap(t *testing.T) {
16+
b, err := syscall.Mmap(-1, 0, syscall.Getpagesize(), syscall.PROT_NONE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
17+
if err != nil {
18+
t.Fatalf("Mmap: %v", err)
19+
}
20+
if err := syscall.Munmap(b); err != nil {
21+
t.Fatalf("Munmap: %v", err)
22+
}
23+
}

src/syscall/syscall_libc.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, e
226226
return (*[1 << 30]byte)(addr)[:length:length], nil
227227
}
228228

229+
func Munmap(b []byte) (err error) {
230+
errCode := libc_munmap(unsafe.Pointer(&b[0]), uintptr(len(b)))
231+
if errCode != 0 {
232+
err = getErrno()
233+
}
234+
return err
235+
}
236+
229237
func Mprotect(b []byte, prot int) (err error) {
230238
errCode := libc_mprotect(unsafe.Pointer(&b[0]), uintptr(len(b)), int32(prot))
231239
if errCode != 0 {
@@ -234,6 +242,10 @@ func Mprotect(b []byte, prot int) (err error) {
234242
return
235243
}
236244

245+
func Getpagesize() int {
246+
return int(libc_getpagesize())
247+
}
248+
237249
func Environ() []string {
238250

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

358+
// int munmap(void *addr, size_t length);
359+
//export munmap
360+
func libc_munmap(addr unsafe.Pointer, length uintptr) int32
361+
346362
// int mprotect(void *addr, size_t len, int prot);
347363
//export mprotect
348364
func libc_mprotect(addr unsafe.Pointer, len uintptr, prot int32) int32
349365

366+
// int getpagesize();
367+
//export getpagesize
368+
func libc_getpagesize() int32
369+
350370
// int chdir(const char *pathname, mode_t mode);
351371
//export chdir
352372
func libc_chdir(pathname *byte) int32

0 commit comments

Comments
 (0)