|
| 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 | +} |
0 commit comments