Skip to content

Commit a333539

Browse files
bsiegertawnumar
andauthored
memcall: netbsd support (#10)
There is no MADV_DONTDUMP or MADV_NOCORE. There is no MAP_ANONYMOUS but MAP_ANON. Co-authored-by: Awn <[email protected]>
1 parent 41ee6eb commit a333539

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

memcall_netbsd.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//go:build netbsd
2+
// +build netbsd
3+
4+
package memcall
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
10+
"golang.org/x/sys/unix"
11+
)
12+
13+
// Lock is a wrapper for mlock(2), with extra precautions.
14+
func Lock(b []byte) error {
15+
// Call mlock.
16+
if err := unix.Mlock(b); err != nil {
17+
return fmt.Errorf("<memcall> could not acquire lock on %p, limit reached? [Err: %s]", _getStartPtr(b), err)
18+
}
19+
20+
return nil
21+
}
22+
23+
// Unlock is a wrapper for munlock(2).
24+
func Unlock(b []byte) error {
25+
if err := unix.Munlock(b); err != nil {
26+
return fmt.Errorf("<memcall> could not free lock on %p [Err: %s]", _getStartPtr(b), err)
27+
}
28+
29+
return nil
30+
}
31+
32+
// Alloc allocates a byte slice of length n and returns it.
33+
func Alloc(n int) ([]byte, error) {
34+
// Allocate the memory.
35+
b, err := unix.Mmap(-1, 0, n, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_PRIVATE|unix.MAP_ANON)
36+
if err != nil {
37+
return nil, fmt.Errorf("<memcall> could not allocate [Err: %s]", err)
38+
}
39+
40+
// Wipe it just in case there is some remnant data.
41+
wipe(b)
42+
43+
// Return the allocated memory.
44+
return b, nil
45+
}
46+
47+
// Free deallocates the byte slice specified.
48+
func Free(b []byte) error {
49+
// Make the memory region readable and writable.
50+
if err := Protect(b, ReadWrite()); err != nil {
51+
return err
52+
}
53+
54+
// Wipe the memory region in case of remnant data.
55+
wipe(b)
56+
57+
// Free the memory back to the kernel.
58+
if err := unix.Munmap(b); err != nil {
59+
return fmt.Errorf("<memcall> could not deallocate %p [Err: %s]", _getStartPtr(b), err)
60+
}
61+
62+
return nil
63+
}
64+
65+
// Protect modifies the protection state for a specified byte slice.
66+
func Protect(b []byte, mpf MemoryProtectionFlag) error {
67+
var prot int
68+
if mpf.flag == ReadWrite().flag {
69+
prot = unix.PROT_READ | unix.PROT_WRITE
70+
} else if mpf.flag == ReadOnly().flag {
71+
prot = unix.PROT_READ
72+
} else if mpf.flag == NoAccess().flag {
73+
prot = unix.PROT_NONE
74+
} else {
75+
return errors.New(ErrInvalidFlag)
76+
}
77+
78+
// Change the protection value of the byte slice.
79+
if err := unix.Mprotect(b, prot); err != nil {
80+
return fmt.Errorf("<memcall> could not set %d on %p [Err: %s]", prot, _getStartPtr(b), err)
81+
}
82+
83+
return nil
84+
}
85+
86+
// DisableCoreDumps disables core dumps on Unix systems.
87+
func DisableCoreDumps() error {
88+
// Disable core dumps.
89+
if err := unix.Setrlimit(unix.RLIMIT_CORE, &unix.Rlimit{Cur: 0, Max: 0}); err != nil {
90+
return fmt.Errorf("<memcall> could not set rlimit [Err: %s]", err)
91+
}
92+
93+
return nil
94+
}

memcall_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !windows && !darwin && !openbsd && !freebsd && !aix
1+
//go:build !windows && !darwin && !openbsd && !freebsd && !aix && !netbsd
22

33
package memcall
44

0 commit comments

Comments
 (0)