selftests/bpf: Fix compilation with RELEASE=1
Subsystems (3)
cover selftests/bpf: Fix compilation with RELEASE=1
When compiling BPF selftests with RELEASE=1 (which most notably adds -O2), GCC reports several warnings. These are by default treated as errors and abort the compilation.
This series fixes all of the issues such that selftests can be compiled with RELEASE=1.
Viktor Malik (4):
selftests/bpf: Check malloc result with ASSERT_NEQ in test_loader selftests/bpf: Check malloc result with ASSERT_NEQ in test_sha256 selftests/bpf: Silence array bounds warning in global_map_resize selftests/bpf: Silence maybe-uninitialized compiler warning in
libarena
.../selftests/bpf/libarena/include/libarena/userspace.h | 2 +- .../testing/selftests/bpf/prog_tests/global_map_resize.c | 8 ++++++-- tools/testing/selftests/bpf/prog_tests/sha256.c | 4 ++-- tools/testing/selftests/bpf/test_loader.c | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-)
Signature · 1 line
2.54.0
PATCH 1/4 selftests/bpf: Check malloc result with ASSERT_NEQ in test_loader
Replace ASSERT_OK_PTR by ASSERT_NEQ(res, NULL, ...) when checking the result of malloc. It is more accurate since malloc returns NULL, not an error code, on failure and it also prevents the following false GCC warning when compiling BPF selftests with -O2:
In file included from test_loader.c:6:
test_loader.c: In function ‘verify_stderr’:
/bpf-next/tools/testing/selftests/bpf/test_progs.h:393:22: error: ‘buf’ may be used uninitialized [-Werror=maybe-uninitialized]
393 | int ___err = libbpf_get_error(___res); \
| ^~~~~~~~~~~~~~~~~~~~~~~~
test_loader.c:810:14: note: in expansion of macro ‘ASSERT_OK_PTR’
810 | if (!ASSERT_OK_PTR(buf, "malloc"))
| ^~~~~~~~~~~~~
In file included from /bpf-next/tools/testing/selftests/bpf/tools/include/bpf/bpf.h:32,
from /bpf-next/tools/testing/selftests/bpf/test_progs.h:37:
/bpf-next/tools/testing/selftests/bpf/tools/include/bpf/libbpf_legacy.h:113:17: note: by argument 1 of type ‘const void *’ to ‘libbpf_get_error’ declared here
113 | LIBBPF_API long libbpf_get_error(const void *ptr);
| ^~~~~~~~~~~~~~~~
--- tools/testing/selftests/bpf/test_loader.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/test_loader.c +1 -1
@@ -807,7 +807,7 @@ static void verify_stderr(int prog_fd, struct expected_msgs *msgs)return ; buf =malloc (TEST_LOADER_LOG_BUF_SZ ); -if (!ASSERT_OK_PTR (buf,"malloc" )) +if (!ASSERT_NEQ (buf,NULL ,"malloc" ))return ; ret = bpf_prog_stream_read(prog_fd, 2, buf, TEST_LOADER_LOG_BUF_SZ - 1,
Signature · 1 line
2.54.0
PATCH 2/4 selftests/bpf: Check malloc result with ASSERT_NEQ in test_sha256
In reply to Viktor MalikReplace ASSERT_OK_PTR by ASSERT_NEQ(res, NULL, ...) when checking the result of malloc. It is more accurate since malloc returns NULL, not an error code, on failure and it also prevents the following false GCC warning when compiling BPF selftests with -O2:
In file included from /bpf-next/tools/testing/selftests/bpf/prog_tests/sha256.c:4:
/bpf-next/tools/testing/selftests/bpf/prog_tests/sha256.c: In function ‘test_sha256’:
./test_progs.h:393:22: error: ‘data’ may be used uninitialized [-Werror=maybe-uninitialized]
393 | int ___err = libbpf_get_error(___res); \
| ^~~~~~~~~~~~~~~~~~~~~~~~
/bpf-next/tools/testing/selftests/bpf/prog_tests/sha256.c:28:14: note: in expansion of macro ‘ASSERT_OK_PTR’
28 | if (!ASSERT_OK_PTR(data, "malloc"))
| ^~~~~~~~~~~~~
In file included from /bpf-next/tools/testing/selftests/bpf/tools/include/bpf/bpf.h:32,
from ./test_progs.h:37:
/bpf-next/tools/testing/selftests/bpf/tools/include/bpf/libbpf_legacy.h:113:17: note: by argument 1 of type ‘const void *’ to ‘libbpf_get_error’ declared here
113 | LIBBPF_API long libbpf_get_error(const void *ptr);
| ^~~~~~~~~~~~~~~~--- tools/testing/selftests/bpf/prog_tests/sha256.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sha256.c +2 -2
@@ -25,10 +25,10 @@ void test_sha256(void)size_t i; data =malloc (MAX_LEN ); -if (!ASSERT_OK_PTR (data,"malloc" )) +if (!ASSERT_NEQ (data,NULL ,"malloc" )) goto out; digests =malloc ((MAX_LEN +1 ) *SHA256_DIGEST_LENGTH ); -if (!ASSERT_OK_PTR (digests,"malloc" )) +if (!ASSERT_NEQ (digests,NULL ,"malloc" )) goto out;/* Generate MAX_LEN bytes of "random" data deterministically. */
Signature · 1 line
2.54.0
PATCH 3/4 selftests/bpf: Silence array bounds warning in global_map_resize
In reply to Viktor MalikWhen compiling BPF selftests with -O2, GCC reports an array bounds violation warning in global_map_resize test:
In function ‘global_map_resize_bss_subtest’,
inlined from ‘test_global_map_resize’ at /bpf-next/tools/testing/selftests/bpf/prog_tests/global_map_resize.c:228:3:
/bpf-next/tools/testing/selftests/bpf/prog_tests/global_map_resize.c:64:33: error: array subscript 1 is above array bounds of ‘int[1]’ [-Werror=array-bounds=]
64 | skel->bss->array[i] = 1;
| ~~~~~~~~~~~~~~~~^~~
In file included from /bpf-next/tools/testing/selftests/bpf/prog_tests/global_map_resize.c:6:
./test_global_map_resize.skel.h: In function ‘test_global_map_resize’:
./test_global_map_resize.skel.h:44:21: note: while referencing ‘array’
44 | int array[1];
| ^~~~~
This is a false positive because array (a BPF map) has been resized from within the BPF program. GCC doesn't know that so let us silence the warning by accessing the array via a plain pointer.
--- .../testing/selftests/bpf/prog_tests/global_map_resize.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/global_map_resize.c +6 -2
@@ -23,6 +23,7 @@ static void global_map_resize_bss_subtest(void)struct bpf_map *map;const __u32 desired_sz =sizeof (skel->bss->sum) +sysconf (_SC_PAGE_SIZE) *2 ;size_t array_len, actual_sz, new_sz; +int *array; skel =test_global_map_resize__open ();if (!ASSERT_OK_PTR (skel,"test_global_map_resize__open" ))
@@ -58,10 +59,13 @@ static void global_map_resize_bss_subtest(void) goto teardown;/* fill the newly resized array with ones, -* skipping the first element which was previously set +* skipping the first element which was previously set; +* access through a plain pointer to avoid -Warray-bounds +* since the array was resized beyond its declared length. */ + array = skel->bss->array;for (int i =1 ; i < array_len; i++) - skel->bss->array[i] =1 ; + array[i] =1 ;/* set global const values before loading */ skel->rodata->pid =getpid ();
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH bpf-next 3/4] selftests/bpf: Silence array bounds warning in global_map_resize
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Multi-line comment opening '/*' is not on its own line. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Multi-line comment opening '/*' is not on its own line.
--
--- Patch [3]: [PATCH bpf-next 3/4] selftests/bpf: Silence array bounds warning in global_map_resize --- commit 477df4be9ab84dd41e948528c2cecec30f56ae6a
Author: Viktor Malik <[email protected]>
selftests/bpf: Silence array bounds warning in global_map_resize
This commit silences a GCC false positive array bounds violation warning in global_map_resize. It achieves this by accessing the BPF map array via a plain pointer after it has been resized from within the BPF program.
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/global_map_resize.c +0
-0
[ ... ]
tools/testing/selftests/bpf/prog_tests/global_map_resize.c static void global_map_resize_bss_subtest(void) · line 65
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/global_map_resize.c +3
-1@@ -58,10 +59,13 @@ static void global_map_resize_bss_subtest(void) goto teardown;/* fill the newly resized array with ones, -* skipping the first element which was previously set +* skipping the first element which was previously set; +* access through a plain pointer to avoid -Warray-bounds +* since the array was resized beyond its declared length. */
[Severity: Low]
This isn't a bug, but does this multi-line comment follow the preferred BPF coding style?
The BPF subsystem guidelines require that multi-line comments have the opening '/*' on its own line, even when modifying surrounding code that uses the old style.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/global_map_resize.c +2
-1+ array = skel->bss->array;for (int i =1 ; i < array_len; i++) - skel->bss->array[i] =1 ; + array[i] =1 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/4 selftests/bpf: Silence maybe-uninitialized compiler warning in libarena
In reply to Viktor MalikWhen compiling BPF selftests with -O2, GCC reports a maybe-uninitialized warning in libarena code:
In file included from /bpf-next/tools/testing/selftests/bpf/prog_tests/libarena_asan.c:11:
In function ‘libarena_asan_init’,
inlined from ‘run_test’ at /bpf-next/tools/testing/selftests/bpf/prog_tests/libarena_asan.c:59:8,
inlined from ‘test_libarena_asan’ at /bpf-next/tools/testing/selftests/bpf/prog_tests/libarena_asan.c:91:2:
/bpf-next/tools/testing/selftests/bpf/libarena/include/libarena/userspace.h:126:14: error: ‘globals_pages’ may be used uninitialized [-Werror=maybe-uninitialized]
126 | args = (struct asan_init_args){
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
127 | .arena_all_pages = arena_all_pages,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128 | .arena_globals_pages = globals_pages,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129 | };
| ~
/bpf-next/tools/testing/selftests/bpf/libarena/include/libarena/userspace.h: In function ‘test_libarena_asan’:
/bpf-next/tools/testing/selftests/bpf/libarena/include/libarena/userspace.h:118:13: note: ‘globals_pages’ was declared here
118 | u64 globals_pages;
| ^~~~~~~~~~~~~
Silence the warning by initializing globals_pages to 0.
--- .../testing/selftests/bpf/libarena/include/libarena/userspace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/libarena/include/libarena/userspace.h +1 -1
@@ -115,7 +115,7 @@ static inline int libarena_asan_init(int arena_asan_init_fd, {LIBBPF_OPTS (bpf_test_run_opts, opts);struct asan_init_args args; -u64 globals_pages; +u64 globals_pages =0 ;int ret; ret = libarena_get_globals_pages(arena_asan_init_fd,
Signature · 1 line
2.54.0
Kumar Kartikeya Dwivedi
maintainerQuoted 8 lines · Viktor Malik
When compiling BPF selftests with RELEASE=1 (which most notably adds -O2), GCC reports several warnings. These are by default treated as errors and abort the compilation.
This series fixes all of the issues such that selftests can be compiled with RELEASE=1.
Are these changes sufficient to fix all issues? I see more when I compile with GCC 15 and RELEASE=1.
Also, is there no way to make ASSERT_OK_PTR work when we're testing for non-NULL-ness? Having to change it to explicit != NULL is kinda sad.
Quoted 6 lines · Kumar Kartikeya Dwivedi
Quoted 5 lines · Viktor Malik
When compiling BPF selftests with RELEASE=1 (which most notably adds -O2), GCC reports several warnings. These are by default treated as errors and abort the compilation.
Quoted 6 lines
Quoted 3 lines
This series fixes all of the issues such that selftests can be compiled with RELEASE=1.
Are these changes sufficient to fix all issues? I see more when I compile with GCC 15 and RELEASE=1.
Strange, I tried compiling with both GCC 15 and 16 and these four patches got rid of all the issues. Can you send the specific issues that you see?
Quoted 3 lines
Also, is there no way to make
ASSERT_OK_PTRwork when we're testing for non-NULL-ness? Having to change it to explicit != NULL is kinda sad.
ASSERT_OK_PTR uses libbpf_get_error(), which returns -errno when the passed pointer is NULL. This is correct for checking pointers returned from libbpf because libbpf always sets errno when it returns NULL as an erroneous value of a pointer. However, that is not the case for other library functions, such as malloc(), so calling libbpf_get_error() on their result may actually return 0, if the function returned NULL and never set errno.
So, I'd say that in the current state, results of non-libbpf ptr-returning functions should be checked with ASSERT_NEQ. Alternatively, we can update ASSERT_OK_PTR to explicitly fail with a default error code when NULL is passed, for example:
tools/testing/selftests/bpf/test_progs.h +1 -1
@@ -390,7 +390,7 @@ void hexdump(const char *prefix, const void *buf, size_t len);#define ASSERT_OK_PTR (ptr, name) ({ \ static int duration = 0; \ const void *___res = (ptr); \ - int ___err = libbpf_get_error(___res); \ + int ___err = ___res ? libbpf_get_error(___res) : -EINVAL; \ bool ___ok = ___err == 0; \ CHECK(!___ok, (name), "unexpected error: %d\n", ___err); \ ___ok; \
However, that would make us lose the error codes passed via errno from libbpf.
Viktor
Kumar Kartikeya Dwivedi
maintainerQuoted 11 lines · Viktor Malik
Quoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Viktor Malik
When compiling BPF selftests with RELEASE=1 (which most notably adds -O2), GCC reports several warnings. These are by default treated as errors and abort the compilation.
This series fixes all of the issues such that selftests can be compiled with RELEASE=1.
Quoted 7 lines
Quoted 2 lines
Are these changes sufficient to fix all issues? I see more when I compile with GCC 15 and RELEASE=1.
Strange, I tried compiling with both GCC 15 and 16 and these four patches got rid of all the issues. Can you send the specific issues that you see?
In file included from /usr/include/features.h:539,
from /usr/include/arpa/inet.h:21,
from tools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c:3: In function ‘inet_ntop’,
inlined from ‘map_batch_verify.constprop’ at tools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c:53:3:
/usr/include/x86_64-linux-gnu/bits/inet-fortified.h:36:10: error: call to ‘__inet_ntop_chk_warn’ declared with attribute warning: inet_ntop called with bigger length than size of destination buffer [-Werror=attribute-warning]
36 | return __glibc_fortify (inet_ntop, __dst_size, sizeof (char),
| ^~~~~~~~~~~~~~~
In file included from /usr/include/fcntl.h:342,
from tools/testing/selftests/bpf/prog_tests/cgroup_xattr.c:5:
In function ‘open’,
inlined from ‘test_read_cgroup_xattr’ at tools/testing/selftests/bpf/prog_tests/cgroup_xattr.c:52:11,
inlined from ‘test_cgroup_xattr’ at tools/testing/selftests/bpf/prog_tests/cgroup_xattr.c:71:3:
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:52:11: error: call to ‘__open_missing_mode’ declared with attribute error: open with O_CREAT or O_TMPFILE in second argument needs 3 arguments
52 | __open_missing_mode ();
| ^~~~~~~~~~~~~~~~~~~~~~That said, it also fixes the ones you pointed out, so we can probably land this.
Quoted 31 lines
Quoted 3 lines
Also, is there no way to make
ASSERT_OK_PTRwork when we're testing for non-NULL-ness? Having to change it to explicit != NULL is kinda sad.
ASSERT_OK_PTRuseslibbpf_get_error(), which returns -errno when the passed pointer is NULL. This is correct for checking pointers returned from libbpf because libbpf always sets errno when it returns NULL as an erroneous value of a pointer. However, that is not the case for other library functions, such asmalloc(), so callinglibbpf_get_error()on their result may actually return 0, if the function returned NULL and never set errno.So, I'd say that in the current state, results of non-libbpf ptr-returning functions should be checked with
ASSERT_NEQ. Alternatively, we can updateASSERT_OK_PTRto explicitly fail with a default error code when NULL is passed, for example:tools/testing/selftests/bpf/test_progs.h +1
-1@@ -390,7 +390,7 @@ void hexdump(const char *prefix, const void *buf, size_t len);#define ASSERT_OK_PTR (ptr, name) ({ \ static int duration = 0; \ const void *___res = (ptr); \ - int ___err = libbpf_get_error(___res); \ + int ___err = ___res ? libbpf_get_error(___res) : -EINVAL; \ bool ___ok = ___err == 0; \ CHECK(!___ok, (name), "unexpected error: %d\n", ___err); \ ___ok; \
Quoted 2 lines
However, that would make us lose the error codes passed via errno from libbpf.
Hmm, I see. I guess we can let it be then. Eduard, any suggestions or preferences here? Otherwise I am inclined to apply this for now.
Quoted 2 lines
Viktor
Eduard Zingerman
maintainerQuoted 73 lines · Kumar Kartikeya Dwivedi
Quoted 11 lines · Viktor Malik
Quoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Viktor Malik
When compiling BPF selftests with RELEASE=1 (which most notably adds -O2), GCC reports several warnings. These are by default treated as errors and abort the compilation.
This series fixes all of the issues such that selftests can be compiled with RELEASE=1.
Quoted 7 lines
Quoted 2 lines
Are these changes sufficient to fix all issues? I see more when I compile with GCC 15 and RELEASE=1.
Strange, I tried compiling with both GCC 15 and 16 and these four patches got rid of all the issues. Can you send the specific issues that you see?
In file included from
/usr/include/features.h:539,
from/usr/include/arpa/inet.h:21,
fromtools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c:3:
In function ‘inet_ntop’,
inlined from ‘map_batch_verify.constprop’ attools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c:53:3:
/usr/include/x86_64-linux-gnu/bits/inet-fortified.h:36:10: error: call to ‘__inet_ntop_chk_warn’ declared with attribute warning:inet_ntopcalled with bigger length than size of destination buffer [-Werror=attribute-warning]
36 | return__glibc_fortify(inet_ntop,__dst_size, sizeof (char),
| ^~~~~~~~~~~~~~~In file included from
/usr/include/fcntl.h:342,
fromtools/testing/selftests/bpf/prog_tests/cgroup_xattr.c:5:
In function ‘open’,
inlined from ‘test_read_cgroup_xattr’ attools/testing/selftests/bpf/prog_tests/cgroup_xattr.c:52:11,
inlined from ‘test_cgroup_xattr’ attools/testing/selftests/bpf/prog_tests/cgroup_xattr.c:71:3:
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:52:11: error: call to ‘__open_missing_mode’ declared with attribute error: open withO_CREATorO_TMPFILEin second argument needs 3 arguments
52 |__open_missing_mode();
| ^~~~~~~~~~~~~~~~~~~~~~That said, it also fixes the ones you pointed out, so we can probably land this.
Quoted 31 lines
Quoted 3 lines
Also, is there no way to make
ASSERT_OK_PTRwork when we're testing for non-NULL-ness? Having to change it to explicit != NULL is kinda sad.
ASSERT_OK_PTRuseslibbpf_get_error(), which returns -errno when the passed pointer is NULL. This is correct for checking pointers returned from libbpf because libbpf always sets errno when it returns NULL as an erroneous value of a pointer. However, that is not the case for other library functions, such asmalloc(), so callinglibbpf_get_error()on their result may actually return 0, if the function returned NULL and never set errno.So, I'd say that in the current state, results of non-libbpf ptr-returning functions should be checked with
ASSERT_NEQ. Alternatively, we can updateASSERT_OK_PTRto explicitly fail with a default error code when NULL is passed, for example:tools/testing/selftests/bpf/test_progs.h +1
-1@@ -390,7 +390,7 @@ void hexdump(const char *prefix, const void *buf, size_t len); #define ASSERT_OK_PTR(ptr, name) ({ \ static int duration = 0; \ const void *___res = (ptr); \ - int ___err = libbpf_get_error(___res); \ + int ___err = ___res ? libbpf_get_error(___res) : -EINVAL; \ bool ___ok = ___err == 0; \ CHECK(!___ok, (name), "unexpected error: %d\n", ___err); \ ___ok; \
Quoted 5 lines
Quoted 2 lines
However, that would make us lose the error codes passed via errno from libbpf.
Hmm, I see. I guess we can let it be then. Eduard, any suggestions or preferences here?
The change to ASSERT_OK_PTR logic seem reasonable, a single test run should show whether there are places that legitimately expect NULL to pass the check, such places can be updated.
Quoted 1 line
Otherwise I am inclined to apply this for now.
I think it's fine to apply this patch as-is.
This series was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:
Quoted 8 lines · On Wed, 15 Jul 2026 13:21:59 +0200 you wrote:
When compiling BPF selftests with RELEASE=1 (which most notably adds -O2), GCC reports several warnings. These are by default treated as errors and abort the compilation.
This series fixes all of the issues such that selftests can be compiled with RELEASE=1.
[...]
Here is the summary with links:
- [bpf-next,1/4] selftests/bpf: Check malloc result with ASSERT_NEQ in test_loader
https://git.kernel.org/bpf/bpf-next/c/40f986aed81f - [bpf-next,2/4] selftests/bpf: Check malloc result with ASSERT_NEQ in test_sha256 https://git.kernel.org/bpf/bpf-next/c/eb5cd154f174 - [bpf-next,3/4] selftests/bpf: Silence array bounds warning in global_map_resize https://git.kernel.org/bpf/bpf-next/c/dcd164ec67f8 - [bpf-next,4/4] selftests/bpf: Silence maybe-uninitialized compiler warning in libarena https://git.kernel.org/bpf/bpf-next/c/5763790965eb
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html