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 2/3] selftests/bpf: Test enum value relocations within the local enumerator range
Date: Thu, 26 Nov 2026 09:56:12 -0800 [thread overview]
Message-ID: <20261126175613.1883881-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20261126175602.1882988-1-yonghong.song@linux.dev>
Test the previous patch with enum value relocations whose target value
is within the range of the type of the local enumerator, so they are
still relocated, even where the target enum differs from the local one
in size or value range.
test_core_reloc_enumval gets a local 1-byte enum with the packed
attribute. Its values are read only if the target has the enum, guarded
by bpf_core_enum_value_exists(), so targets without the enum are not
affected. The local values fit in int, so the enumerators are int, and
a target value out of a byte is relocated. The enumval target gets the
same enum, also packed.
New core_reloc cases, with the target enums in the shapes the kernel
has:
- enumval___enum64: the target enums are 8 bytes, but the relocated
values fit in int, so they work as before.
- enumval___negative: a negative target value for an int enumerator
is relocated.
- enumval___byte_unpacked: 0x100 for an enumerator of the local 1-byte
enum, from a 4-byte target enum, is relocated.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
.../selftests/bpf/prog_tests/core_reloc.c | 40 +++++++++++++
.../btf__core_reloc_enumval___byte_unpacked.c | 3 +
.../progs/btf__core_reloc_enumval___enum64.c | 3 +
.../btf__core_reloc_enumval___negative.c | 3 +
.../selftests/bpf/progs/core_reloc_types.h | 56 ++++++++++++++++++-
.../bpf/progs/test_core_reloc_enumval.c | 14 +++++
6 files changed, 118 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___byte_unpacked.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___enum64.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___negative.c
diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
index 98b3c5fb1028..bb468f3764e6 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,
+ .byte_val1 = 0x11,
+ .byte_val2 = 0xff,
.uint_val1 = 0x80000001,
.uint_val2 = 0xffffffff,
}),
@@ -932,6 +934,44 @@ static const struct core_reloc_test_case test_cases[] = {
.anon_val2 = 0x222,
}),
ENUMVAL_ERR_CASE(enumval___err_missing),
+ ENUMVAL_CASE(enumval___enum64, {
+ .named_val1_exists = true,
+ .named_val2_exists = true,
+ .named_val3_exists = true,
+ .anon_val1_exists = true,
+ .anon_val2_exists = true,
+ .anon_val3_exists = true,
+ .named_val1 = 1001,
+ .named_val2 = 2002,
+ .anon_val1 = 0x11,
+ .anon_val2 = 0x22,
+ }),
+ ENUMVAL_CASE(enumval___negative, {
+ .named_val1_exists = true,
+ .named_val2_exists = true,
+ .named_val3_exists = true,
+ .anon_val1_exists = true,
+ .anon_val2_exists = true,
+ .anon_val3_exists = true,
+ .named_val1 = 1,
+ .named_val2 = 2,
+ .anon_val1 = -0x10,
+ .anon_val2 = 0x20,
+ }),
+ ENUMVAL_CASE(enumval___byte_unpacked, {
+ .named_val1_exists = true,
+ .named_val2_exists = true,
+ .named_val3_exists = true,
+ .anon_val1_exists = true,
+ .anon_val2_exists = true,
+ .anon_val3_exists = true,
+ .named_val1 = 1,
+ .named_val2 = 2,
+ .anon_val1 = 0x10,
+ .anon_val2 = 0x20,
+ .byte_val1 = 0x11,
+ .byte_val2 = 0x100,
+ }),
/* 64bit enumerator value existence and value relocations */
ENUM64VAL_CASE(enum64val, {
diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___byte_unpacked.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___byte_unpacked.c
new file mode 100644
index 000000000000..4650a050bc5d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___byte_unpacked.c
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enumval___byte_unpacked x) {}
diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___enum64.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___enum64.c
new file mode 100644
index 000000000000..6af92c96993e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___enum64.c
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enumval___enum64 x) {}
diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___negative.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___negative.c
new file mode 100644
index 000000000000..3028b0b86ccf
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___negative.c
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enumval___negative x) {}
diff --git a/tools/testing/selftests/bpf/progs/core_reloc_types.h b/tools/testing/selftests/bpf/progs/core_reloc_types.h
index 6d758e74bd58..116a67fdd1b2 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;
+ int byte_val1;
+ int byte_val2;
long uint_val1;
long uint_val2;
};
@@ -1245,6 +1247,11 @@ typedef enum {
ANON_ENUM_VAL3 = 0x30,
} anon_enum;
+enum named_byte_enum {
+ BYTE_ENUM_VAL1 = 0x11,
+ BYTE_ENUM_VAL2 = 0xff,
+} __attribute__((packed));
+
enum named_uint_enum {
UINT_ENUM_VAL1 = 0x80000001,
UINT_ENUM_VAL2 = 0xffffffff,
@@ -1253,7 +1260,8 @@ enum named_uint_enum {
struct core_reloc_enumval {
enum named_enum f1;
anon_enum f2;
- enum named_uint_enum f3;
+ enum named_byte_enum f3;
+ enum named_uint_enum f4;
};
enum named_unsigned_enum64 {
@@ -1355,6 +1363,52 @@ struct core_reloc_enumval___err_missing {
anon_enum___err_missing f2;
};
+/* 8-byte target enums, whose relocated values fit in the int enumerators of
+ * the local enums
+ */
+enum named_enum___enum64 {
+ NAMED_ENUM_VAL1___enum64 = 1001,
+ NAMED_ENUM_VAL2___enum64 = 2002,
+ NAMED_ENUM_VAL3___enum64 = 0x300000000ULL,
+};
+
+typedef enum {
+ ANON_ENUM_VAL1___enum64 = 0x11,
+ ANON_ENUM_VAL2___enum64 = 0x22,
+ ANON_ENUM_VAL3___enum64 = 0x300000000ULL,
+} anon_enum___enum64;
+
+struct core_reloc_enumval___enum64 {
+ enum named_enum___enum64 f1;
+ anon_enum___enum64 f2;
+};
+
+/* negative value for an int enumerator */
+typedef enum {
+ ANON_ENUM_VAL1___negative = -0x10,
+ ANON_ENUM_VAL2___negative = 0x20,
+ ANON_ENUM_VAL3___negative = 0x30,
+} anon_enum___negative;
+
+struct core_reloc_enumval___negative {
+ enum named_enum f1;
+ anon_enum___negative f2;
+};
+
+/* 4-byte target enum with a value out of a byte, for the int enumerators of
+ * the local 1-byte enum
+ */
+enum named_byte_enum___byte_unpacked {
+ BYTE_ENUM_VAL1___byte_unpacked = 0x11,
+ BYTE_ENUM_VAL2___byte_unpacked = 0x100,
+};
+
+struct core_reloc_enumval___byte_unpacked {
+ enum named_enum f1;
+ anon_enum f2;
+ enum named_byte_enum___byte_unpacked f3;
+};
+
enum named_unsigned_enum64___err_missing {
UNSIGNED_ENUM64_VAL1___err_missing = 0x1ffffffffULL,
UNSIGNED_ENUM64_VAL3___err_missing = 0x3ffffffffULL,
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 9933eadc2993..7ec4ff990cb3 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;
+/* 1-byte enum, whose values fit in int, so its enumerators are int */
+enum named_byte_enum {
+ BYTE_ENUM_VAL1 = 1,
+ BYTE_ENUM_VAL2 = 2,
+} __attribute__((packed));
+
/* 4-byte enum with values out of the range of int, so it is unsigned */
enum named_uint_enum {
UINT_ENUM_VAL1 = 0x80000000,
@@ -45,6 +51,8 @@ struct core_reloc_enumval_output {
int named_val2;
int anon_val1;
int anon_val2;
+ int byte_val1;
+ int byte_val2;
long uint_val1;
long uint_val2;
};
@@ -73,6 +81,12 @@ int test_core_enumval(void *ctx)
out->anon_val2 = bpf_core_enum_value(anon, ANON_ENUM_VAL2);
/* ANON_ENUM_VAL3 value is optional */
+ /* enum named_byte_enum is optional */
+ if (bpf_core_enum_value_exists(enum named_byte_enum, BYTE_ENUM_VAL1))
+ out->byte_val1 = bpf_core_enum_value(enum named_byte_enum, BYTE_ENUM_VAL1);
+ if (bpf_core_enum_value_exists(enum named_byte_enum, BYTE_ENUM_VAL2))
+ out->byte_val2 = bpf_core_enum_value(enum named_byte_enum, BYTE_ENUM_VAL2);
+
/* enum named_uint_enum is optional */
#ifdef __clang__
if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL1))
--
2.53.1-Meta
next prev parent reply other threads:[~2026-11-26 17:56 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 17:56 [PATCH bpf-next 0/3] libbpf: Check enum value relocations against the local enumerator type Yonghong Song
2026-11-26 17:56 ` [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range Yonghong Song
2026-11-26 17:56 ` Yonghong Song [this message]
2026-11-26 18:00 ` [PATCH bpf-next 2/3] selftests/bpf: Test enum value relocations within " sashiko-bot
2026-11-26 17:56 ` [PATCH bpf-next 3/3] selftests/bpf: Test enum value relocations out of " Yonghong Song
2026-11-26 18:02 ` sashiko-bot
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=20261126175613.1883881-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox