forked from cilium/ebpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction_test.go
328 lines (269 loc) · 7.76 KB
/
instruction_test.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package asm
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"math"
"testing"
qt "github.com/frankban/quicktest"
)
var test64bitImmProg = []byte{
// r0 = math.MinInt32 - 1
0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x7f,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
}
func TestRead64bitImmediate(t *testing.T) {
var ins Instruction
n, err := ins.Unmarshal(bytes.NewReader(test64bitImmProg), binary.LittleEndian)
if err != nil {
t.Fatal(err)
}
if want := uint64(InstructionSize * 2); n != want {
t.Errorf("Expected %d bytes to be read, got %d", want, n)
}
if c := ins.Constant; c != math.MinInt32-1 {
t.Errorf("Expected immediate to be %v, got %v", int64(math.MinInt32)-1, c)
}
}
func BenchmarkRead64bitImmediate(b *testing.B) {
r := &bytes.Reader{}
for i := 0; i < b.N; i++ {
r.Reset(test64bitImmProg)
var ins Instruction
if _, err := ins.Unmarshal(r, binary.LittleEndian); err != nil {
b.Fatal(err)
}
}
}
func TestWrite64bitImmediate(t *testing.T) {
insns := Instructions{
LoadImm(R0, math.MinInt32-1, DWord),
}
var buf bytes.Buffer
if err := insns.Marshal(&buf, binary.LittleEndian); err != nil {
t.Fatal(err)
}
if prog := buf.Bytes(); !bytes.Equal(prog, test64bitImmProg) {
t.Errorf("Marshalled program does not match:\n%s", hex.Dump(prog))
}
}
func BenchmarkWrite64BitImmediate(b *testing.B) {
ins := LoadImm(R0, math.MinInt32-1, DWord)
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
buf.Reset()
if _, err := ins.Marshal(&buf, binary.LittleEndian); err != nil {
b.Fatal(err)
}
}
}
func TestUnmarshalInstructions(t *testing.T) {
r := bytes.NewReader(test64bitImmProg)
var insns Instructions
if err := insns.Unmarshal(r, binary.LittleEndian); err != nil {
t.Fatal(err)
}
// Unmarshaling into the same Instructions multiple times replaces
// the instruction stream.
r.Reset(test64bitImmProg)
if err := insns.Unmarshal(r, binary.LittleEndian); err != nil {
t.Fatal(err)
}
if len(insns) != 1 {
t.Fatalf("Expected one instruction, got %d", len(insns))
}
}
func TestSignedJump(t *testing.T) {
insns := Instructions{
JSGT.Imm(R0, -1, "foo"),
}
insns[0].Offset = 1
err := insns.Marshal(io.Discard, binary.LittleEndian)
if err != nil {
t.Error("Can't marshal signed jump:", err)
}
}
func TestInstructionRewriteMapConstant(t *testing.T) {
ins := LoadMapValue(R0, 123, 321)
qt.Assert(t, ins.MapPtr(), qt.Equals, 123)
qt.Assert(t, ins.mapOffset(), qt.Equals, uint32(321))
qt.Assert(t, ins.RewriteMapPtr(-1), qt.IsNil)
qt.Assert(t, ins.MapPtr(), qt.Equals, -1)
qt.Assert(t, ins.RewriteMapPtr(1), qt.IsNil)
qt.Assert(t, ins.MapPtr(), qt.Equals, 1)
// mapOffset should be unchanged after rewriting the pointer.
qt.Assert(t, ins.mapOffset(), qt.Equals, uint32(321))
qt.Assert(t, ins.RewriteMapOffset(123), qt.IsNil)
qt.Assert(t, ins.mapOffset(), qt.Equals, uint32(123))
// MapPtr should be unchanged.
qt.Assert(t, ins.MapPtr(), qt.Equals, 1)
ins = Mov.Imm(R1, 32)
if err := ins.RewriteMapPtr(1); err == nil {
t.Error("RewriteMapPtr rewriting bogus instruction")
}
if err := ins.RewriteMapOffset(1); err == nil {
t.Error("RewriteMapOffset rewriting bogus instruction")
}
}
func TestInstructionLoadMapValue(t *testing.T) {
ins := LoadMapValue(R0, 1, 123)
if !ins.IsLoadFromMap() {
t.Error("isLoadFromMap returns false")
}
if fd := ins.mapFd(); fd != 1 {
t.Error("Expected map fd to be 1, got", fd)
}
if off := ins.mapOffset(); off != 123 {
t.Fatal("Expected map offset to be 123 after changin the pointer, got", off)
}
}
func TestInstructionsRewriteMapPtr(t *testing.T) {
insns := Instructions{
LoadMapPtr(R1, 0).WithReference("good"),
Return(),
}
if err := insns.RewriteMapPtr("good", 1); err != nil {
t.Fatal(err)
}
if insns[0].Constant != 1 {
t.Error("Constant should be 1, have", insns[0].Constant)
}
if err := insns.RewriteMapPtr("good", 2); err != nil {
t.Fatal(err)
}
if insns[0].Constant != 2 {
t.Error("Constant should be 2, have", insns[0].Constant)
}
if err := insns.RewriteMapPtr("bad", 1); !errors.Is(err, ErrUnreferencedSymbol) {
t.Error("Rewriting unreferenced map doesn't return appropriate error")
}
}
// You can use format flags to change the way an eBPF
// program is stringified.
func ExampleInstructions_Format() {
insns := Instructions{
FnMapLookupElem.Call().WithSymbol("my_func").WithSource(Comment("bpf_map_lookup_elem()")),
LoadImm(R0, 42, DWord).WithSource(Comment("abc = 42")),
Return(),
}
fmt.Println("Default format:")
fmt.Printf("%v\n", insns)
fmt.Println("Don't indent instructions:")
fmt.Printf("%.0v\n", insns)
fmt.Println("Indent using spaces:")
fmt.Printf("% v\n", insns)
fmt.Println("Control symbol indentation:")
fmt.Printf("%2v\n", insns)
// Output: Default format:
// my_func:
// ; bpf_map_lookup_elem()
// 0: Call FnMapLookupElem
// ; abc = 42
// 1: LdImmDW dst: r0 imm: 42
// 3: Exit
//
// Don't indent instructions:
// my_func:
// ; bpf_map_lookup_elem()
// 0: Call FnMapLookupElem
// ; abc = 42
// 1: LdImmDW dst: r0 imm: 42
// 3: Exit
//
// Indent using spaces:
// my_func:
// ; bpf_map_lookup_elem()
// 0: Call FnMapLookupElem
// ; abc = 42
// 1: LdImmDW dst: r0 imm: 42
// 3: Exit
//
// Control symbol indentation:
// my_func:
// ; bpf_map_lookup_elem()
// 0: Call FnMapLookupElem
// ; abc = 42
// 1: LdImmDW dst: r0 imm: 42
// 3: Exit
}
func TestReadSrcDst(t *testing.T) {
testSrcDstProg := []byte{
// on little-endian: r0 = r1
// on big-endian: be: r1 = r0
0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
testcases := []struct {
bo binary.ByteOrder
dst, src Register
}{
{binary.BigEndian, R1, R0},
{binary.LittleEndian, R0, R1},
}
for _, tc := range testcases {
t.Run(tc.bo.String(), func(t *testing.T) {
var ins Instruction
_, err := ins.Unmarshal(bytes.NewReader(testSrcDstProg), tc.bo)
if err != nil {
t.Fatal(err)
}
if ins.Dst != tc.dst {
t.Errorf("Expected destination to be %v, got %v", tc.dst, ins.Dst)
}
if ins.Src != tc.src {
t.Errorf("Expected source to be %v, got %v", tc.src, ins.Src)
}
})
}
}
func TestInstructionIterator(t *testing.T) {
insns := Instructions{
LoadImm(R0, 0, Word),
LoadImm(R0, 0, DWord),
Return(),
}
offsets := []RawInstructionOffset{0, 1, 3}
iter := insns.Iterate()
for i := 0; i < len(insns); i++ {
if !iter.Next() {
t.Fatalf("Expected %dth call to Next to return true", i)
}
if iter.Ins == nil {
t.Errorf("Expected iter.Ins to be non-nil")
}
if iter.Index != i {
t.Errorf("Expected iter.Index to be %d, got %d", i, iter.Index)
}
if iter.Offset != offsets[i] {
t.Errorf("Expected iter.Offset to be %d, got %d", offsets[i], iter.Offset)
}
}
}
func TestMetadataCopyOnWrite(t *testing.T) {
c := qt.New(t)
// Setting metadata should copy Instruction and modify the metadata pointer
// of the new object without touching the old Instruction.
// Reference
ins := Ja.Label("my_func")
ins2 := ins.WithReference("my_func2")
c.Assert(ins.Reference(), qt.Equals, "my_func", qt.Commentf("WithReference updated ins"))
c.Assert(ins2.Reference(), qt.Equals, "my_func2", qt.Commentf("WithReference didn't update ins2"))
// Symbol
ins = Ja.Label("").WithSymbol("my_sym")
ins2 = ins.WithSymbol("my_sym2")
c.Assert(ins.Symbol(), qt.Equals, "my_sym", qt.Commentf("WithSymbol updated ins"))
c.Assert(ins2.Symbol(), qt.Equals, "my_sym2", qt.Commentf("WithSymbol didn't update ins2"))
// Map
ins = LoadMapPtr(R1, 0)
ins2 = ins
testMap := testFDer(1)
c.Assert(ins2.AssociateMap(testMap), qt.IsNil, qt.Commentf("failed to associate map with ins2"))
c.Assert(ins.Map(), qt.IsNil, qt.Commentf("AssociateMap updated ins"))
c.Assert(ins2.Map(), qt.Equals, testMap, qt.Commentf("AssociateMap didn't update ins2"))
}
type testFDer int
func (t testFDer) FD() int {
return int(t)
}