libpf: Fix for the potential undefined behavior due to shifting
PATCH v3 1/1 libpf: Fix for the potential undefined behavior due to shifting
When compiling libbpf with sanitation, ubsan will report the following:
``` left shift of 1 by 31 places cannot be represented in type 'int' ```
This may lead to undefined behavior according to the compiler implementation, let's fix it by casting to unsigned counterpart before shifting.
---
v3:
- Fix a bug in v2.
tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +1 -1
@@ -11738,7 +11738,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, errstr(bit));return bit; } - attr.config |=1 << bit; + attr.config |=1ULL << bit; } attr.size = attr_sz; attr.type = type;
tools/lib/bpf/libbpf_internal.h +2 -2
@@ -77,7 +77,7 @@#define JUMPTABLES_SEC ".jumptables"#define BTF_INFO_ENC (kind, kind_flag, vlen) \ - ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN)) + (((__u32)(!!(kind_flag)) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))#define BTF_TYPE_ENC (name, info, size_or_type) (name), (info), (size_or_type)#define BTF_INT_ENC (encoding, bits_offset, nr_bits) \ ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
@@ -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
[email protected]
AI reviewThis isn't a bug, but would libbpf: be the intended subject prefix here, and could the ubsan output be indented rather than fenced with ``` so it reads as a normal kernel changelog?
Quoted 10 lines
libpf: Fix for the potential undefined behavior due to shifting
When compiling libbpf with sanitation, ubsan will report the following:
``` left shift of 1 by 31 places cannot be represented in type 'int' ```This may lead to undefined behavior according to the compiler implementation, let's fix it by casting to unsigned counterpart before shifting.
Signed-off-by Tan Wei <[email protected]>
This isn't a bug, but would libbpf: be the intended subject prefix here, and could the ubsan output be indented rather than fenced with ``` so it reads as a normal kernel changelog?
---
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/33027043792
Quoted 35 lines · Tw
When compiling libbpf with sanitation, ubsan will report the following:
``` left shift of 1 by 31 places cannot be represented in type 'int' ```This may lead to undefined behavior according to the compiler implementation, let's fix it by casting to unsigned counterpart before shifting.
Signed-off-by Tan Wei <[email protected]>---
v3:
- Fix a bug in v2.tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)tools/lib/bpf/libbpf.c +1
-1@@ -11738,7 +11738,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, errstr(bit));return bit; } - attr.config |=1 << bit; + attr.config |=1ULL << bit; } attr.size = attr_sz; attr.type = type;tools/lib/bpf/libbpf_internal.h +0
-0
tools/lib/bpf/libbpf_internal.h line 80
Quoted 5 lines
tools/lib/bpf/libbpf_internal.h +1
-1@@ -77,7 +77,7 @@#define JUMPTABLES_SEC ".jumptables"#define BTF_INFO_ENC (kind, kind_flag, vlen) \ - ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN)) + (((__u32)(!!(kind_flag)) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
see CI build failure, this change somehow breaks CI:
In file included from
/codebuild/output/src243362886/src/actions-runner/_work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/btf_sanitize.c:6:
/codebuild/output/src243362886/src/actions-runner/_work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/../test_btf.h:9:9:
error: 'BTF_INFO_ENC' macro redefined [-Werror,-Wmacro-redefined]
9 | #define BTF_INFO_ENC(kind, kind_flag, vlen) \
| ^
/codebuild/output/src243362886/src/actions-runner/_work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf_internal.h:79:9:note: previous definition is here
79 | #define BTF_INFO_ENC(kind, kind_flag, vlen) \
| ^
TEST-OBJ [test_progs] cgroup_attach_multi.test.o
TEST-OBJ [test_progs] cgroup_attach_override.test.o
1 error generated.
make: *** [Makefile:894:
/codebuild/output/src243362886/src/actions-runner/_work/bpf/bpf/tools/testing/selftests/bpf/btf_sanitize.test.o] Error 1
pw-bot: cr
Quoted 16 lines
tools/lib/bpf/libbpf_internal.h +1
-2#define BTF_TYPE_ENC (name, info, size_or_type) (name), (info), (size_or_type)#define BTF_INT_ENC (encoding, bits_offset, nr_bits) \ ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))@@ -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 { --2.54.0
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines · Tw
When compiling libbpf with sanitation, ubsan will report the following:
``` left shift of 1 by 31 places cannot be represented in type 'int' ```This may lead to undefined behavior according to the compiler implementation, let's fix it by casting to unsigned counterpart before shifting.
Quoted 5 lines
Quoted 5 lines
Signed-off-by Tan Wei <[email protected]>---
v3:
- Fix a bug in v2.
and you have a typo in patch subject: s/libpf/libbpf/
Quoted 65 lines
Quoted 22 lines
tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)tools/lib/bpf/libbpf.c +1
-1@@ -11738,7 +11738,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, errstr(bit));return bit; } - attr.config |=1 << bit; + attr.config |=1ULL << bit; } attr.size = attr_sz; attr.type = type;tools/lib/bpf/libbpf_internal.h +0
-0tools/lib/bpf/libbpf_internal.h line 80
Quoted 5 lines
tools/lib/bpf/libbpf_internal.h +1
-1@@ -77,7 +77,7 @@#define JUMPTABLES_SEC ".jumptables"#define BTF_INFO_ENC (kind, kind_flag, vlen) \ - ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN)) + (((__u32)(!!(kind_flag)) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))see CI build failure, this change somehow breaks CI:
In file included from
/codebuild/output/src243362886/src/actions-runner/_work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/btf_sanitize.c:6:/codebuild/output/src243362886/src/actions-runner/_work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/../test_btf.h:9:9: error: 'BTF_INFO_ENC' macro redefined [-Werror,-Wmacro-redefined] 9 | #define BTF_INFO_ENC(kind, kind_flag, vlen) \ | ^ /codebuild/output/src243362886/src/actions-runner/_work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf_internal.h:79:9:note: previous definition is here
79 | #defineBTF_INFO_ENC(kind, kind_flag, vlen)\
| ^
TEST-OBJ [test_progs] cgroup_attach_multi.test.o
TEST-OBJ [test_progs] cgroup_attach_override.test.o
1 error generated.
make: *** [Makefile:894:
/codebuild/output/src243362886/src/actions-runner/_work/bpf/bpf/tools/testing/selftests/bpf/btf_sanitize.test.o] Error 1pw-bot: cr
Quoted 16 lines
tools/lib/bpf/libbpf_internal.h +1
-2#define BTF_TYPE_ENC (name, info, size_or_type) (name), (info), (size_or_type)#define BTF_INT_ENC (encoding, bits_offset, nr_bits) \ ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))@@ -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 { --2.54.0