123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- // Copyright 2016 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package bpf
- import (
- "fmt"
- "io/ioutil"
- "reflect"
- "strconv"
- "strings"
- "testing"
- )
- // This is a direct translation of the program in
- // testdata/all_instructions.txt.
- var allInstructions = []Instruction{
- LoadConstant{Dst: RegA, Val: 42},
- LoadConstant{Dst: RegX, Val: 42},
- LoadScratch{Dst: RegA, N: 3},
- LoadScratch{Dst: RegX, N: 3},
- LoadAbsolute{Off: 42, Size: 1},
- LoadAbsolute{Off: 42, Size: 2},
- LoadAbsolute{Off: 42, Size: 4},
- LoadIndirect{Off: 42, Size: 1},
- LoadIndirect{Off: 42, Size: 2},
- LoadIndirect{Off: 42, Size: 4},
- LoadMemShift{Off: 42},
- LoadExtension{Num: ExtLen},
- LoadExtension{Num: ExtProto},
- LoadExtension{Num: ExtType},
- LoadExtension{Num: ExtRand},
- StoreScratch{Src: RegA, N: 3},
- StoreScratch{Src: RegX, N: 3},
- ALUOpConstant{Op: ALUOpAdd, Val: 42},
- ALUOpConstant{Op: ALUOpSub, Val: 42},
- ALUOpConstant{Op: ALUOpMul, Val: 42},
- ALUOpConstant{Op: ALUOpDiv, Val: 42},
- ALUOpConstant{Op: ALUOpOr, Val: 42},
- ALUOpConstant{Op: ALUOpAnd, Val: 42},
- ALUOpConstant{Op: ALUOpShiftLeft, Val: 42},
- ALUOpConstant{Op: ALUOpShiftRight, Val: 42},
- ALUOpConstant{Op: ALUOpMod, Val: 42},
- ALUOpConstant{Op: ALUOpXor, Val: 42},
- ALUOpX{Op: ALUOpAdd},
- ALUOpX{Op: ALUOpSub},
- ALUOpX{Op: ALUOpMul},
- ALUOpX{Op: ALUOpDiv},
- ALUOpX{Op: ALUOpOr},
- ALUOpX{Op: ALUOpAnd},
- ALUOpX{Op: ALUOpShiftLeft},
- ALUOpX{Op: ALUOpShiftRight},
- ALUOpX{Op: ALUOpMod},
- ALUOpX{Op: ALUOpXor},
- NegateA{},
- Jump{Skip: 17},
- JumpIf{Cond: JumpEqual, Val: 42, SkipTrue: 15, SkipFalse: 16},
- JumpIf{Cond: JumpNotEqual, Val: 42, SkipTrue: 15},
- JumpIf{Cond: JumpLessThan, Val: 42, SkipTrue: 14},
- JumpIf{Cond: JumpLessOrEqual, Val: 42, SkipTrue: 13},
- JumpIf{Cond: JumpGreaterThan, Val: 42, SkipTrue: 11, SkipFalse: 12},
- JumpIf{Cond: JumpGreaterOrEqual, Val: 42, SkipTrue: 10, SkipFalse: 11},
- JumpIf{Cond: JumpBitsSet, Val: 42, SkipTrue: 9, SkipFalse: 10},
- JumpIfX{Cond: JumpEqual, SkipTrue: 8, SkipFalse: 9},
- JumpIfX{Cond: JumpNotEqual, SkipTrue: 8},
- JumpIfX{Cond: JumpLessThan, SkipTrue: 7},
- JumpIfX{Cond: JumpLessOrEqual, SkipTrue: 6},
- JumpIfX{Cond: JumpGreaterThan, SkipTrue: 4, SkipFalse: 5},
- JumpIfX{Cond: JumpGreaterOrEqual, SkipTrue: 3, SkipFalse: 4},
- JumpIfX{Cond: JumpBitsSet, SkipTrue: 2, SkipFalse: 3},
- TAX{},
- TXA{},
- RetA{},
- RetConstant{Val: 42},
- }
- var allInstructionsExpected = "testdata/all_instructions.bpf"
- // Check that we produce the same output as the canonical bpf_asm
- // linux kernel tool.
- func TestInterop(t *testing.T) {
- out, err := Assemble(allInstructions)
- if err != nil {
- t.Fatalf("assembly of allInstructions program failed: %s", err)
- }
- t.Logf("Assembled program is %d instructions long", len(out))
- bs, err := ioutil.ReadFile(allInstructionsExpected)
- if err != nil {
- t.Fatalf("reading %s: %s", allInstructionsExpected, err)
- }
- // First statement is the number of statements, last statement is
- // empty. We just ignore both and rely on slice length.
- stmts := strings.Split(string(bs), ",")
- if len(stmts)-2 != len(out) {
- t.Fatalf("test program lengths don't match: %s has %d, Go implementation has %d", allInstructionsExpected, len(stmts)-2, len(allInstructions))
- }
- for i, stmt := range stmts[1 : len(stmts)-2] {
- nums := strings.Split(stmt, " ")
- if len(nums) != 4 {
- t.Fatalf("malformed instruction %d in %s: %s", i+1, allInstructionsExpected, stmt)
- }
- actual := out[i]
- op, err := strconv.ParseUint(nums[0], 10, 16)
- if err != nil {
- t.Fatalf("malformed opcode %s in instruction %d of %s", nums[0], i+1, allInstructionsExpected)
- }
- if actual.Op != uint16(op) {
- t.Errorf("opcode mismatch on instruction %d (%#v): got 0x%02x, want 0x%02x", i+1, allInstructions[i], actual.Op, op)
- }
- jt, err := strconv.ParseUint(nums[1], 10, 8)
- if err != nil {
- t.Fatalf("malformed jt offset %s in instruction %d of %s", nums[1], i+1, allInstructionsExpected)
- }
- if actual.Jt != uint8(jt) {
- t.Errorf("jt mismatch on instruction %d (%#v): got %d, want %d", i+1, allInstructions[i], actual.Jt, jt)
- }
- jf, err := strconv.ParseUint(nums[2], 10, 8)
- if err != nil {
- t.Fatalf("malformed jf offset %s in instruction %d of %s", nums[2], i+1, allInstructionsExpected)
- }
- if actual.Jf != uint8(jf) {
- t.Errorf("jf mismatch on instruction %d (%#v): got %d, want %d", i+1, allInstructions[i], actual.Jf, jf)
- }
- k, err := strconv.ParseUint(nums[3], 10, 32)
- if err != nil {
- t.Fatalf("malformed constant %s in instruction %d of %s", nums[3], i+1, allInstructionsExpected)
- }
- if actual.K != uint32(k) {
- t.Errorf("constant mismatch on instruction %d (%#v): got %d, want %d", i+1, allInstructions[i], actual.K, k)
- }
- }
- }
- // Check that assembly and disassembly match each other.
- func TestAsmDisasm(t *testing.T) {
- prog1, err := Assemble(allInstructions)
- if err != nil {
- t.Fatalf("assembly of allInstructions program failed: %s", err)
- }
- t.Logf("Assembled program is %d instructions long", len(prog1))
- got, allDecoded := Disassemble(prog1)
- if !allDecoded {
- t.Errorf("Disassemble(Assemble(allInstructions)) produced unrecognized instructions:")
- for i, inst := range got {
- if r, ok := inst.(RawInstruction); ok {
- t.Logf(" insn %d, %#v --> %#v", i+1, allInstructions[i], r)
- }
- }
- }
- if len(allInstructions) != len(got) {
- t.Fatalf("disassembly changed program size: %d insns before, %d insns after", len(allInstructions), len(got))
- }
- if !reflect.DeepEqual(allInstructions, got) {
- t.Errorf("program mutated by disassembly:")
- for i := range got {
- if !reflect.DeepEqual(allInstructions[i], got[i]) {
- t.Logf(" insn %d, s: %#v, p1: %#v, got: %#v", i+1, allInstructions[i], prog1[i], got[i])
- }
- }
- }
- }
- type InvalidInstruction struct{}
- func (a InvalidInstruction) Assemble() (RawInstruction, error) {
- return RawInstruction{}, fmt.Errorf("Invalid Instruction")
- }
- func (a InvalidInstruction) String() string {
- return fmt.Sprintf("unknown instruction: %#v", a)
- }
- func TestString(t *testing.T) {
- testCases := []struct {
- instruction Instruction
- assembler string
- }{
- {
- instruction: LoadConstant{Dst: RegA, Val: 42},
- assembler: "ld #42",
- },
- {
- instruction: LoadConstant{Dst: RegX, Val: 42},
- assembler: "ldx #42",
- },
- {
- instruction: LoadConstant{Dst: 0xffff, Val: 42},
- assembler: "unknown instruction: bpf.LoadConstant{Dst:0xffff, Val:0x2a}",
- },
- {
- instruction: LoadScratch{Dst: RegA, N: 3},
- assembler: "ld M[3]",
- },
- {
- instruction: LoadScratch{Dst: RegX, N: 3},
- assembler: "ldx M[3]",
- },
- {
- instruction: LoadScratch{Dst: 0xffff, N: 3},
- assembler: "unknown instruction: bpf.LoadScratch{Dst:0xffff, N:3}",
- },
- {
- instruction: LoadAbsolute{Off: 42, Size: 1},
- assembler: "ldb [42]",
- },
- {
- instruction: LoadAbsolute{Off: 42, Size: 2},
- assembler: "ldh [42]",
- },
- {
- instruction: LoadAbsolute{Off: 42, Size: 4},
- assembler: "ld [42]",
- },
- {
- instruction: LoadAbsolute{Off: 42, Size: -1},
- assembler: "unknown instruction: bpf.LoadAbsolute{Off:0x2a, Size:-1}",
- },
- {
- instruction: LoadIndirect{Off: 42, Size: 1},
- assembler: "ldb [x + 42]",
- },
- {
- instruction: LoadIndirect{Off: 42, Size: 2},
- assembler: "ldh [x + 42]",
- },
- {
- instruction: LoadIndirect{Off: 42, Size: 4},
- assembler: "ld [x + 42]",
- },
- {
- instruction: LoadIndirect{Off: 42, Size: -1},
- assembler: "unknown instruction: bpf.LoadIndirect{Off:0x2a, Size:-1}",
- },
- {
- instruction: LoadMemShift{Off: 42},
- assembler: "ldx 4*([42]&0xf)",
- },
- {
- instruction: LoadExtension{Num: ExtLen},
- assembler: "ld #len",
- },
- {
- instruction: LoadExtension{Num: ExtProto},
- assembler: "ld #proto",
- },
- {
- instruction: LoadExtension{Num: ExtType},
- assembler: "ld #type",
- },
- {
- instruction: LoadExtension{Num: ExtPayloadOffset},
- assembler: "ld #poff",
- },
- {
- instruction: LoadExtension{Num: ExtInterfaceIndex},
- assembler: "ld #ifidx",
- },
- {
- instruction: LoadExtension{Num: ExtNetlinkAttr},
- assembler: "ld #nla",
- },
- {
- instruction: LoadExtension{Num: ExtNetlinkAttrNested},
- assembler: "ld #nlan",
- },
- {
- instruction: LoadExtension{Num: ExtMark},
- assembler: "ld #mark",
- },
- {
- instruction: LoadExtension{Num: ExtQueue},
- assembler: "ld #queue",
- },
- {
- instruction: LoadExtension{Num: ExtLinkLayerType},
- assembler: "ld #hatype",
- },
- {
- instruction: LoadExtension{Num: ExtRXHash},
- assembler: "ld #rxhash",
- },
- {
- instruction: LoadExtension{Num: ExtCPUID},
- assembler: "ld #cpu",
- },
- {
- instruction: LoadExtension{Num: ExtVLANTag},
- assembler: "ld #vlan_tci",
- },
- {
- instruction: LoadExtension{Num: ExtVLANTagPresent},
- assembler: "ld #vlan_avail",
- },
- {
- instruction: LoadExtension{Num: ExtVLANProto},
- assembler: "ld #vlan_tpid",
- },
- {
- instruction: LoadExtension{Num: ExtRand},
- assembler: "ld #rand",
- },
- {
- instruction: LoadAbsolute{Off: 0xfffff038, Size: 4},
- assembler: "ld #rand",
- },
- {
- instruction: LoadExtension{Num: 0xfff},
- assembler: "unknown instruction: bpf.LoadExtension{Num:4095}",
- },
- {
- instruction: StoreScratch{Src: RegA, N: 3},
- assembler: "st M[3]",
- },
- {
- instruction: StoreScratch{Src: RegX, N: 3},
- assembler: "stx M[3]",
- },
- {
- instruction: StoreScratch{Src: 0xffff, N: 3},
- assembler: "unknown instruction: bpf.StoreScratch{Src:0xffff, N:3}",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpAdd, Val: 42},
- assembler: "add #42",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpSub, Val: 42},
- assembler: "sub #42",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpMul, Val: 42},
- assembler: "mul #42",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpDiv, Val: 42},
- assembler: "div #42",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpOr, Val: 42},
- assembler: "or #42",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpAnd, Val: 42},
- assembler: "and #42",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpShiftLeft, Val: 42},
- assembler: "lsh #42",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpShiftRight, Val: 42},
- assembler: "rsh #42",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpMod, Val: 42},
- assembler: "mod #42",
- },
- {
- instruction: ALUOpConstant{Op: ALUOpXor, Val: 42},
- assembler: "xor #42",
- },
- {
- instruction: ALUOpConstant{Op: 0xffff, Val: 42},
- assembler: "unknown instruction: bpf.ALUOpConstant{Op:0xffff, Val:0x2a}",
- },
- {
- instruction: ALUOpX{Op: ALUOpAdd},
- assembler: "add x",
- },
- {
- instruction: ALUOpX{Op: ALUOpSub},
- assembler: "sub x",
- },
- {
- instruction: ALUOpX{Op: ALUOpMul},
- assembler: "mul x",
- },
- {
- instruction: ALUOpX{Op: ALUOpDiv},
- assembler: "div x",
- },
- {
- instruction: ALUOpX{Op: ALUOpOr},
- assembler: "or x",
- },
- {
- instruction: ALUOpX{Op: ALUOpAnd},
- assembler: "and x",
- },
- {
- instruction: ALUOpX{Op: ALUOpShiftLeft},
- assembler: "lsh x",
- },
- {
- instruction: ALUOpX{Op: ALUOpShiftRight},
- assembler: "rsh x",
- },
- {
- instruction: ALUOpX{Op: ALUOpMod},
- assembler: "mod x",
- },
- {
- instruction: ALUOpX{Op: ALUOpXor},
- assembler: "xor x",
- },
- {
- instruction: ALUOpX{Op: 0xffff},
- assembler: "unknown instruction: bpf.ALUOpX{Op:0xffff}",
- },
- {
- instruction: NegateA{},
- assembler: "neg",
- },
- {
- instruction: Jump{Skip: 10},
- assembler: "ja 10",
- },
- {
- instruction: JumpIf{Cond: JumpEqual, Val: 42, SkipTrue: 8, SkipFalse: 9},
- assembler: "jeq #42,8,9",
- },
- {
- instruction: JumpIf{Cond: JumpEqual, Val: 42, SkipTrue: 8},
- assembler: "jeq #42,8",
- },
- {
- instruction: JumpIf{Cond: JumpEqual, Val: 42, SkipFalse: 8},
- assembler: "jneq #42,8",
- },
- {
- instruction: JumpIf{Cond: JumpNotEqual, Val: 42, SkipTrue: 8},
- assembler: "jneq #42,8",
- },
- {
- instruction: JumpIf{Cond: JumpLessThan, Val: 42, SkipTrue: 7},
- assembler: "jlt #42,7",
- },
- {
- instruction: JumpIf{Cond: JumpLessOrEqual, Val: 42, SkipTrue: 6},
- assembler: "jle #42,6",
- },
- {
- instruction: JumpIf{Cond: JumpGreaterThan, Val: 42, SkipTrue: 4, SkipFalse: 5},
- assembler: "jgt #42,4,5",
- },
- {
- instruction: JumpIf{Cond: JumpGreaterThan, Val: 42, SkipTrue: 4},
- assembler: "jgt #42,4",
- },
- {
- instruction: JumpIf{Cond: JumpGreaterOrEqual, Val: 42, SkipTrue: 3, SkipFalse: 4},
- assembler: "jge #42,3,4",
- },
- {
- instruction: JumpIf{Cond: JumpGreaterOrEqual, Val: 42, SkipTrue: 3},
- assembler: "jge #42,3",
- },
- {
- instruction: JumpIf{Cond: JumpBitsSet, Val: 42, SkipTrue: 2, SkipFalse: 3},
- assembler: "jset #42,2,3",
- },
- {
- instruction: JumpIf{Cond: JumpBitsSet, Val: 42, SkipTrue: 2},
- assembler: "jset #42,2",
- },
- {
- instruction: JumpIf{Cond: JumpBitsNotSet, Val: 42, SkipTrue: 2, SkipFalse: 3},
- assembler: "jset #42,3,2",
- },
- {
- instruction: JumpIf{Cond: JumpBitsNotSet, Val: 42, SkipTrue: 2},
- assembler: "jset #42,0,2",
- },
- {
- instruction: JumpIf{Cond: 0xffff, Val: 42, SkipTrue: 1, SkipFalse: 2},
- assembler: "unknown JumpTest 0xffff",
- },
- {
- instruction: JumpIfX{Cond: JumpEqual, SkipTrue: 8, SkipFalse: 9},
- assembler: "jeq x,8,9",
- },
- {
- instruction: JumpIfX{Cond: JumpEqual, SkipTrue: 8},
- assembler: "jeq x,8",
- },
- {
- instruction: JumpIfX{Cond: JumpEqual, SkipFalse: 8},
- assembler: "jneq x,8",
- },
- {
- instruction: JumpIfX{Cond: JumpNotEqual, SkipTrue: 8},
- assembler: "jneq x,8",
- },
- {
- instruction: JumpIfX{Cond: JumpLessThan, SkipTrue: 7},
- assembler: "jlt x,7",
- },
- {
- instruction: JumpIfX{Cond: JumpLessOrEqual, SkipTrue: 6},
- assembler: "jle x,6",
- },
- {
- instruction: JumpIfX{Cond: JumpGreaterThan, SkipTrue: 4, SkipFalse: 5},
- assembler: "jgt x,4,5",
- },
- {
- instruction: JumpIfX{Cond: JumpGreaterThan, SkipTrue: 4},
- assembler: "jgt x,4",
- },
- {
- instruction: JumpIfX{Cond: JumpGreaterOrEqual, SkipTrue: 3, SkipFalse: 4},
- assembler: "jge x,3,4",
- },
- {
- instruction: JumpIfX{Cond: JumpGreaterOrEqual, SkipTrue: 3},
- assembler: "jge x,3",
- },
- {
- instruction: JumpIfX{Cond: JumpBitsSet, SkipTrue: 2, SkipFalse: 3},
- assembler: "jset x,2,3",
- },
- {
- instruction: JumpIfX{Cond: JumpBitsSet, SkipTrue: 2},
- assembler: "jset x,2",
- },
- {
- instruction: JumpIfX{Cond: JumpBitsNotSet, SkipTrue: 2, SkipFalse: 3},
- assembler: "jset x,3,2",
- },
- {
- instruction: JumpIfX{Cond: JumpBitsNotSet, SkipTrue: 2},
- assembler: "jset x,0,2",
- },
- {
- instruction: JumpIfX{Cond: 0xffff, SkipTrue: 1, SkipFalse: 2},
- assembler: "unknown JumpTest 0xffff",
- },
- {
- instruction: TAX{},
- assembler: "tax",
- },
- {
- instruction: TXA{},
- assembler: "txa",
- },
- {
- instruction: RetA{},
- assembler: "ret a",
- },
- {
- instruction: RetConstant{Val: 42},
- assembler: "ret #42",
- },
- // Invalid instruction
- {
- instruction: InvalidInstruction{},
- assembler: "unknown instruction: bpf.InvalidInstruction{}",
- },
- }
- for _, testCase := range testCases {
- if input, ok := testCase.instruction.(fmt.Stringer); ok {
- got := input.String()
- if got != testCase.assembler {
- t.Errorf("String did not return expected assembler notation, expected: %s, got: %s", testCase.assembler, got)
- }
- } else {
- t.Errorf("Instruction %#v is not a fmt.Stringer", testCase.instruction)
- }
- }
- }
|