Skip to content

Commit eb0f746

Browse files
puwunti-mo
andcommitted
elf_reader: add tests for ELF objects without BTF
Adds infrastructure to test loading of ELF objects compiled without BTF information. Signed-off-by: Pavan More <pavansmore05@gmail.com> Co-authored-by: Timo Beckers <timo@isovalent.com>
1 parent 7cf6126 commit eb0f746

15 files changed

+242
-195
lines changed

Makefile

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ TARGETS := \
4545
testdata/loader-clang-14 \
4646
testdata/loader-clang-17 \
4747
testdata/loader-$(CLANG) \
48+
testdata/loader_nobtf \
4849
testdata/manyprogs \
4950
testdata/btf_map_init \
5051
testdata/invalid_map \
@@ -75,6 +76,8 @@ TARGETS := \
7576
btf/testdata/tags \
7677
cmd/bpf2go/testdata/minimal
7778

79+
HEADERS := $(wildcard testdata/*.h)
80+
7881
.PHONY: all clean container-all container-shell generate
7982

8083
.DEFAULT_TARGET = container-all
@@ -110,19 +113,25 @@ generate:
110113

111114
testdata: $(addsuffix -el.elf,$(TARGETS)) $(addsuffix -eb.elf,$(TARGETS)) $(addsuffix -el.elf,$(TARGETS_EL))
112115

113-
testdata/loader-%-el.elf: testdata/loader.c
116+
testdata/loader-%-el.elf: testdata/loader.c $(HEADERS)
114117
$* $(CFLAGS) -target bpfel -c $< -o $@
115118
$(STRIP) -g $@
116119

117-
testdata/loader-%-eb.elf: testdata/loader.c
120+
testdata/loader-%-eb.elf: testdata/loader.c $(HEADERS)
118121
$* $(CFLAGS) -target bpfeb -c $< -o $@
119122
$(STRIP) -g $@
120123

121-
%-el.elf: %.c
124+
testdata/loader_nobtf-el.elf: testdata/loader.c $(HEADERS)
125+
$(CLANG) $(CFLAGS) -g0 -D__NOBTF__ -target bpfel -c $< -o $@
126+
127+
testdata/loader_nobtf-eb.elf: testdata/loader.c $(HEADERS)
128+
$(CLANG) $(CFLAGS) -g0 -D__NOBTF__ -target bpfeb -c $< -o $@
129+
130+
%-el.elf: %.c $(HEADERS)
122131
$(CLANG) $(CFLAGS) -target bpfel -c $< -o $@
123132
$(STRIP) -g $@
124133

125-
%-eb.elf : %.c
134+
%-eb.elf: %.c $(HEADERS)
126135
$(CLANG) $(CFLAGS) -target bpfeb -c $< -o $@
127136
$(STRIP) -g $@
128137

btf/testdata/tags-eb.elf

3.09 KB
Binary file not shown.

collection_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ func BenchmarkNewCollection(b *testing.B) {
584584
b.Fatal(err)
585585
}
586586

587-
spec.Maps["array_of_hash_map"].InnerMap = spec.Maps["hash_map"]
588587
for _, m := range spec.Maps {
589588
m.Pinning = PinNone
590589
}

elf_reader_test.go

Lines changed: 94 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"flag"
88
"fmt"
9+
"maps"
910
"os"
1011
"path/filepath"
1112
"strings"
@@ -59,76 +60,11 @@ func TestLoadCollectionSpec(t *testing.T) {
5960
ValueSize: 8,
6061
MaxEntries: 2,
6162
},
62-
"array_of_hash_map": {
63-
Name: "array_of_hash_map",
64-
Type: ArrayOfMaps,
65-
KeySize: 4,
66-
MaxEntries: 2,
67-
},
6863
"perf_event_array": {
6964
Name: "perf_event_array",
7065
Type: PerfEventArray,
7166
MaxEntries: 4096,
7267
},
73-
"btf_pin": {
74-
Name: "btf_pin",
75-
Type: Hash,
76-
KeySize: 4,
77-
ValueSize: 8,
78-
MaxEntries: 1,
79-
Pinning: PinByName,
80-
},
81-
"bpf_decl_map": {
82-
Name: "bpf_decl_map",
83-
Type: Array,
84-
KeySize: 4,
85-
ValueSize: 8,
86-
MaxEntries: 1,
87-
Tags: []string{"a", "b"},
88-
},
89-
"btf_decl_map": {
90-
Name: "btf_decl_map",
91-
Type: Array,
92-
KeySize: 4,
93-
ValueSize: 8,
94-
MaxEntries: 1,
95-
Tags: []string{"a", "b"},
96-
},
97-
"btf_outer_map": {
98-
Name: "btf_outer_map",
99-
Type: ArrayOfMaps,
100-
KeySize: 4,
101-
ValueSize: 4,
102-
MaxEntries: 1,
103-
InnerMap: &MapSpec{
104-
Name: "btf_outer_map_inner",
105-
Type: Hash,
106-
KeySize: 4,
107-
ValueSize: 4,
108-
MaxEntries: 1,
109-
},
110-
},
111-
"btf_outer_map_anon": {
112-
Name: "btf_outer_map_anon",
113-
Type: ArrayOfMaps,
114-
KeySize: 4,
115-
ValueSize: 4,
116-
MaxEntries: 1,
117-
InnerMap: &MapSpec{
118-
Name: "btf_outer_map_anon_inner",
119-
Type: Hash,
120-
KeySize: 4,
121-
ValueSize: 4,
122-
MaxEntries: 1,
123-
},
124-
},
125-
"btf_typedef_map": {
126-
Name: "btf_typedef_map",
127-
Type: Array,
128-
KeySize: 4,
129-
ValueSize: 8,
130-
MaxEntries: 1,
131-
},
13268
".bss": {
13369
Name: ".bss",
13470
Type: Array,
@@ -232,38 +168,107 @@ func TestLoadCollectionSpec(t *testing.T) {
232168
},
233169
}
234170

171+
// BTF-only maps.
172+
btfOnly := map[string]*MapSpec{
173+
"btf_pin": {
174+
Name: "btf_pin",
175+
Type: Hash,
176+
KeySize: 4,
177+
ValueSize: 8,
178+
MaxEntries: 1,
179+
Pinning: PinByName,
180+
},
181+
"bpf_decl_map": {
182+
Name: "bpf_decl_map",
183+
Type: Array,
184+
KeySize: 4,
185+
ValueSize: 8,
186+
MaxEntries: 1,
187+
Tags: []string{"a", "b"},
188+
},
189+
"btf_decl_map": {
190+
Name: "btf_decl_map",
191+
Type: Array,
192+
KeySize: 4,
193+
ValueSize: 8,
194+
MaxEntries: 1,
195+
Tags: []string{"a", "b"},
196+
},
197+
"btf_outer_map": {
198+
Name: "btf_outer_map",
199+
Type: ArrayOfMaps,
200+
KeySize: 4,
201+
ValueSize: 4,
202+
MaxEntries: 1,
203+
InnerMap: &MapSpec{
204+
Name: "btf_outer_map_inner",
205+
Type: Hash,
206+
KeySize: 4,
207+
ValueSize: 4,
208+
MaxEntries: 1,
209+
},
210+
},
211+
"btf_outer_map_anon": {
212+
Name: "btf_outer_map_anon",
213+
Type: ArrayOfMaps,
214+
KeySize: 4,
215+
ValueSize: 4,
216+
MaxEntries: 1,
217+
InnerMap: &MapSpec{
218+
Name: "btf_outer_map_anon_inner",
219+
Type: Hash,
220+
KeySize: 4,
221+
ValueSize: 4,
222+
MaxEntries: 1,
223+
},
224+
},
225+
"btf_typedef_map": {
226+
Name: "btf_typedef_map",
227+
Type: Array,
228+
KeySize: 4,
229+
ValueSize: 8,
230+
MaxEntries: 1,
231+
},
232+
}
233+
235234
testutils.Files(t, testutils.Glob(t, "testdata/loader-*.elf"), func(t *testing.T, file string) {
236-
have, err := LoadCollectionSpec(file)
237-
if err != nil {
238-
t.Fatal("Can't parse ELF:", err)
239-
}
235+
got, err := LoadCollectionSpec(file)
236+
qt.Assert(t, qt.IsNil(err))
240237

241-
qt.Assert(t, qt.Equals(have.Maps["perf_event_array"].ValueSize, 0))
242-
qt.Assert(t, qt.IsNotNil(have.Maps["perf_event_array"].Value))
238+
// BTF map definition contains a value type, but the size should remain 0.
239+
// The value type needs to be reflected in the MapSpec.
240+
qt.Assert(t, qt.Equals(got.Maps["perf_event_array"].ValueSize, 0))
241+
qt.Assert(t, qt.IsNotNil(got.Maps["perf_event_array"].Value))
243242

244-
qt.Assert(t, qt.CmpEquals(have, coll, csCmpOpts))
243+
// Copy and extend the CollectionSpec with BTF-only objects.
244+
want := coll.Copy()
245+
maps.Copy(want.Maps, btfOnly)
245246

246-
qt.Assert(t, qt.IsNil(have.Variables["arg"].Set(uint32(1))))
247-
qt.Assert(t, qt.IsNil(have.Variables["arg2"].Set(uint32(2))))
247+
testLoadCollectionSpec(t, got, want)
248+
})
248249

249-
have.Maps["array_of_hash_map"].InnerMap = have.Maps["hash_map"]
250-
coll, err := newCollection(t, have, &CollectionOptions{
251-
Maps: MapOptions{
252-
PinPath: testutils.TempBPFFS(t),
253-
},
254-
Programs: ProgramOptions{
255-
LogLevel: LogLevelBranch,
256-
},
257-
})
250+
testutils.Files(t, testutils.Glob(t, "testdata/loader_nobtf-*.elf"), func(t *testing.T, file string) {
251+
got, err := LoadCollectionSpec(file)
252+
qt.Assert(t, qt.IsNil(err))
258253

259-
testutils.SkipIfNotSupported(t, err)
260-
if err != nil {
261-
t.Fatal(err)
262-
}
254+
testLoadCollectionSpec(t, got, coll.Copy())
255+
})
256+
}
257+
258+
func testLoadCollectionSpec(t *testing.T, got, want *CollectionSpec) {
259+
t.Helper()
263260

264-
ret := mustRun(t, coll.Programs["xdp_prog"], nil)
265-
qt.Assert(t, qt.Equals(ret, 7))
261+
qt.Assert(t, qt.CmpEquals(got, want, csCmpOpts))
262+
263+
coll, err := newCollection(t, got, &CollectionOptions{
264+
Maps: MapOptions{PinPath: testutils.TempBPFFS(t)},
265+
Programs: ProgramOptions{LogLevel: LogLevelBranch},
266266
})
267+
testutils.SkipIfNotSupported(t, err)
268+
qt.Assert(t, qt.IsNil(err))
269+
270+
ret := mustRun(t, coll.Programs["xdp_prog"], nil)
271+
qt.Assert(t, qt.Equals(ret, 7))
267272
}
268273

269274
func BenchmarkELFLoader(b *testing.B) {

testdata/loader-clang-14-eb.elf

-112 Bytes
Binary file not shown.

testdata/loader-clang-14-el.elf

-112 Bytes
Binary file not shown.

testdata/loader-clang-17-eb.elf

-112 Bytes
Binary file not shown.

testdata/loader-clang-17-el.elf

-112 Bytes
Binary file not shown.

testdata/loader-clang-20-eb.elf

-104 Bytes
Binary file not shown.

testdata/loader-clang-20-el.elf

-104 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)