From: Yonghong Song <yonghong.song@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
kernel-team@fb.com
Subject: [PATCH bpf-next v5 2/3] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
Date: Fri, 20 Nov 2026 13:50:07 -0800 [thread overview]
Message-ID: <20261120215007.868921-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20261120214957.868409-1-yonghong.song@linux.dev>
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 <yonghong.song@linux.dev>
---
.../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
next prev parent reply other threads:[~2026-11-20 21:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 21:49 [PATCH bpf-next v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-20 21:50 ` [PATCH bpf-next v5 1/3] " Yonghong Song
2026-11-20 21:50 ` Yonghong Song [this message]
2026-11-20 21:50 ` [PATCH bpf-next v5 3/3] selftests/bpf: Test CO-RE relocations of signed enum values without kflag Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261120215007.868921-1-yonghong.song@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.