libbpf: Fix CO-RE relocations of unsigned 32-bit enum values
Subsystems (4)
Yonghong Song
reviewerv2 cover libbpf: Fix CO-RE relocations of unsigned 32-bit enum values
For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused.
Patch 1 extends the value as given by the kflag of the enum, which is the form clang emits. The kernel builds the same relo_core.c, so it gets the fix too. Patch 2 tests it.
GCC emits every 32-bit enum value sign-extended, since its __builtin_preserve_enum_value() returns int. So with patch 1 alone, GCC objects that loaded before would be refused. Patch 3 accepts the sign-extended form of an unsigned value and patches the target value in the same form. Patch 4 makes the test of patch 2 work with GCC.
Changelog:
v1 -> v2:
- v1: https://lore.kernel.org/bpf/[email protected]/
- Accept the sign-extended enum values emitted by GCC (patch 3), and make the new test work with GCC (patch 4).
Yonghong Song (4):
libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations selftests/bpf: Take unsigned enum values as unsigned int for GCC
include/linux/btf.h | 5 ++++ tools/lib/bpf/relo_core.c | 30 ++++++++++++++++--- .../selftests/bpf/prog_tests/core_reloc.c | 2 ++ .../selftests/bpf/progs/core_reloc_types.h | 8 +++++ .../bpf/progs/test_core_reloc_enumval.c | 21 +++++++++++++ 5 files changed, 62 insertions(+), 4 deletions(-)
Signature · 1 line
2.53.1-Meta
Yonghong Song
reviewerPATCH v2 1/4 libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
For a BPF_CORE_ENUMVAL_VALUE relocation, bpf_core_calc_enumval_relo() reads the value of a 32-bit enumerator from btf_enum->val, which is __s32, so the value is sign-extended to 64 bits even if the enum is unsigned. A value in [0x80000000, 0xffffffff] of an unsigned enum becomes 0xffffffff80000000 or above.
Clang emits such a value zero-extended in the ld_imm64 of the relocation, so the check of the original value fails, and the whole object is refused even if the target value is the same:
enum dentry_flags { DCACHE_PRIVATE = 0x80000000 };
out = bpf_core_enum_value(enum dentry_flags, DCACHE_PRIVATE);libbpf: prog 'p': relo #0: unexpected insn #0 (LDIMM64) value: got
2147483648, exp 18446744071562067968 -> 18446744071562067968
libbpf: prog 'p': relo #0: failed to patch insn #0: -EINVAL
And a target value in that range is patched sign-extended, which is wrong wherever the program uses it as a 64-bit value. The x86 vmlinux has 29 enums with such values, e.g., dentry_flags, init_ns_ino and e820_type.
Commit 89e7a42e3030 ("libbpf: Normalize enum values according to signedness") added btf_enum_value(), which extends the value as given by the kflag of the enum, for btf_dump and map fields, but not for CO-RE relocations, whose relo_core.c is also built into the kernel. Use it in bpf_core_calc_enumval_relo() as well, and add it to the kernel's include/linux/btf.h, so that the kernel gets the same fix.
--- include/linux/btf.h | 5 +++++ tools/lib/bpf/relo_core.c | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-)
include/linux/btf.h +5 -0
@@ -525,6 +525,11 @@ static inline struct btf_enum *btf_enum(const struct btf_type *t)return (struct btf_enum *)(t +1 ); } +static inline u64 btf_enum_value (const struct btf_type *t,const struct btf_enum *e) +{ +return btf_type_kflag (t) ? (s64 )e->val : (u32 )e->val; +} +static inline struct btf_enum64 *btf_enum64 (const struct btf_type *t) {return (struct btf_enum64 *)(t +1 );
tools/lib/bpf/relo_core.c +8 -4
@@ -861,6 +861,13 @@ static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo,return 0 ; } +static __u64 bpf_core_enumval (const struct btf_type *t,int idx) +{ +if (btf_is_enum (t)) +return btf_enum_value (t,btf_enum (t) + idx); +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)
@@ -875,10 +882,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)) - *val =btf_enum (t)[spec->spec[0 ].idx].val; -else - *val =btf_enum64_value (btf_enum64 (t) + spec->spec[0 ].idx); + *val =bpf_core_enumval (t, spec->spec[0 ].idx);break ;default :return -EOPNOTSUPP ;
Signature · 1 line
2.53.1-Meta
[email protected]
AI reviewRe: [PATCH bpf-next v2 1/4] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
TL;DR: Commit message only: the Fixes tag may be wrong, since the sign-extending enum read predates 23b2a3a8f63a and was introduced by eacaaed784e2; the tag may need to change.
Quoted 1 line
libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
TL;DR: Commit message only: the Fixes tag may be wrong, since the sign-extending enum read predates 23b2a3a8f63a and was introduced by eacaaed784e2; the tag may need to change.
Quoted 6 lines
libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
[ ... ]
Fixes 23b2a3a8f63a ("libbpf: Add enum64 relocation support")Signed-off-by Yonghong Song <[email protected]>
The sign-extending read of btf_enum->val in bpf_core_calc_enumval_relo() predates 23b2a3a8f63a, which only wrapped the existing read in an if (btf_is_enum(t)) branch. The old code already had the same __s32 to u64 sign extension:
e = btf_enum(t) + idx;
*val = e->val;The sign-extending read was introduced by eacaaed784e2 ("libbpf: Implement enum value-based CO-RE relocations"). Should the tag be:
The unsigned-enum (kflag) distinction was added later, so the unsigned symptom could arguably be dated to that, but the faulty read itself comes from eacaaed784e2.
---
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/44465301903
Yonghong Song
reviewerRe: [PATCH bpf-next v2 1/4] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
Quoted 19 lines · On 11/18/26 9:54 PM, [email protected] wrote:
Quoted 1 line
libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
TL;DR: Commit message only: the Fixes tag may be wrong, since the sign-extending enum read predates
23b2a3a8f63aand was introduced byeacaaed784e2; the tag may need to change.Quoted 6 lines
libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
[ ... ]
Fixes 23b2a3a8f63a ("libbpf: Add enum64 relocation support")Signed-off-by Yonghong Song <[email protected]>The sign-extending read of
btf_enum->valinbpf_core_calc_enumval_relo()predates23b2a3a8f63a, which only wrapped the existing read in an if (btf_is_enum(t)) branch. The old code already had the same__s32to u64 sign extension:e = btf_enum(t) + idx; *val = e->val;
Quoted 8 lines
The sign-extending read was introduced by
eacaaed784e2("libbpf: Implement enum value-based CO-RE relocations"). Should the tag be:Fixes eacaaed784e2 ("libbpf: Implement enum value-based CO-RE relocations")The unsigned-enum (kflag) distinction was added later, so the unsigned symptom could arguably be dated to that, but the faulty read itself comes from
eacaaed784e2.
I don't think we should use the following:
This commit is dated back to 2020:
commit eacaaed784e2c9da69dea3030c81062c1fd66a37
Author: Andrii Nakryiko <[email protected]> Date: Wed Aug 19 12:45:18 2020 -0700
libbpf: Implement enum value-based CO-RE relocations
and I don't think this can be backported due to in between a lot of changes.
I will choose this one:
Fixes: 776281652ddc ("libbpf: Permit 64bit relocation value")commit 776281652ddcd98bb292335ea3634da341c849b4
Author: Yonghong Song <[email protected]> Date: Mon Jun 6 23:26:05 2022 -0700
libbpf: Permit 64bit relocation value
This commits allows 64bit relocation value and this is directly related to this patch set.
Quoted 7 lines
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/44465301903
Yonghong Song
reviewerPATCH v2 2/4 selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
In reply to Yonghong SongTest the zero 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.
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
--- .../testing/selftests/bpf/prog_tests/core_reloc.c | 2 ++ .../testing/selftests/bpf/progs/core_reloc_types.h | 8 ++++++++ .../selftests/bpf/progs/test_core_reloc_enumval.c | 14 ++++++++++++++ 3 files changed, 24 insertions(+)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +2 -0
@@ -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,
tools/testing/selftests/bpf/progs/core_reloc_types.h +8 -0
@@ -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 {
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +14 -0
@@ -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,6 +45,8 @@ struct core_reloc_enumval_output {int named_val2;int anon_val1;int anon_val2; +long uint_val1; +long uint_val2; };SEC ("raw_tracepoint/sys_enter" )
@@ -64,6 +72,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 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL1); + if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2)) + out->uint_val2 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL2);#else data.skip = true;#endif
Signature · 1 line
2.53.1-Meta
[email protected]
AI reviewRe: [PATCH bpf-next v2 2/4] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
TL;DR: The new enum test likely fails core_reloc/enumval on the GCC-BPF flavor at this commit, since GCC sign-extends the value; fixes come later in the series, so it isn't bisectable.
Quoted 1 line
selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
TL;DR: The new enum test likely fails core_reloc/enumval on the GCC-BPF flavor at this commit, since GCC sign-extends the value; fixes come later in the series, so it isn't bisectable.
Quoted 3 lines
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +0
-0
[ ... ]
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c int test_core_enumval(void *ctx) · line 80
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +6
-0@@ -64,6 +72,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 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL1); + if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2)) + out->uint_val2 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL2);
Will this pass for the GCC-BPF flavor of the object (test_progs-bpf_gcc)?
Under !__clang__, bpf_core_enum_value() expands to __builtin_preserve_enum_value(), which returns int, so GCC emits the ld_imm64 sign-extended (for example lddw %r1,-2147483648 for 0x80000000).
With f3507c4b38e8 applied, bpf_core_calc_enumval_relo() gets orig_val from btf_enum_value(), which is (__u32)e->val for a kflag 0 enum, and bpf_core_patch_insn() then validates it against the instruction:
imm = (__u32)insn[0].imm | ((__u64)insn[1].imm << 32);
if (res->validate && imm != orig_val) {
pr_warn("... unexpected insn #%d (LDIMM64) value: ...");
return -EINVAL;
}For the GCC-built object imm is 0xffffffff80000000 while orig_val is 0x80000000, so bpf_object__load() should fail and ASSERT_OK(err, "obj_load") would fail core_reloc/enumval, and core_reloc_btfgen/enumval the same way. ENUMVAL_CASE(enumval) does not set .fails, neither subtest is in ci/vmtest/configs/DENYLIST.test_progs-bpf_gcc, and the gcc-bpf job (KERNEL_TEST: test_progs-bpf_gcc) runs on x86_64.
Even with only the libbpf fix, the int result stored into the new long uint_val1/uint_val2 fields would read back as 0xffffffff80000001, while core_reloc.c expects 0x80000001 and 0xffffffff.
This looks to be fixed later in the series, and both are needed for GCC:
aa93e7e947e0 ("libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations") makes libbpf accept the sign-extended form.
ec81091d83f1 ("selftests/bpf: Take unsigned enum values as unsigned int for GCC") adds UINT_ENUM_VALUE(), which casts to unsigned int under !__clang__.
Until both land, test_progs-bpf_gcc fails at this commit, so the series is not bisectable for that flavor. Could those changes be folded into this commit and f3507c4b38e8, or the series reordered?
The commit message also describes only the clang behaviour ("whose values clang emits zero-extended in the ld_imm64 of the relocations").
---
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/44465301903
Yonghong Song
reviewerRe: [PATCH bpf-next v2 2/4] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
Quoted 56 lines · On 11/18/26 9:54 PM, [email protected] wrote:
Quoted 1 line
selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
TL;DR: The new enum test likely fails core_reloc/enumval on the GCC-BPF flavor at this commit, since GCC sign-extends the value; fixes come later in the series, so it isn't bisectable.
Quoted 3 lines
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +0
-0[ ... ]
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c int test_core_enumval(void *ctx) · line 80
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +6
-0@@ -64,6 +72,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 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL1); + if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2)) + out->uint_val2 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL2);Will this pass for the GCC-BPF flavor of the object (test_progs-bpf_gcc)?
Under !__clang__,
bpf_core_enum_value()expands to__builtin_preserve_enum_value(), which returns int, so GCC emits theld_imm64sign-extended (for example lddw %r1,-2147483648 for0x80000000).With
f3507c4b38e8applied,bpf_core_calc_enumval_relo()getsorig_valfrombtf_enum_value(), which is (__u32)e->val for a kflag 0 enum, andbpf_core_patch_insn()then validates it against the instruction:imm = (__u32)insn[0].imm | ((__u64)insn[1].imm << 32); if (res->validate && imm != orig_val) { pr_warn("... unexpected insn #%d (LDIMM64) value: ..."); return -EINVAL; }For the GCC-built object imm is
0xffffffff80000000whileorig_valis0x80000000, sobpf_object__load()should fail andASSERT_OK(err, "obj_load")would fail core_reloc/enumval, and core_reloc_btfgen/enumval the same way.ENUMVAL_CASE(enumval)does not set .fails, neither subtest is in ci/vmtest/configs/DENYLIST.test_progs-bpf_gcc, and the gcc-bpf job (KERNEL_TEST: test_progs-bpf_gcc) runs onx86_64.Even with only the libbpf fix, the int result stored into the new long uint_val1/uint_val2 fields would read back as
0xffffffff80000001, whilecore_reloc.cexpects0x80000001and0xffffffff.This looks to be fixed later in the series, and both are needed for GCC:
aa93e7e947e0("libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations") makes libbpf accept the sign-extended form.
ec81091d83f1("selftests/bpf: Take unsigned enum values as unsigned int for GCC") addsUINT_ENUM_VALUE(), which casts to unsigned int under !__clang__.
Quoted 6 lines
Until both land, test_progs-bpf_gcc fails at this commit, so the series is not bisectable for that flavor. Could those changes be folded into this commit and
f3507c4b38e8, or the series reordered?The commit message also describes only the clang behaviour ("whose values clang emits zero-extended in the
ld_imm64of the relocations").
You are correct. this will still have gcc test failure.
So in the next revision, I will merge 1/3 as the first commit and 2/4 as the second commit. This way, we can avoid the selftest failure during bisecting.
Quoted 7 lines
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/44465301903
Yonghong Song
reviewerPATCH v2 3/4 libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations
In reply to Yonghong SongThe value of a 32-bit enum in a BPF_CORE_ENUMVAL_VALUE relocation is in the low 32 bits of the ld_imm64, and the compiler chooses how it is extended to 64 bits. Clang zero-extends the value of an unsigned enum and sign-extends that of a signed one. GCC sign-extends every value, since its __builtin_preserve_enum_value() returns int:
enum E { V = 0x80000000 };
out = bpf_core_enum_value(enum E, V); clang: r1 = 0x80000000 ll
gcc: lddw %r1,-2147483648
The previous patch extends the original value as given by the kflag of the enum, which is the form clang emits. So an object built by GCC, which was accepted when the value was sign-extended for all 32-bit enums, is now refused:
libbpf: prog 'p': relo #0: unexpected insn #0 (LDIMM64) value: got
18446744071562067968, exp 2147483648 -> 2147483648
The two forms differ only for an unsigned value in [0x80000000, 0xffffffff]. For such a value, accept the sign-extended form as well. In that case, also sign-extend the target value, since GCC code uses the ld_imm64 as the int value widened to 64 bits.
An original value below 0x80000000 is the same in both forms, so the instruction doesn't show which compiler emitted it. The target value then gets the extension given by the kflag of the target enum, which is the form clang emits, as its builtin returns long. A target value that doesn't fit in 32 bits, e.g., from an enum64 in the target kernel, is patched unchanged.
--- tools/lib/bpf/relo_core.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
tools/lib/bpf/relo_core.c +18 -0
@@ -1032,6 +1032,19 @@ static int insn_bytes_to_bpf_size(__u32 sz) } } +/* A 32-bit enum value is in the low 32 bits of the ld_imm64, extended as +* the compiler chooses: clang zero-extends an unsigned value, GCC +* sign-extends every value, as its __builtin_preserve_enum_value() returns +* int. Check whether the instruction has the sign-extended form of an +* unsigned value, so that the target value gets that form too. +*/ +static bool bpf_core_enumval_is_sext (const struct bpf_core_relo *relo, +__u64 imm,__u64 orig_val) +{ +return relo->kind ==BPF_CORE_ENUMVAL_VALUE && orig_val <=UINT_MAX && + imm != orig_val && imm == (__u64 )(__s32 )orig_val; +} +/* * Patch relocatable BPF instruction. *
@@ -1148,6 +1161,11 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,return bpf_core_poison_insn (prog_name, relo_idx, insn, insn_idx); imm = (__u32 )insn[0 ].imm | ((__u64 )insn[1 ].imm <<32 ); +if (res->validate &&bpf_core_enumval_is_sext (relo, imm, orig_val)) { + orig_val = imm; +if (new_val <=UINT_MAX ) + new_val = (__s32 )new_val; + }if (res->validate && imm != orig_val) { pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %llu -> %llu\n", prog_name, relo_idx,
Signature · 1 line
2.53.1-Meta
[email protected]
AI reviewRe: [PATCH bpf-next v2 3/4] libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] libbpf: non-standard multi-line comment style above bpf_core_enumval_is_sext() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] libbpf: non-standard multi-line comment style above bpf_core_enumval_is_sext()
--
commit 2ec397921876fd311cd75d218c792d79da6cd995
Author: Yonghong Song <[email protected]>
libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations
This commit accepts sign-extended 32-bit unsigned enum values in BPF_CORE_ENUMVAL_VALUE relocations and sign-extends the patched 32-bit target value to match GCC's output.
Quoted 10 lines
tools/lib/bpf/relo_core.c +2
-0@@ -1032,6 +1032,19 @@ static int insn_bytes_to_bpf_size(__u32 sz) } } +/* A 32-bit enum value is in the low 32 bits of the ld_imm64, extended as +* the compiler chooses: clang zero-extends an unsigned value, GCC
tools/lib/bpf/relo_core.c static int insn_bytes_to_bpf_size(__u32 sz) · line 1046
Quoted 10 lines
tools/lib/bpf/relo_core.c +10
-0+* sign-extends every value, as its __builtin_preserve_enum_value() returns +* int. Check whether the instruction has the sign-extended form of an +* unsigned value, so that the target value gets that form too. +*/ +static bool bpf_core_enumval_is_sext (const struct bpf_core_relo *relo, +__u64 imm,__u64 orig_val) +{ +return relo->kind ==BPF_CORE_ENUMVAL_VALUE && orig_val <=UINT_MAX && + imm != orig_val && imm == (__u64 )(__s32 )orig_val; +}
[Severity: Low]
This isn't a bug, but BPF code uses the multi-line comment style where the opening /* is placed on its own line.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Yonghong Song
reviewerPATCH v2 4/4 selftests/bpf: Take unsigned enum values as unsigned int for GCC
In reply to Yonghong SongGCC's __builtin_preserve_enum_value() returns int, so storing the value of enum named_uint_enum into a long field sign-extends it. With the previous patch, the program built by GCC loads, but uint_val1 reads back as 0xffffffff80000001 while the test expects 0x80000001. 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.
--- .../selftests/bpf/progs/test_core_reloc_enumval.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +9 -2
@@ -49,6 +49,13 @@ struct core_reloc_enumval_output {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) {
@@ -75,9 +82,9 @@ int test_core_enumval(void *ctx)/* enum named_uint_enum is optional */ if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL1)) - out->uint_val1 = bpf_core_enum_value(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 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL2); + out->uint_val2 =UINT_ENUM_VALUE (UINT_ENUM_VAL2 );#else data.skip = true;#endif
Signature · 1 line
2.53.1-Meta