| 1 |
package kallsyms |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"os" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/go-quicktest/qt" |
| 9 |
|
| 10 |
"github.com/cilium/ebpf/internal/platform" |
| 11 |
"github.com/cilium/ebpf/internal/testutils" |
| 12 |
) |
| 13 |
|
| 14 |
var syms = []byte(`0000000000000001 t hid_generic_probe [hid_generic] |
| 15 |
00000000000000EA t writenote |
| 16 |
00000000000000A0 T tcp_connect |
| 17 |
00000000000000B0 B empty_zero_page |
| 18 |
00000000000000C0 D kimage_vaddr |
| 19 |
00000000000000D0 R __start_pci_fixups_early |
| 20 |
00000000000000E0 V hv_root_partition |
| 21 |
00000000000000F0 W calibrate_delay_is_known |
| 22 |
A0000000000000AA a nft_counter_seq [nft_counter] |
| 23 |
A0000000000000BA b bootconfig_found |
| 24 |
A0000000000000CA d __func__.10 |
| 25 |
A0000000000000DA r __ksymtab_LZ4_decompress_fast |
| 26 |
A0000000000000EA t writenote |
| 27 |
A0000000000000FA T bench_sym [bench_mod] |
| 28 |
A0000000000000FF t __kstrtab_功能 [mod]`) |
| 29 |
|
| 30 |
func TestParseSyms(t *testing.T) { |
| 31 |
r := newReader(bytes.NewReader(syms)) |
| 32 |
i := 0 |
| 33 |
for ; r.Line(); i++ { |
| 34 |
s, err, skip := parseSymbol(r, nil) |
| 35 |
qt.Assert(t, qt.IsNil(err)) |
| 36 |
qt.Assert(t, qt.IsFalse(skip)) |
| 37 |
qt.Assert(t, qt.Not(qt.Equals(s.addr, 0))) |
| 38 |
qt.Assert(t, qt.Not(qt.Equals(s.name, []byte("")))) |
| 39 |
} |
| 40 |
qt.Assert(t, qt.IsNil(r.Err())) |
| 41 |
qt.Assert(t, qt.Equals(i, 15)) |
| 42 |
} |
| 43 |
|
| 44 |
func TestParseProcKallsyms(t *testing.T) { |
| 45 |
|
| 46 |
r := newReader(mustOpenProcKallsyms(t)) |
| 47 |
for i := 0; r.Line() && i < 50_000; i++ { |
| 48 |
s, err, skip := parseSymbol(r, nil) |
| 49 |
qt.Assert(t, qt.IsNil(err)) |
| 50 |
qt.Assert(t, qt.IsFalse(skip)) |
| 51 |
qt.Assert(t, qt.Not(qt.Equals(s.name, []byte("")))) |
| 52 |
} |
| 53 |
qt.Assert(t, qt.IsNil(r.Err())) |
| 54 |
} |
| 55 |
|
| 56 |
func TestAssignAddressesCaching(t *testing.T) { |
| 57 |
err := AssignAddresses( |
| 58 |
map[string]uint64{ |
| 59 |
"bpf_perf_event_output": 0, |
| 60 |
"foo": 0, |
| 61 |
}, |
| 62 |
) |
| 63 |
testutils.SkipIfNotSupportedOnOS(t, err) |
| 64 |
qt.Assert(t, qt.IsNil(err)) |
| 65 |
|
| 66 |
v, ok := symAddrs.Load("bpf_perf_event_output") |
| 67 |
qt.Assert(t, qt.IsTrue(ok)) |
| 68 |
qt.Assert(t, qt.Not(qt.Equals(v, 0))) |
| 69 |
|
| 70 |
v, ok = symAddrs.Load("foo") |
| 71 |
qt.Assert(t, qt.IsTrue(ok)) |
| 72 |
qt.Assert(t, qt.Equals(v, 0)) |
| 73 |
} |
| 74 |
|
| 75 |
func TestAssignAddresses(t *testing.T) { |
| 76 |
b := bytes.NewBuffer(syms) |
| 77 |
ksyms := map[string]uint64{ |
| 78 |
"hid_generic_probe": 0, |
| 79 |
"tcp_connect": 0, |
| 80 |
"bootconfig_found": 0, |
| 81 |
} |
| 82 |
qt.Assert(t, qt.IsNil(assignAddresses(b, ksyms))) |
| 83 |
|
| 84 |
qt.Assert(t, qt.Equals(ksyms["hid_generic_probe"], 0x1)) |
| 85 |
qt.Assert(t, qt.Equals(ksyms["tcp_connect"], 0xA0)) |
| 86 |
qt.Assert(t, qt.Equals(ksyms["bootconfig_found"], 0xA0000000000000BA)) |
| 87 |
|
| 88 |
b = bytes.NewBuffer(syms) |
| 89 |
ksyms = map[string]uint64{ |
| 90 |
"hid_generic_probe": 0, |
| 91 |
"writenote": 0, |
| 92 |
} |
| 93 |
qt.Assert(t, qt.ErrorIs(assignAddresses(b, ksyms), errAmbiguousKsym)) |
| 94 |
} |
| 95 |
|
| 96 |
func BenchmarkAssignAddresses(b *testing.B) { |
| 97 |
b.ReportAllocs() |
| 98 |
for b.Loop() { |
| 99 |
b.StopTimer() |
| 100 |
f := bytes.NewBuffer(syms) |
| 101 |
want := map[string]uint64{"bench_sym": 0} |
| 102 |
b.StartTimer() |
| 103 |
|
| 104 |
if err := assignAddresses(f, want); err != nil { |
| 105 |
b.Fatal(err) |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
func BenchmarkAssignAddressesKallsyms(b *testing.B) { |
| 112 |
b.ReportAllocs() |
| 113 |
for b.Loop() { |
| 114 |
b.StopTimer() |
| 115 |
f := mustOpenProcKallsyms(b) |
| 116 |
want := map[string]uint64{ |
| 117 |
"bpf_trace_vprintk": 0, |
| 118 |
"bpf_send_signal": 0, |
| 119 |
"bpf_event_notify": 0, |
| 120 |
"bpf_trace_printk": 0, |
| 121 |
"bpf_perf_event_output": 0, |
| 122 |
} |
| 123 |
b.StartTimer() |
| 124 |
|
| 125 |
if err := assignAddresses(f, want); err != nil { |
| 126 |
b.Fatal(err) |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
func mustOpenProcKallsyms(tb testing.TB) *os.File { |
| 132 |
tb.Helper() |
| 133 |
|
| 134 |
if !platform.IsLinux { |
| 135 |
tb.Skip("/proc/kallsyms is a Linux concept") |
| 136 |
} |
| 137 |
|
| 138 |
f, err := os.Open("/proc/kallsyms") |
| 139 |
qt.Assert(tb, qt.IsNil(err)) |
| 140 |
tb.Cleanup(func() { f.Close() }) |
| 141 |
return f |
| 142 |
} |