Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: avoid uninitialized warnings in verifier_global_subprogs.c

[Changes from V1:
- The warning to disable is -Wmaybe-uninitialized, not -Wuninitialized.
- This warning is only supported in GCC.]

The BPF selftest verifier_global_subprogs.c contains code that
purposedly performs out of bounds access to memory, to check whether
the kernel verifier is able to catch them. For example:

__noinline int global_unsupp(const int *mem)
{
if (!mem)
return 0;
return mem[100]; /* BOOM */
}

With -O1 and higher and no inlining, GCC notices this fact and emits a
"maybe uninitialized" warning. This is by design. Note that the
emission of these warnings is highly dependent on the precise
optimizations that are performed.

This patch adds a compiler pragma to verifier_global_subprogs.c to
ignore these warnings.

Tested in bpf-next master.
No regressions.

Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
Cc: david.faust@oracle.com
Cc: cupertino.miranda@oracle.com
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20240507184756.1772-1-jose.marchesi@oracle.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Jose E. Marchesi and committed by
Alexei Starovoitov
cd3fc3b9 e612b5c1

+7
+7
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c
··· 8 8 #include "xdp_metadata.h" 9 9 #include "bpf_kfuncs.h" 10 10 11 + /* The compiler may be able to detect the access to uninitialized 12 + memory in the routines performing out of bound memory accesses and 13 + emit warnings about it. This is the case of GCC. */ 14 + #if !defined(__clang__) 15 + #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 16 + #endif 17 + 11 18 int arr[1]; 12 19 int unkn_idx; 13 20 const volatile bool call_dead_subprog = false;