File: memcall.go

package info (click to toggle)
golang-github-awnumar-memcall 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,498 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package memcall

import (
	"reflect"
	"runtime"
	"unsafe"
)

// MemoryProtectionFlag specifies some particular memory protection flag.
type MemoryProtectionFlag struct {
	// NOACCESS  := 1 (0001)
	// READ      := 2 (0010)
	// WRITE     := 4 (0100) // unused
	// READWRITE := 6 (0110)

	flag byte
}

// NoAccess specifies that the memory should be marked unreadable and immutable.
func NoAccess() MemoryProtectionFlag {
	return MemoryProtectionFlag{1}
}

// ReadOnly specifies that the memory should be marked read-only (immutable).
func ReadOnly() MemoryProtectionFlag {
	return MemoryProtectionFlag{2}
}

// ReadWrite specifies that the memory should be made readable and writable.
func ReadWrite() MemoryProtectionFlag {
	return MemoryProtectionFlag{6}
}

// ErrInvalidFlag indicates that a given memory protection flag is undefined.
const ErrInvalidFlag = "<memcall> memory protection flag is undefined"

// Wipes a given byte slice.
func wipe(buf []byte) {
	for i := range buf {
		buf[i] = 0
	}
	runtime.KeepAlive(buf)
}

// Placeholder variable for when we need a valid pointer to zero bytes.
var _zero uintptr

// Auxiliary functions.
func _getStartPtr(b []byte) unsafe.Pointer {
	if len(b) > 0 {
		return unsafe.Pointer(&b[0])
	}
	return unsafe.Pointer(&_zero)
}

func _getPtr(b []byte) uintptr {
	return uintptr(_getStartPtr(b))
}

func _getBytes(ptr uintptr, len int, cap int) []byte {
	var sl = reflect.SliceHeader{Data: ptr, Len: len, Cap: cap}
	return *(*[]byte)(unsafe.Pointer(&sl))
}