| 1 | package ringbuf |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/go-quicktest/qt" |
| 7 | |
| 8 | "github.com/cilium/ebpf" |
| 9 | "github.com/cilium/ebpf/asm" |
| 10 | ) |
| 11 | |
| 12 | func mustOutputSamplesProg(tb testing.TB, sampleMessages ...sampleMessage) (*ebpf.Program, *ebpf.Map) { |
| 13 | tb.Helper() |
| 14 | |
| 15 | events, err := ebpf.NewMap(&ebpf.MapSpec{ |
| 16 | Type: ebpf.WindowsRingBuf, |
| 17 | MaxEntries: 4096, |
| 18 | }) |
| 19 | qt.Assert(tb, qt.IsNil(err)) |
| 20 | tb.Cleanup(func() { |
| 21 | events.Close() |
| 22 | }) |
| 23 | |
| 24 | var maxSampleSize int |
| 25 | for _, sampleMessage := range sampleMessages { |
| 26 | if sampleMessage.size > maxSampleSize { |
| 27 | maxSampleSize = sampleMessage.size |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | insns := asm.Instructions{ |
| 32 | asm.LoadImm(asm.R0, 0x0102030404030201, asm.DWord), |
| 33 | asm.Mov.Reg(asm.R9, asm.R1), |
| 34 | } |
| 35 | |
| 36 | bufDwords := (maxSampleSize / 8) + 1 |
| 37 | for i := range bufDwords { |
| 38 | insns = append(insns, |
| 39 | asm.StoreMem(asm.RFP, int16(i+1)*-8, asm.R0, asm.DWord), |
| 40 | ) |
| 41 | } |
| 42 | |
| 43 | for _, sampleMessage := range sampleMessages { |
| 44 | if sampleMessage.discard { |
| 45 | tb.Skip("discard is not supported on Windows") |
| 46 | } |
| 47 | |
| 48 | insns = append(insns, |
| 49 | asm.LoadMapPtr(asm.R1, events.FD()), |
| 50 | asm.Mov.Reg(asm.R2, asm.RFP), |
| 51 | asm.Add.Imm(asm.R2, -int32(8*bufDwords)), |
| 52 | asm.Mov.Imm(asm.R3, int32(sampleMessage.size)), |
| 53 | asm.Mov.Imm(asm.R4, sampleMessage.flags), |
| 54 | asm.WindowsFnRingbufOutput.Call(), |
| 55 | asm.JNE.Imm(asm.R0, 0, "exit"), |
| 56 | ) |
| 57 | } |
| 58 | |
| 59 | insns = append(insns, |
| 60 | asm.Mov.Imm(asm.R0, int32(0)), |
| 61 | asm.Return().WithSymbol("exit"), |
| 62 | ) |
| 63 | |
| 64 | prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{ |
| 65 | License: "MIT", |
| 66 | Type: ebpf.WindowsSample, |
| 67 | Instructions: insns, |
| 68 | }) |
| 69 | qt.Assert(tb, qt.IsNil(err)) |
| 70 | tb.Cleanup(func() { |
| 71 | prog.Close() |
| 72 | }) |
| 73 | |
| 74 | return prog, events |
| 75 | } |