Commit 495d2d81 authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by Andrii Nakryiko
Browse files

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: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/bpf/20231226191148.48536-2-alexei.starovoitov@gmail.com
parent a640de4c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -383,6 +383,7 @@ CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
BPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN)	\
	     -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR)			\
	     -I$(abspath $(OUTPUT)/../usr/include)
# TODO: enable me -Wsign-compare

CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
	       -Wno-compare-distinct-pointer-types
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ struct {
} hashmap1 SEC(".maps");

/* will set before prog run */
volatile const __u32 num_cpus = 0;
volatile const __s32 num_cpus = 0;

/* will collect results during prog run */
__u32 key_sum_a = 0, key_sum_b = 0, key_sum_c = 0;
+1 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ SEC("iter/task_vma") int proc_maps(struct bpf_iter__task_vma *ctx)
		return 0;

	file = vma->vm_file;
	if (task->tgid != pid) {
	if (task->tgid != (pid_t)pid) {
		if (one_task)
			one_task_error = 1;
		return 0;
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ int dump_task(struct bpf_iter__task *ctx)
		return 0;
	}

	if (task->pid != tid)
	if (task->pid != (pid_t)tid)
		num_unknown_tid++;
	else
		num_known_tid++;
+1 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ int dump_bpf_map(struct bpf_iter__bpf_map *ctx)
	}

	/* fill seq_file buffer */
	for (i = 0; i < print_len; i++)
	for (i = 0; i < (int)print_len; i++)
		bpf_seq_write(seq, &seq_num, sizeof(seq_num));

	return ret;
Loading