Skip to content

Commit

Permalink
btf: Make test for ld64imm CO-RE relocations
Browse files Browse the repository at this point in the history
Added a test which makes sure we do not mess up offsets when replacing
a ld64imm with poisoned instructions.

Signed-off-by: Dylan Reimerink <[email protected]>
  • Loading branch information
dylandreimerink authored and lmb committed Feb 21, 2024
1 parent 59cb715 commit b24722c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ TARGETS := \
btf/testdata/relocs \
btf/testdata/relocs_read \
btf/testdata/relocs_read_tgt \
btf/testdata/relocs_enum \
cmd/bpf2go/testdata/minimal

.PHONY: all clean container-all container-shell generate
Expand Down
27 changes: 27 additions & 0 deletions btf/core_reloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,30 @@ func TestCORERelocationRead(t *testing.T) {
}
})
}

func TestLD64IMMReloc(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.4", "vmlinux BTF in sysfs")

testutils.Files(t, testutils.Glob(t, "testdata/relocs_enum-*.elf"), func(t *testing.T, file string) {
fh, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer fh.Close()

spec, err := ebpf.LoadCollectionSpecFromReader(fh)
if err != nil {
t.Fatal(err)
}

if spec.ByteOrder != internal.NativeEndian {
return
}

coll, err := ebpf.NewCollection(spec)
if err != nil {
t.Fatal(err)
}
defer coll.Close()
})
}
Binary file added btf/testdata/relocs_enum-eb.elf
Binary file not shown.
Binary file added btf/testdata/relocs_enum-el.elf
Binary file not shown.
16 changes: 16 additions & 0 deletions btf/testdata/relocs_enum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "bpf_core_read.h"

enum cgroup_subsys_id {
cpuset_cgrp_id,
cpuset_cgrp_id_lublub,
CGROUP_SUBSYS_COUNT,
};

#define __section(NAME) __attribute__((section(NAME), used))

__section("socket/core_ld64imm") int core_ld64imm() {
if (bpf_core_enum_value_exists(enum cgroup_subsys_id, cpuset_cgrp_id_lublub)) {
__attribute__((unused)) const volatile int val = bpf_core_enum_value(enum cgroup_subsys_id, cpuset_cgrp_id_lublub);
}
return 0;
}

0 comments on commit b24722c

Please sign in to comment.