Skip to contentSkip to conversation

libpf: Fix for the compiler sanity checking

PATCH In review Tw 3 participants 3 messages Aug 24 – Aug 25

Subsystems (2)
lore
Share & tools
lore

Tw

author
#0 lore raw

PATCH 1/1 libpf: Fix for the compiler sanity checking

When compiling libbpf with sanitation, ubsan will report the following:

```
left shift of 1 by 31 places cannot be represented in type 'int'
```

let's fix it by casting int to u32 before shifting.

---
 tools/lib/bpf/libbpf_internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf_internal.h +1 -1
index 4c46d34fc..222d2f1c0 100644
@@ -259,7 +259,7 @@ static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
 
 static inline __u32 btf_type_info(int kind, int vlen, int kflag)
 {
-	return (kflag << 31) | (kind << 24) | vlen;
+	return ((__u32)kflag << 31) | (kind << 24) | vlen;
 }
 
 enum map_def_parts {
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Medium] Latent undefined behavior due to signed left-shift overflow in the BTF_INFO_ENC() macro.
- [Medium] Latent undefined behavior due to signed left-shift when setting retprobe bit in attr.config. --

--- Patch [1]: [PATCH] libpf: Fix for the compiler sanity checking --- commit 6bb550337600fb1ee129a0b5ed1c50e66620bbc5
Author: Tw <[email protected]>

libpf: Fix for the compiler sanity checking

This patch fixes an undefined behavior issue during libbpf compilation. It resolves a ubsan warning about left shifting 1 by 31 places in an int by casting the integer to a u32 before performing the shift.

tools/lib/bpf/libbpf_internal.h static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t) · line 263

Quoted 11 lines
tools/lib/bpf/libbpf_internal.h +1 -1
index 4c46d34fc055d..222d2f1c0ed3a 100644
@@ -259,7 +259,7 @@ static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
 
 static inline __u32 btf_type_info(int kind, int vlen, int kflag)
 {
-	return (kflag << 31) | (kind << 24) | vlen;
+	return ((__u32)kflag << 31) | (kind << 24) | vlen;
 }

[Severity: Medium]
This isn't a bug introduced by this patch, but since you are fixing the shift in btf_type_info(), should we also apply this fix to the BTF_INFO_ENC macro in this same file?

tools/lib/bpf/libbpf_internal.h:

#define BTF_INFO_ENC(kind, kind_flag, vlen) \
	((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))

The !! operator results in a signed int. If a caller provides a non-zero kind_flag, shifting it left by 31 places into the sign bit will also result in undefined behavior. Would it make sense to cast to __u32 here as well?

[Severity: Medium]
This is another pre-existing issue, but I noticed a similar left-shift that could cause undefined behavior when setting the retprobe bit:

tools/lib/bpf/libbpf.c:perf_event_open_probe()

	if (retprobe) {
		int bit = uprobe ? determine_uprobe_retprobe_bit()
				 : determine_kprobe_retprobe_bit();

		if (bit < 0) {
[ ... ]
			return bit;
		}
		attr.config |= 1 << bit;
	}

Since bit is read from sysfs, if a future kernel update returns a bit index of 31 or higher, would 1 << bit overflow and incorrectly configure attr.config? Should this use 1ULL << bit to safely set the 64-bit config field?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 8 lines · On Mon, Aug 24, 2026 at 7:30 AM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Medium] Latent undefined behavior due to signed left-shift overflow in the BTF_INFO_ENC() macro.
- [Medium] Latent undefined behavior due to signed left-shift when setting retprobe bit in attr.config. --

let's fix these as well while at it, otherwise this will come up again (however silly this undefined behavior really is)

and please make patch subject more specific, "compiler sanity checking" is a bit to generic

pw-bot: cr

Quoted 59 lines

--- Patch [1]: [PATCH] libpf: Fix for the compiler sanity checking --- commit 6bb550337600fb1ee129a0b5ed1c50e66620bbc5
Author: Tw <[email protected]>

libpf: Fix for the compiler sanity checking

This patch fixes an undefined behavior issue during libbpf compilation. It resolves a ubsan warning about left shifting 1 by 31 places in an int by casting the integer to a u32 before performing the shift.

tools/lib/bpf/libbpf_internal.h static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t) · line 263

Quoted 11 lines
tools/lib/bpf/libbpf_internal.h +1 -1
index 4c46d34fc055d..222d2f1c0ed3a 100644
@@ -259,7 +259,7 @@ static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
 
 static inline __u32 btf_type_info(int kind, int vlen, int kflag)
 {
-     return (kflag << 31) | (kind << 24) | vlen;
+     return ((__u32)kflag << 31) | (kind << 24) | vlen;
 }

[Severity: Medium]
This isn't a bug introduced by this patch, but since you are fixing the shift in btf_type_info(), should we also apply this fix to the BTF_INFO_ENC macro in this same file?

tools/lib/bpf/libbpf_internal.h:

#define BTF_INFO_ENC(kind, kind_flag, vlen) \
        ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))

The !! operator results in a signed int. If a caller provides a non-zero kind_flag, shifting it left by 31 places into the sign bit will also result in undefined behavior. Would it make sense to cast to __u32 here as well?

[Severity: Medium]
This is another pre-existing issue, but I noticed a similar left-shift that could cause undefined behavior when setting the retprobe bit:

tools/lib/bpf/libbpf.c:perf_event_open_probe()

        if (retprobe) {
                int bit = uprobe ? determine_uprobe_retprobe_bit()
                                 : determine_kprobe_retprobe_bit();

                if (bit < 0) {
[ ... ]
                        return bit;
                }
                attr.config |= 1 << bit;
        }

Since bit is read from sysfs, if a future kernel update returns a bit index of 31 or higher, would 1 << bit overflow and incorrectly configure attr.config? Should this use 1ULL << bit to safely set the 64-bit config field?

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1