Add a test for enum64 value relocations. Signed-off-by: Yonghong Song <yhs@xxxxxx> --- .../selftests/bpf/prog_tests/core_reloc.c | 43 +++++++++++++++ .../bpf/progs/btf__core_reloc_enum64val.c | 3 ++ .../progs/btf__core_reloc_enum64val___diff.c | 3 ++ .../btf__core_reloc_enum64val___err_missing.c | 3 ++ ...btf__core_reloc_enum64val___val3_missing.c | 3 ++ .../selftests/bpf/progs/core_reloc_types.h | 47 ++++++++++++++++ .../bpf/progs/test_core_reloc_enum64val.c | 53 +++++++++++++++++++ 7 files changed, 155 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___diff.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_missing.c create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___val3_missing.c create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_enum64val.c diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c index f28f75aa9154..b0054f16c9cd 100644 --- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c +++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c @@ -355,6 +355,25 @@ static int duration = 0; .fails = true, \ } +#define ENUM64VAL_CASE_COMMON(name) \ + .case_name = #name, \ + .bpf_obj_file = "test_core_reloc_enum64val.o", \ + .btf_src_file = "btf__core_reloc_" #name ".o", \ + .raw_tp_name = "sys_enter", \ + .prog_name = "test_core_enum64val" + +#define ENUM64VAL_CASE(name, ...) { \ + ENUM64VAL_CASE_COMMON(name), \ + .output = STRUCT_TO_CHAR_PTR(core_reloc_enum64val_output) \ + __VA_ARGS__, \ + .output_len = sizeof(struct core_reloc_enum64val_output), \ +} + +#define ENUM64VAL_ERR_CASE(name) { \ + ENUM64VAL_CASE_COMMON(name), \ + .fails = true, \ +} + struct core_reloc_test_case; typedef int (*setup_test_fn)(struct core_reloc_test_case *test); @@ -822,6 +841,30 @@ static const struct core_reloc_test_case test_cases[] = { .anon_val2 = 0x222, }), ENUMVAL_ERR_CASE(enumval___err_missing), + + /* 64bit enumerator value existence and value relocations */ + ENUM64VAL_CASE(enum64val, { + .named_val1_exists = true, + .named_val2_exists = true, + .named_val3_exists = true, + .named_val1 = 0x1ffffffffULL, + .named_val2 = 0x2, + }), + ENUM64VAL_CASE(enum64val___diff, { + .named_val1_exists = true, + .named_val2_exists = true, + .named_val3_exists = true, + .named_val1 = 0x101ffffffffULL, + .named_val2 = 0x202ffffffffULL, + }), + ENUM64VAL_CASE(enum64val___val3_missing, { + .named_val1_exists = true, + .named_val2_exists = true, + .named_val3_exists = false, + .named_val1 = 0x111ffffffffULL, + .named_val2 = 0x222, + }), + ENUM64VAL_ERR_CASE(enum64val___err_missing), }; struct data { diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val.c new file mode 100644 index 000000000000..888e79db6a77 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val.c @@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f(struct core_reloc_enum64val x) {} diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___diff.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___diff.c new file mode 100644 index 000000000000..194749130d87 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___diff.c @@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f(struct core_reloc_enum64val___diff x) {} diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_missing.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_missing.c new file mode 100644 index 000000000000..3d732d4193e4 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_missing.c @@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f(struct core_reloc_enum64val___err_missing x) {} diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___val3_missing.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___val3_missing.c new file mode 100644 index 000000000000..17cf5d6a848d --- /dev/null +++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___val3_missing.c @@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f(struct core_reloc_enum64val___val3_missing x) {} diff --git a/tools/testing/selftests/bpf/progs/core_reloc_types.h b/tools/testing/selftests/bpf/progs/core_reloc_types.h index c95c0cabe951..e6e12a93514b 100644 --- a/tools/testing/selftests/bpf/progs/core_reloc_types.h +++ b/tools/testing/selftests/bpf/progs/core_reloc_types.h @@ -1099,6 +1099,15 @@ struct core_reloc_enumval_output { int anon_val2; }; +struct core_reloc_enum64val_output { + bool named_val1_exists; + bool named_val2_exists; + bool named_val3_exists; + + long named_val1; + long named_val2; +}; + enum named_enum { NAMED_ENUM_VAL1 = 1, NAMED_ENUM_VAL2 = 2, @@ -1116,6 +1125,16 @@ struct core_reloc_enumval { anon_enum f2; }; +enum named_enum64 { + NAMED_ENUM64_VAL1 = 0x1ffffffffULL, + NAMED_ENUM64_VAL2 = 0x2, + NAMED_ENUM64_VAL3 = 0x3ffffffffULL, +}; + +struct core_reloc_enum64val { + enum named_enum64 f1; +}; + /* differing enumerator values */ enum named_enum___diff { NAMED_ENUM_VAL1___diff = 101, @@ -1134,6 +1153,16 @@ struct core_reloc_enumval___diff { anon_enum___diff f2; }; +enum named_enum64___diff { + NAMED_ENUM64_VAL1___diff = 0x101ffffffffULL, + NAMED_ENUM64_VAL2___diff = 0x202ffffffffULL, + NAMED_ENUM64_VAL3___diff = 0x303ffffffffULL, +}; + +struct core_reloc_enum64val___diff { + enum named_enum64___diff f1; +}; + /* missing (optional) third enum value */ enum named_enum___val3_missing { NAMED_ENUM_VAL1___val3_missing = 111, @@ -1150,6 +1179,15 @@ struct core_reloc_enumval___val3_missing { anon_enum___val3_missing f2; }; +enum named_enum64___val3_missing { + NAMED_ENUM64_VAL1___val3_missing = 0x111ffffffffULL, + NAMED_ENUM64_VAL2___val3_missing = 0x222, +}; + +struct core_reloc_enum64val___val3_missing { + enum named_enum64___val3_missing f1; +}; + /* missing (mandatory) second enum value, should fail */ enum named_enum___err_missing { NAMED_ENUM_VAL1___err_missing = 1, @@ -1165,3 +1203,12 @@ struct core_reloc_enumval___err_missing { enum named_enum___err_missing f1; anon_enum___err_missing f2; }; + +enum named_enum64___err_missing { + NAMED_ENUM64_VAL1___err_missing = 0x1ffffffffULL, + NAMED_ENUM64_VAL3___err_missing = 0x3ffffffffULL, +}; + +struct core_reloc_enum64val___err_missing { + enum named_enum64___err_missing f1; +}; diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_enum64val.c b/tools/testing/selftests/bpf/progs/test_core_reloc_enum64val.c new file mode 100644 index 000000000000..1bccf9214547 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_core_reloc_enum64val.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2020 Facebook + +#include <linux/bpf.h> +#include <stdint.h> +#include <stdbool.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_core_read.h> + +char _license[] SEC("license") = "GPL"; + +struct { + char in[256]; + char out[256]; + bool skip; +} data = {}; + +enum named_enum64 { + NAMED_ENUM64_VAL1 = 0x1ffffffffULL, + NAMED_ENUM64_VAL2 = 0x2ffffffffULL, + NAMED_ENUM64_VAL3 = 0x3ffffffffULL, +}; + +struct core_reloc_enum64val_output { + bool named_val1_exists; + bool named_val2_exists; + bool named_val3_exists; + + long named_val1; + long named_val2; +}; + +SEC("raw_tracepoint/sys_enter") +int test_core_enum64val(void *ctx) +{ +#if __has_builtin(__builtin_preserve_enum_value) + struct core_reloc_enum64val_output *out = (void *)&data.out; + enum named_enum64 named = 0; + + out->named_val1_exists = bpf_core_enum_value_exists(named, NAMED_ENUM64_VAL1); + out->named_val2_exists = bpf_core_enum_value_exists(enum named_enum64, NAMED_ENUM64_VAL2); + out->named_val3_exists = bpf_core_enum_value_exists(enum named_enum64, NAMED_ENUM64_VAL3); + + out->named_val1 = bpf_core_enum_value(named, NAMED_ENUM64_VAL1); + out->named_val2 = bpf_core_enum_value(named, NAMED_ENUM64_VAL2); + /* NAMED_ENUM64_VAL3 value is optional */ + +#else + data.skip = true; +#endif + + return 0; +} -- 2.30.2