1
2
3
4
5 package bpf
6
7 import (
8 "errors"
9 "fmt"
10 )
11
12
13 type VM struct {
14 filter []Instruction
15 }
16
17
18 func NewVM(filter []Instruction) (*VM, error) {
19 if len(filter) == 0 {
20 return nil, errors.New("one or more Instructions must be specified")
21 }
22
23 for i, ins := range filter {
24 check := len(filter) - (i + 1)
25 switch ins := ins.(type) {
26
27 case Jump:
28 if check <= int(ins.Skip) {
29 return nil, fmt.Errorf("cannot jump %d instructions; jumping past program bounds", ins.Skip)
30 }
31 case JumpIf:
32 if check <= int(ins.SkipTrue) {
33 return nil, fmt.Errorf("cannot jump %d instructions in true case; jumping past program bounds", ins.SkipTrue)
34 }
35 if check <= int(ins.SkipFalse) {
36 return nil, fmt.Errorf("cannot jump %d instructions in false case; jumping past program bounds", ins.SkipFalse)
37 }
38 case JumpIfX:
39 if check <= int(ins.SkipTrue) {
40 return nil, fmt.Errorf("cannot jump %d instructions in true case; jumping past program bounds", ins.SkipTrue)
41 }
42 if check <= int(ins.SkipFalse) {
43 return nil, fmt.Errorf("cannot jump %d instructions in false case; jumping past program bounds", ins.SkipFalse)
44 }
45
46 case ALUOpConstant:
47 if ins.Val != 0 {
48 break
49 }
50
51 switch ins.Op {
52 case ALUOpDiv, ALUOpMod:
53 return nil, errors.New("cannot divide by zero using ALUOpConstant")
54 }
55
56 case LoadExtension:
57 switch ins.Num {
58 case ExtLen:
59 default:
60 return nil, fmt.Errorf("extension %d not implemented", ins.Num)
61 }
62 }
63 }
64
65
66 switch filter[len(filter)-1].(type) {
67 case RetA, RetConstant:
68 default:
69 return nil, errors.New("BPF program must end with RetA or RetConstant")
70 }
71
72
73
74
75 _, err := Assemble(filter)
76
77 return &VM{
78 filter: filter,
79 }, err
80 }
81
82
83
84
85 func (v *VM) Run(in []byte) (int, error) {
86 var (
87
88 regA uint32
89 regX uint32
90 regScratch [16]uint32
91
92
93
94 ok = true
95 )
96
97
98
99
100
101
102
103
104
105
106 for i := 0; i < len(v.filter) && ok; i++ {
107 ins := v.filter[i]
108
109 switch ins := ins.(type) {
110 case ALUOpConstant:
111 regA = aluOpConstant(ins, regA)
112 case ALUOpX:
113 regA, ok = aluOpX(ins, regA, regX)
114 case Jump:
115 i += int(ins.Skip)
116 case JumpIf:
117 jump := jumpIf(ins, regA)
118 i += jump
119 case JumpIfX:
120 jump := jumpIfX(ins, regA, regX)
121 i += jump
122 case LoadAbsolute:
123 regA, ok = loadAbsolute(ins, in)
124 case LoadConstant:
125 regA, regX = loadConstant(ins, regA, regX)
126 case LoadExtension:
127 regA = loadExtension(ins, in)
128 case LoadIndirect:
129 regA, ok = loadIndirect(ins, in, regX)
130 case LoadMemShift:
131 regX, ok = loadMemShift(ins, in)
132 case LoadScratch:
133 regA, regX = loadScratch(ins, regScratch, regA, regX)
134 case RetA:
135 return int(regA), nil
136 case RetConstant:
137 return int(ins.Val), nil
138 case StoreScratch:
139 regScratch = storeScratch(ins, regScratch, regA, regX)
140 case TAX:
141 regX = regA
142 case TXA:
143 regA = regX
144 default:
145 return 0, fmt.Errorf("unknown Instruction at index %d: %T", i, ins)
146 }
147 }
148
149 return 0, nil
150 }
151
View as plain text