Skip to content

Commit abc21e1

Browse files
kkdwvdNobody
authored andcommitted
selftests/bpf: Add test_verifier support to fixup kfunc call insns
This allows us to add tests (esp. negative tests) where we only want to ensure the program doesn't pass through the verifier, and also verify the error. The next commit will add the tests making use of this. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
1 parent 9d1aafe commit abc21e1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/if_ether.h>
3232
#include <linux/btf.h>
3333

34+
#include <bpf/btf.h>
3435
#include <bpf/bpf.h>
3536
#include <bpf/libbpf.h>
3637

@@ -66,6 +67,11 @@ static bool unpriv_disabled = false;
6667
static int skips;
6768
static bool verbose = false;
6869

70+
struct kfunc_btf_id_pair {
71+
const char *kfunc;
72+
int insn_idx;
73+
};
74+
6975
struct bpf_test {
7076
const char *descr;
7177
struct bpf_insn insns[MAX_INSNS];
@@ -92,6 +98,7 @@ struct bpf_test {
9298
int fixup_map_reuseport_array[MAX_FIXUPS];
9399
int fixup_map_ringbuf[MAX_FIXUPS];
94100
int fixup_map_timer[MAX_FIXUPS];
101+
struct kfunc_btf_id_pair fixup_kfunc_btf_id[MAX_FIXUPS];
95102
/* Expected verifier log output for result REJECT or VERBOSE_ACCEPT.
96103
* Can be a tab-separated sequence of expected strings. An empty string
97104
* means no log verification.
@@ -744,6 +751,7 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
744751
int *fixup_map_reuseport_array = test->fixup_map_reuseport_array;
745752
int *fixup_map_ringbuf = test->fixup_map_ringbuf;
746753
int *fixup_map_timer = test->fixup_map_timer;
754+
struct kfunc_btf_id_pair *fixup_kfunc_btf_id = test->fixup_kfunc_btf_id;
747755

748756
if (test->fill_helper) {
749757
test->fill_insns = calloc(MAX_TEST_INSNS, sizeof(struct bpf_insn));
@@ -936,6 +944,26 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
936944
fixup_map_timer++;
937945
} while (*fixup_map_timer);
938946
}
947+
948+
/* Patch in kfunc BTF IDs */
949+
if (fixup_kfunc_btf_id->kfunc) {
950+
struct btf *btf;
951+
int btf_id;
952+
953+
do {
954+
btf_id = 0;
955+
btf = btf__load_vmlinux_btf();
956+
if (btf) {
957+
btf_id = btf__find_by_name_kind(btf,
958+
fixup_kfunc_btf_id->kfunc,
959+
BTF_KIND_FUNC);
960+
btf_id = btf_id < 0 ? 0 : btf_id;
961+
}
962+
btf__free(btf);
963+
prog[fixup_kfunc_btf_id->insn_idx].imm = btf_id;
964+
fixup_kfunc_btf_id++;
965+
} while (fixup_kfunc_btf_id->kfunc);
966+
}
939967
}
940968

941969
struct libcap {

0 commit comments

Comments
 (0)