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.
11 "golang.org/x/net/bpf"
12 "golang.org/x/net/ipv4"
15 func TestVMLoadAbsoluteOffsetOutOfBounds(t *testing.T) {
16 vm, done, err := testVM(t, []bpf.Instruction{
24 t.Fatalf("failed to load BPF program: %v", err)
28 out, err := vm.Run([]byte{
29 0xff, 0xff, 0xff, 0xff,
30 0xff, 0xff, 0xff, 0xff,
34 t.Fatalf("unexpected error while running program: %v", err)
36 if want, got := 0, out; want != got {
37 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
42 func TestVMLoadAbsoluteOffsetPlusSizeOutOfBounds(t *testing.T) {
43 vm, done, err := testVM(t, []bpf.Instruction{
51 t.Fatalf("failed to load BPF program: %v", err)
55 out, err := vm.Run([]byte{
56 0xff, 0xff, 0xff, 0xff,
57 0xff, 0xff, 0xff, 0xff,
61 t.Fatalf("unexpected error while running program: %v", err)
63 if want, got := 0, out; want != got {
64 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
69 func TestVMLoadAbsoluteBadInstructionSize(t *testing.T) {
70 _, _, err := testVM(t, []bpf.Instruction{
76 if errStr(err) != "assembling instruction 1: invalid load byte length 0" {
77 t.Fatalf("unexpected error: %v", err)
81 func TestVMLoadConstantOK(t *testing.T) {
82 vm, done, err := testVM(t, []bpf.Instruction{
91 t.Fatalf("failed to load BPF program: %v", err)
95 out, err := vm.Run([]byte{
96 0xff, 0xff, 0xff, 0xff,
97 0xff, 0xff, 0xff, 0xff,
101 t.Fatalf("unexpected error while running program: %v", err)
103 if want, got := 1, out; want != got {
104 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
109 func TestVMLoadIndirectOutOfBounds(t *testing.T) {
110 vm, done, err := testVM(t, []bpf.Instruction{
118 t.Fatalf("failed to load BPF program: %v", err)
122 out, err := vm.Run([]byte{
123 0xff, 0xff, 0xff, 0xff,
124 0xff, 0xff, 0xff, 0xff,
128 t.Fatalf("unexpected error while running program: %v", err)
130 if want, got := 0, out; want != got {
131 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
136 func TestVMLoadMemShiftOutOfBounds(t *testing.T) {
137 vm, done, err := testVM(t, []bpf.Instruction{
144 t.Fatalf("failed to load BPF program: %v", err)
148 out, err := vm.Run([]byte{
149 0xff, 0xff, 0xff, 0xff,
150 0xff, 0xff, 0xff, 0xff,
154 t.Fatalf("unexpected error while running program: %v", err)
156 if want, got := 0, out; want != got {
157 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
166 func TestVMLoadMemShiftLoadIndirectNoResult(t *testing.T) {
167 vm, in, done := testDHCPv4(t)
170 // Append mostly empty UDP header with incorrect DHCPv4 port
171 in = append(in, []byte{
178 out, err := vm.Run(in)
180 t.Fatalf("unexpected error while running program: %v", err)
182 if want, got := 0, out; want != got {
183 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
188 func TestVMLoadMemShiftLoadIndirectOK(t *testing.T) {
189 vm, in, done := testDHCPv4(t)
192 // Append mostly empty UDP header with correct DHCPv4 port
193 in = append(in, []byte{
200 out, err := vm.Run(in)
202 t.Fatalf("unexpected error while running program: %v", err)
204 if want, got := len(in)-8, out; want != got {
205 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
210 func testDHCPv4(t *testing.T) (virtualMachine, []byte, func()) {
211 // DHCPv4 test data courtesy of David Anderson:
212 // https://github.com/google/netboot/blob/master/dhcp4/conn_linux.go#L59-L70
213 vm, done, err := testVM(t, []bpf.Instruction{
214 // Load IPv4 packet length
215 bpf.LoadMemShift{Off: 8},
217 bpf.LoadIndirect{Off: 8 + 2, Size: 2},
219 bpf.JumpIf{Cond: bpf.JumpEqual, Val: dhcp4Port, SkipFalse: 1},
221 bpf.RetConstant{Val: 1500},
223 bpf.RetConstant{Val: 0},
226 t.Fatalf("failed to load BPF program: %v", err)
229 // Minimal requirements to make a valid IPv4 header
232 Src: net.IPv4(192, 168, 1, 1),
233 Dst: net.IPv4(192, 168, 1, 2),
235 hb, err := h.Marshal()
237 t.Fatalf("failed to marshal IPv4 header: %v", err)
241 0xff, 0xff, 0xff, 0xff,
242 0xff, 0xff, 0xff, 0xff,