(t *testing.T)
| 25 | ) |
| 26 | |
| 27 | func TestProgramRun(t *testing.T) { |
| 28 | pat := []byte{0xDE, 0xAD, 0xBE, 0xEF} |
| 29 | buf := internal.EmptyBPFContext |
| 30 | |
| 31 | // r1 : ctx_start |
| 32 | // r1+4: ctx_end |
| 33 | ins := asm.Instructions{ |
| 34 | // r2 = *(r1+4) |
| 35 | asm.LoadMem(asm.R2, asm.R1, 4, asm.Word), |
| 36 | // r1 = *(r1+0) |
| 37 | asm.LoadMem(asm.R1, asm.R1, 0, asm.Word), |
| 38 | // r3 = r1 |
| 39 | asm.Mov.Reg(asm.R3, asm.R1), |
| 40 | // r3 += len(buf) |
| 41 | asm.Add.Imm(asm.R3, int32(len(buf))), |
| 42 | // if r3 > r2 goto +len(pat) |
| 43 | asm.JGT.Reg(asm.R3, asm.R2, "out"), |
| 44 | } |
| 45 | for i, b := range pat { |
| 46 | ins = append(ins, asm.StoreImm(asm.R1, int16(i), int64(b), asm.Byte)) |
| 47 | } |
| 48 | ins = append(ins, |
| 49 | // return 42 |
| 50 | asm.LoadImm(asm.R0, 42, asm.DWord).WithSymbol("out"), |
| 51 | asm.Return(), |
| 52 | ) |
| 53 | |
| 54 | if platform.IsWindows { |
| 55 | // Windows uses an incompatible context for XDP. Pointers are |
| 56 | // 64 bit. |
| 57 | // See https://github.com/microsoft/ebpf-for-windows/issues/3873 |
| 58 | // r2 = *(r1+8) |
| 59 | ins[0] = asm.LoadMem(asm.R2, asm.R1, 8, asm.DWord) |
| 60 | // r1 = *(r1+0) |
| 61 | ins[1] = asm.LoadMem(asm.R1, asm.R1, 0, asm.DWord) |
| 62 | } |
| 63 | |
| 64 | t.Log(ins) |
| 65 | |
| 66 | prog := mustNewProgram(t, &ProgramSpec{ |
| 67 | Name: "test", |
| 68 | Type: XDP, |
| 69 | Instructions: ins, |
| 70 | License: "MIT", |
| 71 | }, nil) |
| 72 | |
| 73 | p2, err := prog.Clone() |
| 74 | if err != nil { |
| 75 | t.Fatal("Can't clone program") |
| 76 | } |
| 77 | defer p2.Close() |
| 78 | |
| 79 | prog.Close() |
| 80 | prog = p2 |
| 81 | |
| 82 | out := make([]byte, len(buf)) |
| 83 | ret := mustRun(t, prog, &RunOptions{Data: buf, DataOut: out}) |
| 84 | qt.Assert(t, qt.Equals(ret, 42)) |
nothing calls this directly
no test coverage detected
searching dependent graphs…