Debian Codegraph @

btf/kernel_test.go

raw symbols SCIP
1 package btf
2
3 import (
4 "os"
5 "runtime"
6 "sync"
7 "testing"
8
9 "github.com/cilium/ebpf/internal/testutils"
10
11 "github.com/go-quicktest/qt"
12 )
13
14 func TestLoadKernelSpec(t *testing.T) {
15 if _, err := os.Stat("/sys/kernel/btf/vmlinux"); os.IsNotExist(err) {
16 t.Skip("/sys/kernel/btf/vmlinux not present")
17 }
18
19 spec, err := LoadKernelSpec()
20 if err != nil {
21 t.Fatal("Can't load kernel spec:", err)
22 }
23
24 if !testutils.IsVersionLessThan(t, "linux:6.16") {
25 maps, err := os.ReadFile("/proc/self/maps")
26 qt.Assert(t, qt.IsNil(err))
27 qt.Assert(t, qt.StringContains(string(maps), " /sys/kernel/btf/vmlinux\n"))
28 }
29
30 // Prevent finalizer from unmapping vmlinux.
31 runtime.KeepAlive(spec)
32 }
33
34 func TestLoadKernelModuleSpec(t *testing.T) {
35 if _, err := os.Stat("/sys/kernel/btf/bpf_testmod"); os.IsNotExist(err) {
36 t.Skip("/sys/kernel/btf/bpf_testmod not present")
37 }
38
39 _, err := LoadKernelModuleSpec("bpf_testmod")
40 qt.Assert(t, qt.IsNil(err))
41 }
42
43 func TestCache(t *testing.T) {
44 c := NewCache()
45
46 qt.Assert(t, qt.IsNil(c.kernelTypes))
47 qt.Assert(t, qt.HasLen(c.moduleTypes, 0))
48 qt.Assert(t, qt.IsNil(c.loadedModules))
49
50 // Test that Kernel() creates only one copy per Cache instance.
51 spec1, err := c.Kernel()
52 testutils.SkipIfNotSupported(t, err)
53 qt.Assert(t, qt.IsNil(err))
54 qt.Assert(t, qt.IsNotNil(spec1))
55
56 spec2, err := c.Kernel()
57 qt.Assert(t, qt.IsNil(err))
58 qt.Assert(t, qt.IsNotNil(spec2))
59
60 qt.Assert(t, qt.Equals(spec1, spec2))
61
62 // Test that Module() creates only one copy per Cache instance.
63 mod1, err := c.Module("bpf_testmod")
64 if !os.IsNotExist(err) {
65 qt.Assert(t, qt.IsNil(err))
66 qt.Assert(t, qt.IsNotNil(mod1))
67
68 mod2, err := c.Module("bpf_testmod")
69 qt.Assert(t, qt.IsNil(err))
70 qt.Assert(t, qt.IsNotNil(mod2))
71
72 qt.Assert(t, qt.Equals(mod1, mod2))
73 }
74
75 // Test that Modules only reads modules once.
76 _, err = c.Modules()
77 qt.Assert(t, qt.IsNil(err))
78 qt.Assert(t, qt.IsNotNil(c.loadedModules))
79 }
80
81 func TestCacheConcurrentKernel(t *testing.T) {
82 if _, err := os.Stat("/sys/kernel/btf/vmlinux"); os.IsNotExist(err) {
83 t.Skip("/sys/kernel/btf/vmlinux not present")
84 }
85
86 const goroutines = 8
87
88 c := NewCache()
89 specs := make([]*Spec, goroutines)
90 errs := make([]error, goroutines)
91
92 var wg sync.WaitGroup
93 wg.Add(goroutines)
94 for i := range goroutines {
95 go func(i int) {
96 defer wg.Done()
97 specs[i], errs[i] = c.Kernel()
98 }(i)
99 }
100 wg.Wait()
101
102 for _, err := range errs {
103 qt.Assert(t, qt.IsNil(err))
104 }
105 for i := 1; i < goroutines; i++ {
106 qt.Assert(t, qt.Equals(specs[0], specs[i]))
107 }
108 }
109
110 func TestCacheConcurrentModule(t *testing.T) {
111 if _, err := os.Stat("/sys/kernel/btf/bpf_testmod"); os.IsNotExist(err) {
112 t.Skip("/sys/kernel/btf/bpf_testmod not present")
113 }
114
115 const goroutines = 8
116
117 c := NewCache()
118 specs := make([]*Spec, goroutines)
119 errs := make([]error, goroutines)
120
121 var wg sync.WaitGroup
122 wg.Add(goroutines)
123 for i := range goroutines {
124 go func(i int) {
125 defer wg.Done()
126 specs[i], errs[i] = c.Module("bpf_testmod")
127 }(i)
128 }
129 wg.Wait()
130
131 for _, err := range errs {
132 qt.Assert(t, qt.IsNil(err))
133 }
134 for i := 1; i < goroutines; i++ {
135 qt.Assert(t, qt.Equals(specs[0], specs[i]))
136 }
137 }