* [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
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 ` Yonghong Song
2026-11-26 18:36 ` bot+bpf-ci
2026-11-26 20:20 ` Alexei Starovoitov
2026-11-26 17:56 ` [PATCH bpf-next 2/3] selftests/bpf: Test enum value relocations within " Yonghong Song
2026-11-26 17:56 ` [PATCH bpf-next 3/3] selftests/bpf: Test enum value relocations out of " Yonghong Song
2 siblings, 2 replies; 9+ messages in thread
From: Yonghong Song @ 2026-11-26 17:56 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf patches the 64-bit value
of the target enumerator into the ld_imm64 without checking whether it
fits in the type of the local enumerator.
This has been fine because such a relocation could only come from
__builtin_preserve_enum_value(), e.g., through bpf_core_enum_value().
The builtin returns a long, whatever the type of the local enumerator,
so the program uses all 64 bits of the patched value:
enum E { N = 10 };
long g(void) { return bpf_core_enum_value(enum E, N); }
is compiled to:
g:
r0 = 0xa ll /* CO-RE <enumval_value> enum E::N */
exit
If N is 0x100000000 in the kernel, g() returns 0x100000000, although
the local N has type int. This is why commit 776281652ddc ("libbpf:
Permit 64bit relocation value") allowed any 64-bit value, so that a
local enum can match a kernel enum64.
Clang now accepts the preserve_access_index attribute on an enum, see
llvm commit c080a0b9b673 ("[BPF] Support preserve_access_index attribute
for enum"). Each reference to an enumerator of such an enum is
relocated, and the relocated value is converted to the type of the
enumerator. For example:
enum E { N = 10 } __attribute__((preserve_access_index));
enum U { M = 0x80000000 } __attribute__((preserve_access_index));
unsigned f1(unsigned hash) { return hash % N; }
long f2(void) { return N; }
unsigned long f3(void) { return M; }
is compiled to:
f1:
w0 = w1
r1 = 0xa ll /* CO-RE <enumval_value> enum E::N */
w0 %= w1
exit
f2:
r0 = 0xa ll /* CO-RE <enumval_value> enum E::N */
r0 <<= 0x20
r0 s>>= 0x20
exit
f3:
r0 = 0x80000000 ll /* CO-RE <enumval_value> enum U::M */
r0 <<= 0x20
r0 >>= 0x20
exit
N has type int and M has type unsigned int, so only the low 32 bits of
the patched value are used, even where the result is 64-bit:
- If N is 0x100000000 in the kernel, f1() computes hash % 0 and f2()
returns 0.
- If N is 0x80000000 in the kernel, f2() returns 0xffffffff80000000,
a negative value.
- If M is 0x100000000 in the kernel, f3() returns 0.
In each case the program loads and silently uses a wrong value.
The type of the local enumerator follows from the local enum, as in C:
int if all values of the enum fit in int, and otherwise the type of the
enum, i.e., unsigned int for a 4-byte enum, and long or unsigned long
for an 8-byte one. A 1-byte enum made with the packed or mode(byte)
attribute, as in the kernel, still has int enumerators.
If the target value is out of the range of that type, poison the
instruction, as is done when the enumerator does not exist in the
target, and warn:
libbpf: prog 'test_core_enumval': relo #7: enumerator
'NAMED_ENUM_VAL2___err_int' value 0x80000000 does not fit in
int type of local enumerator
So a program is only rejected if the poisoned instruction is reachable.
This also applies to bpf_core_enum_value(), since libbpf cannot tell the
two forms apart. A program like g() above, which could use a target
value out of the range of the local enumerator type before, is now
rejected if the instruction is reachable. Document this in
bpf_core_read.h.
The target value is taken as signed or unsigned according to the kflag
of the target enum, which pahole sets for a signed enum since v1.24, the
minimum version now required for kernel BTF.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
tools/lib/bpf/bpf_core_read.h | 5 ++
tools/lib/bpf/relo_core.c | 88 ++++++++++++++++++++++++++++++++---
2 files changed, 86 insertions(+), 7 deletions(-)
diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h
index b997c68bd945..00e8567e0a98 100644
--- a/tools/lib/bpf/bpf_core_read.h
+++ b/tools/lib/bpf/bpf_core_read.h
@@ -283,6 +283,11 @@ enum bpf_enum_value_kind {
* 64-bit value, if specified enum type and its enumerator value are
* present in target kernel's BTF;
* 0, if no matching enum and/or enum value within that enum is found.
+ * If the target value does not fit in the type of the local enumerator, the
+ * instruction is poisoned, so the program is rejected if it is reachable.
+ * The local enumerator has type int if all values of the local enum fit in
+ * int. Otherwise, it has type unsigned int for a 4-byte enum, and long or
+ * unsigned long for an 8-byte enum, depending on whether the enum is signed.
*/
#ifdef __clang__
#define bpf_core_enum_value(enum_type, enum_value) \
diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 58c782cc60e8..b78f8ae15294 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -861,12 +861,22 @@ static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo,
return 0;
}
+static __u64 bpf_core_enum_val(const struct btf_type *t, int idx)
+{
+ const struct btf_enum *e;
+
+ if (btf_is_enum(t)) {
+ e = btf_enum(t) + idx;
+ return BTF_INFO_KFLAG(t->info) ? (__s64)e->val : (__u32)e->val;
+ }
+ return btf_enum64_value(btf_enum64(t) + idx);
+}
+
static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
const struct bpf_core_spec *spec,
__u64 *val)
{
const struct btf_type *t;
- const struct btf_enum *e;
switch (relo->kind) {
case BPF_CORE_ENUMVAL_EXISTS:
@@ -876,12 +886,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
if (!spec)
return -EUCLEAN; /* request instruction poisoning */
t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
- if (btf_is_enum(t)) {
- e = btf_enum(t) + spec->spec[0].idx;
- *val = BTF_INFO_KFLAG(t->info) ? (__s64)e->val : (__u32)e->val;
- } else {
- *val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
- }
+ *val = bpf_core_enum_val(t, spec->spec[0].idx);
break;
default:
return -EOPNOTSUPP;
@@ -890,6 +895,71 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
return 0;
}
+static bool bpf_core_enum_val_fits_int(__u64 val, bool val_signed)
+{
+ return val_signed ? (__s64)val == (__s32)val : val <= INT_MAX;
+}
+
+/*
+ * As in C, the enumerators of an enum have type int if all values of the enum
+ * fit in int, even for a 1-byte enum made with the packed or mode(byte)
+ * attribute, and otherwise the type of the enum, which is then 4 or 8 bytes.
+ */
+static bool bpf_core_enum_fits_int(const struct btf_type *t)
+{
+ bool is_signed = BTF_INFO_KFLAG(t->info);
+ int i, n = btf_vlen(t);
+
+ for (i = 0; i < n; i++) {
+ if (!bpf_core_enum_val_fits_int(bpf_core_enum_val(t, i), is_signed))
+ return false;
+ }
+ return true;
+}
+
+/*
+ * Check whether the target value of an ENUMVAL_VALUE relocation fits in the
+ * type of the local enumerator. The compiler may convert the relocated 64-bit
+ * value to that type, e.g., for an enumerator of an enum with the
+ * preserve_access_index attribute. A target value out of the range of that
+ * type then becomes a different value, e.g., 0x100000000 becomes 0 for an int
+ * enumerator, and the program uses it without any error.
+ */
+static int bpf_core_check_enumval_range(const char *prog_name, int relo_idx,
+ const struct bpf_core_spec *local_spec,
+ const struct bpf_core_spec *targ_spec,
+ __u64 targ_val)
+{
+ const struct btf_type *local_t, *targ_t;
+ bool local_signed, targ_signed, fits;
+ const char *type;
+
+ local_t = btf_type_by_id(local_spec->btf, local_spec->spec[0].type_id);
+ targ_t = btf_type_by_id(targ_spec->btf, targ_spec->spec[0].type_id);
+ local_signed = BTF_INFO_KFLAG(local_t->info);
+ targ_signed = BTF_INFO_KFLAG(targ_t->info);
+
+ if (bpf_core_enum_fits_int(local_t)) {
+ type = "int";
+ fits = bpf_core_enum_val_fits_int(targ_val, targ_signed);
+ } else if (local_t->size == 4) {
+ type = "unsigned int";
+ /* a negative value is sign-extended, so it is also too big */
+ fits = targ_val <= UINT_MAX;
+ } else {
+ /* the 64-bit value is used as is, only its sign may change */
+ type = local_signed ? "long" : "unsigned long";
+ fits = local_signed == targ_signed || (__s64)targ_val >= 0;
+ }
+ if (fits)
+ return 0;
+
+ pr_warn("prog '%s': relo #%d: enumerator '%s' value 0x%llx does not fit in %s type of local enumerator\n",
+ prog_name, relo_idx, targ_spec->spec[0].name,
+ (unsigned long long)targ_val, type);
+ return -EUCLEAN; /* request instruction poisoning */
+}
+
/* Calculate original and target relocation values, given local and target
* specs and relocation kind. These values are calculated for each candidate.
* If there are multiple candidates, resulting values should all be consistent
@@ -965,6 +1035,10 @@ static int bpf_core_calc_relo(const char *prog_name,
} else if (core_relo_is_enumval_based(relo->kind)) {
err = bpf_core_calc_enumval_relo(relo, local_spec, &res->orig_val);
err = err ?: bpf_core_calc_enumval_relo(relo, targ_spec, &res->new_val);
+ if (!err && relo->kind == BPF_CORE_ENUMVAL_VALUE)
+ err = bpf_core_check_enumval_range(prog_name, relo_idx,
+ local_spec, targ_spec,
+ res->new_val);
}
done:
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
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 19:35 ` Yonghong Song
2026-11-26 20:20 ` Alexei Starovoitov
1 sibling, 1 reply; 9+ messages in thread
From: bot+bpf-ci @ 2026-11-26 18:36 UTC (permalink / raw)
To: yonghong.song, bpf
Cc: ast, andrii, daniel, eddyz87, kernel-team, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 3559 bytes --]
> libbpf: Poison an enum value relocation out of the local enumerator range
TL;DR: Possible wrong behaviour in relo_core.c: enum value relocations may be
wrongly poisoned when target BTF lacks the kflag (pre-6.0 kernels) and
negative enumerators, so valid programs could fail to load.
> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
> --- a/tools/lib/bpf/relo_core.c
> +++ b/tools/lib/bpf/relo_core.c
[ ... ]
> +static bool bpf_core_enum_val_fits_int(__u64 val, bool val_signed)
> +{
> + return val_signed ? (__s64)val == (__s32)val : val <= INT_MAX;
> +}
[ ... ]
> + local_t = btf_type_by_id(local_spec->btf, local_spec->spec[0].type_id);
> + targ_t = btf_type_by_id(targ_spec->btf, targ_spec->spec[0].type_id);
> + local_signed = BTF_INFO_KFLAG(local_t->info);
> + targ_signed = BTF_INFO_KFLAG(targ_t->info);
> +
> + if (bpf_core_enum_fits_int(local_t)) {
> + type = "int";
> + fits = bpf_core_enum_val_fits_int(targ_val, targ_signed);
> + } else if (local_t->size == 4) {
> + type = "unsigned int";
> + /* a negative value is sign-extended, so it is also too big */
> + fits = targ_val <= UINT_MAX;
Can this reject valid programs when the target BTF has no kflag on a signed
enum?
Before commit 6089fb325cf7 ("bpf: Add btf enum64 support"),
btf_enum_check_meta() rejected any BTF_KIND_ENUM with kind_flag set, so
vmlinux and module BTF from pre-6.0 kernels (and from kernels built with
pahole older than 1.24) have kflag == 0 and store negative enumerators as
their u32 bit pattern.
For kflag == 0, bpf_core_enum_val() returns (__u32)e->val, so a target
enumerator of -1 becomes targ_val = 0xffffffff with targ_signed = false.
Consider a local enum such as enum perf_event_state from a modern
vmlinux.h, where PERF_EVENT_STATE_OFF = -1, running on a 5.x kernel:
bpf_core_enum_fits_int(local_t) -> true
bpf_core_enum_val_fits_int(0xffffffff, false) -> 0xffffffff <= INT_MAX
-> false
The relocation is poisoned and the program fails to load if the
instruction is reachable. Before this patch the ld_imm64 got 0xffffffff,
which truncates to the correct -1 in an int context such as:
int x = bpf_core_enum_value(enum perf_event_state,
PERF_EVENT_STATE_OFF);
The mirror case also looks affected. If the local enum comes from a
vmlinux.h generated on an old kernel, btf_dump prints the kflag == 0
values with %u (e.g. PERF_EVENT_STATE_OFF = 4294967295), so the local
enum is a 4-byte unsigned enum. On a 6.0+ kernel with kflag set, targ_val
is 0xffffffffffffffff, which fails "targ_val <= UINT_MAX" and is
poisoned, where bpf_core_enum_value() previously returned the kernel's -1.
The commit message says pahole sets the kflag for a signed enum since
v1.24, "the minimum version now required for kernel BTF". Doesn't that
only cover kernels built from this tree, rather than the older running
kernels that CO-RE targets?
The selftests added later in the series appear to use only target BTF
compiled by a modern clang that sets kflag, so they would not exercise
this.
Would it make sense to judge a 4-byte BTF_KIND_ENUM target without kflag
by its low 32 bits, accepting the value if it fits as either int or
unsigned int against a 32-bit local enumerator?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45873143557
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
2026-11-26 18:36 ` bot+bpf-ci
@ 2026-11-26 19:35 ` Yonghong Song
0 siblings, 0 replies; 9+ messages in thread
From: Yonghong Song @ 2026-11-26 19:35 UTC (permalink / raw)
To: bot+bpf-ci, bpf
Cc: ast, andrii, daniel, eddyz87, kernel-team, martin.lau, mason,
ihor.solodrai
On 11/26/26 10:36 AM, bot+bpf-ci@kernel.org wrote:
>> libbpf: Poison an enum value relocation out of the local enumerator range
> TL;DR: Possible wrong behaviour in relo_core.c: enum value relocations may be
> wrongly poisoned when target BTF lacks the kflag (pre-6.0 kernels) and
> negative enumerators, so valid programs could fail to load.
Please take a look at commit 6036922777d3 or
https://lore.kernel.org/all/20261124000514.2955878-1-yonghong.song@linux.dev/
We do assume that kflag 0 is taken as unsigned.
>
>> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
>> --- a/tools/lib/bpf/relo_core.c
>> +++ b/tools/lib/bpf/relo_core.c
> [ ... ]
>
>> +static bool bpf_core_enum_val_fits_int(__u64 val, bool val_signed)
>> +{
>> + return val_signed ? (__s64)val == (__s32)val : val <= INT_MAX;
>> +}
> [ ... ]
>
>> + local_t = btf_type_by_id(local_spec->btf, local_spec->spec[0].type_id);
>> + targ_t = btf_type_by_id(targ_spec->btf, targ_spec->spec[0].type_id);
>> + local_signed = BTF_INFO_KFLAG(local_t->info);
>> + targ_signed = BTF_INFO_KFLAG(targ_t->info);
>> +
>> + if (bpf_core_enum_fits_int(local_t)) {
>> + type = "int";
>> + fits = bpf_core_enum_val_fits_int(targ_val, targ_signed);
>> + } else if (local_t->size == 4) {
>> + type = "unsigned int";
>> + /* a negative value is sign-extended, so it is also too big */
>> + fits = targ_val <= UINT_MAX;
> Can this reject valid programs when the target BTF has no kflag on a signed
> enum?
>
> Before commit 6089fb325cf7 ("bpf: Add btf enum64 support"),
> btf_enum_check_meta() rejected any BTF_KIND_ENUM with kind_flag set, so
> vmlinux and module BTF from pre-6.0 kernels (and from kernels built with
> pahole older than 1.24) have kflag == 0 and store negative enumerators as
> their u32 bit pattern.
>
> For kflag == 0, bpf_core_enum_val() returns (__u32)e->val, so a target
> enumerator of -1 becomes targ_val = 0xffffffff with targ_signed = false.
>
> Consider a local enum such as enum perf_event_state from a modern
> vmlinux.h, where PERF_EVENT_STATE_OFF = -1, running on a 5.x kernel:
>
> bpf_core_enum_fits_int(local_t) -> true
> bpf_core_enum_val_fits_int(0xffffffff, false) -> 0xffffffff <= INT_MAX
> -> false
>
> The relocation is poisoned and the program fails to load if the
> instruction is reachable. Before this patch the ld_imm64 got 0xffffffff,
> which truncates to the correct -1 in an int context such as:
>
> int x = bpf_core_enum_value(enum perf_event_state,
> PERF_EVENT_STATE_OFF);
>
> The mirror case also looks affected. If the local enum comes from a
> vmlinux.h generated on an old kernel, btf_dump prints the kflag == 0
> values with %u (e.g. PERF_EVENT_STATE_OFF = 4294967295), so the local
> enum is a 4-byte unsigned enum. On a 6.0+ kernel with kflag set, targ_val
> is 0xffffffffffffffff, which fails "targ_val <= UINT_MAX" and is
> poisoned, where bpf_core_enum_value() previously returned the kernel's -1.
>
> The commit message says pahole sets the kflag for a signed enum since
> v1.24, "the minimum version now required for kernel BTF". Doesn't that
> only cover kernels built from this tree, rather than the older running
> kernels that CO-RE targets?
>
> The selftests added later in the series appear to use only target BTF
> compiled by a modern clang that sets kflag, so they would not exercise
> this.
>
> Would it make sense to judge a 4-byte BTF_KIND_ENUM target without kflag
> by its low 32 bits, accepting the value if it fits as either int or
> unsigned int against a 32-bit local enumerator?
>
>
> ---
> AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
> See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
>
> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45873143557
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
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 20:20 ` Alexei Starovoitov
1 sibling, 0 replies; 9+ messages in thread
From: Alexei Starovoitov @ 2026-11-26 20:20 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Andrii Nakryiko, Daniel Borkmann, Eduard Zingerman, kernel-team
On Thu, Nov 26, 2026 at 09:56 AM Yonghong Song <yonghong.song@linux.dev> wrote:
> So a program is only rejected if the poisoned instruction is reachable.
>
> This also applies to bpf_core_enum_value(), since libbpf cannot tell the
> two forms apart. A program like g() above, which could use a target
> value out of the range of the local enumerator type before, is now
> rejected if the instruction is reachable. Document this in
> bpf_core_read.h.
The usual guard doesn't make it unreachable.
bpf_core_enum_value_exists() returns 1 here.
I think this takes back what your commit 776281652ddc allowed.
The local value is often a placeholder, like in profiler.inc.h:
pids_cgrp_id___local = 123, /* value doesn't matter */
What bpf_core_enum_value() gets today and with the patch:
local enumerator target today patch
--------------------------------- ----------- ---------- --------
SCX_ENQ_PREEMPT___local = 0 1ULL << 32 1ULL << 32 poisoned
PERF_EVENT_STATE_OFF = -1 -1, kflag 0 0xffffffff poisoned
PERF_EVENT_STATE_OFF = 4294967295 -1, kflag 1 -1 poisoned
Row 2 runs on v5.15. Before commit 4fad237494f1 it got -1.
Row 3 has vmlinux.h of v5.15 and runs on bpf-next.
The kernel builds relo_core.c too.
A light skeleton with row 1 stops loading after a kernel upgrade.
> The target value is taken as signed or unsigned according to the kflag
> of the target enum, which pahole sets for a signed enum since v1.24, the
> minimum version now required for kernel BTF.
That covers new kernels only.
Before v6.0 btf_enum_check_meta() refuses an enum with kflag.
Row 2 ends up in:
val <= INT_MAX val = 0xffffffff, val_signed = 0
Code like f2() gets -1 there today, since s>>= restores the sign.
So the attribute form fails to load too, and its value is correct.
> +/*
> + * As in C, the enumerators of an enum have type int if all values of the enum
> + * fit in int, even for a 1-byte enum made with the packed or mode(byte)
> + * attribute, and otherwise the type of the enum, which is then 4 or 8 bytes.
> + */
> +static bool bpf_core_enum_fits_int(const struct btf_type *t)
Not with a fixed underlying type. sizeof(N) is 8 with clang 21:
enum E : unsigned long { N = 10 };
This function says int, and 0x100000000 gets poisoned.
BTF doesn't have the type of the enumerator. The compiler has it.
can clang emit mov for 32-bit enumerators, like for field relos ?
enumerator insn
------------ ---------------
int r0 = 0xa
unsigned int w0 = 0x80000000
long r0 = 0xa ll
bpf_core_enum_value() keeps ld_imm64 and works as before.
bpf_core_patch_insn() patches ALU imm already, without a range check.
With mov the diff poisons all three examples from the log.
Rows 2 and 3 load in both forms.
The diff skips the sign of a long and fixed types of 1 and 2 bytes.
No need for bpf_core_check_enumval_range(), bpf_core_enum_fits_int(),
and for the shifts in f2() and f3().
relo_core.c in place of patch 1, compiles for libbpf and kernel, not run:
diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 58c782c..b2c8632 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -1031,6 +1031,20 @@ static int insn_bytes_to_bpf_size(__u32 sz)
}
}
+/*
+ * ALU zero-extends imm, ALU64 sign-extends it. BTF before v6.0 has no sign of
+ * an enum, so accept both forms when bit 31 of the local value is set.
+ */
+static bool insn_imm_fits(const struct bpf_insn *insn, __u64 val)
+{
+ bool zext = val == (__u32)val;
+ bool sext = (__s64)val == (__s32)val;
+
+ if (insn->imm < 0)
+ return zext || sext;
+ return BPF_CLASS(insn->code) == BPF_ALU ? zext : sext;
+}
+
/*
* Patch relocatable BPF instruction.
*
@@ -1067,13 +1081,18 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
goto bad_insn;
if (res->poison)
return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
- if (res->validate && insn->imm != orig_val) {
+ if (res->validate && insn->imm != (__s32)orig_val) {
pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %d, exp %llu -> %llu\n",
prog_name, relo_idx,
insn_idx, insn->imm, (unsigned long long)orig_val,
(unsigned long long)new_val);
return -EINVAL;
}
+ if (!insn_imm_fits(insn, new_val)) {
+ pr_warn("prog '%s': relo #%d: insn #%d (ALU/ALU64) value out of range: 0x%llx\n",
+ prog_name, relo_idx, insn_idx, (unsigned long long)new_val);
+ return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
+ }
orig_val = insn->imm;
insn->imm = new_val;
pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %llu -> %llu\n",
pw-bot: cr
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH bpf-next 2/3] selftests/bpf: Test enum value relocations within the local enumerator range
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
2026-11-26 18:00 ` sashiko-bot
2026-11-26 17:56 ` [PATCH bpf-next 3/3] selftests/bpf: Test enum value relocations out of " Yonghong Song
2 siblings, 1 reply; 9+ messages in thread
From: Yonghong Song @ 2026-11-26 17:56 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
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
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH bpf-next 2/3] selftests/bpf: Test enum value relocations within the local enumerator range
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
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-26 18:00 UTC (permalink / raw)
To: Yonghong Song, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/bpf: non-standard multi-line comment style in
core_reloc_types.h
--
commit 7b2de00cc3d3bd98446d7a82129006ec470a720b
Author: Yonghong Song <yonghong.song@linux.dev>
selftests/bpf: Test enum value relocations within the local enumerator range
This commit adds CO-RE enum value relocation tests for target enum values
that fit within the range of the local enumerator type across 8-byte,
negative, and unpacked 1-byte target enums.
> diff --git a/tools/testing/selftests/bpf/progs/core_reloc_types.h b/tools/testing/selftests/bpf/progs/core_reloc_types.h
> index 6d758e74bd586..116a67fdd1b2e 100644
> --- a/tools/testing/selftests/bpf/progs/core_reloc_types.h
> +++ b/tools/testing/selftests/bpf/progs/core_reloc_types.h
[ ... ]
> @@ -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
> + */
[Severity: Low]
This isn't a bug, but BPF coding style places the opening /* of multi-line
comments on its own line in these two comments.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126175602.1882988-1-yonghong.song@linux.dev?part=2
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH bpf-next 3/3] selftests/bpf: Test enum value relocations out of the local enumerator range
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 ` [PATCH bpf-next 2/3] selftests/bpf: Test enum value relocations within " Yonghong Song
@ 2026-11-26 17:56 ` Yonghong Song
2026-11-26 18:02 ` sashiko-bot
2 siblings, 1 reply; 9+ messages in thread
From: Yonghong Song @ 2026-11-26 17:56 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
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
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH bpf-next 3/3] selftests/bpf: Test enum value relocations out of the local enumerator range
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
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-26 18:02 UTC (permalink / raw)
To: Yonghong Song, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/bpf: non-standard multi-line comments in
core_reloc_types.h
--
commit b84e0514078e764d621bc1f043d99af2bc50dbfb
Author: Yonghong Song <yonghong.song@linux.dev>
selftests/bpf: Test enum value relocations out of the local enumerator range
This commit adds CO-RE relocation selftests verifying that libbpf poisons
enum and enum64 value relocations whose target values fall outside the
range of the local enumerator type.
> diff --git a/tools/testing/selftests/bpf/progs/core_reloc_types.h b/tools/testing/selftests/bpf/progs/core_reloc_types.h
> index 116a67fdd1b2e..01d67521efd12 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
> + */
[Severity: Low]
This isn't a bug, but the five new multi-line comments in
core_reloc_types.h place text on the same line as the opening /*. Per
BPF coding style, the opening /* of a multi-line comment should be on its
own line with the comment text starting on the next line.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126175602.1882988-1-yonghong.song@linux.dev?part=3
^ permalink raw reply [flat|nested] 9+ messages in thread