Extended Berkeley Packet Filter (eBPF) assembler and virtual machine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

352 lines
6.2 KiB

package ebpf
import (
"math"
"testing"
)
func TestLoadConstant(t *testing.T) {
testInstruction{
Test: LoadConstant{
Dst: R1,
Value: 0x12345678,
},
WantRaw: RawInstruction{
Op: opClassLoad | sizeDoubleWord | modeImmediate,
Dst: R1,
Immediate: 0x12345678,
},
WantString: "lddw R1, 0x12345678",
}.Run(t)
}
func TestLoadAbsolute(t *testing.T) {
op := opClassLoad | modeAbsolute
tests := testInstructionSuite{
{
Test: LoadAbsolute{
Dst: R0,
Src: R1,
Size: 1,
Value: 0xff,
},
WantRaw: RawInstruction{
Op: op | sizeByte,
Dst: R0,
Src: R1,
Immediate: 0xff,
},
WantString: "ldabsb R1, R0, 0xff",
},
{
Test: LoadAbsolute{
Dst: R0,
Src: R1,
Size: 2,
Value: 0xffff,
},
WantRaw: RawInstruction{
Op: op | sizeHalfWord,
Src: R1,
Dst: R0,
Immediate: 0xffff,
},
WantString: "ldabsh R1, R0, 0xffff",
},
{
Test: LoadAbsolute{
Dst: R0,
Src: R1,
Size: 4,
Value: 0x12345678,
},
WantRaw: RawInstruction{
Op: op | sizeWord,
Src: R1,
Dst: R0,
Immediate: 0x12345678,
},
WantString: "ldabsw R1, R0, 0x12345678",
},
{
Test: LoadAbsolute{
Dst: R0,
Src: R1,
Size: 8,
Value: math.MaxUint32,
},
WantRaw: RawInstruction{
Op: op | sizeDoubleWord,
Src: R1,
Dst: R0,
Immediate: math.MaxUint32,
},
WantString: "ldabsdw R1, R0, 0xffffffff",
},
}
tests.Run(t)
}
func TestLoadIndirect(t *testing.T) {
op := opClassLoad | modeIndirect
tests := testInstructionSuite{
{
Test: LoadIndirect{
Dst: R0,
Src: R1,
Size: 1,
Value: 0xff,
},
WantRaw: RawInstruction{
Op: op | sizeByte,
Dst: R0,
Src: R1,
Immediate: 0xff,
},
WantString: "ldindb R1, R0, 0xff",
},
{
Test: LoadIndirect{
Dst: R0,
Src: R1,
Size: 2,
Value: 0xffff,
},
WantRaw: RawInstruction{
Op: op | sizeHalfWord,
Src: R1,
Dst: R0,
Immediate: 0xffff,
},
WantString: "ldindh R1, R0, 0xffff",
},
{
Test: LoadIndirect{
Dst: R0,
Src: R1,
Size: 4,
Value: 0x12345678,
},
WantRaw: RawInstruction{
Op: op | sizeWord,
Src: R1,
Dst: R0,
Immediate: 0x12345678,
},
WantString: "ldindw R1, R0, 0x12345678",
},
{
Test: LoadIndirect{
Dst: R0,
Src: R1,
Size: 8,
Value: math.MaxUint32,
},
WantRaw: RawInstruction{
Op: op | sizeDoubleWord,
Src: R1,
Dst: R0,
Immediate: math.MaxUint32,
},
WantString: "ldinddw R1, R0, 0xffffffff",
},
}
tests.Run(t)
}
func TestLoadMemory(t *testing.T) {
op := opClassLoadX | modeMemory
tests := testInstructionSuite{
{
Test: LoadRegister{
Dst: R0,
Src: R1,
Size: 1,
Offset: 0xff,
},
WantRaw: RawInstruction{
Op: op | sizeByte,
Dst: R0,
Src: R1,
Offset: 0xff,
},
WantString: "ldxb R0, [R1+0xff]",
},
{
Test: LoadRegister{
Dst: R0,
Src: R1,
Size: 2,
Offset: 0x7fff,
},
WantRaw: RawInstruction{
Op: op | sizeHalfWord,
Src: R1,
Dst: R0,
Offset: 0x7fff,
},
WantString: "ldxh R0, [R1+0x7fff]",
},
{
Test: LoadRegister{
Dst: R0,
Src: R1,
Size: 4,
Offset: 0x1234,
},
WantRaw: RawInstruction{
Op: op | sizeWord,
Src: R1,
Dst: R0,
Offset: 0x1234,
},
WantString: "ldxw R0, [R1+0x1234]",
},
{
Test: LoadRegister{
Dst: R0,
Src: R1,
Size: 8,
Offset: math.MaxInt16,
},
WantRaw: RawInstruction{
Op: op | sizeDoubleWord,
Src: R1,
Dst: R0,
Offset: math.MaxInt16,
},
WantString: "ldxdw R0, [R1+0x7fff]",
},
}
tests.Run(t)
}
func TestStore(t *testing.T) {
op := opClassStore | modeMemory
tests := testInstructionSuite{
{
Test: StoreImmediate{
Dst: R0,
Value: 0xff,
Size: 1,
Offset: 0xff,
},
WantRaw: RawInstruction{
Op: op | sizeByte,
Dst: R0,
Offset: 0xff,
Immediate: 0xff,
},
WantString: "stb [R0+0xff], 0xff",
},
{
Test: StoreImmediate{
Dst: R0,
Size: 2,
Offset: 0x7fff,
},
WantRaw: RawInstruction{
Op: op | sizeHalfWord,
Dst: R0,
Offset: 0x7fff,
},
WantString: "sth [R0+0x7fff], 0x0",
},
{
Test: StoreImmediate{
Dst: R0,
Size: 4,
Value: math.MaxUint32,
Offset: 0x1234,
},
WantRaw: RawInstruction{
Op: op | sizeWord,
Dst: R0,
Offset: 0x1234,
Immediate: math.MaxUint32,
},
WantString: "stw [R0+0x1234], 0xffffffff",
},
{
Test: StoreImmediate{
Dst: R0,
Size: 8,
Offset: math.MinInt16,
},
WantRaw: RawInstruction{
Op: op | sizeDoubleWord,
Dst: R0,
Offset: math.MinInt16,
},
WantString: "stdw [R0-0x8000], 0x0",
},
}
tests.Run(t)
}
func TestStoreMemory(t *testing.T) {
op := opClassStoreX | modeMemory
tests := testInstructionSuite{
{
Test: StoreRegister{
Dst: R0,
Src: R1,
Size: 1,
Offset: 0xff,
},
WantRaw: RawInstruction{
Op: op | sizeByte,
Dst: R0,
Src: R1,
Offset: 0xff,
},
WantString: "stxb [R0+0xff], R1",
},
{
Test: StoreRegister{
Dst: R0,
Src: R1,
Size: 2,
Offset: 0x7fff,
},
WantRaw: RawInstruction{
Op: op | sizeHalfWord,
Src: R1,
Dst: R0,
Offset: 0x7fff,
},
WantString: "stxh [R0+0x7fff], R1",
},
{
Test: StoreRegister{
Dst: R0,
Src: R1,
Size: 4,
Offset: 0x1234,
},
WantRaw: RawInstruction{
Op: op | sizeWord,
Src: R1,
Dst: R0,
Offset: 0x1234,
},
WantString: "stxw [R0+0x1234], R1",
},
{
Test: StoreRegister{
Dst: R0,
Src: R1,
Size: 8,
Offset: math.MaxInt16,
},
WantRaw: RawInstruction{
Op: op | sizeDoubleWord,
Src: R1,
Dst: R0,
Offset: math.MaxInt16,
},
WantString: "stxdw [R0+0x7fff], R1",
},
}
tests.Run(t)
}