Debian Codegraph @

insn_test.go

raw symbols SCIP
1 package cbpfc
2
3 import (
4 "bytes"
5 "flag"
6 "fmt"
7 "math"
8 "os"
9 "testing"
10
11 "github.com/cilium/ebpf"
12 "golang.org/x/net/bpf"
13
14 // syscall has a wonky RLIM_INFINITY, and no RLIMIT_MEMLOCK
15 "golang.org/x/sys/unix"
16 )
17
18 func TestMain(m *testing.M) {
19 // Needed for testing.Short
20 flag.Parse()
21
22 if !testing.Short() {
23 // Remove any locked memory limits so we can load BPF programs
24 err := unix.Setrlimit(unix.RLIMIT_MEMLOCK, &unix.Rlimit{
25 Cur: unix.RLIM_INFINITY,
26 Max: unix.RLIM_INFINITY,
27 })
28 if err != nil {
29 panic(err)
30 }
31 }
32
33 os.Exit(m.Run())
34 }
35
36 func TestZeroInitA(t *testing.T) {
37 t.Parallel()
38
39 filter := []bpf.Instruction{
40 bpf.RetA{},
41 }
42
43 checkBackends(t, filter, []byte{}, noMatch)
44 }
45
46 func TestZeroInitX(t *testing.T) {
47 t.Parallel()
48
49 filter := []bpf.Instruction{
50 bpf.TXA{},
51 bpf.RetA{},
52 }
53
54 checkBackends(t, filter, []byte{}, noMatch)
55 }
56
57 func TestPartialZeroInitX(t *testing.T) {
58 t.Parallel()
59
60 filter := []bpf.Instruction{
61 bpf.LoadAbsolute{Off: 0, Size: 1},
62 bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 1}, // jump to block 1 or 2
63
64 // block 1
65 bpf.TAX{}, // initialize RegX
66 // Fall through
67
68 // block 2
69 bpf.TXA{}, // RegX used potentially uninitialized
70 bpf.RetA{},
71 }
72
73 checkBackends(t, filter, []byte{0}, noMatch)
74 checkBackends(t, filter, []byte{3}, match)
75 }
76
77 func TestLoadConstantA(t *testing.T) {
78 t.Parallel()
79
80 filter := func(val uint32) []bpf.Instruction {
81 return []bpf.Instruction{
82 bpf.LoadConstant{Dst: bpf.RegA, Val: val},
83 bpf.JumpIf{Cond: bpf.JumpEqual, Val: val, SkipTrue: 1},
84 bpf.RetConstant{Val: 0},
85 bpf.RetConstant{Val: 1},
86 }
87 }
88
89 checkBackends(t, filter(1), []byte{}, match)
90 checkBackends(t, filter(28), []byte{}, match)
91 checkBackends(t, filter(0), []byte{}, match)
92 }
93
94 func TestLoadConstantX(t *testing.T) {
95 t.Parallel()
96
97 filter := func(val uint32) []bpf.Instruction {
98 return []bpf.Instruction{
99 bpf.LoadConstant{Dst: bpf.RegX, Val: val},
100 bpf.TXA{},
101 bpf.JumpIf{Cond: bpf.JumpEqual, Val: val, SkipTrue: 1},
102 bpf.RetConstant{Val: 0},
103 bpf.RetConstant{Val: 1},
104 }
105 }
106
107 checkBackends(t, filter(1), []byte{}, match)
108 checkBackends(t, filter(28), []byte{}, match)
109 checkBackends(t, filter(0), []byte{}, match)
110 }
111
112 func TestLoadAbsolute(t *testing.T) {
113 t.Parallel()
114
115 filter := func(val uint32, size int) []bpf.Instruction {
116 return []bpf.Instruction{
117 bpf.LoadAbsolute{Off: 2, Size: size},
118 bpf.JumpIf{Cond: bpf.JumpEqual, Val: val, SkipTrue: 1},
119 bpf.RetConstant{Val: 0},
120 bpf.RetConstant{Val: 1},
121 }
122 }
123
124 // 1
125 checkBackends(t, filter(5, 1), []byte{0, 0, 5}, match)
126 checkBackends(t, filter(6, 1), []byte{0, 0, 5}, noMatch)
127
128 // 2
129 checkBackends(t, filter(0xDEAD, 2), []byte{0, 0, 0xDE, 0xAD}, match)
130 checkBackends(t, filter(0xDEAF, 2), []byte{0, 0, 0xDE, 0xAD}, noMatch)
131
132 // 4
133 checkBackends(t, filter(0xDEADBEEF, 4), []byte{0, 0, 0xDE, 0xAD, 0xBE, 0xEF}, match)
134 checkBackends(t, filter(0xDEAFBEEF, 4), []byte{0, 0, 0xDE, 0xAD, 0xBE, 0xEF}, noMatch)
135 }
136
137 func TestLoadAbsoluteBigOffset(t *testing.T) {
138 t.Parallel()
139
140 // XDP limits packets to one page, so there's no way to feed a packet big enough to test the offsets
141 // we want through BPF_PROG_TEST_RUN.
142 // All we can check is that the verifier accepts the program and it doesn't match.
143 filter := func(load bpf.Instruction) []bpf.Instruction {
144 return []bpf.Instruction{
145 load,
146 bpf.ALUOpConstant{Op: bpf.ALUOpAdd, Val: 2},
147 bpf.RetA{},
148 }
149 }
150
151 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxPacketOffset - 1, Size: 1}), nil, noMatch)
152 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxPacketOffset, Size: 1}), nil, noMatch)
153 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxPacketOffset - 2, Size: 2}), nil, noMatch)
154 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxPacketOffset - 1, Size: 2}), nil, noMatch)
155 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxPacketOffset - 4, Size: 4}), nil, noMatch)
156 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxPacketOffset - 3, Size: 4}), nil, noMatch)
157
158 checkBackends(t, filter(bpf.LoadMemShift{Off: maxPacketOffset - 1}), nil, noMatch)
159 checkBackends(t, filter(bpf.LoadMemShift{Off: maxPacketOffset}), nil, noMatch)
160 }
161
162 // Absolute load with an offset packet pointer.
163 func TestLoadAbsolutePacketStartMaxOffset(t *testing.T) {
164 t.Parallel()
165
166 // XDP limits packets to one page, so there's no way to feed a packet big enough to test the offsets
167 // we want through BPF_PROG_TEST_RUN.
168 // All we can check is that the verifier accepts the program and it doesn't match.
169 filter := func(load bpf.Instruction) []bpf.Instruction {
170 return []bpf.Instruction{
171 load,
172 // Make sure we return a different value if the load succeeds.
173 bpf.ALUOpConstant{Op: bpf.ALUOpAdd, Val: 2},
174 bpf.RetA{},
175 }
176 }
177
178 for _, offset := range []uint16{0, 1, 14, 18, 64} {
179 t.Run(fmt.Sprint(offset), func(t *testing.T) {
180 t.Parallel()
181
182 opts := backendOpts{offset: offset}
183
184 // Smallest out of bounds offset once the packet start offset is accounted for,
185 // mirroring maxPacketOffset in TestLoadAbsoluteBigOffset.
186 maxOffset := uint32(maxPacketOffset - offset)
187
188 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxOffset - 1, Size: 1}), nil, noMatch, opts)
189 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxOffset, Size: 1}), nil, noMatch, opts)
190 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxOffset - 2, Size: 2}), nil, noMatch, opts)
191 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxOffset - 1, Size: 2}), nil, noMatch, opts)
192 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxOffset - 4, Size: 4}), nil, noMatch, opts)
193 checkBackends(t, filter(bpf.LoadAbsolute{Off: maxOffset - 3, Size: 4}), nil, noMatch, opts)
194
195 checkBackends(t, filter(bpf.LoadMemShift{Off: maxOffset - 1}), nil, noMatch, opts)
196 checkBackends(t, filter(bpf.LoadMemShift{Off: maxOffset}), nil, noMatch, opts)
197 })
198 }
199 }
200
201 func TestLoadIndirect(t *testing.T) {
202 t.Parallel()
203
204 // With a constant RegX, the verifier knows it's exact value.
205 t.Run("constant_valid", func(t *testing.T) {
206 filter := func(regX int32, off int32, size int, val uint32) []bpf.Instruction {
207 return []bpf.Instruction{
208 bpf.LoadConstant{Dst: bpf.RegX, Val: uint32(regX)},
209 bpf.LoadIndirect{Off: uint32(off), Size: size},
210 bpf.JumpIf{Cond: bpf.JumpEqual, Val: val, SkipTrue: 1},
211 bpf.RetConstant{Val: 0},
212 bpf.RetConstant{Val: 1},
213 }
214 }
215
216 // RegX: INT32_MIN+4, Off: INT32_MAX, Size: 1
217 checkBackends(t, filter(math.MinInt32+4, math.MaxInt32, 1, 5), []byte{0, 0, 0, 5}, match)
218 checkBackends(t, filter(math.MinInt32+4, math.MaxInt32, 1, 6), []byte{0, 0, 0, 5}, noMatch)
219
220 // RegX: 3, Off: -1, Size: 2
221 checkBackends(t, filter(3, -1, 2, 0xDEAD), []byte{0, 0, 0xDE, 0xAD}, match)
222 checkBackends(t, filter(3, -1, 2, 0xDEAD), []byte{0, 0, 0xDE, 0xAF}, noMatch)
223
224 // RegX: 1, Off: 2, Size: 4
225 checkBackends(t, filter(1, 2, 4, 0xDEADBEEF), []byte{0, 0, 0, 0xDE, 0xAD, 0xBE, 0xEF}, match)
226 checkBackends(t, filter(1, 2, 4, 0xDEADBEEF), []byte{0, 0, 0, 0xDE, 0xAA, 0xBE, 0xEF}, noMatch)
227 })
228
229 // But with a variable RegX, the verifier only has whatever checks we perform.
230 t.Run("variable_valid", func(t *testing.T) {
231 filter := func(off int32, size int, val uint32) []bpf.Instruction {
232 return []bpf.Instruction{
233 bpf.LoadAbsolute{Off: 0, Size: 4},
234 bpf.TAX{},
235 bpf.LoadIndirect{Off: uint32(off), Size: size},
236 bpf.JumpIf{Cond: bpf.JumpEqual, Val: val, SkipTrue: 1},
237 bpf.RetConstant{Val: 0},
238 bpf.RetConstant{Val: 1},
239 }
240 }
241
242 // RegX: -6, Off: 13, Size: 1
243 checkBackends(t, filter(13, 1, 5), []byte{0xFF, 0xFF, 0xFF, 0xFA, 0, 0, 0, 5}, match)
244 checkBackends(t, filter(13, 1, 6), []byte{0xFF, 0xFF, 0xFF, 0xFA, 0, 0, 0, 5}, noMatch)
245
246 // RegX: INT32_MAX, Off: INT32_MIN+7, Size: 2
247 checkBackends(t, filter(math.MinInt32+7, 2, 0xDEAD), []byte{0x7F, 0xFF, 0xFF, 0xFF, 0, 0, 0xDE, 0xAD}, match)
248 checkBackends(t, filter(math.MinInt32+7, 2, 0xDEAD), []byte{0x7F, 0xFF, 0xFF, 0xFF, 0, 0, 0xDE, 0xAF}, noMatch)
249
250 // RegX: 3, Off: 4, Size: 4
251 checkBackends(t, filter(4, 4, 0xDEADBEEF), []byte{0x00, 0x00, 0x00, 0x03, 0, 0, 0, 0xDE, 0xAD, 0xBE, 0xEF}, match)
252 checkBackends(t, filter(4, 4, 0xDEADBEEF), []byte{0x00, 0x00, 0x00, 0x03, 0, 0, 0, 0xDE, 0xAA, 0xBE, 0xEF}, noMatch)
253 })
254
255 t.Run("constant_outofbounds", func(t *testing.T) {
256 // Always return match to ensure noMatch comes from the packet load.
257 filter := func(regX, off int32, size int) []bpf.Instruction {
258 return []bpf.Instruction{
259 bpf.LoadConstant{Dst: bpf.RegX, Val: uint32(regX)},
260 bpf.LoadIndirect{Off: uint32(off), Size: size},
261 bpf.RetConstant{Val: 1},
262 }
263 }
264
265 // Not out of bounds, sanity check
266 checkBackends(t, filter(-3, 3, 1), nil, match)
267
268 // Before packet
269 // RegX: -16, Off: 15
270 checkBackends(t, filter(-16, 15, 1), nil, noMatch)
271 // RegX: -1, Off: -2
272 checkBackends(t, filter(-1, -2, 4), nil, noMatch)
273 // RegX: 255, Off: -300
274 checkBackends(t, filter(255, -300, 2), nil, noMatch)
275
276 // After packet
277 checkBackends(t, filter(-16, 30, 1), nil, noMatch)
278 })
279
280 t.Run("variable_outofbounds", func(t *testing.T) {
281 // Always return match to ensure noMatch comes from the packet load.
282 filter := func(off int32, size int) []bpf.Instruction {
283 return []bpf.Instruction{
284 bpf.LoadAbsolute{Off: 0, Size: 4},
285 bpf.TAX{},
286 bpf.LoadIndirect{Off: uint32(off), Size: size},
287 bpf.RetConstant{Val: 1},
288 }
289 }
290
291 // Not out of bounds, sanity check
292 checkBackends(t, filter(3, 1), []byte{0xFF, 0xFF, 0xFF, 0xFD}, match)
293
294 // Before packet
295 // RegX: -16, Off: 15
296 checkBackends(t, filter(15, 1), []byte{0xFF, 0xFF, 0xFF, 0xF0}, noMatch)
297 // RegX: -1, Off: -2
298 checkBackends(t, filter(-2, 4), []byte{0xFF, 0xFF, 0xFF, 0xFF}, noMatch)
299 // RegX: 255, Off: -300
300 checkBackends(t, filter(-300, 2), []byte{0x00, 0x00, 0x00, 0xFF}, noMatch)
301
302 // After packet
303 checkBackends(t, filter(30, 1), []byte{0xFF, 0xFF, 0xFF, 0xF0}, noMatch)
304 })
305 }
306
307 func TestLoadIndirectBigOffset(t *testing.T) {
308 filter := func(off uint32, size int) []bpf.Instruction {
309 return []bpf.Instruction{
310 // RegX is 0 initialized
311 bpf.LoadIndirect{Off: off, Size: size},
312 bpf.ALUOpConstant{Op: bpf.ALUOpAdd, Val: 2},
313 bpf.RetA{},
314 }
315 }
316
317 // XDP limits packets to one page, so there's no way to feed a packet big enough to test the offsets
318 // we want through BPF_PROG_TEST_RUN.
319 // All we can check is that the verifier accepts the program and it doesn't match.
320 checkBackends(t, filter(0, 1), nil, match)
321 checkBackends(t, filter(0, 4), nil, match)
322
323 checkBackends(t, filter(maxPacketOffset-1, 1), nil, noMatch)
324 checkBackends(t, filter(maxPacketOffset, 1), nil, noMatch)
325
326 checkBackends(t, filter(maxPacketOffset-2, 2), nil, noMatch)
327 checkBackends(t, filter(maxPacketOffset-1, 2), nil, noMatch)
328
329 checkBackends(t, filter(maxPacketOffset-4, 4), nil, noMatch)
330 checkBackends(t, filter(maxPacketOffset-3, 4), nil, noMatch)
331 }
332
333 // Indirect offset that would cause packetGuardIndirect.length() to overflow.
334 func TestLoadIndirectGuardOverflow(t *testing.T) {
335 var min int32 = math.MinInt32
336
337 checkBackends(t, []bpf.Instruction{
338 // Variable RegX
339 bpf.LoadAbsolute{Off: 0, Size: 4},
340 bpf.TAX{},
341 // Two loads, with offsets more than maxPacketOffset apart
342 bpf.LoadIndirect{Off: uint32(min), Size: 1},
343 bpf.LoadIndirect{Off: 0, Size: 2},
344 bpf.LoadIndirect{Off: math.MaxInt32, Size: 4},
345 bpf.TXA{},
346 bpf.RetA{},
347 }, nil, noMatch)
348 }
349
350 // Indirect load with an offset packet pointer.
351 func TestLoadIndirectPacketStartMaxOffset(t *testing.T) {
352 t.Parallel()
353
354 filter := []bpf.Instruction{
355 // Variable RegX
356 bpf.LoadAbsolute{Off: 0, Size: 4},
357 bpf.TAX{},
358 bpf.LoadIndirect{Off: 4, Size: 4},
359 bpf.JumpIf{Cond: bpf.JumpEqual, Val: 0xDEADBEEF, SkipTrue: 1},
360 bpf.RetConstant{Val: 0},
361 bpf.RetConstant{Val: 1},
362 }
363
364 // RegX is the first 4 bytes after offset.
365 packet := func(offset uint16, val []byte) []byte {
366 in := append(make([]byte, offset), 0, 0, 0, 3, 0, 0, 0)
367 return append(in, val...)
368 }
369
370 for _, offset := range []uint16{0, 1, 14, 18, 64} {
371 t.Run(fmt.Sprint(offset), func(t *testing.T) {
372 t.Parallel()
373
374 checkBackends(t, filter, packet(offset, []byte{0xDE, 0xAD, 0xBE, 0xEF}), match, backendOpts{
375 offset: offset,
376 })
377 checkBackends(t, filter, packet(offset, []byte{0xDE, 0xAD, 0xBE, 0xEE}), noMatch, backendOpts{
378 offset: offset,
379 })
380 })
381 }
382 }
383
384 // The 0 scratch slot is usable.
385 func TestScratchZero(t *testing.T) {
386 t.Parallel()
387
388 filter := []bpf.Instruction{
389 bpf.LoadConstant{Dst: bpf.RegA, Val: 4},
390 bpf.StoreScratch{Src: bpf.RegA, N: 0},
391
392 // clobber the reg in the mean time
393 bpf.LoadConstant{Dst: bpf.RegA, Val: 0},
394
395 bpf.LoadScratch{Dst: bpf.RegA, N: 0},
396 bpf.JumpIf{Cond: bpf.JumpEqual, Val: 4, SkipTrue: 1},
397 bpf.RetConstant{Val: 0},
398 bpf.RetConstant{Val: 1},
399 }
400
401 checkBackends(t, filter, nil, match)
402 }
403
404 func TestScratchA(t *testing.T) {
405 t.Parallel()
406
407 filter := func(val uint32) []bpf.Instruction {
408 return []bpf.Instruction{
409 bpf.LoadConstant{Dst: bpf.RegA, Val: val},
410 bpf.StoreScratch{Src: bpf.RegA, N: 7},
411
412 // clobber the reg in the mean time
413 bpf.LoadConstant{Dst: bpf.RegA, Val: 0},
414
415 // load garbage in the adjacent slots
416 bpf.LoadConstant{Dst: bpf.RegA, Val: 0xFFFFFFFF},
417 bpf.StoreScratch{Src: bpf.RegA, N: 6},
418 bpf.LoadConstant{Dst: bpf.RegA, Val: 0xFFFFFFFF},
419 bpf.StoreScratch{Src: bpf.RegA, N: 8},
420
421 bpf.LoadScratch{Dst: bpf.RegA, N: 7},
422
423 bpf.JumpIf{Cond: bpf.JumpEqual, Val: val, SkipTrue: 1},
424 bpf.RetConstant{Val: 0},
425 bpf.RetConstant{Val: 1},
426 }
427 }
428
429 checkBackends(t, filter(0xdeadbeef), []byte{}, match)
430 checkBackends(t, filter(0), []byte{}, match)
431 }
432
433 func TestScratchX(t *testing.T) {
434 t.Parallel()
435
436 filter := func(val uint32) []bpf.Instruction {
437 return []bpf.Instruction{
438 bpf.LoadConstant{Dst: bpf.RegX, Val: val},
439 bpf.StoreScratch{Src: bpf.RegX, N: 7},
440
441 // clobber the reg in the mean time
442 bpf.LoadConstant{Dst: bpf.RegX, Val: 0},
443
444 // load garbage in the adjacent slots
445 bpf.LoadConstant{Dst: bpf.RegX, Val: 0xFFFFFFFF},
446 bpf.StoreScratch{Src: bpf.RegX, N: 6},
447 bpf.LoadConstant{Dst: bpf.RegX, Val: 0xFFFFFFFF},
448 bpf.StoreScratch{Src: bpf.RegX, N: 8},
449
450 bpf.LoadScratch{Dst: bpf.RegX, N: 7},
451 bpf.TXA{},
452
453 bpf.JumpIf{Cond: bpf.JumpEqual, Val: val, SkipTrue: 1},
454 bpf.RetConstant{Val: 0},
455 bpf.RetConstant{Val: 1},
456 }
457 }
458
459 checkBackends(t, filter(0xdeadbeef), []byte{}, match)
460 checkBackends(t, filter(0), []byte{}, match)
461 }
462
463 func TestMemShift(t *testing.T) {
464 t.Parallel()
465
466 filter := func(val uint32) []bpf.Instruction {
467 return []bpf.Instruction{
468 bpf.LoadConstant{Dst: bpf.RegA, Val: val},
469 bpf.LoadMemShift{Off: 2},
470 bpf.JumpIfX{Cond: bpf.JumpEqual, SkipTrue: 1},
471 bpf.RetConstant{Val: 0},
472 bpf.RetConstant{Val: 1},
473 }
474 }
475
476 checkBackends(t, filter(40), []byte{0, 0, 0xAA}, match)
477 checkBackends(t, filter(0), []byte{0, 0, 0xF0}, match)
478 }
479
480 func TestLoadExtLen(t *testing.T) {
481 t.Parallel()
482
483 filter := func(pktLen uint32) []bpf.Instruction {
484 return []bpf.Instruction{
485 bpf.LoadExtension{Num: bpf.ExtLen},
486 bpf.JumpIf{Cond: bpf.JumpEqual, Val: pktLen, SkipTrue: 1},
487 bpf.RetConstant{Val: 0},
488 bpf.RetConstant{Val: 1},
489 }
490 }
491
492 checkBackends(t, filter(16), []byte{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef}, match)
493 }
494
495 // check a OP b == res for both ALUOpConstant and ALUOpX
496 func checkAlu(t *testing.T, op bpf.ALUOp, a, b, res uint32) {
497 t.Helper()
498
499 constFilter := []bpf.Instruction{
500 bpf.LoadConstant{Dst: bpf.RegA, Val: a},
501 bpf.ALUOpConstant{Op: op, Val: b},
502
503 bpf.JumpIf{Cond: bpf.JumpEqual, Val: res, SkipTrue: 1},
504 bpf.RetConstant{Val: 0},
505 bpf.RetConstant{Val: 1},
506 }
507
508 checkBackends(t, constFilter, []byte{}, match)
509
510 xFilter := []bpf.Instruction{
511 bpf.LoadConstant{Dst: bpf.RegA, Val: a},
512 bpf.LoadConstant{Dst: bpf.RegX, Val: b},
513
514 bpf.ALUOpX{Op: op},
515
516 bpf.JumpIf{Cond: bpf.JumpEqual, Val: res, SkipTrue: 1},
517 bpf.RetConstant{Val: 0},
518 bpf.RetConstant{Val: 1},
519 }
520
521 checkBackends(t, xFilter, []byte{}, match)
522 }
523
524 func TestALUAdd(t *testing.T) {
525 t.Parallel()
526
527 checkAlu(t, bpf.ALUOpAdd, 1, 0, 1)
528 checkAlu(t, bpf.ALUOpAdd, 4, 13, 17)
529 }
530
531 func TestALUSub(t *testing.T) {
532 t.Parallel()
533
534 checkAlu(t, bpf.ALUOpSub, 1, 1, 0)
535 checkAlu(t, bpf.ALUOpSub, 13, 9, 4)
536 }
537
538 func TestALUMul(t *testing.T) {
539 t.Parallel()
540
541 checkAlu(t, bpf.ALUOpMul, 0, 1, 0)
542 checkAlu(t, bpf.ALUOpMul, 4, 13, 52)
543 // overflow - 2^31 * 2
544 checkAlu(t, bpf.ALUOpMul, 2, 0x80000000, 0)
545 }
546
547 func TestALUDiv(t *testing.T) {
548 t.Parallel()
549
550 checkAlu(t, bpf.ALUOpDiv, 2, 2, 1)
551 checkAlu(t, bpf.ALUOpDiv, 19, 3, 6)
552 }
553
554 func TestALUDivZero(t *testing.T) {
555 t.Parallel()
556
557 filter := []bpf.Instruction{
558 bpf.LoadAbsolute{Size: 1, Off: 0},
559 bpf.TAX{},
560
561 bpf.LoadConstant{Dst: bpf.RegA, Val: 10},
562
563 bpf.ALUOpX{Op: bpf.ALUOpDiv},
564
565 bpf.RetConstant{Val: 1},
566 }
567
568 checkBackends(t, filter, []byte{0}, noMatch)
569 checkBackends(t, filter, []byte{1}, match)
570 }
571
572 // Check that ALU operations aren't signed.
573 // The only operator that is implemented differently for signed vs unsigned math with two's complement is division.
574 func TestALUDivNegative(t *testing.T) {
575 checkBackends(t, []bpf.Instruction{
576 bpf.LoadConstant{Dst: bpf.RegA, Val: 0x00000004}, // 4
577 bpf.LoadConstant{Dst: bpf.RegX, Val: 0xFFFFFFFE}, // -2
578 bpf.ALUOpX{Op: bpf.ALUOpDiv},
579 bpf.JumpIf{Cond: bpf.JumpEqual, Val: 0, SkipTrue: 1}, // 0 means division is unsigned, -2 signed
580 bpf.RetConstant{},
581 bpf.RetConstant{Val: 1},
582 }, nil, match)
583 }
584
585 func TestALUOr(t *testing.T) {
586 t.Parallel()
587
588 checkAlu(t, bpf.ALUOpOr, 1, 0, 1)
589 checkAlu(t, bpf.ALUOpOr, 0xF0, 0x0F, 0xFF)
590 }
591
592 func TestALUAnd(t *testing.T) {
593 t.Parallel()
594
595 checkAlu(t, bpf.ALUOpAnd, 1, 0, 0)
596 checkAlu(t, bpf.ALUOpAnd, 0xF0, 0x80, 0x80)
597 checkAlu(t, bpf.ALUOpAnd, 0xF0, 0x0F, 0x00)
598 }
599
600 func TestALUShiftLeft(t *testing.T) {
601 t.Parallel()
602
603 checkAlu(t, bpf.ALUOpShiftLeft, 1, 0, 1)
604 checkAlu(t, bpf.ALUOpShiftLeft, 1, 4, 0x10)
605 }
606
607 func TestALUShiftRight(t *testing.T) {
608 t.Parallel()
609
610 checkAlu(t, bpf.ALUOpShiftRight, 0xF0, 4, 0x0F)
611 checkAlu(t, bpf.ALUOpShiftRight, 0xF0, 8, 0)
612 }
613
614 func TestALUMod(t *testing.T) {
615 t.Parallel()
616
617 checkAlu(t, bpf.ALUOpMod, 16, 4, 0)
618 checkAlu(t, bpf.ALUOpMod, 17, 4, 1)
619 }
620
621 func TestALUXor(t *testing.T) {
622 t.Parallel()
623
624 checkAlu(t, bpf.ALUOpXor, 1, 1, 0)
625 checkAlu(t, bpf.ALUOpMod, 6, 4, 2)
626 }
627
628 func TestNegateA(t *testing.T) {
629 t.Parallel()
630
631 filter := []bpf.Instruction{
632 bpf.LoadConstant{Dst: bpf.RegA, Val: 26},
633
634 bpf.NegateA{},
635
636 bpf.JumpIf{Cond: bpf.JumpEqual, Val: uint32(26 | 0x80000000), SkipTrue: 1},
637 bpf.RetConstant{Val: 0},
638 bpf.RetConstant{Val: 1},
639 }
640
641 checkBackends(t, filter, []byte{}, noMatch)
642 }
643
644 func TestJump(t *testing.T) {
645 t.Parallel()
646
647 filter := []bpf.Instruction{
648 // "dummy" jump so the unreachable code after the real jump isn't removed
649 bpf.LoadAbsolute{Off: 0, Size: 1},
650 bpf.JumpIf{Cond: bpf.JumpEqual, Val: 1, SkipTrue: 1},
651
652 bpf.Jump{Skip: 1},
653
654 bpf.LoadConstant{Dst: bpf.RegA, Val: 1},
655 bpf.RetA{},
656 }
657
658 checkBackends(t, filter, []byte{}, noMatch)
659 }
660
661 // Jump that does nothing.
662 func TestJump0(t *testing.T) {
663 t.Parallel()
664
665 filter := []bpf.Instruction{
666 bpf.LoadAbsolute{Off: 0, Size: 1},
667 bpf.Jump{Skip: 1},
668 bpf.JumpIf{Cond: bpf.JumpEqual},
669 bpf.JumpIfX{Cond: bpf.JumpEqual},
670 bpf.RetA{},
671 }
672
673 checkBackends(t, filter, []byte{}, noMatch)
674 checkBackends(t, filter, []byte{1}, match)
675 }
676
677 // a needs to be != 0
678 func checkJump(t *testing.T, cond bpf.JumpTest, a, b uint32, result bool) {
679 t.Helper()
680
681 if a == 0 {
682 t.Fatal("a must be non 0")
683 }
684
685 // match if cond is true
686 expected := noMatch
687 if result {
688 expected = match
689 }
690
691 // constant skipTrue
692 constTrueFilter := []bpf.Instruction{
693 bpf.LoadConstant{Dst: bpf.RegA, Val: a},
694
695 bpf.JumpIf{Cond: cond, Val: b, SkipTrue: 1},
696 bpf.RetConstant{Val: 0},
697 bpf.RetConstant{Val: 1},
698 }
699 checkBackends(t, constTrueFilter, []byte{}, expected)
700
701 // constant skipTrue & skipFalse
702 constBothFilter := []bpf.Instruction{
703 bpf.LoadConstant{Dst: bpf.RegA, Val: a},
704
705 // "dummy" interleaved jump so the actual test jump can use both skipFalse and skipTrue
706 bpf.JumpIf{Cond: bpf.JumpEqual, Val: 0, SkipTrue: 1},
707
708 bpf.JumpIf{Cond: cond, Val: b, SkipTrue: 2, SkipFalse: 1},
709
710 // "dummy" target
711 bpf.RetConstant{Val: 1},
712
713 bpf.RetConstant{Val: 0},
714 bpf.RetConstant{Val: 1},
715 }
716 checkBackends(t, constBothFilter, []byte{}, expected)
717
718 // X skipTrue
719 xTrueFilter := []bpf.Instruction{
720 bpf.LoadConstant{Dst: bpf.RegA, Val: a},
721 bpf.LoadConstant{Dst: bpf.RegX, Val: b},
722
723 bpf.JumpIfX{Cond: cond, SkipTrue: 1},
724 bpf.RetConstant{Val: 0},
725 bpf.RetConstant{Val: 1},
726 }
727 checkBackends(t, xTrueFilter, []byte{}, expected)
728
729 // X skipTrue & skipFalse
730 xBothFilter := []bpf.Instruction{
731 bpf.LoadConstant{Dst: bpf.RegA, Val: a},
732 bpf.LoadConstant{Dst: bpf.RegX, Val: b},
733
734 // "dummy" interleaved jump so the actual test jump can use both skipFalse and skipTrue
735 bpf.JumpIf{Cond: bpf.JumpEqual, Val: 0, SkipTrue: 1},
736
737 bpf.JumpIfX{Cond: cond, SkipTrue: 2, SkipFalse: 1},
738
739 // "dummy" target
740 bpf.RetConstant{Val: 1},
741
742 bpf.RetConstant{Val: 0},
743 bpf.RetConstant{Val: 1},
744 }
745 checkBackends(t, xBothFilter, []byte{}, expected)
746 }
747
748 func TestJumpIfEqual(t *testing.T) {
749 t.Parallel()
750
751 checkJump(t, bpf.JumpEqual, 23, 23, true)
752 checkJump(t, bpf.JumpEqual, 23, 21, false)
753 }
754
755 func TestJumpIfNotEqual(t *testing.T) {
756 t.Parallel()
757
758 checkJump(t, bpf.JumpNotEqual, 23, 23, false)
759 checkJump(t, bpf.JumpNotEqual, 23, 21, true)
760 }
761
762 func TestJumpIfGreaterThan(t *testing.T) {
763 t.Parallel()
764
765 checkJump(t, bpf.JumpGreaterThan, 24, 23, true)
766 checkJump(t, bpf.JumpGreaterThan, 23, 23, false)
767 checkJump(t, bpf.JumpGreaterThan, 22, 23, false)
768 }
769
770 func TestJumpIfLessThan(t *testing.T) {
771 t.Parallel()
772
773 checkJump(t, bpf.JumpLessThan, 24, 23, false)
774 checkJump(t, bpf.JumpLessThan, 23, 23, false)
775 checkJump(t, bpf.JumpLessThan, 22, 23, true)
776 }
777
778 func TestJumpIfGreaterOrEqual(t *testing.T) {
779 t.Parallel()
780
781 checkJump(t, bpf.JumpGreaterOrEqual, 24, 23, true)
782 checkJump(t, bpf.JumpGreaterOrEqual, 23, 23, true)
783 checkJump(t, bpf.JumpGreaterOrEqual, 22, 23, false)
784 }
785
786 func TestJumpIfLessOrEqual(t *testing.T) {
787 t.Parallel()
788
789 checkJump(t, bpf.JumpLessOrEqual, 24, 23, false)
790 checkJump(t, bpf.JumpLessOrEqual, 23, 23, true)
791 checkJump(t, bpf.JumpLessOrEqual, 22, 23, true)
792 }
793
794 func TestJumpIfBitsSet(t *testing.T) {
795 t.Parallel()
796
797 checkJump(t, bpf.JumpBitsSet, 6, 4, true)
798 checkJump(t, bpf.JumpBitsSet, 6, 2, true)
799 checkJump(t, bpf.JumpBitsSet, 6, 8, false)
800 }
801
802 func TestJumpIfBitsNotSet(t *testing.T) {
803 t.Parallel()
804
805 checkJump(t, bpf.JumpBitsNotSet, 6, 4, false)
806 checkJump(t, bpf.JumpBitsNotSet, 6, 2, false)
807 checkJump(t, bpf.JumpBitsNotSet, 6, 8, true)
808 }
809
810 func TestRetA(t *testing.T) {
811 t.Parallel()
812
813 filter := []bpf.Instruction{
814 bpf.LoadConstant{Dst: bpf.RegA, Val: 1},
815 bpf.RetA{},
816 }
817
818 checkBackends(t, filter, []byte{}, match)
819 }
820
821 func TestRetConstant(t *testing.T) {
822 t.Parallel()
823
824 filter := []bpf.Instruction{
825 bpf.RetConstant{Val: 1},
826 }
827
828 checkBackends(t, filter, []byte{}, match)
829
830 filter = []bpf.Instruction{
831 bpf.RetConstant{Val: 0},
832 }
833
834 checkBackends(t, filter, []byte{}, noMatch)
835 }
836
837 func TestTXA(t *testing.T) {
838 t.Parallel()
839
840 filter := []bpf.Instruction{
841 bpf.LoadConstant{Dst: bpf.RegX, Val: 1},
842 bpf.TXA{},
843 bpf.RetA{},
844 }
845
846 checkBackends(t, filter, []byte{}, match)
847 }
848
849 func TestTAX(t *testing.T) {
850 t.Parallel()
851
852 filter := []bpf.Instruction{
853 bpf.LoadConstant{Dst: bpf.RegA, Val: 1},
854 bpf.TAX{},
855 bpf.TXA{},
856 bpf.RetA{},
857 }
858
859 checkBackends(t, filter, []byte{}, match)
860 }
861
862 type result int
863
864 const (
865 match result = iota
866 noMatch
867 )
868
869 func (r result) String() string {
870 switch r {
871 case match:
872 return "match"
873 case noMatch:
874 return "no match"
875 default:
876 return fmt.Sprintf("result(%d)", int(r))
877 }
878 }
879
880 // True IFF packet matches filter
881 type backend func(testing.TB, []bpf.Instruction, []byte, backendOpts) result
882
883 type backendOpts struct {
884 // Fixed offset into the packet to use.
885 offset uint16
886 }
887
888 // checkBackends checks if all the backends match the packet as expected.
889 // Input packet is 0 padded to min ethernet length.
890 func checkBackends(t *testing.T, filter []bpf.Instruction, in []byte, expected result, opts ...backendOpts) {
891 t.Helper()
892
893 if len(in) < 14 {
894 t := make([]byte, 14)
895 copy(t, in)
896 in = t
897 }
898
899 var options backendOpts
900 switch len(opts) {
901 case 0:
902 case 1:
903 options = opts[0]
904 default:
905 t.Fatal("multiple backendOpts provided")
906 }
907
908 check := func(b backend) func(*testing.T) {
909 return func(t *testing.T) {
910 if got := b(t, filter, in, options); got != expected {
911 t.Fatalf("Got %q, expected %q", got, expected)
912 }
913 }
914 }
915
916 t.Run("C", check(cBackend))
917 t.Run("eBPF", check(ebpfBackend))
918 t.Run("kernel", check(kernelBackend))
919 }
920
921 type XDPAction int
922
923 func (r XDPAction) String() string {
924 switch r {
925 case XDPAborted:
926 return "XDPAborted"
927 case XDPDrop:
928 return "XDPDrop"
929 case XDPPass:
930 return "XDPPass"
931 case XDPTx:
932 return "XDPTx"
933 default:
934 return fmt.Sprintf("XDPResult(%d)", int(r))
935 }
936 }
937
938 const (
939 XDPAborted XDPAction = iota
940 XDPDrop
941 XDPPass
942 XDPTx
943 )
944
945 // testProg runs an eBPF program and checks it has not modified the packet
946 func testProg(tb testing.TB, progSpec *ebpf.ProgramSpec, in []byte) result {
947 // -short skips tests that require permissions
948 // Skipping the tests this late ensures the eBPF program still builds at least
949 if testing.Short() {
950 tb.SkipNow()
951 }
952
953 prog, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
954 LogLevel: 2, // Get full verifier logs.
955 })
956 if err != nil {
957 tb.Fatal(err)
958 }
959 defer prog.Close()
960
961 ret, out, err := prog.Test(in)
962 if err != nil {
963 tb.Fatal(err)
964 }
965
966 if !bytes.Equal(in, out) {
967 tb.Fatalf("Program modified input:\nIn: %v\nOut: %v\n", in, out)
968 }
969
970 // The XDP programs we build drop matching packets
971 switch r := XDPAction(ret); r {
972 case XDPDrop:
973 return match
974 case XDPPass:
975 return noMatch
976 default:
977 tb.Fatalf("Unexpected XDP return code %v", r)
978 panic("unreachable")
979 }
980 }