Skip to content

Commit 495d2d8

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
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 <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Jiri Olsa <[email protected]> Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent a640de4 commit 495d2d8

25 files changed

+30
-29
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
383383
BPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
384384
-I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
385385
-I$(abspath $(OUTPUT)/../usr/include)
386+
# TODO: enable me -Wsign-compare
386387

387388
CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
388389
-Wno-compare-distinct-pointer-types

tools/testing/selftests/bpf/progs/bpf_iter_bpf_percpu_hash_map.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct {
2020
} hashmap1 SEC(".maps");
2121

2222
/* will set before prog run */
23-
volatile const __u32 num_cpus = 0;
23+
volatile const __s32 num_cpus = 0;
2424

2525
/* will collect results during prog run */
2626
__u32 key_sum_a = 0, key_sum_b = 0, key_sum_c = 0;

tools/testing/selftests/bpf/progs/bpf_iter_task_vmas.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SEC("iter/task_vma") int proc_maps(struct bpf_iter__task_vma *ctx)
3535
return 0;
3636

3737
file = vma->vm_file;
38-
if (task->tgid != pid) {
38+
if (task->tgid != (pid_t)pid) {
3939
if (one_task)
4040
one_task_error = 1;
4141
return 0;

tools/testing/selftests/bpf/progs/bpf_iter_tasks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int dump_task(struct bpf_iter__task *ctx)
2222
return 0;
2323
}
2424

25-
if (task->pid != tid)
25+
if (task->pid != (pid_t)tid)
2626
num_unknown_tid++;
2727
else
2828
num_known_tid++;

tools/testing/selftests/bpf/progs/bpf_iter_test_kern4.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ int dump_bpf_map(struct bpf_iter__bpf_map *ctx)
4545
}
4646

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

5151
return ret;

tools/testing/selftests/bpf/progs/cgroup_getset_retval_setsockopt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
__u32 invocations = 0;
1212
__u32 assertion_error = 0;
1313
__u32 retval_value = 0;
14-
__u32 page_size = 0;
14+
__s32 page_size = 0;
1515

1616
SEC("cgroup/setsockopt")
1717
int get_retval(struct bpf_sockopt *ctx)

tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct {
1515
__type(value, long);
1616
} map_a SEC(".maps");
1717

18-
__u32 target_pid;
18+
__s32 target_pid;
1919
__u64 cgroup_id;
2020
int target_hid;
2121
bool is_cgroup1;

tools/testing/selftests/bpf/progs/cpumask_success.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ SEC("tp_btf/task_newtask")
332332
int BPF_PROG(test_copy_any_anyand, struct task_struct *task, u64 clone_flags)
333333
{
334334
struct bpf_cpumask *mask1, *mask2, *dst1, *dst2;
335-
u32 cpu;
335+
int cpu;
336336

337337
if (!is_test_task())
338338
return 0;

tools/testing/selftests/bpf/progs/iters.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <bpf/bpf_helpers.h>
77
#include "bpf_misc.h"
88

9-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
9+
#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0]))
1010

1111
static volatile int zero = 0;
1212

@@ -676,7 +676,7 @@ static __noinline int sum(struct bpf_iter_num *it, int *arr, __u32 n)
676676

677677
while ((t = bpf_iter_num_next(it))) {
678678
i = *t;
679-
if (i >= n)
679+
if ((__u32)i >= n)
680680
break;
681681
sum += arr[i];
682682
}

tools/testing/selftests/bpf/progs/linked_funcs1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "bpf_misc.h"
99

1010
/* weak and shared between two files */
11-
const volatile int my_tid __weak;
11+
const volatile __u32 my_tid __weak;
1212
long syscall_id __weak;
1313

1414
int output_val1;

0 commit comments

Comments
 (0)