/
btf_test.go
379 lines (318 loc) · 8.51 KB
/
btf_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package btf
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"sync"
"testing"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
)
var vmlinux struct {
sync.Once
err error
raw []byte
}
func readVMLinux(tb testing.TB) *bytes.Reader {
tb.Helper()
vmlinux.Do(func() {
vmlinux.raw, vmlinux.err = internal.ReadAllCompressed("testdata/vmlinux-btf.gz")
})
if vmlinux.err != nil {
tb.Fatal(vmlinux.err)
}
return bytes.NewReader(vmlinux.raw)
}
func parseELFBTF(tb testing.TB, file string) *Spec {
tb.Helper()
spec, err := LoadSpec(file)
if err != nil {
tb.Fatal("Can't load BTF:", err)
}
return spec
}
func TestAnyTypesByName(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "testdata/relocs-*.elf"), func(t *testing.T, file string) {
spec := parseELFBTF(t, file)
types, err := spec.AnyTypesByName("ambiguous")
if err != nil {
t.Fatal(err)
}
if len(types) != 1 {
t.Fatalf("expected to receive exactly 1 types from querying ambiguous type, got: %v", types)
}
types, err = spec.AnyTypesByName("ambiguous___flavour")
if err != nil {
t.Fatal(err)
}
if len(types) != 1 {
t.Fatalf("expected to receive exactly 1 type from querying ambiguous flavour, got: %v", types)
}
})
}
func TestTypeByNameAmbiguous(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "testdata/relocs-*.elf"), func(t *testing.T, file string) {
spec := parseELFBTF(t, file)
var typ *Struct
if err := spec.TypeByName("ambiguous", &typ); err != nil {
t.Fatal(err)
}
if name := typ.TypeName(); name != "ambiguous" {
t.Fatal("expected type name 'ambiguous', got:", name)
}
if err := spec.TypeByName("ambiguous___flavour", &typ); err != nil {
t.Fatal(err)
}
if name := typ.TypeName(); name != "ambiguous___flavour" {
t.Fatal("expected type name 'ambiguous___flavour', got:", name)
}
})
}
func TestTypeByName(t *testing.T) {
spec, err := LoadSpecFromReader(readVMLinux(t))
if err != nil {
t.Fatal(err)
}
for _, typ := range []interface{}{
nil,
Struct{},
&Struct{},
[]Struct{},
&[]Struct{},
map[int]Struct{},
&map[int]Struct{},
int(0),
new(int),
} {
t.Run(fmt.Sprintf("%T", typ), func(t *testing.T) {
// spec.TypeByName MUST fail if typ is a nil btf.Type.
if err := spec.TypeByName("iphdr", typ); err == nil {
t.Fatalf("TypeByName does not fail with type %T", typ)
}
})
}
// spec.TypeByName MUST return the same address for multiple calls with the same type name.
var iphdr1, iphdr2 *Struct
if err := spec.TypeByName("iphdr", &iphdr1); err != nil {
t.Fatal(err)
}
if err := spec.TypeByName("iphdr", &iphdr2); err != nil {
t.Fatal(err)
}
if iphdr1 != iphdr2 {
t.Fatal("multiple TypeByName calls for `iphdr` name do not return the same addresses")
}
// Excerpt from linux/ip.h, https://elixir.bootlin.com/linux/latest/A/ident/iphdr
//
// struct iphdr {
// #if defined(__LITTLE_ENDIAN_BITFIELD)
// __u8 ihl:4, version:4;
// #elif defined (__BIG_ENDIAN_BITFIELD)
// __u8 version:4, ihl:4;
// #else
// ...
// }
//
// The BTF we test against is for little endian.
m := iphdr1.Members[1]
if m.Name != "version" {
t.Fatal("Expected version as the second member, got", m.Name)
}
td, ok := m.Type.(*Typedef)
if !ok {
t.Fatalf("version member of iphdr should be a __u8 typedef: actual: %T", m.Type)
}
u8, ok := td.Type.(*Int)
if !ok {
t.Fatalf("__u8 typedef should point to an Int type: actual: %T", td.Type)
}
if m.BitfieldSize != 4 {
t.Fatalf("incorrect bitfield size: expected: 4 actual: %d", m.BitfieldSize)
}
if u8.Encoding != 0 {
t.Fatalf("incorrect encoding of an __u8 int: expected: 0 actual: %x", u8.Encoding)
}
if m.Offset != 4 {
t.Fatalf("incorrect bitfield offset: expected: 4 actual: %d", m.Offset)
}
}
func BenchmarkParseVmlinux(b *testing.B) {
rd := readVMLinux(b)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, err := rd.Seek(0, io.SeekStart); err != nil {
b.Fatal(err)
}
if _, err := loadRawSpec(rd, binary.LittleEndian, nil, nil); err != nil {
b.Fatal("Can't load BTF:", err)
}
}
}
func TestParseCurrentKernelBTF(t *testing.T) {
spec, err := loadKernelSpec()
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't load BTF:", err)
}
if len(spec.namedTypes) == 0 {
t.Fatal("Empty kernel BTF")
}
totalBytes := 0
distinct := 0
seen := make(map[string]bool)
for _, str := range spec.strings.strings {
totalBytes += len(str)
if !seen[str] {
distinct++
seen[str] = true
}
}
t.Logf("%d strings total, %d distinct", len(spec.strings.strings), distinct)
t.Logf("Average string size: %.0f", float64(totalBytes)/float64(len(spec.strings.strings)))
}
func TestFindVMLinux(t *testing.T) {
file, err := findVMLinux()
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't find vmlinux:", err)
}
defer file.Close()
spec, err := loadSpecFromELF(file)
if err != nil {
t.Fatal("Can't load BTF:", err)
}
if len(spec.namedTypes) == 0 {
t.Fatal("Empty kernel BTF")
}
}
func TestLoadSpecFromElf(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "../testdata/loader-e*.elf"), func(t *testing.T, file string) {
spec := parseELFBTF(t, file)
vt, err := spec.TypeByID(0)
if err != nil {
t.Error("Can't retrieve void type by ID:", err)
}
if _, ok := vt.(*Void); !ok {
t.Errorf("Expected Void for type id 0, but got: %T", vt)
}
var bpfMapDef *Struct
if err := spec.TypeByName("bpf_map_def", &bpfMapDef); err != nil {
t.Error("Can't find bpf_map_def:", err)
}
var tmp *Void
if err := spec.TypeByName("totally_bogus_type", &tmp); !errors.Is(err, ErrNotFound) {
t.Error("TypeByName doesn't return ErrNotFound:", err)
}
var fn *Func
if err := spec.TypeByName("global_fn", &fn); err != nil {
t.Error("Can't find global_fn():", err)
} else {
if fn.Linkage != GlobalFunc {
t.Error("Expected global linkage:", fn)
}
}
var v *Var
if err := spec.TypeByName("key3", &v); err != nil {
t.Error("Cant find key3:", err)
} else {
if v.Linkage != GlobalVar {
t.Error("Expected global linkage:", v)
}
}
if spec.byteOrder != internal.NativeEndian {
return
}
t.Run("Handle", func(t *testing.T) {
btf, err := NewHandle(spec)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't load BTF:", err)
}
defer btf.Close()
})
})
}
func TestLoadKernelSpec(t *testing.T) {
if _, err := os.Stat("/sys/kernel/btf/vmlinux"); os.IsNotExist(err) {
t.Skip("/sys/kernel/btf/vmlinux not present")
}
_, err := LoadKernelSpec()
if err != nil {
t.Fatal("Can't load kernel spec:", err)
}
}
func TestGuessBTFByteOrder(t *testing.T) {
bo := guessRawBTFByteOrder(readVMLinux(t))
if bo != binary.LittleEndian {
t.Fatalf("Guessed %s instead of %s", bo, binary.LittleEndian)
}
}
func TestSpecCopy(t *testing.T) {
spec := parseELFBTF(t, "../testdata/loader-el.elf")
if len(spec.types) < 1 {
t.Fatal("Not enough types")
}
cpy := spec.Copy()
for i := range cpy.types {
if _, ok := cpy.types[i].(*Void); ok {
// Since Void is an empty struct, a Type interface value containing
// &Void{} stores (*Void, nil). Since interface equality first compares
// the type and then the concrete value, Void is always equal.
continue
}
if cpy.types[i] == spec.types[i] {
t.Fatalf("Type at index %d is not a copy: %T == %T", i, cpy.types[i], spec.types[i])
}
}
}
func TestHaveBTF(t *testing.T) {
testutils.CheckFeatureTest(t, haveBTF)
}
func TestHaveFuncLinkage(t *testing.T) {
testutils.CheckFeatureTest(t, haveFuncLinkage)
}
func ExampleSpec_TypeByName() {
// Acquire a Spec via one of its constructors.
spec := new(Spec)
// Declare a variable of the desired type
var foo *Struct
if err := spec.TypeByName("foo", &foo); err != nil {
// There is no struct with name foo, or there
// are multiple possibilities.
}
// We've found struct foo
fmt.Println(foo.Name)
}
func TestTypesIterator(t *testing.T) {
spec, err := LoadSpecFromReader(readVMLinux(t))
if err != nil {
t.Fatal(err)
}
if len(spec.types) < 1 {
t.Fatal("Not enough types")
}
// Assertion that 'iphdr' type exists within the spec
_, err = spec.AnyTypeByName("iphdr")
if err != nil {
t.Fatalf("Failed to find 'iphdr' type by name: %s", err)
}
found := false
count := 0
iter := spec.NewTypesIterator()
for iter.Next() {
if !found && iter.Type.TypeName() == "iphdr" {
found = true
}
count += 1
}
if l := len(spec.types); l != count {
t.Fatalf("Failed to iterate over all types (%d vs %d)", l, count)
}
if !found {
t.Fatal("Cannot find 'iphdr' type")
}
}