Debian Codegraph @

link/uprobe_multi_test.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "errors"
7 "math"
8 "os"
9 "os/exec"
10 "testing"
11
12 "github.com/go-quicktest/qt"
13
14 "github.com/cilium/ebpf"
15 "github.com/cilium/ebpf/features"
16 "github.com/cilium/ebpf/internal/testutils"
17 )
18
19 func TestUprobeMulti(t *testing.T) {
20 testutils.SkipIfNotSupported(t, features.HaveBPFLinkUprobeMulti())
21
22 prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "")
23
24 // uprobe
25 um, err := bashEx.UprobeMulti(bashSyms, prog, nil)
26 if err != nil {
27 t.Fatal(err)
28 }
29
30 testLink(t, um, prog)
31 _ = um.Close()
32
33 // uretprobe
34 um, err = bashEx.UretprobeMulti(bashSyms, prog, nil)
35 if err != nil {
36 t.Fatal(err)
37 }
38
39 testLink(t, um, prog)
40 _ = um.Close()
41 }
42
43 func TestUprobeMultiInfo(t *testing.T) {
44 testutils.SkipIfNotSupported(t, features.HaveBPFLinkKprobeMulti())
45 testutils.SkipOnOldKernel(t, "6.8", "bpf_link_info_uprobe_multi")
46
47 prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "")
48
49 // uprobe
50 um, err := bashEx.UprobeMulti(bashSyms, prog, nil)
51 if err != nil {
52 t.Fatal(err)
53 }
54 defer um.Close()
55
56 linkInfo, err := um.Info()
57 if err != nil {
58 t.Fatal(err)
59 }
60 qt.Assert(t, qt.Equals(linkInfo.Type, UprobeMultiType))
61 uprobeDetails := linkInfo.UprobeMulti()
62 // On some platforms, /bin/bash may point to /usr/bin/bash, thus only a contains and no equals check
63 qt.Assert(t, qt.StringContains(uprobeDetails.File, bashEx.path))
64 uprobeOffsets, ok := uprobeDetails.Offsets()
65 qt.Assert(t, qt.IsTrue(ok))
66 qt.Assert(t, qt.HasLen(uprobeOffsets, len(bashSyms)))
67 bashElf, err := OpenExecutable(uprobeDetails.File)
68 qt.Assert(t, qt.IsNil(err))
69 var symnames = make([]string, len(uprobeOffsets))
70 for i, offset := range uprobeOffsets {
71 symOffset, err := bashElf.Symbol(offset.Offset)
72 qt.Assert(t, qt.IsNil(err))
73 qt.Assert(t, qt.Equals(symOffset.Offset, 0))
74 symnames[i] = symOffset.Symbol
75 }
76 qt.Assert(t, qt.ContentEquals(symnames, bashSyms))
77 }
78
79 func TestUprobeMultiInput(t *testing.T) {
80 testutils.SkipIfNotSupported(t, features.HaveBPFLinkUprobeMulti())
81
82 prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "")
83
84 // Always doing same test for both uprobe and uretprobe
85
86 // One of symbols or offsets must be given.
87 _, err := bashEx.UprobeMulti([]string{}, prog, nil)
88 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
89
90 _, err = bashEx.UretprobeMulti([]string{}, prog, nil)
91 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
92
93 // One address, two cookies.
94 _, err = bashEx.UprobeMulti([]string{}, prog, &UprobeMultiOptions{
95 Addresses: []uint64{1},
96 Cookies: []uint64{2, 3},
97 })
98 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
99
100 _, err = bashEx.UretprobeMulti([]string{}, prog, &UprobeMultiOptions{
101 Addresses: []uint64{1},
102 Cookies: []uint64{2, 3},
103 })
104 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
105
106 // Two addresses, one refctr offset.
107 _, err = bashEx.UprobeMulti([]string{}, prog, &UprobeMultiOptions{
108 Addresses: []uint64{1, 2},
109 RefCtrOffsets: []uint64{4},
110 })
111 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
112
113 _, err = bashEx.UretprobeMulti([]string{}, prog, &UprobeMultiOptions{
114 Addresses: []uint64{1, 2},
115 RefCtrOffsets: []uint64{4},
116 })
117 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
118
119 // It's either symbols or addresses.
120 _, err = bashEx.UprobeMulti(bashSyms, prog, &UprobeMultiOptions{
121 Addresses: []uint64{1},
122 })
123 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
124
125 _, err = bashEx.UretprobeMulti(bashSyms, prog, &UprobeMultiOptions{
126 Addresses: []uint64{1},
127 })
128 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
129
130 // No addresses and no symbols
131 _, err = bashEx.UprobeMulti([]string{}, prog, nil)
132 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
133
134 _, err = bashEx.UretprobeMulti([]string{}, prog, nil)
135 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
136
137 // PID not found
138 _, err = bashEx.UprobeMulti(bashSyms, prog, &UprobeMultiOptions{
139 // pid_t is int32, overflowing it will return EINVAL.
140 PID: math.MaxInt32,
141 })
142 qt.Assert(t, qt.ErrorIs(err, os.ErrNotExist))
143
144 _, err = bashEx.UretprobeMulti(bashSyms, prog, &UprobeMultiOptions{
145 PID: math.MaxInt32,
146 })
147 qt.Assert(t, qt.ErrorIs(err, os.ErrNotExist))
148 }
149
150 func TestUprobeMultiResolveOk(t *testing.T) {
151 addrSym1, err := bashEx.address(bashSyms[0], 0, 0)
152 qt.Assert(t, qt.IsNil(err))
153 addrSym2, err := bashEx.address(bashSyms[1], 0, 0)
154 qt.Assert(t, qt.IsNil(err))
155 addrSym3, err := bashEx.address(bashSyms[2], 0, 0)
156 qt.Assert(t, qt.IsNil(err))
157
158 addrs, err := bashEx.addresses(bashSyms, nil, nil)
159 qt.Assert(t, qt.IsNil(err))
160 qt.Assert(t, qt.DeepEquals(addrs, []uint64{addrSym1, addrSym2, addrSym3}))
161
162 addrs, err = bashEx.addresses(bashSyms, nil, []uint64{5, 10, 11})
163 qt.Assert(t, qt.IsNil(err))
164 qt.Assert(t, qt.DeepEquals(addrs, []uint64{addrSym1 + 5, addrSym2 + 10, addrSym3 + 11}))
165
166 addrs, err = bashEx.addresses(bashSyms, []uint64{1, 2, 3}, nil)
167 qt.Assert(t, qt.IsNil(err))
168
169 qt.Assert(t, qt.DeepEquals(addrs, []uint64{1, 2, 3}))
170 }
171
172 func TestUprobeMultiResolveFail(t *testing.T) {
173 // No input
174 _, err := bashEx.addresses(nil, nil, nil)
175 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
176
177 // Different dimensions for Addresses and Offsets
178 _, err = bashEx.addresses(nil, []uint64{100, 200}, []uint64{5, 10, 11})
179 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
180
181 // Different dimensions for symbols and Offsets
182 _, err = bashEx.addresses(bashSyms, nil, []uint64{5, 10})
183 qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
184 }
185
186 func TestUprobeMultiCookie(t *testing.T) {
187 testutils.SkipIfNotSupported(t, features.HaveBPFLinkUprobeMulti())
188
189 prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "")
190
191 // uprobe
192 um, err := bashEx.UprobeMulti(bashSyms, prog,
193 &UprobeMultiOptions{
194 Cookies: []uint64{1, 2, 3},
195 })
196 if err != nil {
197 t.Fatal(err)
198 }
199 _ = um.Close()
200
201 // uretprobe
202 um, err = bashEx.UretprobeMulti(bashSyms, prog,
203 &UprobeMultiOptions{
204 Cookies: []uint64{3, 2, 1},
205 })
206 if err != nil {
207 t.Fatal(err)
208 }
209 _ = um.Close()
210 }
211
212 func TestUprobeMultiProgramCall(t *testing.T) {
213 testutils.SkipIfNotSupported(t, features.HaveBPFLinkUprobeMulti())
214
215 // We execute 'bash --help'
216 args := []string{"--help"}
217 elf := "/bin/bash"
218
219 test := func(retprobe bool, expected uint32) {
220 m, p := newUpdaterMapProg(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti)
221
222 var err error
223
224 // Load the executable.
225 ex, err := OpenExecutable(elf)
226 if err != nil {
227 t.Fatal(err)
228 }
229
230 var um Link
231
232 // Open UprobeMulti on the executable for the given symbol
233 // and attach it to the ebpf program created above.
234 if retprobe {
235 um, err = ex.UretprobeMulti(bashSyms, p, nil)
236 } else {
237 um, err = ex.UprobeMulti(bashSyms, p, nil)
238 }
239 if errors.Is(err, ErrNoSymbol) {
240 // Assume bash_Syms symbols always exist and skip the test
241 // if the symbol can't be found as certain OS (eg. Debian)
242 // strip binaries.
243 t.Skipf("executable %s appear to be stripped, skipping", elf)
244 }
245 if err != nil {
246 t.Fatal(err)
247 }
248
249 // Trigger ebpf program call.
250 trigger := func(t *testing.T) {
251 if err := exec.Command(elf, args...).Run(); err != nil {
252 t.Fatal(err)
253 }
254 }
255 trigger(t)
256
257 // Detach link.
258 if err := um.Close(); err != nil {
259 t.Fatal(err)
260 }
261
262 assertMapValueGE(t, m, 0, expected)
263
264 // Reset map value to 0 at index 0.
265 if err := m.Update(uint32(0), uint32(0), ebpf.UpdateExist); err != nil {
266 t.Fatal(err)
267 }
268
269 // Retrigger the ebpf program call.
270 trigger(t)
271
272 // Assert that this time the value has not been updated.
273 assertMapValue(t, m, 0, 0)
274 }
275
276 // all 3 uprobes should trigger for entry uprobes
277 test(false, 3)
278
279 // We have return uprobe installed on main, _start and check_dev_tty
280 // functions, but only check_dev_tty is triggered, because 'bash --help'
281 // calls exit(0).
282 test(true, 1)
283 }