Debian Codegraph @

prog_linux_test.go

raw symbols SCIP
1 package ebpf
2
3 import (
4 "fmt"
5 "math"
6 "runtime"
7 "slices"
8 "testing"
9 "time"
10
11 "github.com/go-quicktest/qt"
12
13 "github.com/cilium/ebpf/asm"
14 "github.com/cilium/ebpf/internal"
15 "github.com/cilium/ebpf/internal/sys"
16 "github.com/cilium/ebpf/internal/testutils"
17 "github.com/cilium/ebpf/internal/unix"
18 )
19
20 func TestProgramTestRunInterrupt(t *testing.T) {
21 testutils.SkipOnOldKernel(t, "5.0", "EINTR from BPF_PROG_TEST_RUN")
22
23 prog := createBasicProgram(t)
24
25 var (
26 tgid = unix.Getpid()
27 tidChan = make(chan int, 1)
28 exit = make(chan struct{})
29 errs = make(chan error, 1)
30 timeout = time.After(5 * time.Second)
31 )
32
33 defer close(exit)
34
35 go func() {
36 runtime.LockOSThread()
37 defer func() {
38 // Wait for the test to allow us to unlock the OS thread, to
39 // ensure that we don't send SIGUSR1 to the wrong thread.
40 <-exit
41 runtime.UnlockOSThread()
42 }()
43
44 tidChan <- unix.Gettid()
45
46 // Block this thread in the BPF syscall, so that we can
47 // trigger EINTR by sending a signal.
48 opts := RunOptions{
49 Data: internal.EmptyBPFContext,
50 Repeat: math.MaxInt32,
51 Reset: func() {
52 // We don't know how long finishing the
53 // test run would take, so flag that we've seen
54 // an interruption and abort the goroutine.
55 close(errs)
56 runtime.Goexit()
57 },
58 }
59 _, _, err := prog.run(&opts)
60
61 errs <- err
62 }()
63
64 tid := <-tidChan
65 for {
66 err := unix.Tgkill(tgid, tid, unix.SIGUSR1)
67 if err != nil {
68 t.Fatal("Can't send signal to goroutine thread:", err)
69 }
70
71 select {
72 case err, ok := <-errs:
73 if !ok {
74 return
75 }
76
77 testutils.SkipIfNotSupported(t, err)
78 if err == nil {
79 t.Fatal("testRun wasn't interrupted")
80 }
81
82 t.Fatal("testRun returned an error:", err)
83
84 case <-timeout:
85 t.Fatal("Timed out trying to interrupt the goroutine")
86
87 default:
88 }
89 }
90 }
91
92 func TestProgramVerifierLogLinux(t *testing.T) {
93 check := func(t *testing.T, err error) {
94 t.Helper()
95
96 var ve *internal.VerifierError
97 qt.Assert(t, qt.ErrorAs(err, &ve))
98
99 loglen := len(fmt.Sprintf("%+v", ve))
100 qt.Assert(t, qt.IsTrue(loglen > minVerifierLogSize),
101 qt.Commentf("Log buffer didn't grow past minimum, got %d bytes", loglen))
102 }
103
104 // Generate a base program of sufficient size whose verifier log does not fit
105 // in the minimum buffer size. Stay under 4096 insn limit of older kernels.
106 var base asm.Instructions
107 for range 4093 {
108 base = append(base, asm.Mov.Reg(asm.R0, asm.R1))
109 }
110
111 // Touch R10 (read-only frame pointer) to reliably force a verifier error.
112 invalid := slices.Clone(base)
113 invalid = append(invalid, asm.Mov.Reg(asm.R10, asm.R0))
114 invalid = append(invalid, asm.Return())
115
116 valid := slices.Clone(base)
117 valid = append(valid, asm.Return())
118
119 // Start out with testing against the invalid program.
120 spec := &ProgramSpec{
121 Type: SocketFilter,
122 License: "MIT",
123 Instructions: invalid,
124 }
125
126 _, err := newProgram(t, spec, nil)
127 check(t, err)
128
129 // Run tests against a valid program from here on out.
130 spec.Instructions = valid
131
132 // Explicitly request verifier log for a valid program and a start size.
133 prog := mustNewProgram(t, spec, &ProgramOptions{
134 LogLevel: LogLevelInstruction,
135 LogSizeStart: minVerifierLogSize * 2,
136 })
137 qt.Assert(t, qt.IsTrue(len(prog.VerifierLog) > minVerifierLogSize))
138 }
139
140 func TestProgramTestRunSyscall(t *testing.T) {
141 testutils.SkipOnOldKernel(t, "5.14", "BPF_PROG_TYPE_SYSCALL")
142
143 prog := mustNewProgram(t, &ProgramSpec{
144 Type: Syscall,
145 Flags: sys.BPF_F_SLEEPABLE,
146 License: "MIT",
147 Instructions: []asm.Instruction{
148 // fn (ctx *u64) { *ctx++; return *ctx }
149 asm.LoadMem(asm.R0, asm.R1, 0, asm.DWord),
150 asm.Add.Imm(asm.R0, 1),
151 asm.StoreMem(asm.R1, 0, asm.R0, asm.DWord),
152 asm.Return(),
153 },
154 }, nil)
155
156 // only Context
157 rc, err := prog.Run(&RunOptions{Context: uint64(42)})
158 testutils.SkipIfNotSupported(t, err)
159 if err != nil {
160 t.Fatal(err)
161 }
162 qt.Assert(t, qt.Equals(rc, 43))
163
164 // Context and ContextOut
165 out := uint64(0)
166 rc, err = prog.Run(&RunOptions{Context: uint64(99), ContextOut: &out})
167 if err != nil {
168 t.Fatal(err)
169 }
170 qt.Assert(t, qt.Equals(rc, 100))
171 qt.Assert(t, qt.Equals(out, 100))
172 }