selftests/bpf: Attempt to build BPF programs with -Wsign-compare
authorAlexei Starovoitov <ast@kernel.org>
Tue, 26 Dec 2023 19:11:43 +0000 (11:11 -0800)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 3 Jan 2024 18:41:22 +0000 (10:41 -0800)
commit495d2d8133fd1407519170a5238f455abbd9ec9b
tree12f60945f5c43cbd7daaaf7be7e3594612a477c4
parenta640de4cf9fec0caf43ccb7404ec9f0fde9a6a65
selftests/bpf: Attempt to build BPF programs with -Wsign-compare

GCC's -Wall includes -Wsign-compare while clang does not.
Since BPF programs are built with clang we need to add this flag explicitly
to catch problematic comparisons like:

  int i = -1;
  unsigned int j = 1;
  if (i < j) // this is false.

  long i = -1;
  unsigned int j = 1;
  if (i < j) // this is true.

C standard for reference:

- If either operand is unsigned long the other shall be converted to unsigned long.

- Otherwise, if one operand is a long int and the other unsigned int, then if a
long int can represent all the values of an unsigned int, the unsigned int
shall be converted to a long int; otherwise both operands shall be converted to
unsigned long int.

- Otherwise, if either operand is long, the other shall be converted to long.

- Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

Unfortunately clang's -Wsign-compare is very noisy.
It complains about (s32)a == (u32)b which is safe and doen't have surprising behavior.

This patch fixes some of the issues. It needs a follow up to fix the rest.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/bpf/20231226191148.48536-2-alexei.starovoitov@gmail.com
25 files changed:
tools/testing/selftests/bpf/Makefile
tools/testing/selftests/bpf/progs/bpf_iter_bpf_percpu_hash_map.c
tools/testing/selftests/bpf/progs/bpf_iter_task_vmas.c
tools/testing/selftests/bpf/progs/bpf_iter_tasks.c
tools/testing/selftests/bpf/progs/bpf_iter_test_kern4.c
tools/testing/selftests/bpf/progs/cgroup_getset_retval_setsockopt.c
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c
tools/testing/selftests/bpf/progs/cpumask_success.c
tools/testing/selftests/bpf/progs/iters.c
tools/testing/selftests/bpf/progs/linked_funcs1.c
tools/testing/selftests/bpf/progs/linked_funcs2.c
tools/testing/selftests/bpf/progs/linked_list.c
tools/testing/selftests/bpf/progs/local_storage.c
tools/testing/selftests/bpf/progs/lsm.c
tools/testing/selftests/bpf/progs/normal_map_btf.c
tools/testing/selftests/bpf/progs/profiler.inc.h
tools/testing/selftests/bpf/progs/sockopt_inherit.c
tools/testing/selftests/bpf/progs/sockopt_multi.c
tools/testing/selftests/bpf/progs/sockopt_qos_to_cc.c
tools/testing/selftests/bpf/progs/test_bpf_ma.c
tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
tools/testing/selftests/bpf/progs/test_core_reloc_module.c
tools/testing/selftests/bpf/progs/test_fsverity.c
tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
tools/testing/selftests/bpf/progs/test_xdp_do_redirect.c