-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory_plain.go
119 lines (100 loc) · 3.04 KB
/
memory_plain.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package mimic
import (
"encoding/binary"
"fmt"
"github.com/cilium/ebpf/asm"
)
var _ VMMem = (*PlainMemory)(nil)
// PlainMemory is the simplest implementation of VMMem possible, it is just a []byte with no additional information
// about its contents. The ByteOrder is used when Load'in or Store'ing scalar values. If ByteOrder is not set
// the native endianness will be used.
type PlainMemory struct {
Backing []byte
ByteOrder binary.ByteOrder
}
// TODO it is fairly common to reuse plain memories of the same size, the process stack for example. This would be a
// good candidate for a freelist/memory pool since they can be large. In the case of plain memory we would have to
// group them by size.
// Load loads a scalar value of the given `size` and `offset` from the memory.
func (pm *PlainMemory) Load(offset uint32, size asm.Size) (uint64, error) {
bytes := size.Sizeof()
if int(offset)+bytes > len(pm.Backing) {
return 0, fmt.Errorf(
"reading %d bytes at offset %d will read out of the memory bounds of %d bytes",
bytes,
offset,
len(pm.Backing),
)
}
if pm.ByteOrder == nil {
pm.ByteOrder = GetNativeEndianness()
}
switch size {
case asm.Byte:
return uint64(pm.Backing[offset]), nil
case asm.Half:
return uint64(pm.ByteOrder.Uint16(pm.Backing[offset : offset+2])), nil
case asm.Word:
return uint64(pm.ByteOrder.Uint32(pm.Backing[offset : offset+4])), nil
case asm.DWord:
return pm.ByteOrder.Uint64(pm.Backing[offset : offset+8]), nil
default:
return 0, fmt.Errorf("unknown size '%v'", size)
}
}
// Store stores a scalar value of the given `size` and `offset` from the memory.
func (pm *PlainMemory) Store(offset uint32, value uint64, size asm.Size) error {
bytes := size.Sizeof()
if int(offset)+bytes > len(pm.Backing) {
return fmt.Errorf(
"writing %d bytes at offset %d will overflow the memory of %d bytes",
bytes,
offset,
len(pm.Backing),
)
}
if pm.ByteOrder == nil {
pm.ByteOrder = GetNativeEndianness()
}
b := make([]byte, bytes)
switch size {
case asm.Byte:
b[0] = byte(value)
case asm.Half:
pm.ByteOrder.PutUint16(b, uint16(value))
case asm.Word:
pm.ByteOrder.PutUint32(b, uint32(value))
case asm.DWord:
pm.ByteOrder.PutUint64(b, value)
default:
return fmt.Errorf("unknown size '%v'", size)
}
copy(pm.Backing[offset:], b)
return nil
}
// Read reads a byte slice of arbitrary size, the length of 'b' is used to determine the requested size
func (pm *PlainMemory) Read(offset uint32, b []byte) error {
if int(offset)+len(b) > len(pm.Backing) {
return fmt.Errorf(
"reading %d bytes at offset %d will read out of the memory bounds of %d bytes",
len(b),
offset,
len(pm.Backing),
)
}
copy(b, pm.Backing[offset:])
return nil
}
// Write write a byte slice of arbitrary size to the memory
func (pm *PlainMemory) Write(offset uint32, b []byte) error {
if int(offset)+len(b) > len(pm.Backing) {
return fmt.Errorf(
"writing %d bytes at offset %d will overflow the memory of %d bytes",
len(b),
offset,
len(pm.Backing),
)
}
copy(pm.Backing[offset:], b)
return nil
}