Debian Codegraph @

link/kprobe_multi_test.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "errors"
7 "os"
8 "testing"
9
10 "github.com/go-quicktest/qt"
11
12 "github.com/cilium/ebpf"
13 "github.com/cilium/ebpf/features"
14 "github.com/cilium/ebpf/internal/linux"
15 "github.com/cilium/ebpf/internal/testutils"
16 "github.com/cilium/ebpf/internal/unix"
17 )
18
19 var kprobeMultiSyms = []string{"vprintk", "inet6_release"}
20
21 func TestKprobeMulti(t *testing.T) {
22 testutils.SkipIfNotSupported(t, features.HaveBPFLinkKprobeMulti())
23
24 prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceKprobeMulti, "")
25
26 km, err := KprobeMulti(prog, KprobeMultiOptions{Symbols: kprobeMultiSyms})
27 if err != nil {
28 t.Fatal(err)
29 }
30 defer km.Close()
31
32 testLink(t, km, prog)
33 }
34
35 func TestKprobeMultiInfo(t *testing.T) {
36 testutils.SkipIfNotSupported(t, features.HaveBPFLinkKprobeMulti())
37 testutils.SkipOnOldKernel(t, "6.6", "bpf_link_info_kprobe_multi")
38
39 prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceKprobeMulti, "")
40
41 km, err := KprobeMulti(prog, KprobeMultiOptions{Symbols: kprobeMultiSyms})
42 if err != nil {
43 t.Fatal(err)
44 }
45 defer km.Close()
46
47 info, err := km.Info()
48 if err != nil {
49 t.Fatal(err)
50 }
51 kmInfo := info.KprobeMulti()
52 addresses, ok := kmInfo.Addresses()
53 qt.Assert(t, qt.IsTrue(ok))
54 // kprobe_multi only returns addresses, no symbols, so we can't verify that the addresses are correct
55 qt.Assert(t, qt.HasLen(addresses, len(kprobeMultiSyms)))
56 }
57
58 func TestKprobeMultiInput(t *testing.T) {
59 // Program type that loads on all kernels. Not expected to link successfully.
60 prog := mustLoadProgram(t, ebpf.SocketFilter, 0, "")
61
62 // One of Symbols or Addresses must be given.
63 _, err := KprobeMulti(prog, KprobeMultiOptions{})
64 if !errors.Is(err, errInvalidInput) {
65 t.Fatalf("expected errInvalidInput, got: %v", err)
66 }
67
68 // Symbols and Addresses are mutually exclusive.
69 _, err = KprobeMulti(prog, KprobeMultiOptions{
70 Symbols: []string{"foo"},
71 Addresses: []uintptr{1},
72 })
73 if !errors.Is(err, errInvalidInput) {
74 t.Fatalf("expected errInvalidInput, got: %v", err)
75 }
76
77 // One Symbol, two cookies..
78 _, err = KprobeMulti(prog, KprobeMultiOptions{
79 Symbols: []string{"one"},
80 Cookies: []uint64{2, 3},
81 })
82 if !errors.Is(err, errInvalidInput) {
83 t.Fatalf("expected errInvalidInput, got: %v", err)
84 }
85 }
86
87 func TestKprobeMultiErrors(t *testing.T) {
88 testutils.SkipIfNotSupported(t, features.HaveBPFLinkKprobeMulti())
89
90 prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceKprobeMulti, "")
91
92 // Nonexistent kernel symbol.
93 _, err := KprobeMulti(prog, KprobeMultiOptions{Symbols: []string{"bogus"}})
94 if !errors.Is(err, os.ErrNotExist) && !errors.Is(err, unix.EINVAL) {
95 t.Fatalf("expected ErrNotExist or EINVAL, got: %s", err)
96 }
97
98 // Only have a negative test for addresses as it would be hard to maintain a
99 // proper one.
100 _, err = KprobeMulti(prog, KprobeMultiOptions{
101 Addresses: []uintptr{^uintptr(0)},
102 })
103 if !errors.Is(err, os.ErrNotExist) && !errors.Is(err, unix.EINVAL) {
104 t.Fatalf("expected ErrNotExist or EINVAL, got: %s", err)
105 }
106 }
107
108 func TestKprobeMultiCookie(t *testing.T) {
109 testutils.SkipIfNotSupported(t, features.HaveBPFLinkKprobeMulti())
110
111 prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceKprobeMulti, "")
112
113 km, err := KprobeMulti(prog, KprobeMultiOptions{
114 Symbols: kprobeMultiSyms,
115 Cookies: []uint64{0, 1},
116 })
117 if err != nil {
118 t.Fatal(err)
119 }
120 _ = km.Close()
121 }
122
123 func TestKprobeMultiProgramCall(t *testing.T) {
124 testutils.SkipIfNotSupported(t, features.HaveBPFLinkKprobeMulti())
125
126 m, p := newUpdaterMapProg(t, ebpf.Kprobe, ebpf.AttachTraceKprobeMulti)
127
128 // Use actual syscall names with platform prefix.
129 // For simplicity, just assert the increment happens with any symbol in the array.
130 prefix := linux.PlatformPrefix()
131 opts := KprobeMultiOptions{
132 Symbols: []string{prefix + "sys_getpid", prefix + "sys_gettid"},
133 }
134 km, err := KprobeMulti(p, opts)
135 if err != nil {
136 t.Fatal(err)
137 }
138
139 // Trigger ebpf program call.
140 unix.Getpid()
141 unix.Gettid()
142
143 // Assert that the value got incremented to at least 2, while allowing
144 // for bigger values, because we could race with other getpid/gettid
145 // callers.
146 assertMapValueGE(t, m, 0, 2)
147
148 // Close the link.
149 if err := km.Close(); err != nil {
150 t.Fatal(err)
151 }
152
153 // Reset map value to 0 at index 0.
154 if err := m.Update(uint32(0), uint32(0), ebpf.UpdateExist); err != nil {
155 t.Fatal(err)
156 }
157
158 // Retrigger the ebpf program call.
159 unix.Getpid()
160 unix.Gettid()
161
162 // Assert that this time the value has not been updated.
163 assertMapValue(t, m, 0, 0)
164 }
165
166 func TestKprobeSession(t *testing.T) {
167 testutils.SkipIfNotSupported(t, features.HaveBPFLinkKprobeSession())
168
169 prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceKprobeSession, "")
170
171 km, err := KprobeMulti(prog, KprobeMultiOptions{Symbols: kprobeMultiSyms, Session: true})
172 testutils.SkipIfNotSupported(t, err)
173 qt.Assert(t, qt.IsNil(err))
174 defer km.Close()
175
176 testLink(t, km, prog)
177 }