Debian Codegraph @

cbpfc_test.go

raw symbols SCIP
1 package cbpfc
2
3 import (
4 "reflect"
5 "strings"
6 "testing"
7
8 "golang.org/x/net/bpf"
9 )
10
11 // requireError ensures an error is not nil, and it contains contains.
12 func requireError(tb testing.TB, err error, contains string) {
13 tb.Helper()
14
15 if err == nil {
16 tb.Fatalf("expected error %s", contains)
17 }
18
19 if !strings.Contains(err.Error(), contains) {
20 tb.Fatalf("error %v does not contain %s", err, contains)
21 }
22 }
23
24 // Make sure we bail out with 0 instructions
25 func TestZero(t *testing.T) {
26 _, err := compile([]bpf.Instruction{}, compileOpts{})
27
28 requireError(t, err, "can't compile 0 instructions")
29 }
30
31 func TestRaw(t *testing.T) {
32 _, err := compile([]bpf.Instruction{
33 bpf.RawInstruction{},
34 }, compileOpts{})
35
36 requireError(t, err, "unsupported instruction 0:")
37 }
38
39 // Absolute / constant loads can't use negative offsets, they're for extensions.
40 func TestLoadAbsoluteNegativeOffset(t *testing.T) {
41 off := (^uint32(1)) + 1 // -1
42
43 for _, insn := range []bpf.Instruction{
44 bpf.LoadAbsolute{Off: off, Size: 1},
45 bpf.LoadMemShift{Off: off},
46 } {
47 _, err := compile([]bpf.Instruction{
48 insn,
49 bpf.RetA{},
50 }, compileOpts{})
51
52 requireError(t, err, "negative offset -1")
53 }
54 }
55
56 func TestExtension(t *testing.T) {
57 // No extensions > 256 right now
58 for i := 0; i < 256; i++ {
59 ext := bpf.Extension(i)
60
61 _, err := compile([]bpf.Instruction{
62 bpf.LoadExtension{Num: ext},
63 bpf.RetA{},
64 }, compileOpts{})
65
66 switch ext {
67 case bpf.ExtLen:
68 if err != nil {
69 t.Fatal("ExtLen not accepted", err)
70 }
71 default:
72 requireError(t, err, "unsupported BPF extension 0:")
73 }
74 }
75 }
76
77 // Test out of bound jumps
78 func TestJumpOut(t *testing.T) {
79 _, err := compile([]bpf.Instruction{
80 bpf.LoadConstant{Dst: bpf.RegX, Val: 0},
81 bpf.Jump{Skip: 0},
82 }, compileOpts{})
83
84 requireError(t, err, "instruction 1: ja 0 flows past last instruction")
85 }
86
87 func TestJumpIfOut(t *testing.T) {
88 _, err := compile([]bpf.Instruction{
89 bpf.LoadConstant{Dst: bpf.RegA, Val: 0},
90 bpf.JumpIf{Cond: bpf.JumpEqual, Val: 2, SkipTrue: 0, SkipFalse: 1},
91 }, compileOpts{})
92
93 requireError(t, err, "instruction 1: jneq #2,1 flows past last instruction")
94 }
95
96 func TestJumpIfXOut(t *testing.T) {
97 _, err := compile([]bpf.Instruction{
98 bpf.LoadConstant{Dst: bpf.RegA, Val: 0},
99 bpf.LoadConstant{Dst: bpf.RegX, Val: 3},
100 bpf.JumpIfX{Cond: bpf.JumpEqual, SkipTrue: 1, SkipFalse: 0},
101 }, compileOpts{})
102
103 requireError(t, err, "instruction 2: jeq x,1 flows past last instruction")
104 }
105
106 // Out of bounds fall through - last block doesn't end in return
107 func TestFallthroughOut(t *testing.T) {
108 _, err := compile([]bpf.Instruction{
109 bpf.LoadConstant{Dst: bpf.RegA, Val: 0},
110 }, compileOpts{})
111
112 requireError(t, err, "instruction 0: ld #0 flows past last instruction")
113 }
114
115 // Jump normalization
116 func TestNormalizeJumps(t *testing.T) {
117 insns := func(skipTrue, skipFalse uint8) []instruction {
118 return toInstructions([]bpf.Instruction{
119 bpf.JumpIf{Cond: bpf.JumpEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
120 bpf.JumpIfX{Cond: bpf.JumpEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
121
122 bpf.JumpIf{Cond: bpf.JumpNotEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
123 bpf.JumpIfX{Cond: bpf.JumpNotEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
124
125 bpf.JumpIf{Cond: bpf.JumpGreaterThan, SkipTrue: skipTrue, SkipFalse: skipFalse},
126 bpf.JumpIfX{Cond: bpf.JumpGreaterThan, SkipTrue: skipTrue, SkipFalse: skipFalse},
127
128 bpf.JumpIf{Cond: bpf.JumpLessThan, SkipTrue: skipTrue, SkipFalse: skipFalse},
129 bpf.JumpIfX{Cond: bpf.JumpLessThan, SkipTrue: skipTrue, SkipFalse: skipFalse},
130
131 bpf.JumpIf{Cond: bpf.JumpGreaterOrEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
132 bpf.JumpIfX{Cond: bpf.JumpGreaterOrEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
133
134 bpf.JumpIf{Cond: bpf.JumpLessOrEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
135 bpf.JumpIfX{Cond: bpf.JumpLessOrEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
136
137 bpf.JumpIf{Cond: bpf.JumpBitsSet, SkipTrue: skipTrue, SkipFalse: skipFalse},
138 bpf.JumpIfX{Cond: bpf.JumpBitsSet, SkipTrue: skipTrue, SkipFalse: skipFalse},
139
140 bpf.JumpIf{Cond: bpf.JumpBitsNotSet, SkipTrue: skipTrue, SkipFalse: skipFalse},
141 bpf.JumpIfX{Cond: bpf.JumpBitsNotSet, SkipTrue: skipTrue, SkipFalse: skipFalse},
142 })
143 }
144
145 // same insns, but with the conditions inverted
146 invertedInsns := func(skipTrue, skipFalse uint8) []instruction {
147 return toInstructions([]bpf.Instruction{
148 bpf.JumpIf{Cond: bpf.JumpNotEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
149 bpf.JumpIfX{Cond: bpf.JumpNotEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
150
151 bpf.JumpIf{Cond: bpf.JumpEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
152 bpf.JumpIfX{Cond: bpf.JumpEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
153
154 bpf.JumpIf{Cond: bpf.JumpLessOrEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
155 bpf.JumpIfX{Cond: bpf.JumpLessOrEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
156
157 bpf.JumpIf{Cond: bpf.JumpGreaterOrEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
158 bpf.JumpIfX{Cond: bpf.JumpGreaterOrEqual, SkipTrue: skipTrue, SkipFalse: skipFalse},
159
160 bpf.JumpIf{Cond: bpf.JumpLessThan, SkipTrue: skipTrue, SkipFalse: skipFalse},
161 bpf.JumpIfX{Cond: bpf.JumpLessThan, SkipTrue: skipTrue, SkipFalse: skipFalse},
162
163 bpf.JumpIf{Cond: bpf.JumpGreaterThan, SkipTrue: skipTrue, SkipFalse: skipFalse},
164 bpf.JumpIfX{Cond: bpf.JumpGreaterThan, SkipTrue: skipTrue, SkipFalse: skipFalse},
165
166 bpf.JumpIf{Cond: bpf.JumpBitsNotSet, SkipTrue: skipTrue, SkipFalse: skipFalse},
167 bpf.JumpIfX{Cond: bpf.JumpBitsNotSet, SkipTrue: skipTrue, SkipFalse: skipFalse},
168
169 bpf.JumpIf{Cond: bpf.JumpBitsSet, SkipTrue: skipTrue, SkipFalse: skipFalse},
170 bpf.JumpIfX{Cond: bpf.JumpBitsSet, SkipTrue: skipTrue, SkipFalse: skipFalse},
171 })
172 }
173
174 check := func(t *testing.T, input []instruction, expected []instruction) {
175 normalizeJumps(input)
176
177 if !reflect.DeepEqual(input, expected) {
178 t.Fatalf("\nGot:\n%v\n\nExpected:\n%v", input, expected)
179 }
180 }
181
182 // skipTrue only - no change
183 check(t, insns(1, 0), insns(1, 0))
184
185 // skipFalse & skipTrue - no change
186 check(t, insns(1, 3), insns(1, 3))
187
188 // skipFalse only - inverted
189 check(t, insns(0, 3), invertedInsns(3, 0))
190 }
191
192 // instruction read / writes
193 func TestInstructionReadsRegA(t *testing.T) {
194 checkMemoryStatus(t, map[bpf.Instruction]bool{
195 bpf.ALUOpConstant{}: true,
196 bpf.ALUOpX{}: true,
197
198 bpf.Jump{}: false,
199 bpf.JumpIf{}: true,
200 bpf.JumpIfX{}: true,
201
202 bpf.LoadAbsolute{}: false,
203 bpf.LoadConstant{Dst: bpf.RegA}: false,
204 bpf.LoadConstant{Dst: bpf.RegX}: false,
205 bpf.LoadExtension{}: false,
206 bpf.LoadIndirect{}: false,
207 bpf.LoadMemShift{}: false,
208 bpf.LoadScratch{Dst: bpf.RegA}: false,
209 bpf.LoadScratch{Dst: bpf.RegX}: false,
210
211 bpf.NegateA{}: true,
212
213 bpf.RetA{}: true,
214 bpf.RetConstant{}: false,
215
216 bpf.StoreScratch{Src: bpf.RegA}: true,
217 bpf.StoreScratch{Src: bpf.RegX}: false,
218
219 bpf.TAX{}: true,
220 bpf.TXA{}: false,
221 }, func(insn bpf.Instruction) bool {
222 return memReads(insn).regs[bpf.RegA]
223 })
224 }
225
226 func TestInstructionWritesRegA(t *testing.T) {
227 checkMemoryStatus(t, map[bpf.Instruction]bool{
228 bpf.ALUOpConstant{}: true,
229 bpf.ALUOpX{}: true,
230
231 bpf.Jump{}: false,
232 bpf.JumpIf{}: false,
233 bpf.JumpIfX{}: false,
234
235 bpf.LoadAbsolute{}: true,
236 bpf.LoadConstant{Dst: bpf.RegA}: true,
237 bpf.LoadConstant{Dst: bpf.RegX}: false,
238 bpf.LoadExtension{}: true,
239 bpf.LoadIndirect{}: true,
240 bpf.LoadMemShift{}: false,
241 bpf.LoadScratch{Dst: bpf.RegA}: true,
242 bpf.LoadScratch{Dst: bpf.RegX}: false,
243
244 bpf.NegateA{}: true,
245
246 bpf.RetA{}: false,
247 bpf.RetConstant{}: false,
248
249 bpf.StoreScratch{Src: bpf.RegA}: false,
250 bpf.StoreScratch{Src: bpf.RegX}: false,
251
252 bpf.TAX{}: false,
253 bpf.TXA{}: true,
254 }, func(insn bpf.Instruction) bool {
255 return memWrites(insn).regs[bpf.RegA]
256 })
257 }
258
259 func TestInstructionReadsRegX(t *testing.T) {
260 checkMemoryStatus(t, map[bpf.Instruction]bool{
261 bpf.ALUOpConstant{}: false,
262 bpf.ALUOpX{}: true,
263
264 bpf.Jump{}: false,
265 bpf.JumpIf{}: false,
266 bpf.JumpIfX{}: true,
267
268 bpf.LoadAbsolute{}: false,
269 bpf.LoadConstant{Dst: bpf.RegA}: false,
270 bpf.LoadConstant{Dst: bpf.RegX}: false,
271 bpf.LoadExtension{}: false,
272 bpf.LoadIndirect{}: true,
273 bpf.LoadMemShift{}: false,
274 bpf.LoadScratch{Dst: bpf.RegA}: false,
275 bpf.LoadScratch{Dst: bpf.RegX}: false,
276
277 bpf.NegateA{}: false,
278
279 bpf.RetA{}: false,
280 bpf.RetConstant{}: false,
281
282 bpf.StoreScratch{Src: bpf.RegA}: false,
283 bpf.StoreScratch{Src: bpf.RegX}: true,
284
285 bpf.TAX{}: false,
286 bpf.TXA{}: true,
287 }, func(insn bpf.Instruction) bool {
288 return memReads(insn).regs[bpf.RegX]
289 })
290 }
291
292 func TestInstructionWritesRegX(t *testing.T) {
293 checkMemoryStatus(t, map[bpf.Instruction]bool{
294 bpf.ALUOpConstant{}: false,
295 bpf.ALUOpX{}: false,
296
297 bpf.Jump{}: false,
298 bpf.JumpIf{}: false,
299 bpf.JumpIfX{}: false,
300
301 bpf.LoadAbsolute{}: false,
302 bpf.LoadConstant{Dst: bpf.RegA}: false,
303 bpf.LoadConstant{Dst: bpf.RegX}: true,
304 bpf.LoadExtension{}: false,
305 bpf.LoadIndirect{}: false,
306 bpf.LoadMemShift{}: true,
307 bpf.LoadScratch{Dst: bpf.RegA}: false,
308 bpf.LoadScratch{Dst: bpf.RegX}: true,
309
310 bpf.NegateA{}: false,
311
312 bpf.RetA{}: false,
313 bpf.RetConstant{}: false,
314
315 bpf.StoreScratch{Src: bpf.RegA}: false,
316 bpf.StoreScratch{Src: bpf.RegX}: false,
317
318 bpf.TAX{}: true,
319 bpf.TXA{}: false,
320 }, func(insn bpf.Instruction) bool {
321 return memWrites(insn).regs[bpf.RegX]
322 })
323 }
324
325 func TestInstructionReadsScratch(t *testing.T) {
326 checkMemoryStatus(t, map[bpf.Instruction]bool{
327 bpf.ALUOpConstant{}: false,
328 bpf.ALUOpX{}: false,
329
330 bpf.Jump{}: false,
331 bpf.JumpIf{}: false,
332 bpf.JumpIfX{}: false,
333
334 bpf.LoadAbsolute{}: false,
335 bpf.LoadConstant{Dst: bpf.RegA}: false,
336 bpf.LoadConstant{Dst: bpf.RegX}: false,
337 bpf.LoadExtension{}: false,
338 bpf.LoadIndirect{}: false,
339 bpf.LoadMemShift{}: false,
340 bpf.LoadScratch{Dst: bpf.RegA, N: 3}: true,
341 bpf.LoadScratch{Dst: bpf.RegX, N: 3}: true,
342
343 bpf.NegateA{}: false,
344
345 bpf.RetA{}: false,
346 bpf.RetConstant{}: false,
347
348 bpf.StoreScratch{Src: bpf.RegA, N: 3}: false,
349 bpf.StoreScratch{Src: bpf.RegX, N: 3}: false,
350
351 bpf.TAX{}: false,
352 bpf.TXA{}: false,
353 }, func(insn bpf.Instruction) bool {
354 return memReads(insn).scratch[3]
355 })
356 }
357
358 func TestInstructionWritesScratch(t *testing.T) {
359 checkMemoryStatus(t, map[bpf.Instruction]bool{
360 bpf.ALUOpConstant{}: false,
361 bpf.ALUOpX{}: false,
362
363 bpf.Jump{}: false,
364 bpf.JumpIf{}: false,
365 bpf.JumpIfX{}: false,
366
367 bpf.LoadAbsolute{}: false,
368 bpf.LoadConstant{Dst: bpf.RegA}: false,
369 bpf.LoadConstant{Dst: bpf.RegX}: false,
370 bpf.LoadExtension{}: false,
371 bpf.LoadIndirect{}: false,
372 bpf.LoadMemShift{}: false,
373 bpf.LoadScratch{Dst: bpf.RegA, N: 3}: false,
374 bpf.LoadScratch{Dst: bpf.RegX, N: 3}: false,
375
376 bpf.NegateA{}: false,
377
378 bpf.RetA{}: false,
379 bpf.RetConstant{}: false,
380
381 bpf.StoreScratch{Src: bpf.RegA, N: 3}: true,
382 bpf.StoreScratch{Src: bpf.RegX, N: 3}: true,
383
384 bpf.TAX{}: false,
385 bpf.TXA{}: false,
386 }, func(insn bpf.Instruction) bool {
387 return memWrites(insn).scratch[3]
388 })
389 }
390
391 func checkMemoryStatus(t *testing.T, expected map[bpf.Instruction]bool, test func(bpf.Instruction) bool) {
392 t.Helper()
393
394 for insn, value := range expected {
395 if test(insn) != value {
396 t.Fatalf("Instruction %v expected %v got %v", insn, value, test(insn))
397 }
398 }
399 }
400
401 // reg uninitialized and used in one block
402 func TestUninitializedReg(t *testing.T) {
403 insns := toInstructions([]bpf.Instruction{
404 // block 0
405 /* 0 */ bpf.RetA{},
406 })
407
408 blocks := mustSplitBlocks(t, 1, insns)
409
410 err := initializeMemory(blocks)
411 if err != nil {
412 t.Fatal(err)
413 }
414
415 matchBlock(t, blocks[0], join(
416 []instruction{{Instruction: bpf.LoadConstant{Dst: bpf.RegA, Val: 0}}},
417 insns,
418 ), nil)
419 }
420
421 // reg initialized in one branch, but not the other
422 func TestPartiallyUninitializedReg(t *testing.T) {
423 insns := toInstructions([]bpf.Instruction{
424 // block 0
425 /* 0 */ bpf.LoadConstant{Dst: bpf.RegA, Val: 3},
426 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 1}, // jump to block 1 or 2
427
428 // block 1
429 /* 2 */ bpf.TAX{}, // initialize RegX
430 // fall through to block 2
431
432 // block 2
433 /* 3 */ bpf.TXA{}, // RegX used potentially uninitialized
434 /* 4 */ bpf.RetA{},
435 })
436
437 blocks := mustSplitBlocks(t, 3, insns)
438
439 err := initializeMemory(blocks)
440 if err != nil {
441 t.Fatal(err)
442 }
443
444 matchBlock(t, blocks[0], join(
445 []instruction{{Instruction: bpf.LoadConstant{Dst: bpf.RegX, Val: 0}}},
446 insns[:2],
447 ), nil)
448 matchBlock(t, blocks[1], insns[2:3], nil)
449 matchBlock(t, blocks[2], insns[3:], nil)
450 }
451
452 // scratch reg uninitialized and used in one block
453 func TestUninitializedScratch(t *testing.T) {
454 insns := toInstructions([]bpf.Instruction{
455 // block 0
456 /* 0 */ bpf.LoadScratch{Dst: bpf.RegA, N: 2},
457 /* 1 */ bpf.RetA{},
458 })
459
460 blocks := mustSplitBlocks(t, 1, insns)
461 requireError(t, initializeMemory(blocks), "instruction 0: ld M[2] reads potentially uninitialized scratch register M[2]")
462 }
463
464 // scratch reg initialized in one branch, but not the other
465 func TestPartiallyUninitializedScratch(t *testing.T) {
466 insns := toInstructions([]bpf.Instruction{
467 // block 0
468 /* 0 */ bpf.LoadConstant{Dst: bpf.RegA, Val: 3},
469 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 1}, // jump to block 1 or 2
470
471 // block 1
472 /* 2 */ bpf.StoreScratch{Src: bpf.RegA, N: 5}, // initialize m[2]
473 // fall through to block 2
474
475 // block 2
476 /* 3 */ bpf.LoadScratch{Dst: bpf.RegA, N: 5},
477 /* 4 */ bpf.RetA{},
478 })
479
480 blocks := mustSplitBlocks(t, 3, insns)
481 requireError(t, initializeMemory(blocks), "instruction 3: ld M[5] reads potentially uninitialized scratch register M[5]")
482 }
483
484 // Test block splitting
485 func TestBlocksJump(t *testing.T) {
486 insns := toInstructions([]bpf.Instruction{
487 /* 0 */ bpf.LoadConstant{Dst: bpf.RegX, Val: 3},
488 /* 1 */ bpf.Jump{Skip: 1},
489 /* 2 */ bpf.RetConstant{Val: 0}, // unreachable
490 /* 3 */ bpf.RetConstant{Val: 1},
491 })
492
493 blocks := mustSplitBlocks(t, 2, insns)
494
495 // Unreachable code will never make it into a block
496 matchBlock(t, blocks[0], insns[:2], map[pos]*block{3: blocks[1]})
497 matchBlock(t, blocks[1], insns[3:], map[pos]*block{})
498 }
499
500 func TestBlocksJumpIf(t *testing.T) {
501 insns := toInstructions([]bpf.Instruction{
502 /* 0 */ bpf.LoadConstant{Dst: bpf.RegA, Val: 0},
503 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 1, SkipFalse: 0},
504 /* 2 */ bpf.RetConstant{Val: 0},
505 /* 3 */ bpf.RetConstant{Val: 1},
506 })
507
508 blocks := mustSplitBlocks(t, 3, insns)
509
510 matchBlock(t, blocks[0], insns[0:2], map[pos]*block{2: blocks[1], 3: blocks[2]})
511 matchBlock(t, blocks[1], insns[2:3], map[pos]*block{})
512 matchBlock(t, blocks[2], insns[3:4], map[pos]*block{})
513 }
514
515 func TestBlocksJumpIfX(t *testing.T) {
516 insns := toInstructions([]bpf.Instruction{
517 /* 0 */ bpf.LoadConstant{Dst: bpf.RegA, Val: 0},
518 /* 1 */ bpf.LoadConstant{Dst: bpf.RegX, Val: 3},
519 /* 2 */ bpf.JumpIfX{Cond: bpf.JumpEqual, SkipTrue: 1, SkipFalse: 0},
520 /* 3 */ bpf.RetConstant{Val: 0},
521 /* 4 */ bpf.RetConstant{Val: 1},
522 })
523
524 blocks := mustSplitBlocks(t, 3, insns)
525
526 matchBlock(t, blocks[0], insns[0:3], map[pos]*block{3: blocks[1], 4: blocks[2]})
527 matchBlock(t, blocks[1], insns[3:4], map[pos]*block{})
528 matchBlock(t, blocks[2], insns[4:5], map[pos]*block{})
529 }
530
531 // Division by constant 0
532 func TestDivisionByZeroImm(t *testing.T) {
533 test := func(t *testing.T, op bpf.ALUOp) {
534 t.Helper()
535
536 blocks := mustSplitBlocks(t, 1, toInstructions([]bpf.Instruction{
537 bpf.ALUOpConstant{Op: op, Val: 0},
538 bpf.RetConstant{},
539 }))
540
541 requireError(t, addDivideByZeroGuards(blocks), "divides by 0")
542 }
543
544 test(t, bpf.ALUOpDiv)
545 test(t, bpf.ALUOpMod)
546 }
547
548 // Division by RegX
549 func TestDivisionByZeroX(t *testing.T) {
550 test := func(t *testing.T, op bpf.ALUOp) {
551 t.Helper()
552
553 insns := toInstructions([]bpf.Instruction{
554 bpf.LoadAbsolute{Size: 1, Off: 0},
555 bpf.TXA{},
556 bpf.ALUOpX{Op: op},
557 bpf.RetConstant{},
558 })
559
560 blocks := mustSplitBlocks(t, 1, insns)
561
562 err := addDivideByZeroGuards(blocks)
563 if err != nil {
564 t.Fatal(err)
565 }
566
567 matchBlock(t, blocks[0], join(
568 insns[:2],
569 []instruction{{Instruction: checkXNotZero{}}},
570 insns[2:],
571 ), nil)
572 }
573
574 test(t, bpf.ALUOpDiv)
575 test(t, bpf.ALUOpMod)
576 }
577
578 // Division by RegX twice in same block
579 func TestDivisionByZeroXTwice(t *testing.T) {
580 test := func(t *testing.T, op bpf.ALUOp) {
581 t.Helper()
582
583 insns := toInstructions([]bpf.Instruction{
584 bpf.LoadAbsolute{Size: 1, Off: 0},
585 bpf.TXA{},
586 bpf.ALUOpX{Op: op},
587 bpf.ALUOpX{Op: op},
588 bpf.RetConstant{},
589 })
590
591 blocks := mustSplitBlocks(t, 1, insns)
592
593 err := addDivideByZeroGuards(blocks)
594 if err != nil {
595 t.Fatal(err)
596 }
597
598 matchBlock(t, blocks[0], join(
599 insns[:2],
600 []instruction{{Instruction: checkXNotZero{}}},
601 insns[2:],
602 ), nil)
603 }
604
605 test(t, bpf.ALUOpDiv)
606 test(t, bpf.ALUOpMod)
607 }
608
609 // Division by RegX after RegX clobbered
610 func TestDivisionByZeroXConstant(t *testing.T) {
611 test := func(t *testing.T, op bpf.ALUOp) {
612 t.Helper()
613
614 insns := toInstructions([]bpf.Instruction{
615 bpf.LoadAbsolute{Size: 1, Off: 0},
616 bpf.TXA{},
617 bpf.ALUOpX{Op: op},
618
619 bpf.LoadConstant{Dst: bpf.RegX}, // Clobber X
620 bpf.ALUOpX{Op: op},
621
622 bpf.RetConstant{},
623 })
624
625 blocks := mustSplitBlocks(t, 1, insns)
626
627 err := addDivideByZeroGuards(blocks)
628 if err != nil {
629 t.Fatal(err)
630 }
631
632 matchBlock(t, blocks[0], join(
633 insns[:2],
634 []instruction{{Instruction: checkXNotZero{}}},
635 insns[2:4],
636 []instruction{{Instruction: checkXNotZero{}}},
637 insns[4:],
638 ), nil)
639 }
640
641 test(t, bpf.ALUOpDiv)
642 test(t, bpf.ALUOpMod)
643 }
644
645 func TestDivisionByZeroXMemShift(t *testing.T) {
646 test := func(t *testing.T, op bpf.ALUOp) {
647 t.Helper()
648
649 insns := toInstructions([]bpf.Instruction{
650 bpf.LoadAbsolute{Size: 1, Off: 0},
651 bpf.TXA{},
652 bpf.ALUOpX{Op: op},
653
654 bpf.LoadMemShift{Off: 2}, // Clobber X
655 bpf.ALUOpX{Op: op},
656
657 bpf.RetConstant{},
658 })
659
660 blocks := mustSplitBlocks(t, 1, insns)
661
662 err := addDivideByZeroGuards(blocks)
663 if err != nil {
664 t.Fatal(err)
665 }
666
667 matchBlock(t, blocks[0], join(
668 insns[:2],
669 []instruction{{Instruction: checkXNotZero{}}},
670 insns[2:4],
671 []instruction{{Instruction: checkXNotZero{}}},
672 insns[4:],
673 ), nil)
674 }
675
676 test(t, bpf.ALUOpDiv)
677 test(t, bpf.ALUOpMod)
678 }
679
680 func TestDivisionByZeroXTXA(t *testing.T) {
681 test := func(t *testing.T, op bpf.ALUOp) {
682 t.Helper()
683
684 insns := toInstructions([]bpf.Instruction{
685 bpf.LoadAbsolute{Size: 1, Off: 0},
686 bpf.TXA{},
687 bpf.ALUOpX{Op: op},
688
689 bpf.TAX{}, // Clobber X
690 bpf.ALUOpX{Op: op},
691
692 bpf.RetConstant{},
693 })
694
695 blocks := mustSplitBlocks(t, 1, insns)
696
697 err := addDivideByZeroGuards(blocks)
698 if err != nil {
699 t.Fatal(err)
700 }
701
702 matchBlock(t, blocks[0], join(
703 insns[:2],
704 []instruction{{Instruction: checkXNotZero{}}},
705 insns[2:4],
706 []instruction{{Instruction: checkXNotZero{}}},
707 insns[4:],
708 ), nil)
709 }
710
711 test(t, bpf.ALUOpDiv)
712 test(t, bpf.ALUOpMod)
713 }
714
715 // Check we use parent guards
716 func TestDivisionByZeroParentsOK(t *testing.T) {
717 test := func(t *testing.T, op bpf.ALUOp) {
718 t.Helper()
719
720 insns := toInstructions([]bpf.Instruction{
721 // block 0
722 /* 0 */ bpf.LoadAbsolute{Size: 1, Off: 0},
723 /* 1 */ bpf.TXA{},
724 /* 2 */ bpf.ALUOpX{Op: op},
725 /* 3 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 2}, // jump to block 1 or 2
726
727 // block 1
728 /* 4 */ bpf.LoadAbsolute{Size: 1, Off: 1},
729 /* 5 */ bpf.Jump{Skip: 1}, // jump to block 3
730
731 // block 2
732 /* 6 */ bpf.LoadAbsolute{Size: 1, Off: 2},
733 // fall through to block 3
734
735 // block 3
736 /* 7 */ bpf.ALUOpX{Op: op},
737 /* 8 */ bpf.RetConstant{},
738 })
739
740 blocks := mustSplitBlocks(t, 4, insns)
741
742 err := addDivideByZeroGuards(blocks)
743 if err != nil {
744 t.Fatal(err)
745 }
746
747 matchBlock(t, blocks[0], join(
748 insns[:2],
749 []instruction{{Instruction: checkXNotZero{}}},
750 insns[2:4],
751 ), nil)
752 matchBlock(t, blocks[1], insns[4:6], nil)
753 matchBlock(t, blocks[2], insns[6:7], nil)
754 matchBlock(t, blocks[3], insns[7:], nil)
755 }
756
757 test(t, bpf.ALUOpDiv)
758 test(t, bpf.ALUOpMod)
759 }
760
761 // Check we add new guards with partial parent guards
762 func TestDivisionByZeroParentsNOK(t *testing.T) {
763 test := func(t *testing.T, op bpf.ALUOp) {
764 t.Helper()
765
766 insns := toInstructions([]bpf.Instruction{
767 // block 0
768 /* 0 */ bpf.LoadAbsolute{Size: 1, Off: 0},
769 /* 1 */ bpf.TXA{},
770 /* 2 */ bpf.ALUOpX{Op: op},
771 /* 3 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 2}, // jump to block 1 or 2
772
773 // block 1
774 /* 4 */ bpf.LoadMemShift{Off: 1}, // clobber X
775 /* 5 */ bpf.Jump{Skip: 1}, // jump to block 3
776
777 // block 2
778 /* 6 */ bpf.LoadAbsolute{Size: 1, Off: 2},
779 // fall through to block 3
780
781 // block 3
782 /* 7 */ bpf.ALUOpX{Op: op},
783 /* 8 */ bpf.RetConstant{},
784 })
785
786 blocks := mustSplitBlocks(t, 4, insns)
787
788 err := addDivideByZeroGuards(blocks)
789 if err != nil {
790 t.Fatal(err)
791 }
792
793 matchBlock(t, blocks[0], join(
794 insns[:2],
795 []instruction{{Instruction: checkXNotZero{}}},
796 insns[2:4],
797 ), nil)
798 matchBlock(t, blocks[1], insns[4:6], nil)
799 matchBlock(t, blocks[2], insns[6:7], nil)
800 matchBlock(t, blocks[3], join(
801 []instruction{{Instruction: checkXNotZero{}}},
802 insns[7:],
803 ), nil)
804 }
805
806 test(t, bpf.ALUOpDiv)
807 test(t, bpf.ALUOpMod)
808 }
809
810 func TestRewriteLargePacketOffsets(t *testing.T) {
811 testOK := func(t *testing.T, load bpf.Instruction, packetOff uint16) {
812 t.Helper()
813
814 insns := toInstructions([]bpf.Instruction{
815 load,
816 bpf.RetA{},
817 })
818
819 blocks := mustSplitBlocks(t, 1, insns)
820 rewriteLargePacketOffsets(&blocks, compileOpts{
821 packetStartMaxOffset: packetOff,
822 })
823
824 matchBlock(t, blocks[0], insns, nil)
825 }
826
827 testOOB := func(t *testing.T, load bpf.Instruction, packetOff uint16) {
828 t.Helper()
829
830 insns := toInstructions([]bpf.Instruction{
831 load,
832 bpf.RetA{},
833 })
834
835 blocks := mustSplitBlocks(t, 1, insns)
836 rewriteLargePacketOffsets(&blocks, compileOpts{
837 packetStartMaxOffset: packetOff,
838 })
839
840 matchBlock(t, blocks[0], []instruction{
841 {Instruction: bpf.RetConstant{}},
842 }, nil)
843 }
844
845 testOK(t, bpf.LoadAbsolute{Size: 1, Off: 65534}, 0)
846 testOOB(t, bpf.LoadAbsolute{Size: 1, Off: 65535}, 0)
847 testOK(t, bpf.LoadAbsolute{Size: 2, Off: 65533}, 0)
848 testOOB(t, bpf.LoadAbsolute{Size: 2, Off: 65534}, 0)
849 testOK(t, bpf.LoadAbsolute{Size: 4, Off: 65531}, 0)
850 testOOB(t, bpf.LoadAbsolute{Size: 4, Off: 65532}, 0)
851
852 testOK(t, bpf.LoadMemShift{Off: 65534}, 0)
853 testOOB(t, bpf.LoadMemShift{Off: 65535}, 0)
854
855 // With packet offsets.
856 testOK(t, bpf.LoadAbsolute{Size: 1, Off: 65530}, 4)
857 testOOB(t, bpf.LoadAbsolute{Size: 1, Off: 65531}, 4)
858
859 testOK(t, bpf.LoadMemShift{Off: 65530}, 4)
860 testOOB(t, bpf.LoadMemShift{Off: 65531}, 4)
861 }
862
863 // Test unreachable blocks due to large packet offsets are removed.
864 func TestRewriteLargePacketOffsetsDeadBlock(t *testing.T) {
865 filter := []bpf.Instruction{
866 // block 0
867 /* 0 */ bpf.LoadAbsolute{Size: 4, Off: 2},
868 /* 1 */ bpf.JumpIf{Cond: bpf.JumpGreaterThan, Val: 2, SkipTrue: 6}, // jump to block 1 or 6
869
870 // block 1
871 /* 2 */ bpf.JumpIf{Cond: bpf.JumpLessThan, Val: 2, SkipTrue: 3}, // jump to block 2 or 3
872
873 // block 2
874 /* 3 */ bpf.LoadAbsolute{Size: 1, Off: 65598},
875 /* 4 */ bpf.ALUOpConstant{Op: bpf.ALUOpMul, Val: 4},
876 /* 5 */ bpf.Jump{Skip: 1}, // jump to block 4
877
878 // block 3
879 /* 6 */ bpf.LoadAbsolute{Size: 4, Off: 65532},
880
881 // block 4
882 /* 7 */ bpf.ALUOpConstant{Op: bpf.ALUOpAdd, Val: 2},
883
884 // block 5
885 /* 8 */ bpf.RetA{},
886 }
887 insns := toInstructions(filter)
888
889 blocks := mustSplitBlocks(t, 6, insns)
890 rewriteLargePacketOffsets(&blocks, compileOpts{})
891 if len(blocks) != 5 {
892 t.Fatalf("expected 5 blocks, got %v", blocks)
893 }
894
895 matchBlock(t, blocks[0], insns[0:2], nil)
896 matchBlock(t, blocks[1], insns[2:3], nil)
897 matchBlock(t, blocks[2], []instruction{
898 {Instruction: bpf.RetConstant{}},
899 }, nil)
900 matchBlock(t, blocks[3], []instruction{
901 {Instruction: bpf.RetConstant{}},
902 }, nil)
903 // block 4 is unreachable and removed, block 5 replaces it
904 matchBlock(t, blocks[4], insns[8:], nil)
905
906 // Make sure this is accepted by the verifier.
907 checkBackends(t, filter, []byte{}, noMatch)
908 }
909
910 // Test absolute guards
911 func TestAbsoluteGuardSize(t *testing.T) {
912 insns := toInstructions([]bpf.Instruction{
913 bpf.LoadAbsolute{Size: 4, Off: 10}, // guard 14
914 bpf.LoadAbsolute{Size: 1, Off: 10}, // guard 11
915 bpf.RetConstant{},
916 })
917
918 blocks := mustSplitBlocks(t, 1, insns)
919
920 addAbsolutePacketGuards(blocks)
921
922 matchBlock(t, blocks[0], join(
923 []instruction{{Instruction: packetGuardAbsolute{end: 14}}},
924 insns,
925 ), nil)
926 }
927
928 // Check we don't add a guard if there are no packet loads
929 func TestNoAbsoluteGuard(t *testing.T) {
930 insns := toInstructions([]bpf.Instruction{
931 bpf.LoadConstant{Dst: bpf.RegA, Val: 23},
932 bpf.RetA{},
933 })
934
935 blocks := mustSplitBlocks(t, 1, insns)
936
937 addAbsolutePacketGuards(blocks)
938
939 matchBlock(t, blocks[0], insns, nil)
940 }
941
942 // Check we use parent guards if they're big enough
943 func TestAbsoluteGuardParentsOK(t *testing.T) {
944 insns := toInstructions([]bpf.Instruction{
945 // block 0
946 /* 0 */ bpf.LoadAbsolute{Size: 4, Off: 10}, // guard 14
947 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 2}, // jump to block 1 or 2
948
949 // block 1
950 /* 2 */ bpf.LoadAbsolute{Size: 4, Off: 10}, // guard 14
951 /* 3 */ bpf.Jump{Skip: 1}, // jump to block 3
952
953 // block 2
954 /* 4 */ bpf.LoadAbsolute{Size: 2, Off: 8}, // guard 10
955 // fall through to block 3
956
957 // block 3
958 /* 5 */ bpf.LoadAbsolute{Size: 1, Off: 9}, // guard 10
959 /* 6 */ bpf.RetA{},
960 })
961
962 blocks := mustSplitBlocks(t, 4, insns)
963
964 addAbsolutePacketGuards(blocks)
965
966 matchBlock(t, blocks[0], join(
967 []instruction{{Instruction: packetGuardAbsolute{end: 14}}},
968 insns[:2],
969 ), nil)
970 matchBlock(t, blocks[1], insns[2:4], nil)
971 matchBlock(t, blocks[2], insns[4:5], nil)
972 matchBlock(t, blocks[3], insns[5:], nil)
973 }
974
975 // Check the parent guard is extended to cover children that always return no match
976 func TestAbsoluteGuardParentNoMatch(t *testing.T) {
977 insns := toInstructions([]bpf.Instruction{
978 // block 0
979 /* 0 */ bpf.LoadAbsolute{Size: 4, Off: 10}, // guard 14
980 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 3}, // jump to block 1 or 3
981
982 // block 1
983 /* 2 */ bpf.LoadAbsolute{Size: 4, Off: 12}, // guard 16
984 /* 3 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 1}, // jump to block 2 or 3
985
986 // block 2
987 /* 4 */ bpf.RetA{}, // potential match
988
989 // block 3
990 /* 5 */ bpf.RetConstant{}, // no match
991 })
992
993 blocks := mustSplitBlocks(t, 4, insns)
994
995 addAbsolutePacketGuards(blocks)
996
997 matchBlock(t, blocks[0], join(
998 []instruction{{Instruction: packetGuardAbsolute{end: 16}}},
999 insns[:2],
1000 ), nil)
1001 matchBlock(t, blocks[1], insns[2:4], nil)
1002 matchBlock(t, blocks[2], insns[4:5], nil)
1003 matchBlock(t, blocks[3], insns[5:], nil)
1004 }
1005
1006 // Check the parent guard is extended to cover indirect children that always return no match
1007 func TestAbsoluteGuardParentDeepNoMatch(t *testing.T) {
1008 insns := toInstructions([]bpf.Instruction{
1009 // block 0
1010 /* 0 */ bpf.LoadAbsolute{Size: 4, Off: 10}, // guard 14
1011 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 5}, // jump to block 1 or 4
1012
1013 // block 1
1014 /* 2 */ bpf.LoadAbsolute{Size: 4, Off: 12}, // guard 16
1015 /* 3 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 3}, // jump to block 2 or 4
1016
1017 // block 2
1018 /* 4 */ bpf.LoadAbsolute{Size: 4, Off: 14}, // guard 18
1019 /* 5 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 1}, // jump to block 3 or 4
1020
1021 // block 3
1022 /* 6 */ bpf.RetA{}, // potential match
1023
1024 // block 4
1025 /* 7 */ bpf.RetConstant{}, // no match
1026 })
1027
1028 blocks := mustSplitBlocks(t, 5, insns)
1029
1030 addAbsolutePacketGuards(blocks)
1031
1032 matchBlock(t, blocks[0], join(
1033 []instruction{{Instruction: packetGuardAbsolute{end: 18}}},
1034 insns[:2],
1035 ), nil)
1036 matchBlock(t, blocks[1], insns[2:4], nil)
1037 matchBlock(t, blocks[2], insns[4:6], nil)
1038 matchBlock(t, blocks[3], insns[6:7], nil)
1039 matchBlock(t, blocks[4], insns[7:], nil)
1040 }
1041
1042 // Check the parent guard isn't extended to cover children that could match
1043 func TestAbsoluteGuardParentMatch(t *testing.T) {
1044 insns := toInstructions([]bpf.Instruction{
1045 // block 0
1046 /* 0 */ bpf.LoadAbsolute{Size: 4, Off: 10}, // guard 14
1047 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 2}, // jump to block 1 or 2
1048
1049 // block 1
1050 /* 2 */ bpf.LoadAbsolute{Size: 4, Off: 11}, // guard 15
1051 /* 3 */ bpf.RetA{}, // potential match
1052
1053 // block 2
1054 /* 4 */ bpf.LoadAbsolute{Size: 1, Off: 15}, // guard 16
1055 /* 5 */ bpf.RetConstant{}, // no match
1056 })
1057
1058 blocks := mustSplitBlocks(t, 3, insns)
1059
1060 addAbsolutePacketGuards(blocks)
1061
1062 matchBlock(t, blocks[0], join(
1063 []instruction{{Instruction: packetGuardAbsolute{end: 15}}},
1064 insns[:2],
1065 ), nil)
1066 matchBlock(t, blocks[1], insns[2:4], nil)
1067 matchBlock(t, blocks[2], join(
1068 []instruction{{Instruction: packetGuardAbsolute{end: 16}}},
1069 insns[4:],
1070 ), nil)
1071 }
1072
1073 func TestIndirectGuardSize(t *testing.T) {
1074 insns := toInstructions([]bpf.Instruction{
1075 bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1076 bpf.LoadIndirect{Size: 1, Off: 8}, // guard [8:9]
1077 bpf.RetConstant{},
1078 })
1079
1080 blocks := mustSplitBlocks(t, 1, insns)
1081
1082 addIndirectPacketGuards(blocks, compileOpts{})
1083
1084 matchBlock(t, blocks[0], join(
1085 []instruction{{Instruction: packetGuardIndirect{start: 8, end: 14}}},
1086 insns,
1087 ), nil)
1088 }
1089
1090 // Check we don't add a guard if there are no packet loads
1091 func TestNoIndirectGuard(t *testing.T) {
1092 insns := toInstructions([]bpf.Instruction{
1093 bpf.LoadConstant{Dst: bpf.RegA, Val: 23},
1094 bpf.RetA{},
1095 })
1096
1097 blocks := mustSplitBlocks(t, 1, insns)
1098
1099 addIndirectPacketGuards(blocks, compileOpts{})
1100
1101 matchBlock(t, blocks[0], insns, nil)
1102 }
1103
1104 // Check we add new guards if current is not big enough due to RegX clobber
1105 func TestIndirectGuardClobber(t *testing.T) {
1106 check := func(clobber bpf.Instruction) func(t *testing.T) {
1107 return func(t *testing.T) {
1108 insns := toInstructions([]bpf.Instruction{
1109 bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1110 clobber, // clobber X, packet guard no longer valid
1111 bpf.LoadIndirect{Size: 2, Off: 8}, // guard [8:10]
1112 bpf.RetA{},
1113 })
1114
1115 blocks := mustSplitBlocks(t, 1, insns)
1116
1117 addIndirectPacketGuards(blocks, compileOpts{})
1118
1119 matchBlock(t, blocks[0], join(
1120 []instruction{{Instruction: packetGuardIndirect{start: 10, end: 14}}},
1121 insns[:2],
1122 []instruction{{Instruction: packetGuardIndirect{start: 8, end: 10}}},
1123 insns[2:],
1124 ), nil)
1125 }
1126 }
1127
1128 t.Run("constant", check(bpf.LoadConstant{Dst: bpf.RegX}))
1129 t.Run("scratch", check(bpf.LoadScratch{Dst: bpf.RegX}))
1130 t.Run("memshift", check(bpf.LoadMemShift{Off: 2}))
1131 }
1132
1133 // #20: we didn't always emit packet guards for the last instruction of a block.
1134 func TestIndirectGuardClobberLast(t *testing.T) {
1135 insns := toInstructions([]bpf.Instruction{
1136 // block 0
1137 /* 0 */ bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1138 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 1}, // jump to block 1 or 2
1139
1140 // block 1
1141 /* 2 */ bpf.LoadConstant{Dst: bpf.RegX, Val: 23},
1142 // fall through to block 2
1143
1144 // block 2
1145 /* 3 */ bpf.LoadIndirect{Size: 1, Off: 10}, // guard [10:11]
1146 /* 4 */ bpf.TXA{},
1147 /* 5 */ bpf.RetA{},
1148 })
1149
1150 blocks := mustSplitBlocks(t, 3, insns)
1151
1152 addIndirectPacketGuards(blocks, compileOpts{})
1153
1154 matchBlock(t, blocks[0], join(
1155 []instruction{{Instruction: packetGuardIndirect{start: 10, end: 14}}},
1156 insns[:2],
1157 ), nil)
1158 matchBlock(t, blocks[1], insns[2:3], nil)
1159 matchBlock(t, blocks[2], join(
1160 []instruction{{Instruction: packetGuardIndirect{start: 10, end: 11}}},
1161 insns[3:],
1162 ), nil)
1163 }
1164
1165 // Check we use parent guards if they're big enough.
1166 func TestIndirectGuardParentsOK(t *testing.T) {
1167 insns := toInstructions([]bpf.Instruction{
1168 // block 0
1169 /* 0 */ bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1170 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 2}, // jump to block 1 or 2
1171
1172 // block 1
1173 /* 2 */ bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1174 /* 3 */ bpf.Jump{Skip: 1}, // jump to block 3
1175
1176 // block 2
1177 /* 4 */ bpf.LoadIndirect{Size: 2, Off: 12}, // guard [12:14]
1178 // fall through to block 3
1179
1180 // block 3
1181 /* 5 */ bpf.LoadIndirect{Size: 1, Off: 10}, // guard [10:11]
1182 /* 6 */ bpf.RetA{},
1183 })
1184
1185 blocks := mustSplitBlocks(t, 4, insns)
1186
1187 addIndirectPacketGuards(blocks, compileOpts{})
1188
1189 matchBlock(t, blocks[0], join(
1190 []instruction{{Instruction: packetGuardIndirect{start: 10, end: 14}}},
1191 insns[:2],
1192 ), nil)
1193 matchBlock(t, blocks[1], insns[2:4], nil)
1194 matchBlock(t, blocks[2], insns[4:5], nil)
1195 matchBlock(t, blocks[3], insns[5:], nil)
1196 }
1197
1198 // Check the parent guard is extended to cover children that always return no match
1199 func TestIndirectGuardParentNoMatch(t *testing.T) {
1200 insns := toInstructions([]bpf.Instruction{
1201 // block 0
1202 /* 0 */ bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1203 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 3}, // jump to block 1 or 3
1204
1205 // block 1
1206 /* 2 */ bpf.LoadIndirect{Size: 4, Off: 12}, // guard [12:16]
1207 /* 3 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 1}, // jump to block 2 or 3
1208
1209 // block 2
1210 /* 4 */ bpf.RetA{}, // potential match
1211
1212 // block 3
1213 /* 5 */ bpf.RetConstant{}, // no match
1214 })
1215
1216 blocks := mustSplitBlocks(t, 4, insns)
1217
1218 addIndirectPacketGuards(blocks, compileOpts{})
1219
1220 matchBlock(t, blocks[0], join(
1221 []instruction{{Instruction: packetGuardIndirect{start: 10, end: 16}}},
1222 insns[:2],
1223 ), nil)
1224 matchBlock(t, blocks[1], insns[2:4], nil)
1225 matchBlock(t, blocks[2], insns[4:5], nil)
1226 matchBlock(t, blocks[3], insns[5:], nil)
1227 }
1228
1229 // Check the parent guard is extended to cover indirect children that always return no match
1230 func TestIndirectGuardParentDeepNoMatch(t *testing.T) {
1231 insns := toInstructions([]bpf.Instruction{
1232 // block 0
1233 /* 0 */ bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1234 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 5}, // jump to block 1 or 4
1235
1236 // block 1
1237 /* 2 */ bpf.LoadIndirect{Size: 4, Off: 12}, // guard [12:16]
1238 /* 3 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 3}, // jump to block 2 or 4
1239
1240 // block 2
1241 /* 4 */ bpf.LoadIndirect{Size: 4, Off: 14}, // guard [14:18]
1242 /* 5 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 1}, // jump to block 3 or 4
1243
1244 // block 3
1245 /* 6 */ bpf.RetA{}, // potential match
1246
1247 // block 4
1248 /* 7 */ bpf.RetConstant{}, // no match
1249 })
1250
1251 blocks := mustSplitBlocks(t, 5, insns)
1252
1253 addIndirectPacketGuards(blocks, compileOpts{})
1254
1255 matchBlock(t, blocks[0], join(
1256 []instruction{{Instruction: packetGuardIndirect{start: 10, end: 18}}},
1257 insns[:2],
1258 ), nil)
1259 matchBlock(t, blocks[1], insns[2:4], nil)
1260 matchBlock(t, blocks[2], insns[4:6], nil)
1261 matchBlock(t, blocks[3], insns[6:7], nil)
1262 matchBlock(t, blocks[4], insns[7:], nil)
1263 }
1264
1265 // Check the parent guard isn't extended to cover children that could match
1266 func TestIndirectGuardParentMatch(t *testing.T) {
1267 insns := toInstructions([]bpf.Instruction{
1268 // block 0
1269 /* 0 */ bpf.LoadIndirect{Size: 2, Off: 10}, // guard [10:12]
1270 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 2}, // jump to block 1 or 2
1271
1272 // block 1
1273 /* 2 */ bpf.LoadIndirect{Size: 4, Off: 9}, // guard [9:13]
1274 /* 3 */ bpf.RetA{}, // potential match
1275
1276 // block 2
1277 /* 4 */ bpf.LoadIndirect{Size: 1, Off: 15}, // guard [15:16]
1278 /* 5 */ bpf.RetConstant{}, // no match
1279 })
1280
1281 blocks := mustSplitBlocks(t, 3, insns)
1282
1283 addIndirectPacketGuards(blocks, compileOpts{})
1284
1285 matchBlock(t, blocks[0], join(
1286 []instruction{{Instruction: packetGuardIndirect{start: 9, end: 13}}},
1287 insns[:2],
1288 ), nil)
1289 matchBlock(t, blocks[1], insns[2:4], nil)
1290 matchBlock(t, blocks[2], join(
1291 []instruction{{Instruction: packetGuardIndirect{start: 9, end: 16}}}, // Extra guard info is preserved
1292 insns[4:],
1293 ), nil)
1294 }
1295
1296 // Check we add new guards if one of the parent guards is not big enough due to RegX clobber
1297 func TestIndirectGuardParentClobber(t *testing.T) {
1298 check := func(clobber bpf.Instruction) func(t *testing.T) {
1299 return func(t *testing.T) {
1300 insns := toInstructions([]bpf.Instruction{
1301 // block 0
1302 /* 0 */ bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1303 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 3}, // jump to block 1 or 2
1304
1305 // block 1
1306 /* 2 */ bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1307 /* 3 */ clobber, // clobber X, packet guard no longer valid
1308 /* 4 */ bpf.Jump{Skip: 1}, // jump to block 3
1309
1310 // block 2
1311 /* 5 */ bpf.LoadIndirect{Size: 2, Off: 12}, // guard [12:14]
1312 // fall through to block 3
1313
1314 // block 3
1315 /* 6 */ bpf.LoadIndirect{Size: 1, Off: 1}, // guard [1:2]
1316 /* 7 */ bpf.RetA{},
1317 })
1318
1319 blocks := mustSplitBlocks(t, 4, insns)
1320
1321 addIndirectPacketGuards(blocks, compileOpts{})
1322
1323 matchBlock(t, blocks[0], join(
1324 []instruction{{Instruction: packetGuardIndirect{start: 10, end: 14}}},
1325 insns[:2],
1326 ), nil)
1327 matchBlock(t, blocks[1], insns[2:5], nil)
1328 matchBlock(t, blocks[2], insns[5:6], nil)
1329 matchBlock(t, blocks[3], join(
1330 []instruction{{Instruction: packetGuardIndirect{start: 1, end: 2}}},
1331 insns[6:],
1332 ), nil)
1333 }
1334 }
1335
1336 t.Run("constant", check(bpf.LoadConstant{Dst: bpf.RegX}))
1337 t.Run("scratch", check(bpf.LoadScratch{Dst: bpf.RegX}))
1338 t.Run("memshift", check(bpf.LoadMemShift{Off: 2}))
1339 }
1340
1341 // Check we don't extend guards past RegX clobbers
1342 func TestIndirectGuardExtendClobber(t *testing.T) {
1343 check := func(clobber bpf.Instruction) func(t *testing.T) {
1344 return func(t *testing.T) {
1345 insns := toInstructions([]bpf.Instruction{
1346 // block 0
1347 /* 0 */ bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1348 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 5}, // jump to block 1 or 4
1349
1350 // block 1
1351 /* 2 */ clobber, // clobber X, packet guard no longer valid
1352 /* 3 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 3}, // jump to block 2 or 4
1353
1354 // block 2
1355 /* 4 */ bpf.LoadIndirect{Size: 4, Off: 14}, // guard [14:18]
1356 /* 5 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 1}, // jump to block 3 or 4
1357
1358 // block 3
1359 /* 6 */ bpf.RetA{}, // potential match
1360
1361 // block 4
1362 /* 7 */ bpf.RetConstant{}, // no match
1363 })
1364
1365 blocks := mustSplitBlocks(t, 5, insns)
1366
1367 addIndirectPacketGuards(blocks, compileOpts{})
1368
1369 matchBlock(t, blocks[0], join(
1370 []instruction{{Instruction: packetGuardIndirect{start: 10, end: 14}}},
1371 insns[:2],
1372 ), nil)
1373 matchBlock(t, blocks[1], insns[2:4], nil)
1374 matchBlock(t, blocks[2], join(
1375 []instruction{{Instruction: packetGuardIndirect{start: 14, end: 18}}},
1376 insns[4:6],
1377 ), nil)
1378 matchBlock(t, blocks[3], insns[6:7], nil)
1379 matchBlock(t, blocks[4], insns[7:], nil)
1380 }
1381 }
1382
1383 t.Run("constant", check(bpf.LoadConstant{Dst: bpf.RegX}))
1384 t.Run("scratch", check(bpf.LoadScratch{Dst: bpf.RegX}))
1385 t.Run("memshift", check(bpf.LoadMemShift{Off: 2}))
1386 }
1387
1388 // Check we use new guards if the parent ones aren't big enough
1389 func TestIndirectGuardParentsNotOK(t *testing.T) {
1390 insns := toInstructions([]bpf.Instruction{
1391 // block 0
1392 /* 0 */ bpf.LoadIndirect{Size: 4, Off: 10}, // guard [10:14]
1393 /* 1 */ bpf.JumpIf{Cond: bpf.JumpEqual, Val: 3, SkipTrue: 0, SkipFalse: 2}, // jump to block 1 or 2
1394
1395 // block 1
1396 /* 2 */ bpf.LoadIndirect{Size: 4, Off: 8}, // guard [8:12]
1397 /* 3 */ bpf.Jump{Skip: 1}, // jump to block 3
1398
1399 // block 2
1400 /* 4 */ bpf.LoadIndirect{Size: 2, Off: 13}, // guard [13:15]
1401 // fall through to block 3
1402
1403 // block 3
1404 /* 5 */ bpf.LoadIndirect{Size: 1, Off: 9}, // guard [9:1]
1405 /* 6 */ bpf.RetA{},
1406 })
1407
1408 blocks := mustSplitBlocks(t, 4, insns)
1409
1410 addIndirectPacketGuards(blocks, compileOpts{})
1411
1412 matchBlock(t, blocks[0], join(
1413 []instruction{{Instruction: packetGuardIndirect{start: 9, end: 14}}},
1414 insns[:2],
1415 ), nil)
1416 matchBlock(t, blocks[1], join(
1417 []instruction{{Instruction: packetGuardIndirect{start: 8, end: 14}}},
1418 insns[2:4],
1419 ), nil)
1420 matchBlock(t, blocks[2], join(
1421 []instruction{{Instruction: packetGuardIndirect{start: 9, end: 15}}},
1422 insns[4:5],
1423 ), nil)
1424 matchBlock(t, blocks[3], insns[5:], nil)
1425 }
1426
1427 func join(insns ...[]instruction) []instruction {
1428 res := []instruction{}
1429
1430 for _, insn := range insns {
1431 res = append(res, insn...)
1432 }
1433
1434 return res
1435 }
1436
1437 // matchBlock checks a block has the given instructions and jumps
1438 func matchBlock(t *testing.T, b *block, expected []instruction, jumps map[pos]*block) {
1439 t.Helper()
1440
1441 if !reflect.DeepEqual(expected, b.insns) {
1442 t.Fatalf("expected instructions %v, got %v", expected, b.insns)
1443 }
1444
1445 if jumps != nil && !reflect.DeepEqual(jumps, b.jumps) {
1446 t.Fatalf("expected jumps %v, got %v", jumps, b.jumps)
1447 }
1448 }
1449
1450 func mustSplitBlocks(t *testing.T, blockCount int, insns []instruction) []*block {
1451 blocks, err := splitBlocks(insns)
1452 if err != nil {
1453 t.Fatal("splitBlocks failed:", err)
1454 }
1455
1456 if len(blocks) != blockCount {
1457 t.Fatalf("expected %d blocks got %d", blockCount, len(blocks))
1458 }
1459
1460 return blocks
1461 }