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 3/3] selftests/bpf: Test enum value relocations out of the local enumerator range
Date: Thu, 26 Nov 2026 09:56:18 -0800 [thread overview]
Message-ID: <20261126175618.1884490-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20261126175602.1882988-1-yonghong.song@linux.dev>
Test the libbpf patch, which poisons an enum value relocation whose
target value is out of the range of the type of the local enumerator.
In each new core_reloc case, the poisoned instruction is reachable, so
the load fails:
- enumval___err_too_big: 0x200000000 for an int enumerator, from an
8-byte target enum.
- enumval___err_too_small: -0x100000000 for an int enumerator, from a
signed 8-byte target enum.
- enumval___err_int: 0x80000000 for an int enumerator, from an
unsigned 4-byte target enum.
- enumval___err_uint: 0x100000000 for an unsigned int enumerator, from
an 8-byte target enum.
- enum64val___err_sign: 0x8000000000000000 for a long enumerator, from
an unsigned 8-byte target enum.
With GCC, the values of enum named_uint_enum are not read, and the
enumval___err_uint case is skipped, as for the enumval case, and so is
enum64val___err_sign, as for all enum64val cases.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
.../selftests/bpf/prog_tests/core_reloc.c | 5 ++
.../btf__core_reloc_enum64val___err_sign.c | 3 +
.../progs/btf__core_reloc_enumval___err_int.c | 3 +
.../btf__core_reloc_enumval___err_too_big.c | 3 +
.../btf__core_reloc_enumval___err_too_small.c | 3 +
.../btf__core_reloc_enumval___err_uint.c | 3 +
.../selftests/bpf/progs/core_reloc_types.h | 70 +++++++++++++++++++
7 files changed, 90 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_sign.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_int.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_big.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_small.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_uint.c
diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
index bb468f3764e6..580db3266401 100644
--- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c
+++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
@@ -972,6 +972,10 @@ static const struct core_reloc_test_case test_cases[] = {
.byte_val1 = 0x11,
.byte_val2 = 0x100,
}),
+ ENUMVAL_ERR_CASE(enumval___err_too_big),
+ ENUMVAL_ERR_CASE(enumval___err_too_small),
+ ENUMVAL_ERR_CASE(enumval___err_int),
+ ENUMVAL_ERR_CASE(enumval___err_uint),
/* 64bit enumerator value existence and value relocations */
ENUM64VAL_CASE(enum64val, {
@@ -1011,6 +1015,7 @@ static const struct core_reloc_test_case test_cases[] = {
.signed_val2 = -222,
}),
ENUM64VAL_ERR_CASE(enum64val___err_missing),
+ ENUM64VAL_ERR_CASE(enum64val___err_sign),
};
struct data {
diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_sign.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_sign.c
new file mode 100644
index 000000000000..81cfed621a94
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_sign.c
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enum64val___err_sign x) {}
diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_int.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_int.c
new file mode 100644
index 000000000000..8235eb7f303d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_int.c
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enumval___err_int x) {}
diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_big.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_big.c
new file mode 100644
index 000000000000..6d329e2a4d4e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_big.c
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enumval___err_too_big x) {}
diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_small.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_small.c
new file mode 100644
index 000000000000..bb4b19c71e1d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_small.c
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enumval___err_too_small x) {}
diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_uint.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_uint.c
new file mode 100644
index 000000000000..33ad12fd61bd
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_uint.c
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enumval___err_uint x) {}
diff --git a/tools/testing/selftests/bpf/progs/core_reloc_types.h b/tools/testing/selftests/bpf/progs/core_reloc_types.h
index 116a67fdd1b2..01d67521efd1 100644
--- a/tools/testing/selftests/bpf/progs/core_reloc_types.h
+++ b/tools/testing/selftests/bpf/progs/core_reloc_types.h
@@ -1409,6 +1409,62 @@ struct core_reloc_enumval___byte_unpacked {
enum named_byte_enum___byte_unpacked f3;
};
+/* second enum value out of the range of the local int enumerator, as the
+ * target enum is 8 bytes, should fail
+ */
+enum named_enum___err_too_big {
+ NAMED_ENUM_VAL1___err_too_big = 1,
+ NAMED_ENUM_VAL2___err_too_big = 0x200000000ULL,
+ NAMED_ENUM_VAL3___err_too_big = 3,
+};
+
+struct core_reloc_enumval___err_too_big {
+ enum named_enum___err_too_big f1;
+ anon_enum f2;
+};
+
+/* second enum value out of the range of the local int enumerator, as the
+ * target enum is signed and 8 bytes, should fail
+ */
+enum named_enum___err_too_small {
+ NAMED_ENUM_VAL1___err_too_small = 1,
+ NAMED_ENUM_VAL2___err_too_small = -0x100000000LL,
+ NAMED_ENUM_VAL3___err_too_small = 3,
+};
+
+struct core_reloc_enumval___err_too_small {
+ enum named_enum___err_too_small f1;
+ anon_enum f2;
+};
+
+/* second enum value out of the range of the local int enumerator, as the
+ * target enum is unsigned, should fail
+ */
+enum named_enum___err_int {
+ NAMED_ENUM_VAL1___err_int = 1,
+ NAMED_ENUM_VAL2___err_int = 0x80000000,
+ NAMED_ENUM_VAL3___err_int = 3,
+};
+
+struct core_reloc_enumval___err_int {
+ enum named_enum___err_int f1;
+ anon_enum f2;
+};
+
+/* first unsigned enum value out of the range of the local unsigned int
+ * enumerator, as the target enum is 8 bytes, should fail
+ */
+enum named_uint_enum___err_uint {
+ UINT_ENUM_VAL1___err_uint = 0x100000000ULL,
+ UINT_ENUM_VAL2___err_uint = 0xffffffff,
+};
+
+struct core_reloc_enumval___err_uint {
+ enum named_enum f1;
+ anon_enum f2;
+ enum named_uint_enum___err_uint f3;
+};
+
enum named_unsigned_enum64___err_missing {
UNSIGNED_ENUM64_VAL1___err_missing = 0x1ffffffffULL,
UNSIGNED_ENUM64_VAL3___err_missing = 0x3ffffffffULL,
@@ -1423,3 +1479,17 @@ struct core_reloc_enum64val___err_missing {
enum named_unsigned_enum64___err_missing f1;
enum named_signed_enum64___err_missing f2;
};
+
+/* first signed enum value out of the range of the local long enumerator, as
+ * the target enum is unsigned, should fail
+ */
+enum named_signed_enum64___err_sign {
+ SIGNED_ENUM64_VAL1___err_sign = 0x8000000000000000ULL,
+ SIGNED_ENUM64_VAL2___err_sign = 2,
+ SIGNED_ENUM64_VAL3___err_sign = 3,
+};
+
+struct core_reloc_enum64val___err_sign {
+ enum named_unsigned_enum64 f1;
+ enum named_signed_enum64___err_sign f2;
+};
--
2.53.1-Meta
next prev parent reply other threads:[~2026-11-26 17:56 UTC|newest]
Thread overview: 7+ 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 18:36 ` bot+bpf-ci
2026-11-26 17:56 ` [PATCH bpf-next 2/3] selftests/bpf: Test enum value relocations within " Yonghong Song
2026-11-26 18:00 ` sashiko-bot
2026-11-26 17:56 ` Yonghong Song [this message]
2026-11-26 18:02 ` [PATCH bpf-next 3/3] selftests/bpf: Test enum value relocations out of " 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=20261126175618.1884490-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