Test the extension of unsigned 32-bit enum values in CO-RE relocations from the previous patch. test_core_reloc_enumval gets an optional local enum with values out of the range of int, i.e., an unsigned 4-byte enum, whose values clang emits zero-extended in the ld_imm64 of the relocations. The enumval target has the same enum with different values, 0x80000001 and 0xffffffff, which are expected zero-extended in the 64-bit outputs. GCC's __builtin_preserve_enum_value() returns int, so storing the value of enum named_uint_enum into a long field sign-extends it, and uint_val1 would read back as 0xffffffff80000001. Convert the result to unsigned int for GCC to get the enumerator value. Clang still stores the 64-bit relocated value, so its upper 32 bits keep being checked. Without the fix, the check of the original value fails, and the whole object is refused: libbpf: prog 'test_core_enumval': relo #11: unexpected insn #53 (LDIMM64) value: got 2147483648, exp 18446744071562067968 -> 18446744071562067969 libbpf: prog 'test_core_enumval': relo #11: failed to patch insn #53: -EINVAL #122/71 core_reloc/enumval:FAIL Signed-off-by: Yonghong Song --- .../selftests/bpf/prog_tests/core_reloc.c | 2 ++ .../selftests/bpf/progs/core_reloc_types.h | 8 +++++++ .../bpf/progs/test_core_reloc_enumval.c | 21 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c index 80432c4656b5..98b3c5fb1028 100644 --- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c +++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c @@ -904,6 +904,8 @@ static const struct core_reloc_test_case test_cases[] = { .named_val2 = 2, .anon_val1 = 0x10, .anon_val2 = 0x20, + .uint_val1 = 0x80000001, + .uint_val2 = 0xffffffff, }), ENUMVAL_CASE(enumval___diff, { .named_val1_exists = true, diff --git a/tools/testing/selftests/bpf/progs/core_reloc_types.h b/tools/testing/selftests/bpf/progs/core_reloc_types.h index 5760ae015e09..6d758e74bd58 100644 --- a/tools/testing/selftests/bpf/progs/core_reloc_types.h +++ b/tools/testing/selftests/bpf/progs/core_reloc_types.h @@ -1215,6 +1215,8 @@ struct core_reloc_enumval_output { int named_val2; int anon_val1; int anon_val2; + long uint_val1; + long uint_val2; }; struct core_reloc_enum64val_output { @@ -1243,9 +1245,15 @@ typedef enum { ANON_ENUM_VAL3 = 0x30, } anon_enum; +enum named_uint_enum { + UINT_ENUM_VAL1 = 0x80000001, + UINT_ENUM_VAL2 = 0xffffffff, +}; + struct core_reloc_enumval { enum named_enum f1; anon_enum f2; + enum named_uint_enum f3; }; enum named_unsigned_enum64 { diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c index e7ef3dada2bf..8b8abbaaf533 100644 --- a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +++ b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c @@ -27,6 +27,12 @@ typedef enum { ANON_ENUM_VAL3 = 0x30, } anon_enum; +/* 4-byte enum with values out of the range of int, so it is unsigned */ +enum named_uint_enum { + UINT_ENUM_VAL1 = 0x80000000, + UINT_ENUM_VAL2 = 0xfffffff0, +}; + struct core_reloc_enumval_output { bool named_val1_exists; bool named_val2_exists; @@ -39,8 +45,17 @@ struct core_reloc_enumval_output { int named_val2; int anon_val1; int anon_val2; + long uint_val1; + long uint_val2; }; +#ifdef __clang__ +#define UINT_ENUM_VALUE(v) bpf_core_enum_value(enum named_uint_enum, v) +#else +/* GCC's __builtin_preserve_enum_value() returns int */ +#define UINT_ENUM_VALUE(v) ((unsigned int)bpf_core_enum_value(enum named_uint_enum, v)) +#endif + SEC("raw_tracepoint/sys_enter") int test_core_enumval(void *ctx) { @@ -64,6 +79,12 @@ int test_core_enumval(void *ctx) out->anon_val1 = bpf_core_enum_value(anon, ANON_ENUM_VAL1); out->anon_val2 = bpf_core_enum_value(anon, ANON_ENUM_VAL2); /* ANON_ENUM_VAL3 value is optional */ + + /* enum named_uint_enum is optional */ + if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL1)) + out->uint_val1 = UINT_ENUM_VALUE(UINT_ENUM_VAL1); + if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2)) + out->uint_val2 = UINT_ENUM_VALUE(UINT_ENUM_VAL2); #else data.skip = true; #endif -- 2.53.1-Meta