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
|
package kallsyms
import (
"bytes"
"os"
"testing"
"github.com/go-quicktest/qt"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
)
var syms = []byte(`0000000000000001 t hid_generic_probe [hid_generic]
00000000000000EA t writenote
00000000000000A0 T tcp_connect
00000000000000B0 B empty_zero_page
00000000000000C0 D kimage_vaddr
00000000000000D0 R __start_pci_fixups_early
00000000000000E0 V hv_root_partition
00000000000000F0 W calibrate_delay_is_known
A0000000000000AA a nft_counter_seq [nft_counter]
A0000000000000BA b bootconfig_found
A0000000000000CA d __func__.10
A0000000000000DA r __ksymtab_LZ4_decompress_fast
A0000000000000EA t writenote`)
func TestParseSyms(t *testing.T) {
r := newReader(bytes.NewReader(syms))
i := 0
for ; r.Line(); i++ {
s, err, skip := parseSymbol(r, nil)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.IsFalse(skip))
qt.Assert(t, qt.Not(qt.Equals(s.addr, 0)))
qt.Assert(t, qt.Not(qt.Equals(s.name, "")))
}
qt.Assert(t, qt.IsNil(r.Err()))
qt.Assert(t, qt.Equals(i, 13))
}
func TestParseProcKallsyms(t *testing.T) {
// Read up to 50k symbols from kallsyms to avoid a slow test.
r := newReader(mustOpenProcKallsyms(t))
for i := 0; r.Line() && i < 50_000; i++ {
s, err, skip := parseSymbol(r, nil)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.IsFalse(skip))
qt.Assert(t, qt.Not(qt.Equals(s.name, "")))
}
qt.Assert(t, qt.IsNil(r.Err()))
}
func TestAssignModulesCaching(t *testing.T) {
err := AssignModules(
map[string]string{
"bpf_perf_event_output": "",
"foo": "",
},
)
testutils.SkipIfNotSupportedOnOS(t, err)
qt.Assert(t, qt.IsNil(err))
// Can't assume any kernel modules are loaded, but this symbol should at least
// exist in the kernel. There is no semantic difference between a missing
// symbol and a symbol that doesn't belong to a module.
v, ok := symModules.Load("bpf_perf_event_output")
qt.Assert(t, qt.IsTrue(ok))
qt.Assert(t, qt.Equals(v, ""))
v, ok = symModules.Load("foo")
qt.Assert(t, qt.IsTrue(ok))
qt.Assert(t, qt.Equals(v, ""))
}
func TestAssignModules(t *testing.T) {
mods := map[string]string{
"hid_generic_probe": "",
"nft_counter_seq": "",
"tcp_connect": "",
"foo": "",
}
qt.Assert(t, qt.IsNil(assignModules(bytes.NewBuffer(syms), mods)))
qt.Assert(t, qt.DeepEquals(mods, map[string]string{
"hid_generic_probe": "hid_generic",
"nft_counter_seq": "", // wrong symbol type
"tcp_connect": "",
"foo": "",
}))
qt.Assert(t, qt.ErrorIs(assignModules(bytes.NewBuffer(syms),
map[string]string{"writenote": ""}), errAmbiguousKsym))
}
func TestAssignAddressesCaching(t *testing.T) {
err := AssignAddresses(
map[string]uint64{
"bpf_perf_event_output": 0,
"foo": 0,
},
)
testutils.SkipIfNotSupportedOnOS(t, err)
qt.Assert(t, qt.IsNil(err))
v, ok := symAddrs.Load("bpf_perf_event_output")
qt.Assert(t, qt.IsTrue(ok))
qt.Assert(t, qt.Not(qt.Equals(v, 0)))
v, ok = symAddrs.Load("foo")
qt.Assert(t, qt.IsTrue(ok))
qt.Assert(t, qt.Equals(v, 0))
}
func TestAssignAddresses(t *testing.T) {
b := bytes.NewBuffer(syms)
ksyms := map[string]uint64{
"hid_generic_probe": 0,
"tcp_connect": 0,
"bootconfig_found": 0,
}
qt.Assert(t, qt.IsNil(assignAddresses(b, ksyms)))
qt.Assert(t, qt.Equals(ksyms["hid_generic_probe"], 0x1))
qt.Assert(t, qt.Equals(ksyms["tcp_connect"], 0xA0))
qt.Assert(t, qt.Equals(ksyms["bootconfig_found"], 0xA0000000000000BA))
b = bytes.NewBuffer(syms)
ksyms = map[string]uint64{
"hid_generic_probe": 0,
"writenote": 0,
}
qt.Assert(t, qt.ErrorIs(assignAddresses(b, ksyms), errAmbiguousKsym))
}
func BenchmarkSymbolKmods(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
b.StopTimer()
f := mustOpenProcKallsyms(b)
want := map[string]string{
"bpf_trace_vprintk": "",
"bpf_send_signal": "",
"bpf_event_notify": "",
"bpf_trace_printk": "",
"bpf_perf_event_output": "",
}
b.StartTimer()
if err := assignModules(f, want); err != nil {
b.Fatal(err)
}
}
}
// Benchmark getting 5 kernel symbols from /proc/kallsyms.
func BenchmarkAssignAddresses(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
b.StopTimer()
f := mustOpenProcKallsyms(b)
want := map[string]uint64{
"bpf_trace_vprintk": 0,
"bpf_send_signal": 0,
"bpf_event_notify": 0,
"bpf_trace_printk": 0,
"bpf_perf_event_output": 0,
}
b.StartTimer()
if err := assignAddresses(f, want); err != nil {
b.Fatal(err)
}
}
}
func mustOpenProcKallsyms(tb testing.TB) *os.File {
tb.Helper()
if !internal.OnLinux {
tb.Skip("/proc/kallsyms is a Linux concept")
}
f, err := os.Open("/proc/kallsyms")
qt.Assert(tb, qt.IsNil(err))
tb.Cleanup(func() { f.Close() })
return f
}
|