Skip to content

Commit 79fe74b

Browse files
anakryikogregkh
authored andcommitted
selftests/bpf: Fix field existence CO-RE reloc tests
[ Upstream commit 5a30eb2 ] Negative field existence cases for have a broken assumption that FIELD_EXISTS CO-RE relo will fail for fields that match the name but have incompatible type signature. That's not how CO-RE relocations generally behave. Types and fields that match by name but not by expected type are treated as non-matching candidates and are skipped. Error later is reported if no matching candidate was found. That's what happens for most relocations, but existence relocations (FIELD_EXISTS and TYPE_EXISTS) are more permissive and they are designed to return 0 or 1, depending if a match is found. This allows to handle name-conflicting but incompatible types in BPF code easily. Combined with ___flavor suffixes, it's possible to handle pretty much any structural type changes in kernel within the compiled once BPF source code. So, long story short, negative field existence test cases are invalid in their assumptions, so this patch reworks them into a single consolidated positive case that doesn't match any of the fields. Fixes: c7566a6 ("selftests/bpf: Add field existence CO-RE relocs tests") Reported-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Lorenz Bauer <lmb@cloudflare.com> Link: https://lore.kernel.org/bpf/20210426192949.416837-5-andrii@kernel.org Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 68d9839 commit 79fe74b

9 files changed

Lines changed: 24 additions & 48 deletions

‎tools/testing/selftests/bpf/prog_tests/core_reloc.c‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,6 @@ static int duration = 0;
210210
.bpf_obj_file = "test_core_reloc_existence.o", \
211211
.btf_src_file = "btf__core_reloc_" #name ".o" \
212212

213-
#define FIELD_EXISTS_ERR_CASE(name) { \
214-
FIELD_EXISTS_CASE_COMMON(name), \
215-
.fails = true, \
216-
}
217-
218213
#define BITFIELDS_CASE_COMMON(objfile, test_name_prefix, name) \
219214
.case_name = test_name_prefix#name, \
220215
.bpf_obj_file = objfile, \
@@ -642,13 +637,25 @@ static struct core_reloc_test_case test_cases[] = {
642637
},
643638
.output_len = sizeof(struct core_reloc_existence_output),
644639
},
645-
646-
FIELD_EXISTS_ERR_CASE(existence__err_int_sz),
647-
FIELD_EXISTS_ERR_CASE(existence__err_int_type),
648-
FIELD_EXISTS_ERR_CASE(existence__err_int_kind),
649-
FIELD_EXISTS_ERR_CASE(existence__err_arr_kind),
650-
FIELD_EXISTS_ERR_CASE(existence__err_arr_value_type),
651-
FIELD_EXISTS_ERR_CASE(existence__err_struct_type),
640+
{
641+
FIELD_EXISTS_CASE_COMMON(existence___wrong_field_defs),
642+
.input = STRUCT_TO_CHAR_PTR(core_reloc_existence___wrong_field_defs) {
643+
},
644+
.input_len = sizeof(struct core_reloc_existence___wrong_field_defs),
645+
.output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) {
646+
.a_exists = 0,
647+
.b_exists = 0,
648+
.c_exists = 0,
649+
.arr_exists = 0,
650+
.s_exists = 0,
651+
.a_value = 0xff000001u,
652+
.b_value = 0xff000002u,
653+
.c_value = 0xff000003u,
654+
.arr_value = 0xff000004u,
655+
.s_value = 0xff000005u,
656+
},
657+
.output_len = sizeof(struct core_reloc_existence_output),
658+
},
652659

653660
/* bitfield relocation checks */
654661
BITFIELDS_CASE(bitfields, {

‎tools/testing/selftests/bpf/progs/btf__core_reloc_existence___err_wrong_arr_kind.c‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎tools/testing/selftests/bpf/progs/btf__core_reloc_existence___err_wrong_arr_value_type.c‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎tools/testing/selftests/bpf/progs/btf__core_reloc_existence___err_wrong_int_kind.c‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎tools/testing/selftests/bpf/progs/btf__core_reloc_existence___err_wrong_int_sz.c‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎tools/testing/selftests/bpf/progs/btf__core_reloc_existence___err_wrong_int_type.c‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎tools/testing/selftests/bpf/progs/btf__core_reloc_existence___err_wrong_struct_type.c‎

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "core_reloc_types.h"
2+
3+
void f(struct core_reloc_existence___wrong_field_defs x) {}

‎tools/testing/selftests/bpf/progs/core_reloc_types.h‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -700,27 +700,11 @@ struct core_reloc_existence___minimal {
700700
int a;
701701
};
702702

703-
struct core_reloc_existence___err_wrong_int_sz {
704-
short a;
705-
};
706-
707-
struct core_reloc_existence___err_wrong_int_type {
703+
struct core_reloc_existence___wrong_field_defs {
704+
void *a;
708705
int b[1];
709-
};
710-
711-
struct core_reloc_existence___err_wrong_int_kind {
712706
struct{ int x; } c;
713-
};
714-
715-
struct core_reloc_existence___err_wrong_arr_kind {
716707
int arr;
717-
};
718-
719-
struct core_reloc_existence___err_wrong_arr_value_type {
720-
short arr[1];
721-
};
722-
723-
struct core_reloc_existence___err_wrong_struct_type {
724708
int s;
725709
};
726710

0 commit comments

Comments
 (0)