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.
10 "golang.org/x/net/bpf"
13 func TestVMStoreScratchInvalidScratchRegisterTooSmall(t *testing.T) {
14 _, _, err := testVM(t, []bpf.Instruction{
21 if errStr(err) != "assembling instruction 1: invalid scratch slot -1" {
22 t.Fatalf("unexpected error: %v", err)
26 func TestVMStoreScratchInvalidScratchRegisterTooLarge(t *testing.T) {
27 _, _, err := testVM(t, []bpf.Instruction{
34 if errStr(err) != "assembling instruction 1: invalid scratch slot 16" {
35 t.Fatalf("unexpected error: %v", err)
39 func TestVMStoreScratchUnknownSourceRegister(t *testing.T) {
40 _, _, err := testVM(t, []bpf.Instruction{
47 if errStr(err) != "assembling instruction 1: invalid source register 100" {
48 t.Fatalf("unexpected error: %v", err)
52 func TestVMLoadScratchInvalidScratchRegisterTooSmall(t *testing.T) {
53 _, _, err := testVM(t, []bpf.Instruction{
60 if errStr(err) != "assembling instruction 1: invalid scratch slot -1" {
61 t.Fatalf("unexpected error: %v", err)
65 func TestVMLoadScratchInvalidScratchRegisterTooLarge(t *testing.T) {
66 _, _, err := testVM(t, []bpf.Instruction{
73 if errStr(err) != "assembling instruction 1: invalid scratch slot 16" {
74 t.Fatalf("unexpected error: %v", err)
78 func TestVMLoadScratchUnknownDestinationRegister(t *testing.T) {
79 _, _, err := testVM(t, []bpf.Instruction{
86 if errStr(err) != "assembling instruction 1: invalid target register 100" {
87 t.Fatalf("unexpected error: %v", err)
91 func TestVMStoreScratchLoadScratchOneValue(t *testing.T) {
92 vm, done, err := testVM(t, []bpf.Instruction{
98 // Copy to X and store in scratch[0]
109 // Overwrite 1 with 255 from scratch[0]
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 := 3, out; want != got {
131 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
136 func TestVMStoreScratchLoadScratchMultipleValues(t *testing.T) {
137 vm, done, err := testVM(t, []bpf.Instruction{
143 // Store in scratch[0]
153 // Store in scratch[1]
163 // Store in scratch[2]
173 // Store in scratch[3]
178 // Load in byte 10 to X
185 // Verify value is 10
191 // Fail test if incorrect
195 // Load in byte 20 to A
200 // Verify value is 20
206 // Fail test if incorrect
210 // Load in byte 30 to A
215 // Verify value is 30
221 // Fail test if incorrect
225 // Return first two bytes on success
231 t.Fatalf("failed to load BPF program: %v", err)
235 out, err := vm.Run([]byte{
236 0xff, 0xff, 0xff, 0xff,
237 0xff, 0xff, 0xff, 0xff,
241 t.Fatalf("unexpected error while running program: %v", err)
243 if want, got := 2, out; want != got {
244 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",