1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 // This is a direct translation of the program in
17 // testdata/all_instructions.txt.
18 var allInstructions = []Instruction{
19 LoadConstant{Dst: RegA, Val: 42},
20 LoadConstant{Dst: RegX, Val: 42},
22 LoadScratch{Dst: RegA, N: 3},
23 LoadScratch{Dst: RegX, N: 3},
25 LoadAbsolute{Off: 42, Size: 1},
26 LoadAbsolute{Off: 42, Size: 2},
27 LoadAbsolute{Off: 42, Size: 4},
29 LoadIndirect{Off: 42, Size: 1},
30 LoadIndirect{Off: 42, Size: 2},
31 LoadIndirect{Off: 42, Size: 4},
33 LoadMemShift{Off: 42},
35 LoadExtension{Num: ExtLen},
36 LoadExtension{Num: ExtProto},
37 LoadExtension{Num: ExtType},
38 LoadExtension{Num: ExtRand},
40 StoreScratch{Src: RegA, N: 3},
41 StoreScratch{Src: RegX, N: 3},
43 ALUOpConstant{Op: ALUOpAdd, Val: 42},
44 ALUOpConstant{Op: ALUOpSub, Val: 42},
45 ALUOpConstant{Op: ALUOpMul, Val: 42},
46 ALUOpConstant{Op: ALUOpDiv, Val: 42},
47 ALUOpConstant{Op: ALUOpOr, Val: 42},
48 ALUOpConstant{Op: ALUOpAnd, Val: 42},
49 ALUOpConstant{Op: ALUOpShiftLeft, Val: 42},
50 ALUOpConstant{Op: ALUOpShiftRight, Val: 42},
51 ALUOpConstant{Op: ALUOpMod, Val: 42},
52 ALUOpConstant{Op: ALUOpXor, Val: 42},
60 ALUOpX{Op: ALUOpShiftLeft},
61 ALUOpX{Op: ALUOpShiftRight},
68 JumpIf{Cond: JumpEqual, Val: 42, SkipTrue: 8, SkipFalse: 9},
69 JumpIf{Cond: JumpNotEqual, Val: 42, SkipTrue: 8},
70 JumpIf{Cond: JumpLessThan, Val: 42, SkipTrue: 7},
71 JumpIf{Cond: JumpLessOrEqual, Val: 42, SkipTrue: 6},
72 JumpIf{Cond: JumpGreaterThan, Val: 42, SkipTrue: 4, SkipFalse: 5},
73 JumpIf{Cond: JumpGreaterOrEqual, Val: 42, SkipTrue: 3, SkipFalse: 4},
74 JumpIf{Cond: JumpBitsSet, Val: 42, SkipTrue: 2, SkipFalse: 3},
82 var allInstructionsExpected = "testdata/all_instructions.bpf"
84 // Check that we produce the same output as the canonical bpf_asm
86 func TestInterop(t *testing.T) {
87 out, err := Assemble(allInstructions)
89 t.Fatalf("assembly of allInstructions program failed: %s", err)
91 t.Logf("Assembled program is %d instructions long", len(out))
93 bs, err := ioutil.ReadFile(allInstructionsExpected)
95 t.Fatalf("reading %s: %s", allInstructionsExpected, err)
97 // First statement is the number of statements, last statement is
98 // empty. We just ignore both and rely on slice length.
99 stmts := strings.Split(string(bs), ",")
100 if len(stmts)-2 != len(out) {
101 t.Fatalf("test program lengths don't match: %s has %d, Go implementation has %d", allInstructionsExpected, len(stmts)-2, len(allInstructions))
104 for i, stmt := range stmts[1 : len(stmts)-2] {
105 nums := strings.Split(stmt, " ")
107 t.Fatalf("malformed instruction %d in %s: %s", i+1, allInstructionsExpected, stmt)
112 op, err := strconv.ParseUint(nums[0], 10, 16)
114 t.Fatalf("malformed opcode %s in instruction %d of %s", nums[0], i+1, allInstructionsExpected)
116 if actual.Op != uint16(op) {
117 t.Errorf("opcode mismatch on instruction %d (%#v): got 0x%02x, want 0x%02x", i+1, allInstructions[i], actual.Op, op)
120 jt, err := strconv.ParseUint(nums[1], 10, 8)
122 t.Fatalf("malformed jt offset %s in instruction %d of %s", nums[1], i+1, allInstructionsExpected)
124 if actual.Jt != uint8(jt) {
125 t.Errorf("jt mismatch on instruction %d (%#v): got %d, want %d", i+1, allInstructions[i], actual.Jt, jt)
128 jf, err := strconv.ParseUint(nums[2], 10, 8)
130 t.Fatalf("malformed jf offset %s in instruction %d of %s", nums[2], i+1, allInstructionsExpected)
132 if actual.Jf != uint8(jf) {
133 t.Errorf("jf mismatch on instruction %d (%#v): got %d, want %d", i+1, allInstructions[i], actual.Jf, jf)
136 k, err := strconv.ParseUint(nums[3], 10, 32)
138 t.Fatalf("malformed constant %s in instruction %d of %s", nums[3], i+1, allInstructionsExpected)
140 if actual.K != uint32(k) {
141 t.Errorf("constant mismatch on instruction %d (%#v): got %d, want %d", i+1, allInstructions[i], actual.K, k)
146 // Check that assembly and disassembly match each other.
147 func TestAsmDisasm(t *testing.T) {
148 prog1, err := Assemble(allInstructions)
150 t.Fatalf("assembly of allInstructions program failed: %s", err)
152 t.Logf("Assembled program is %d instructions long", len(prog1))
154 got, allDecoded := Disassemble(prog1)
156 t.Errorf("Disassemble(Assemble(allInstructions)) produced unrecognized instructions:")
157 for i, inst := range got {
158 if r, ok := inst.(RawInstruction); ok {
159 t.Logf(" insn %d, %#v --> %#v", i+1, allInstructions[i], r)
164 if len(allInstructions) != len(got) {
165 t.Fatalf("disassembly changed program size: %d insns before, %d insns after", len(allInstructions), len(got))
167 if !reflect.DeepEqual(allInstructions, got) {
168 t.Errorf("program mutated by disassembly:")
170 if !reflect.DeepEqual(allInstructions[i], got[i]) {
171 t.Logf(" insn %d, s: %#v, p1: %#v, got: %#v", i+1, allInstructions[i], prog1[i], got[i])
177 type InvalidInstruction struct{}
179 func (a InvalidInstruction) Assemble() (RawInstruction, error) {
180 return RawInstruction{}, fmt.Errorf("Invalid Instruction")
183 func (a InvalidInstruction) String() string {
184 return fmt.Sprintf("unknown instruction: %#v", a)
187 func TestString(t *testing.T) {
188 testCases := []struct {
189 instruction Instruction
193 instruction: LoadConstant{Dst: RegA, Val: 42},
197 instruction: LoadConstant{Dst: RegX, Val: 42},
198 assembler: "ldx #42",
201 instruction: LoadConstant{Dst: 0xffff, Val: 42},
202 assembler: "unknown instruction: bpf.LoadConstant{Dst:0xffff, Val:0x2a}",
205 instruction: LoadScratch{Dst: RegA, N: 3},
206 assembler: "ld M[3]",
209 instruction: LoadScratch{Dst: RegX, N: 3},
210 assembler: "ldx M[3]",
213 instruction: LoadScratch{Dst: 0xffff, N: 3},
214 assembler: "unknown instruction: bpf.LoadScratch{Dst:0xffff, N:3}",
217 instruction: LoadAbsolute{Off: 42, Size: 1},
218 assembler: "ldb [42]",
221 instruction: LoadAbsolute{Off: 42, Size: 2},
222 assembler: "ldh [42]",
225 instruction: LoadAbsolute{Off: 42, Size: 4},
226 assembler: "ld [42]",
229 instruction: LoadAbsolute{Off: 42, Size: -1},
230 assembler: "unknown instruction: bpf.LoadAbsolute{Off:0x2a, Size:-1}",
233 instruction: LoadIndirect{Off: 42, Size: 1},
234 assembler: "ldb [x + 42]",
237 instruction: LoadIndirect{Off: 42, Size: 2},
238 assembler: "ldh [x + 42]",
241 instruction: LoadIndirect{Off: 42, Size: 4},
242 assembler: "ld [x + 42]",
245 instruction: LoadIndirect{Off: 42, Size: -1},
246 assembler: "unknown instruction: bpf.LoadIndirect{Off:0x2a, Size:-1}",
249 instruction: LoadMemShift{Off: 42},
250 assembler: "ldx 4*([42]&0xf)",
253 instruction: LoadExtension{Num: ExtLen},
254 assembler: "ld #len",
257 instruction: LoadExtension{Num: ExtProto},
258 assembler: "ld #proto",
261 instruction: LoadExtension{Num: ExtType},
262 assembler: "ld #type",
265 instruction: LoadExtension{Num: ExtPayloadOffset},
266 assembler: "ld #poff",
269 instruction: LoadExtension{Num: ExtInterfaceIndex},
270 assembler: "ld #ifidx",
273 instruction: LoadExtension{Num: ExtNetlinkAttr},
274 assembler: "ld #nla",
277 instruction: LoadExtension{Num: ExtNetlinkAttrNested},
278 assembler: "ld #nlan",
281 instruction: LoadExtension{Num: ExtMark},
282 assembler: "ld #mark",
285 instruction: LoadExtension{Num: ExtQueue},
286 assembler: "ld #queue",
289 instruction: LoadExtension{Num: ExtLinkLayerType},
290 assembler: "ld #hatype",
293 instruction: LoadExtension{Num: ExtRXHash},
294 assembler: "ld #rxhash",
297 instruction: LoadExtension{Num: ExtCPUID},
298 assembler: "ld #cpu",
301 instruction: LoadExtension{Num: ExtVLANTag},
302 assembler: "ld #vlan_tci",
305 instruction: LoadExtension{Num: ExtVLANTagPresent},
306 assembler: "ld #vlan_avail",
309 instruction: LoadExtension{Num: ExtVLANProto},
310 assembler: "ld #vlan_tpid",
313 instruction: LoadExtension{Num: ExtRand},
314 assembler: "ld #rand",
317 instruction: LoadAbsolute{Off: 0xfffff038, Size: 4},
318 assembler: "ld #rand",
321 instruction: LoadExtension{Num: 0xfff},
322 assembler: "unknown instruction: bpf.LoadExtension{Num:4095}",
325 instruction: StoreScratch{Src: RegA, N: 3},
326 assembler: "st M[3]",
329 instruction: StoreScratch{Src: RegX, N: 3},
330 assembler: "stx M[3]",
333 instruction: StoreScratch{Src: 0xffff, N: 3},
334 assembler: "unknown instruction: bpf.StoreScratch{Src:0xffff, N:3}",
337 instruction: ALUOpConstant{Op: ALUOpAdd, Val: 42},
338 assembler: "add #42",
341 instruction: ALUOpConstant{Op: ALUOpSub, Val: 42},
342 assembler: "sub #42",
345 instruction: ALUOpConstant{Op: ALUOpMul, Val: 42},
346 assembler: "mul #42",
349 instruction: ALUOpConstant{Op: ALUOpDiv, Val: 42},
350 assembler: "div #42",
353 instruction: ALUOpConstant{Op: ALUOpOr, Val: 42},
357 instruction: ALUOpConstant{Op: ALUOpAnd, Val: 42},
358 assembler: "and #42",
361 instruction: ALUOpConstant{Op: ALUOpShiftLeft, Val: 42},
362 assembler: "lsh #42",
365 instruction: ALUOpConstant{Op: ALUOpShiftRight, Val: 42},
366 assembler: "rsh #42",
369 instruction: ALUOpConstant{Op: ALUOpMod, Val: 42},
370 assembler: "mod #42",
373 instruction: ALUOpConstant{Op: ALUOpXor, Val: 42},
374 assembler: "xor #42",
377 instruction: ALUOpConstant{Op: 0xffff, Val: 42},
378 assembler: "unknown instruction: bpf.ALUOpConstant{Op:0xffff, Val:0x2a}",
381 instruction: ALUOpX{Op: ALUOpAdd},
385 instruction: ALUOpX{Op: ALUOpSub},
389 instruction: ALUOpX{Op: ALUOpMul},
393 instruction: ALUOpX{Op: ALUOpDiv},
397 instruction: ALUOpX{Op: ALUOpOr},
401 instruction: ALUOpX{Op: ALUOpAnd},
405 instruction: ALUOpX{Op: ALUOpShiftLeft},
409 instruction: ALUOpX{Op: ALUOpShiftRight},
413 instruction: ALUOpX{Op: ALUOpMod},
417 instruction: ALUOpX{Op: ALUOpXor},
421 instruction: ALUOpX{Op: 0xffff},
422 assembler: "unknown instruction: bpf.ALUOpX{Op:0xffff}",
425 instruction: NegateA{},
429 instruction: Jump{Skip: 10},
433 instruction: JumpIf{Cond: JumpEqual, Val: 42, SkipTrue: 8, SkipFalse: 9},
434 assembler: "jeq #42,8,9",
437 instruction: JumpIf{Cond: JumpEqual, Val: 42, SkipTrue: 8},
438 assembler: "jeq #42,8",
441 instruction: JumpIf{Cond: JumpEqual, Val: 42, SkipFalse: 8},
442 assembler: "jneq #42,8",
445 instruction: JumpIf{Cond: JumpNotEqual, Val: 42, SkipTrue: 8},
446 assembler: "jneq #42,8",
449 instruction: JumpIf{Cond: JumpLessThan, Val: 42, SkipTrue: 7},
450 assembler: "jlt #42,7",
453 instruction: JumpIf{Cond: JumpLessOrEqual, Val: 42, SkipTrue: 6},
454 assembler: "jle #42,6",
457 instruction: JumpIf{Cond: JumpGreaterThan, Val: 42, SkipTrue: 4, SkipFalse: 5},
458 assembler: "jgt #42,4,5",
461 instruction: JumpIf{Cond: JumpGreaterThan, Val: 42, SkipTrue: 4},
462 assembler: "jgt #42,4",
465 instruction: JumpIf{Cond: JumpGreaterOrEqual, Val: 42, SkipTrue: 3, SkipFalse: 4},
466 assembler: "jge #42,3,4",
469 instruction: JumpIf{Cond: JumpGreaterOrEqual, Val: 42, SkipTrue: 3},
470 assembler: "jge #42,3",
473 instruction: JumpIf{Cond: JumpBitsSet, Val: 42, SkipTrue: 2, SkipFalse: 3},
474 assembler: "jset #42,2,3",
477 instruction: JumpIf{Cond: JumpBitsSet, Val: 42, SkipTrue: 2},
478 assembler: "jset #42,2",
481 instruction: JumpIf{Cond: JumpBitsNotSet, Val: 42, SkipTrue: 2, SkipFalse: 3},
482 assembler: "jset #42,3,2",
485 instruction: JumpIf{Cond: JumpBitsNotSet, Val: 42, SkipTrue: 2},
486 assembler: "jset #42,0,2",
489 instruction: JumpIf{Cond: 0xffff, Val: 42, SkipTrue: 1, SkipFalse: 2},
490 assembler: "unknown instruction: bpf.JumpIf{Cond:0xffff, Val:0x2a, SkipTrue:0x1, SkipFalse:0x2}",
505 instruction: RetConstant{Val: 42},
506 assembler: "ret #42",
508 // Invalid instruction
510 instruction: InvalidInstruction{},
511 assembler: "unknown instruction: bpf.InvalidInstruction{}",
515 for _, testCase := range testCases {
516 if input, ok := testCase.instruction.(fmt.Stringer); ok {
517 got := input.String()
518 if got != testCase.assembler {
519 t.Errorf("String did not return expected assembler notation, expected: %s, got: %s", testCase.assembler, got)
522 t.Errorf("Instruction %#v is not a fmt.Stringer", testCase.instruction)