Debian Codegraph @

asm/instruction_test.go

raw symbols SCIP
1 package asm
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "encoding/hex"
7 "fmt"
8 "io"
9 "math"
10 "testing"
11
12 "github.com/go-quicktest/qt"
13
14 "github.com/cilium/ebpf/internal/platform"
15 )
16
17 var test64bitImmProg = []byte{
18 // r0 = math.MinInt32 - 1
19 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x7f,
20 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
21 }
22
23 func TestRead64bitImmediate(t *testing.T) {
24 var ins Instruction
25 err := ins.Unmarshal(bytes.NewReader(test64bitImmProg), binary.LittleEndian, platform.Linux)
26 if err != nil {
27 t.Fatal(err)
28 }
29
30 if c := ins.Constant; c != math.MinInt32-1 {
31 t.Errorf("Expected immediate to be %v, got %v", int64(math.MinInt32)-1, c)
32 }
33 }
34
35 func BenchmarkRead64bitImmediate(b *testing.B) {
36 r := &bytes.Reader{}
37 for b.Loop() {
38 r.Reset(test64bitImmProg)
39
40 var ins Instruction
41 if err := ins.Unmarshal(r, binary.LittleEndian, platform.Linux); err != nil {
42 b.Fatal(err)
43 }
44 }
45 }
46
47 func TestWrite64bitImmediate(t *testing.T) {
48 insns := Instructions{
49 LoadImm(R0, math.MinInt32-1, DWord),
50 }
51
52 var buf bytes.Buffer
53 if err := insns.Marshal(&buf, binary.LittleEndian); err != nil {
54 t.Fatal(err)
55 }
56
57 if prog := buf.Bytes(); !bytes.Equal(prog, test64bitImmProg) {
58 t.Errorf("Marshalled program does not match:\n%s", hex.Dump(prog))
59 }
60 }
61
62 func BenchmarkWrite64BitImmediate(b *testing.B) {
63 ins := LoadImm(R0, math.MinInt32-1, DWord)
64
65 var buf bytes.Buffer
66 for b.Loop() {
67 buf.Reset()
68
69 if _, err := ins.Marshal(&buf, binary.LittleEndian); err != nil {
70 b.Fatal(err)
71 }
72 }
73 }
74
75 func TestAppendInstructions(t *testing.T) {
76 r := bytes.NewReader(test64bitImmProg)
77
78 insns, err := AppendInstructions(nil, r, binary.LittleEndian, platform.Linux)
79 qt.Assert(t, qt.IsNil(err))
80
81 if len(insns) != 1 {
82 t.Fatalf("Expected one instruction, got %d", len(insns))
83 }
84 }
85
86 func TestSignedJump(t *testing.T) {
87 insns := Instructions{
88 JSGT.Imm(R0, -1, "foo"),
89 }
90
91 insns[0].Offset = 1
92
93 err := insns.Marshal(io.Discard, binary.LittleEndian)
94 if err != nil {
95 t.Error("Can't marshal signed jump:", err)
96 }
97 }
98
99 func TestInstructionLoadMapValue(t *testing.T) {
100 ins := LoadMapValue(R0, 1, 123)
101 if !ins.IsLoadFromMap() {
102 t.Error("isLoadFromMap returns false")
103 }
104 if fd := ins.mapFd(); fd != 1 {
105 t.Error("Expected map fd to be 1, got", fd)
106 }
107 if off := ins.mapOffset(); off != 123 {
108 t.Fatal("Expected map offset to be 123 after changing the pointer, got", off)
109 }
110 }
111
112 func TestInstructionWithMetadata(t *testing.T) {
113 ins := LoadImm(R0, 123, DWord).WithSymbol("abc")
114 ins2 := LoadImm(R0, 567, DWord).WithMetadata(ins.Metadata)
115
116 if want, got := "abc", ins2.Symbol(); want != got {
117 t.Fatalf("unexpected Symbol value on ins2: want: %s, got: %s", want, got)
118 }
119
120 if want, got := ins.Metadata, ins2.Metadata; want != got {
121 t.Fatal("expected ins and isn2 Metadata to match")
122 }
123 }
124
125 func TestReadCallToNegativeOne(t *testing.T) {
126 raw := []byte{
127 0x85, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
128 }
129 var ins Instruction
130 err := ins.Unmarshal(bytes.NewReader(raw), binary.LittleEndian, platform.Linux)
131 qt.Assert(t, qt.IsNil(err))
132 qt.Assert(t, qt.Equals(ins.Constant, -1))
133 }
134
135 // You can use format flags to change the way an eBPF
136 // program is stringified.
137 func ExampleInstructions_Format() {
138
139 insns := Instructions{
140 FnMapLookupElem.Call().WithSymbol("my_func").WithSource(Comment("bpf_map_lookup_elem()")),
141 LoadImm(R0, 42, DWord).WithSource(Comment("abc = 42")),
142 Return(),
143 }
144
145 fmt.Println("Default format:")
146 fmt.Printf("%v\n", insns)
147
148 fmt.Println("Don't indent instructions:")
149 fmt.Printf("%.0v\n", insns)
150
151 fmt.Println("Indent using spaces:")
152 fmt.Printf("% v\n", insns)
153
154 fmt.Println("Control symbol indentation:")
155 fmt.Printf("%2v\n", insns)
156
157 // Output: Default format:
158 // my_func:
159 // ; bpf_map_lookup_elem()
160 // 0: Call FnMapLookupElem
161 // ; abc = 42
162 // 1: LdImmDW dst: r0 imm: 42
163 // 3: Exit
164 //
165 // Don't indent instructions:
166 // my_func:
167 // ; bpf_map_lookup_elem()
168 // 0: Call FnMapLookupElem
169 // ; abc = 42
170 // 1: LdImmDW dst: r0 imm: 42
171 // 3: Exit
172 //
173 // Indent using spaces:
174 // my_func:
175 // ; bpf_map_lookup_elem()
176 // 0: Call FnMapLookupElem
177 // ; abc = 42
178 // 1: LdImmDW dst: r0 imm: 42
179 // 3: Exit
180 //
181 // Control symbol indentation:
182 // my_func:
183 // ; bpf_map_lookup_elem()
184 // 0: Call FnMapLookupElem
185 // ; abc = 42
186 // 1: LdImmDW dst: r0 imm: 42
187 // 3: Exit
188 }
189
190 func TestReadSrcDst(t *testing.T) {
191 testSrcDstProg := []byte{
192 // on little-endian: r0 = r1
193 // on big-endian: be: r1 = r0
194 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
195 }
196
197 testcases := []struct {
198 bo binary.ByteOrder
199 dst, src Register
200 }{
201 {binary.BigEndian, R1, R0},
202 {binary.LittleEndian, R0, R1},
203 }
204
205 for _, tc := range testcases {
206 t.Run(tc.bo.String(), func(t *testing.T) {
207 var ins Instruction
208 err := ins.Unmarshal(bytes.NewReader(testSrcDstProg), tc.bo, platform.Linux)
209 if err != nil {
210 t.Fatal(err)
211 }
212 if ins.Dst != tc.dst {
213 t.Errorf("Expected destination to be %v, got %v", tc.dst, ins.Dst)
214 }
215 if ins.Src != tc.src {
216 t.Errorf("Expected source to be %v, got %v", tc.src, ins.Src)
217 }
218 })
219 }
220 }
221
222 func TestInstructionIterator(t *testing.T) {
223 insns := Instructions{
224 LoadImm(R0, 0, Word),
225 LoadImm(R0, 0, DWord),
226 Return(),
227 }
228 offsets := []RawInstructionOffset{0, 1, 3}
229
230 iter := insns.Iterate()
231 for i := range insns {
232 if !iter.Next() {
233 t.Fatalf("Expected %dth call to Next to return true", i)
234 }
235
236 if iter.Ins == nil {
237 t.Errorf("Expected iter.Ins to be non-nil")
238 }
239 if iter.Index != i {
240 t.Errorf("Expected iter.Index to be %d, got %d", i, iter.Index)
241 }
242 if iter.Offset != offsets[i] {
243 t.Errorf("Expected iter.Offset to be %d, got %d", offsets[i], iter.Offset)
244 }
245 }
246 }
247
248 func TestMetadataCopyOnWrite(t *testing.T) {
249 // Setting metadata should copy Instruction and modify the metadata pointer
250 // of the new object without touching the old Instruction.
251
252 // Reference
253 ins := Ja.Label("my_func")
254 ins2 := ins.WithReference("my_func2")
255
256 qt.Assert(t, qt.Equals(ins.Reference(), "my_func"), qt.Commentf("WithReference updated ins"))
257 qt.Assert(t, qt.Equals(ins2.Reference(), "my_func2"), qt.Commentf("WithReference didn't update ins2"))
258
259 // Symbol
260 ins = Ja.Label("").WithSymbol("my_sym")
261 ins2 = ins.WithSymbol("my_sym2")
262
263 qt.Assert(t, qt.Equals(ins.Symbol(), "my_sym"), qt.Commentf("WithSymbol updated ins"))
264 qt.Assert(t, qt.Equals(ins2.Symbol(), "my_sym2"), qt.Commentf("WithSymbol didn't update ins2"))
265
266 // Map
267 ins = LoadMapPtr(R1, 0)
268 ins2 = ins
269
270 testMap := testFDer(1)
271 qt.Assert(t, qt.IsNil(ins2.AssociateMap(testMap)), qt.Commentf("failed to associate map with ins2"))
272
273 qt.Assert(t, qt.IsNil(ins.Map()), qt.Commentf("AssociateMap updated ins"))
274 qt.Assert(t, qt.Equals[FDer](ins2.Map(), testMap), qt.Commentf("AssociateMap didn't update ins2"))
275 }
276
277 type testFDer int
278
279 func (t testFDer) FD() int {
280 return int(t)
281 }
282
283 func TestAtomics(t *testing.T) {
284 rawInsns := []byte{
285 0xc3, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // lock *(u32 *)(r1 + 0x1) += w2
286 0xc3, 0x21, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, // lock *(u32 *)(r1 + 0x1) &= w2
287 0xc3, 0x21, 0x01, 0x00, 0xa0, 0x00, 0x00, 0x00, // lock *(u32 *)(r1 + 0x1) ^= w2
288 0xc3, 0x21, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, // lock *(u32 *)(r1 + 0x1) |= w2
289
290 0xdb, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // lock *(u64 *)(r1 + 0x1) += r2
291 0xdb, 0x21, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, // lock *(u64 *)(r1 + 0x1) &= r2
292 0xdb, 0x21, 0x01, 0x00, 0xa0, 0x00, 0x00, 0x00, // lock *(u64 *)(r1 + 0x1) ^= r2
293 0xdb, 0x21, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, // lock *(u64 *)(r1 + 0x1) |= r2
294
295 0xc3, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // w0 = atomic_fetch_add((u32 *)(r1 + 0x0), w0)
296 0xc3, 0x01, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00, // w0 = atomic_fetch_and((u32 *)(r1 + 0x0), w0)
297 0xc3, 0x01, 0x00, 0x00, 0xa1, 0x00, 0x00, 0x00, // w0 = atomic_fetch_xor((u32 *)(r1 + 0x0), w0)
298 0xc3, 0x01, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, // w0 = atomic_fetch_or((u32 *)(r1 + 0x0), w0)
299
300 0xdb, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // r0 = atomic_fetch_add((u64 *)(r1 + 0x0), r0)
301 0xdb, 0x01, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00, // r0 = atomic_fetch_and((u64 *)(r1 + 0x0), r0)
302 0xdb, 0x01, 0x00, 0x00, 0xa1, 0x00, 0x00, 0x00, // r0 = atomic_fetch_xor((u64 *)(r1 + 0x0), r0)
303 0xdb, 0x01, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, // r0 = atomic_fetch_or((u64 *)(r1 + 0x0), r0)
304
305 0xc3, 0x01, 0x00, 0x00, 0xe1, 0x00, 0x00, 0x00, // w0 = xchg32_32(r1 + 0x0, w0)
306 0xdb, 0x01, 0x00, 0x00, 0xe1, 0x00, 0x00, 0x00, // r0 = xchg_64(r1 + 0x0, r0)
307
308 0xc3, 0x11, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00, // w0 = cmpxchg32_32(r1 + 0x0, w0, w1)
309 0xdb, 0x11, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00, // r0 = cmpxchg_64(r1 + 0x0, r0, r1)
310 }
311
312 insns, err := AppendInstructions(nil, bytes.NewReader(rawInsns), binary.LittleEndian, platform.Linux)
313 if err != nil {
314 t.Fatal(err)
315 }
316
317 lines := []string{
318 "StXAtomicAddW dst: r1 src: r2 off: 1",
319 "StXAtomicAndW dst: r1 src: r2 off: 1",
320 "StXAtomicXorW dst: r1 src: r2 off: 1",
321 "StXAtomicOrW dst: r1 src: r2 off: 1",
322 "StXAtomicAddDW dst: r1 src: r2 off: 1",
323 "StXAtomicAndDW dst: r1 src: r2 off: 1",
324 "StXAtomicXorDW dst: r1 src: r2 off: 1",
325 "StXAtomicOrDW dst: r1 src: r2 off: 1",
326 "StXAtomicFetchAddW dst: r1 src: r0 off: 0",
327 "StXAtomicFetchAndW dst: r1 src: r0 off: 0",
328 "StXAtomicFetchXorW dst: r1 src: r0 off: 0",
329 "StXAtomicFetchOrW dst: r1 src: r0 off: 0",
330 "StXAtomicFetchAddDW dst: r1 src: r0 off: 0",
331 "StXAtomicFetchAndDW dst: r1 src: r0 off: 0",
332 "StXAtomicFetchXorDW dst: r1 src: r0 off: 0",
333 "StXAtomicFetchOrDW dst: r1 src: r0 off: 0",
334 "StXAtomicXchgW dst: r1 src: r0 off: 0",
335 "StXAtomicXchgDW dst: r1 src: r0 off: 0",
336 "StXAtomicCmpXchgW dst: r1 src: r1 off: 0",
337 "StXAtomicCmpXchgDW dst: r1 src: r1 off: 0",
338 }
339
340 for i, ins := range insns {
341 if want, got := lines[i], fmt.Sprint(ins); want != got {
342 t.Errorf("Expected %q, got %q", want, got)
343 }
344 }
345
346 // Marshal and unmarshal again to make sure the instructions are
347 // still valid.
348 var buf bytes.Buffer
349 err = insns.Marshal(&buf, binary.LittleEndian)
350 if err != nil {
351 t.Fatal(err)
352 }
353
354 if !bytes.Equal(buf.Bytes(), rawInsns) {
355 t.Error("Expected instructions to be equal after marshalling")
356 }
357 }
358
359 func TestISAv4(t *testing.T) {
360 rawInsns := []byte{
361 0xd7, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // r1 = bswap16 r1
362 0xd7, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, // r2 = bswap32 r2
363 0xd7, 0x03, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, // r3 = bswap64 r3
364
365 0x91, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r1 = *(s8 *)(r4 + 0x0)
366 0x89, 0x52, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // r2 = *(s16 *)(r5 + 0x4)
367 0x81, 0x63, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // r3 = *(s32 *)(r6 + 0x8)
368
369 0x91, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r1 = *(s8 *)(r4 + 0x0)
370 0x89, 0x52, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // r2 = *(s16 *)(r5 + 0x4)
371
372 0xbf, 0x41, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // r1 = (s8)r4
373 0xbf, 0x52, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // r2 = (s16)r5
374 0xbf, 0x63, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, // r3 = (s32)r6
375
376 0xbc, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // w1 = (s8)w3
377 0xbc, 0x42, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // w2 = (s16)w4
378
379 0x06, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, // gotol +3
380
381 0x3f, 0x31, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // r1 s/= r3
382 0x9f, 0x42, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // r2 s%= r4
383
384 0x3c, 0x31, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // w1 s/= w3
385 0x9c, 0x42, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // w2 s%= w4
386
387 0xd3, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, // w0 = load_acquire((u8 *)(r1 + 0x0))
388 0xcb, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, // w0 = load_acquire((u16 *)(r1 + 0x0))
389 0xc3, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, // w0 = load_acquire((u32 *)(r1 + 0x0))
390 0xdb, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, // r0 = load_acquire((u64 *)(r1 + 0x0))
391
392 0xd3, 0x21, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, // store_release((u8 *)(r1 + 0x0), w2)
393 0xcb, 0x21, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, // store_release((u16 *)(r1 + 0x0), w2)
394 0xc3, 0x21, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, // store_release((u32 *)(r1 + 0x0), w2)
395 0xdb, 0x21, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, // store_release((u64 *)(r1 + 0x0), r2)
396
397 0xe5, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, // may_goto +30
398 }
399
400 insns, err := AppendInstructions(nil, bytes.NewReader(rawInsns), binary.LittleEndian, platform.Linux)
401 if err != nil {
402 t.Fatal(err)
403 }
404
405 lines := []string{
406 "BSwap16 dst: r1 ",
407 "BSwap32 dst: r2 ",
408 "BSwap64 dst: r3 ",
409 "LdXMemSXB dst: r1 src: r4 off: 0 imm: 0",
410 "LdXMemSXH dst: r2 src: r5 off: 4 imm: 0",
411 "LdXMemSXW dst: r3 src: r6 off: 8 imm: 0",
412 "LdXMemSXB dst: r1 src: r4 off: 0 imm: 0",
413 "LdXMemSXH dst: r2 src: r5 off: 4 imm: 0",
414 "MovSX8Reg dst: r1 src: r4",
415 "MovSX16Reg dst: r2 src: r5",
416 "MovSX32Reg dst: r3 src: r6",
417 "MovSX8Reg32 dst: r1 src: r3",
418 "MovSX16Reg32 dst: r2 src: r4",
419 "Ja32 imm: 3",
420 "SDivReg dst: r1 src: r3",
421 "SModReg dst: r2 src: r4",
422 "SDivReg32 dst: r1 src: r3",
423 "SModReg32 dst: r2 src: r4",
424 "StXAtomicLdAcqB dst: r0 src: r1 off: 0",
425 "StXAtomicLdAcqH dst: r0 src: r1 off: 0",
426 "StXAtomicLdAcqW dst: r0 src: r1 off: 0",
427 "StXAtomicLdAcqDW dst: r0 src: r1 off: 0",
428 "StXAtomicStRelB dst: r1 src: r2 off: 0",
429 "StXAtomicStRelH dst: r1 src: r2 off: 0",
430 "StXAtomicStRelW dst: r1 src: r2 off: 0",
431 "StXAtomicStRelDW dst: r1 src: r2 off: 0",
432 "JCond may_goto off: 30",
433 }
434
435 for i, ins := range insns {
436 if want, got := lines[i], fmt.Sprint(ins); want != got {
437 t.Errorf("Expected %q, got %q", want, got)
438 }
439 }
440
441 // Marshal and unmarshal again to make sure the instructions are
442 // still valid.
443 var buf bytes.Buffer
444 err = insns.Marshal(&buf, binary.LittleEndian)
445 if err != nil {
446 t.Fatal(err)
447 }
448
449 if !bytes.Equal(buf.Bytes(), rawInsns) {
450 t.Error("Expected instructions to be equal after marshalling")
451 }
452 }
453
454 func TestLongJumpPatching(t *testing.T) {
455 insns := Instructions{
456 LongJump("exit"),
457 Xor.Reg(R0, R0),
458 Xor.Reg(R0, R0),
459 Xor.Reg(R0, R0),
460 Return().WithSymbol("exit"),
461 }
462
463 err := insns.encodeFunctionReferences()
464 if err != nil {
465 t.Fatal(err)
466 }
467
468 if insns[0].Constant != 3 {
469 t.Errorf("Expected offset to be 3, got %d", insns[1].Constant)
470 }
471 }