Skip to content

Commit 2b73e53

Browse files
authored
Fix no support for MADV_DONTDUMP on solaris/illumos (#13)
1 parent 649d45b commit 2b73e53

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

memcall_solaris.go

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

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 && !netbsd
1+
//go:build !windows && !darwin && !openbsd && !freebsd && !aix && !netbsd && !solaris
22

33
package memcall
44

0 commit comments

Comments
 (0)