[PATCH] selftests/bpf: fix array_size.cocci warning

Guo Zhengkui posted 1 patch 4 years, 3 months ago
There is a newer version of this series
.../selftests/bpf/prog_tests/cgroup_attach_autodetach.c     | 2 +-
.../testing/selftests/bpf/prog_tests/cgroup_attach_multi.c  | 2 +-
.../selftests/bpf/prog_tests/cgroup_attach_override.c       | 2 +-
tools/testing/selftests/bpf/prog_tests/global_data.c        | 6 +++---
tools/testing/selftests/bpf/prog_tests/obj_name.c           | 2 +-
tools/testing/selftests/bpf/progs/syscall.c                 | 3 ++-
tools/testing/selftests/bpf/progs/test_rdonly_maps.c        | 3 ++-
tools/testing/selftests/bpf/test_cgroup_storage.c           | 2 +-
tools/testing/selftests/bpf/test_lru_map.c                  | 4 ++--
tools/testing/selftests/bpf/test_sock_addr.c                | 6 +++---
tools/testing/selftests/bpf/test_sockmap.c                  | 4 ++--
11 files changed, 19 insertions(+), 17 deletions(-)
[PATCH] selftests/bpf: fix array_size.cocci warning
Posted by Guo Zhengkui 4 years, 3 months ago
Fix the array_size.cocci warning in tools/testing/selftests/bpf/

Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.

syscall.c and test_rdonly_maps.c don't contain header files which
implement ARRAY_SIZE() macro. So I add `#include <linux/kernel.h>`,
in which ARRAY_SIZE(arr) not only calculates the size of `arr`, but also
checks that `arr` is really an array (using __must_be_array(arr)).

Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
---
 .../selftests/bpf/prog_tests/cgroup_attach_autodetach.c     | 2 +-
 .../testing/selftests/bpf/prog_tests/cgroup_attach_multi.c  | 2 +-
 .../selftests/bpf/prog_tests/cgroup_attach_override.c       | 2 +-
 tools/testing/selftests/bpf/prog_tests/global_data.c        | 6 +++---
 tools/testing/selftests/bpf/prog_tests/obj_name.c           | 2 +-
 tools/testing/selftests/bpf/progs/syscall.c                 | 3 ++-
 tools/testing/selftests/bpf/progs/test_rdonly_maps.c        | 3 ++-
 tools/testing/selftests/bpf/test_cgroup_storage.c           | 2 +-
 tools/testing/selftests/bpf/test_lru_map.c                  | 4 ++--
 tools/testing/selftests/bpf/test_sock_addr.c                | 6 +++---
 tools/testing/selftests/bpf/test_sockmap.c                  | 4 ++--
 11 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
index 858916d11e2e..9367bd2f0ae1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
@@ -14,7 +14,7 @@ static int prog_load(void)
 		BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 
 	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
 			       prog, insns_cnt, "GPL", 0,
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
index 38b3c47293da..db0b7bac78d1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
@@ -63,7 +63,7 @@ static int prog_load_cnt(int verdict, int val)
 		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	int ret;
 
 	ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
index 356547e849e2..9421a5b7f4e1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
@@ -16,7 +16,7 @@ static int prog_load(int verdict)
 		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 
 	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
 			       prog, insns_cnt, "GPL", 0,
diff --git a/tools/testing/selftests/bpf/prog_tests/global_data.c b/tools/testing/selftests/bpf/prog_tests/global_data.c
index 6fb3d3155c35..027685858925 100644
--- a/tools/testing/selftests/bpf/prog_tests/global_data.c
+++ b/tools/testing/selftests/bpf/prog_tests/global_data.c
@@ -29,7 +29,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)
 		{ "relocate .rodata reference", 10, ~0 },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &num);
 		CHECK(err || num != tests[i].num, tests[i].name,
 		      "err %d result %llx expected %llx\n",
@@ -58,7 +58,7 @@ static void test_global_data_string(struct bpf_object *obj, __u32 duration)
 		{ "relocate .bss reference",    4, "\0\0hello" },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, str);
 		CHECK(err || memcmp(str, tests[i].str, sizeof(str)),
 		      tests[i].name, "err %d result \'%s\' expected \'%s\'\n",
@@ -92,7 +92,7 @@ static void test_global_data_struct(struct bpf_object *obj, __u32 duration)
 		{ "relocate .data reference",   3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val);
 		CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)),
 		      tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",
diff --git a/tools/testing/selftests/bpf/prog_tests/obj_name.c b/tools/testing/selftests/bpf/prog_tests/obj_name.c
index 6194b776a28b..7093edca6e08 100644
--- a/tools/testing/selftests/bpf/prog_tests/obj_name.c
+++ b/tools/testing/selftests/bpf/prog_tests/obj_name.c
@@ -20,7 +20,7 @@ void test_obj_name(void)
 	__u32 duration = 0;
 	int i;
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		size_t name_len = strlen(tests[i].name) + 1;
 		union bpf_attr attr;
 		size_t ncopy;
diff --git a/tools/testing/selftests/bpf/progs/syscall.c b/tools/testing/selftests/bpf/progs/syscall.c
index e550f728962d..85c6e7849463 100644
--- a/tools/testing/selftests/bpf/progs/syscall.c
+++ b/tools/testing/selftests/bpf/progs/syscall.c
@@ -6,6 +6,7 @@
 #include <bpf/bpf_tracing.h>
 #include <../../../tools/include/linux/filter.h>
 #include <linux/btf.h>
+#include <linux/kernel.h>
 
 char _license[] SEC("license") = "GPL";
 
@@ -82,7 +83,7 @@ int bpf_prog(struct args *ctx)
 	static __u64 value = 34;
 	static union bpf_attr prog_load_attr = {
 		.prog_type = BPF_PROG_TYPE_XDP,
-		.insn_cnt = sizeof(insns) / sizeof(insns[0]),
+		.insn_cnt = ARRAY_SIZE(insns),
 	};
 	int ret;
 
diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
index fc8e8a34a3db..ca75aac745a4 100644
--- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
+++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
@@ -3,6 +3,7 @@
 
 #include <linux/ptrace.h>
 #include <linux/bpf.h>
+#include <linux/kernel.h>
 #include <bpf/bpf_helpers.h>
 
 const struct {
@@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
 {
 	/* prevent compiler to optimize everything out */
 	unsigned * volatile p = (void *)&rdonly_values.a;
-	int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
+	int i = ARRAY_SIZE(rdonly_values.a);
 	unsigned iters = 0, sum = 0;
 
 	/* validate verifier can allow full loop as well */
diff --git a/tools/testing/selftests/bpf/test_cgroup_storage.c b/tools/testing/selftests/bpf/test_cgroup_storage.c
index 5b8314cd77fd..d6a1be4d8020 100644
--- a/tools/testing/selftests/bpf/test_cgroup_storage.c
+++ b/tools/testing/selftests/bpf/test_cgroup_storage.c
@@ -36,7 +36,7 @@ int main(int argc, char **argv)
 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	int error = EXIT_FAILURE;
 	int map_fd, percpu_map_fd, prog_fd, cgroup_fd;
 	struct bpf_cgroup_storage_key key;
diff --git a/tools/testing/selftests/bpf/test_lru_map.c b/tools/testing/selftests/bpf/test_lru_map.c
index 6e6235185a86..563bbe18c172 100644
--- a/tools/testing/selftests/bpf/test_lru_map.c
+++ b/tools/testing/selftests/bpf/test_lru_map.c
@@ -878,11 +878,11 @@ int main(int argc, char **argv)
 	assert(nr_cpus != -1);
 	printf("nr_cpus:%d\n\n", nr_cpus);
 
-	for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
+	for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
 		unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ?
 			PERCPU_FREE_TARGET : LOCAL_FREE_TARGET;
 
-		for (t = 0; t < sizeof(map_types) / sizeof(*map_types); t++) {
+		for (t = 0; t < ARRAY_SIZE(map_types); t++) {
 			test_lru_sanity0(map_types[t], map_flags[f]);
 			test_lru_sanity1(map_types[t], map_flags[f], tgt_free);
 			test_lru_sanity2(map_types[t], map_flags[f], tgt_free);
diff --git a/tools/testing/selftests/bpf/test_sock_addr.c b/tools/testing/selftests/bpf/test_sock_addr.c
index f0c8d05ba6d1..f3d5d7ac6505 100644
--- a/tools/testing/selftests/bpf/test_sock_addr.c
+++ b/tools/testing/selftests/bpf/test_sock_addr.c
@@ -723,7 +723,7 @@ static int xmsg_ret_only_prog_load(const struct sock_addr_test *test,
 		BPF_MOV64_IMM(BPF_REG_0, rc),
 		BPF_EXIT_INSN(),
 	};
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int sendmsg_allow_prog_load(const struct sock_addr_test *test)
@@ -795,7 +795,7 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
 		BPF_EXIT_INSN(),
 	};
 
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test)
@@ -858,7 +858,7 @@ static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
 		BPF_EXIT_INSN(),
 	};
 
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
diff --git a/tools/testing/selftests/bpf/test_sockmap.c b/tools/testing/selftests/bpf/test_sockmap.c
index 1ba7e7346afb..dfb4f5c0fcb9 100644
--- a/tools/testing/selftests/bpf/test_sockmap.c
+++ b/tools/testing/selftests/bpf/test_sockmap.c
@@ -1786,7 +1786,7 @@ static int populate_progs(char *bpf_file)
 		i++;
 	}
 
-	for (i = 0; i < sizeof(map_fd)/sizeof(int); i++) {
+	for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
 		maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
 		map_fd[i] = bpf_map__fd(maps[i]);
 		if (map_fd[i] < 0) {
@@ -1867,7 +1867,7 @@ static int __test_selftests(int cg_fd, struct sockmap_options *opt)
 	}
 
 	/* Tests basic commands and APIs */
-	for (i = 0; i < sizeof(test)/sizeof(struct _test); i++) {
+	for (i = 0; i < ARRAY_SIZE(test); i++) {
 		struct _test t = test[i];
 
 		if (check_whitelist(&t, opt) != 0)
-- 
2.20.1
Re: [PATCH] selftests/bpf: fix array_size.cocci warning
Posted by Daniel Borkmann 4 years, 3 months ago
On 3/8/22 10:17 AM, Guo Zhengkui wrote:
> Fix the array_size.cocci warning in tools/testing/selftests/bpf/
> 
> Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.
> 
> syscall.c and test_rdonly_maps.c don't contain header files which
> implement ARRAY_SIZE() macro. So I add `#include <linux/kernel.h>`,
> in which ARRAY_SIZE(arr) not only calculates the size of `arr`, but also
> checks that `arr` is really an array (using __must_be_array(arr)).
> 
> Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
[...]
> diff --git a/tools/testing/selftests/bpf/progs/syscall.c b/tools/testing/selftests/bpf/progs/syscall.c
> index e550f728962d..85c6e7849463 100644
> --- a/tools/testing/selftests/bpf/progs/syscall.c
> +++ b/tools/testing/selftests/bpf/progs/syscall.c
> @@ -6,6 +6,7 @@
>   #include <bpf/bpf_tracing.h>
>   #include <../../../tools/include/linux/filter.h>
>   #include <linux/btf.h>
> +#include <linux/kernel.h>
>   
>   char _license[] SEC("license") = "GPL";
>   
> @@ -82,7 +83,7 @@ int bpf_prog(struct args *ctx)
>   	static __u64 value = 34;
>   	static union bpf_attr prog_load_attr = {
>   		.prog_type = BPF_PROG_TYPE_XDP,
> -		.insn_cnt = sizeof(insns) / sizeof(insns[0]),
> +		.insn_cnt = ARRAY_SIZE(insns),
>   	};
>   	int ret;
>   
> diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> index fc8e8a34a3db..ca75aac745a4 100644
> --- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> +++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> @@ -3,6 +3,7 @@
>   
>   #include <linux/ptrace.h>
>   #include <linux/bpf.h>
> +#include <linux/kernel.h>
>   #include <bpf/bpf_helpers.h>
>   
>   const struct {
> @@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
>   {
>   	/* prevent compiler to optimize everything out */
>   	unsigned * volatile p = (void *)&rdonly_values.a;
> -	int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
> +	int i = ARRAY_SIZE(rdonly_values.a);
>   	unsigned iters = 0, sum = 0;
>   

There's bpf_util.h with ARRAY_SIZE definition which is used in selftests, pls change to
reuse that one.

Thanks,
Daniel
Re: [PATCH] selftests/bpf: fix array_size.cocci warning
Posted by Daniel Borkmann 4 years, 3 months ago
On 3/8/22 3:59 PM, Daniel Borkmann wrote:
> On 3/8/22 10:17 AM, Guo Zhengkui wrote:
>> Fix the array_size.cocci warning in tools/testing/selftests/bpf/
>>
>> Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.
>>
>> syscall.c and test_rdonly_maps.c don't contain header files which
>> implement ARRAY_SIZE() macro. So I add `#include <linux/kernel.h>`,
>> in which ARRAY_SIZE(arr) not only calculates the size of `arr`, but also
>> checks that `arr` is really an array (using __must_be_array(arr)).
>>
>> Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
> [...]
>> diff --git a/tools/testing/selftests/bpf/progs/syscall.c b/tools/testing/selftests/bpf/progs/syscall.c
>> index e550f728962d..85c6e7849463 100644
>> --- a/tools/testing/selftests/bpf/progs/syscall.c
>> +++ b/tools/testing/selftests/bpf/progs/syscall.c
>> @@ -6,6 +6,7 @@
>>   #include <bpf/bpf_tracing.h>
>>   #include <../../../tools/include/linux/filter.h>
>>   #include <linux/btf.h>
>> +#include <linux/kernel.h>
>>   char _license[] SEC("license") = "GPL";
>> @@ -82,7 +83,7 @@ int bpf_prog(struct args *ctx)
>>       static __u64 value = 34;
>>       static union bpf_attr prog_load_attr = {
>>           .prog_type = BPF_PROG_TYPE_XDP,
>> -        .insn_cnt = sizeof(insns) / sizeof(insns[0]),
>> +        .insn_cnt = ARRAY_SIZE(insns),
>>       };
>>       int ret;
>> diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
>> index fc8e8a34a3db..ca75aac745a4 100644
>> --- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
>> +++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
>> @@ -3,6 +3,7 @@
>>   #include <linux/ptrace.h>
>>   #include <linux/bpf.h>
>> +#include <linux/kernel.h>
>>   #include <bpf/bpf_helpers.h>
>>   const struct {
>> @@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
>>   {
>>       /* prevent compiler to optimize everything out */
>>       unsigned * volatile p = (void *)&rdonly_values.a;
>> -    int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
>> +    int i = ARRAY_SIZE(rdonly_values.a);
>>       unsigned iters = 0, sum = 0;
> 
> There's bpf_util.h with ARRAY_SIZE definition which is used in selftests, pls change to
> reuse that one.

Also, CI failed with your patch:

https://github.com/kernel-patches/bpf/runs/5462211251?check_suite_focus=true

   In file included from progs/test_rdonly_maps.c:6:
   In file included from /usr/include/linux/kernel.h:5:
   /usr/include/linux/sysinfo.h:9:2: error: unknown type name '__kernel_long_t'
           __kernel_long_t uptime;         /* Seconds since boot */
           ^
   /usr/include/linux/sysinfo.h:10:2: error: unknown type name '__kernel_ulong_t'
           __kernel_ulong_t loads[3];      /* 1, 5, and 15 minute load averages */
           ^
   /usr/include/linux/sysinfo.h:11:2: error: unknown type name '__kernel_ulong_t'
           __kernel_ulong_t totalram;      /* Total usable main memory size */
           ^
   /usr/include/linux/sysinfo.h:12:2: error: unknown type name '__kernel_ulong_t'
           __kernel_ulong_t freeram;       /* Available memory size */
           ^
   /usr/include/linux/sysinfo.h:13:2: error: unknown type name '__kernel_ulong_t'
           __kernel_ulong_t sharedram;     /* Amount of shared memory */
           ^
   /usr/include/linux/sysinfo.h:14:2: error: unknown type name '__kernel_ulong_t'
           __kernel_ulong_t bufferram;     /* Memory used by buffers */
           ^
   /usr/include/linux/sysinfo.h:15:2: error: unknown type name '__kernel_ulong_t'
           __kernel_ulong_t totalswap;     /* Total swap space size */
           ^
   /usr/include/linux/sysinfo.h:16:2: error: unknown type name '__kernel_ulong_t'
           __kernel_ulong_t freeswap;      /* swap space still available */
           ^
   /usr/include/linux/sysinfo.h:19:2: error: unknown type name '__kernel_ulong_t'
           __kernel_ulong_t totalhigh;     /* Total high memory size */
           ^
   /usr/include/linux/sysinfo.h:20:2: error: unknown type name '__kernel_ulong_t'
           __kernel_ulong_t freehigh;      /* Available high memory size */
           ^
   /usr/include/linux/sysinfo.h:22:22: error: use of undeclared identifier '__kernel_ulong_t'
           char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)];   /* Padding: libc5 uses this.. */
                               ^
   progs/test_rdonly_maps.c:68:10: error: implicit declaration of function 'ARRAY_SIZE' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
           int i = ARRAY_SIZE(rdonly_values.a);
                   ^
   12 errors generated.
   make: *** [Makefile:488: /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/test_rdonly_maps.o] Error 1
   make: *** Waiting for unfinished jobs....
   Error: Process completed with exit code 2.
[PATCH v2] selftests/bpf: fix array_size.cocci warning
Posted by Guo Zhengkui 4 years, 3 months ago
Fix the array_size.cocci warning in tools/testing/selftests/bpf/

Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.

syscall.c and test_rdonly_maps.c don't contain header files which
implement ARRAY_SIZE() macro. So I add `#include <linux/kernel.h>`,
in which ARRAY_SIZE(arr) not only calculates the size of `arr`, but also
checks that `arr` is really an array (using __must_be_array(arr)).

Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
---
 .../selftests/bpf/prog_tests/cgroup_attach_autodetach.c     | 2 +-
 .../testing/selftests/bpf/prog_tests/cgroup_attach_multi.c  | 2 +-
 .../selftests/bpf/prog_tests/cgroup_attach_override.c       | 2 +-
 tools/testing/selftests/bpf/prog_tests/global_data.c        | 6 +++---
 tools/testing/selftests/bpf/prog_tests/obj_name.c           | 2 +-
 tools/testing/selftests/bpf/progs/syscall.c                 | 3 ++-
 tools/testing/selftests/bpf/progs/test_rdonly_maps.c        | 3 ++-
 tools/testing/selftests/bpf/test_cgroup_storage.c           | 2 +-
 tools/testing/selftests/bpf/test_lru_map.c                  | 4 ++--
 tools/testing/selftests/bpf/test_sock_addr.c                | 6 +++---
 tools/testing/selftests/bpf/test_sockmap.c                  | 4 ++--
 11 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
index 858916d11e2e..9367bd2f0ae1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
@@ -14,7 +14,7 @@ static int prog_load(void)
 		BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 
 	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
 			       prog, insns_cnt, "GPL", 0,
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
index 38b3c47293da..db0b7bac78d1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
@@ -63,7 +63,7 @@ static int prog_load_cnt(int verdict, int val)
 		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	int ret;
 
 	ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
index 356547e849e2..9421a5b7f4e1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
@@ -16,7 +16,7 @@ static int prog_load(int verdict)
 		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 
 	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
 			       prog, insns_cnt, "GPL", 0,
diff --git a/tools/testing/selftests/bpf/prog_tests/global_data.c b/tools/testing/selftests/bpf/prog_tests/global_data.c
index 6fb3d3155c35..027685858925 100644
--- a/tools/testing/selftests/bpf/prog_tests/global_data.c
+++ b/tools/testing/selftests/bpf/prog_tests/global_data.c
@@ -29,7 +29,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)
 		{ "relocate .rodata reference", 10, ~0 },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &num);
 		CHECK(err || num != tests[i].num, tests[i].name,
 		      "err %d result %llx expected %llx\n",
@@ -58,7 +58,7 @@ static void test_global_data_string(struct bpf_object *obj, __u32 duration)
 		{ "relocate .bss reference",    4, "\0\0hello" },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, str);
 		CHECK(err || memcmp(str, tests[i].str, sizeof(str)),
 		      tests[i].name, "err %d result \'%s\' expected \'%s\'\n",
@@ -92,7 +92,7 @@ static void test_global_data_struct(struct bpf_object *obj, __u32 duration)
 		{ "relocate .data reference",   3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val);
 		CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)),
 		      tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",
diff --git a/tools/testing/selftests/bpf/prog_tests/obj_name.c b/tools/testing/selftests/bpf/prog_tests/obj_name.c
index 6194b776a28b..7093edca6e08 100644
--- a/tools/testing/selftests/bpf/prog_tests/obj_name.c
+++ b/tools/testing/selftests/bpf/prog_tests/obj_name.c
@@ -20,7 +20,7 @@ void test_obj_name(void)
 	__u32 duration = 0;
 	int i;
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		size_t name_len = strlen(tests[i].name) + 1;
 		union bpf_attr attr;
 		size_t ncopy;
diff --git a/tools/testing/selftests/bpf/progs/syscall.c b/tools/testing/selftests/bpf/progs/syscall.c
index e550f728962d..62e6fa49a4ab 100644
--- a/tools/testing/selftests/bpf/progs/syscall.c
+++ b/tools/testing/selftests/bpf/progs/syscall.c
@@ -6,6 +6,7 @@
 #include <bpf/bpf_tracing.h>
 #include <../../../tools/include/linux/filter.h>
 #include <linux/btf.h>
+#include <bpf_util.h>
 
 char _license[] SEC("license") = "GPL";
 
@@ -82,7 +83,7 @@ int bpf_prog(struct args *ctx)
 	static __u64 value = 34;
 	static union bpf_attr prog_load_attr = {
 		.prog_type = BPF_PROG_TYPE_XDP,
-		.insn_cnt = sizeof(insns) / sizeof(insns[0]),
+		.insn_cnt = ARRAY_SIZE(insns),
 	};
 	int ret;
 
diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
index fc8e8a34a3db..a500f2c15970 100644
--- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
+++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
@@ -3,6 +3,7 @@
 
 #include <linux/ptrace.h>
 #include <linux/bpf.h>
+#include <bpf_util.h>
 #include <bpf/bpf_helpers.h>
 
 const struct {
@@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
 {
 	/* prevent compiler to optimize everything out */
 	unsigned * volatile p = (void *)&rdonly_values.a;
-	int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
+	int i = ARRAY_SIZE(rdonly_values.a);
 	unsigned iters = 0, sum = 0;
 
 	/* validate verifier can allow full loop as well */
diff --git a/tools/testing/selftests/bpf/test_cgroup_storage.c b/tools/testing/selftests/bpf/test_cgroup_storage.c
index 5b8314cd77fd..d6a1be4d8020 100644
--- a/tools/testing/selftests/bpf/test_cgroup_storage.c
+++ b/tools/testing/selftests/bpf/test_cgroup_storage.c
@@ -36,7 +36,7 @@ int main(int argc, char **argv)
 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	int error = EXIT_FAILURE;
 	int map_fd, percpu_map_fd, prog_fd, cgroup_fd;
 	struct bpf_cgroup_storage_key key;
diff --git a/tools/testing/selftests/bpf/test_lru_map.c b/tools/testing/selftests/bpf/test_lru_map.c
index 6e6235185a86..563bbe18c172 100644
--- a/tools/testing/selftests/bpf/test_lru_map.c
+++ b/tools/testing/selftests/bpf/test_lru_map.c
@@ -878,11 +878,11 @@ int main(int argc, char **argv)
 	assert(nr_cpus != -1);
 	printf("nr_cpus:%d\n\n", nr_cpus);
 
-	for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
+	for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
 		unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ?
 			PERCPU_FREE_TARGET : LOCAL_FREE_TARGET;
 
-		for (t = 0; t < sizeof(map_types) / sizeof(*map_types); t++) {
+		for (t = 0; t < ARRAY_SIZE(map_types); t++) {
 			test_lru_sanity0(map_types[t], map_flags[f]);
 			test_lru_sanity1(map_types[t], map_flags[f], tgt_free);
 			test_lru_sanity2(map_types[t], map_flags[f], tgt_free);
diff --git a/tools/testing/selftests/bpf/test_sock_addr.c b/tools/testing/selftests/bpf/test_sock_addr.c
index f0c8d05ba6d1..f3d5d7ac6505 100644
--- a/tools/testing/selftests/bpf/test_sock_addr.c
+++ b/tools/testing/selftests/bpf/test_sock_addr.c
@@ -723,7 +723,7 @@ static int xmsg_ret_only_prog_load(const struct sock_addr_test *test,
 		BPF_MOV64_IMM(BPF_REG_0, rc),
 		BPF_EXIT_INSN(),
 	};
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int sendmsg_allow_prog_load(const struct sock_addr_test *test)
@@ -795,7 +795,7 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
 		BPF_EXIT_INSN(),
 	};
 
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test)
@@ -858,7 +858,7 @@ static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
 		BPF_EXIT_INSN(),
 	};
 
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
diff --git a/tools/testing/selftests/bpf/test_sockmap.c b/tools/testing/selftests/bpf/test_sockmap.c
index 1ba7e7346afb..dfb4f5c0fcb9 100644
--- a/tools/testing/selftests/bpf/test_sockmap.c
+++ b/tools/testing/selftests/bpf/test_sockmap.c
@@ -1786,7 +1786,7 @@ static int populate_progs(char *bpf_file)
 		i++;
 	}
 
-	for (i = 0; i < sizeof(map_fd)/sizeof(int); i++) {
+	for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
 		maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
 		map_fd[i] = bpf_map__fd(maps[i]);
 		if (map_fd[i] < 0) {
@@ -1867,7 +1867,7 @@ static int __test_selftests(int cg_fd, struct sockmap_options *opt)
 	}
 
 	/* Tests basic commands and APIs */
-	for (i = 0; i < sizeof(test)/sizeof(struct _test); i++) {
+	for (i = 0; i < ARRAY_SIZE(test); i++) {
 		struct _test t = test[i];
 
 		if (check_whitelist(&t, opt) != 0)
-- 
2.20.1
Re: [PATCH v2] selftests/bpf: fix array_size.cocci warning
Posted by Guo Zhengkui 4 years, 3 months ago
Sorry for my mistake. I forgot to change commit message. I will send 
another patch.

On 2022/3/9 11:22, Guo Zhengkui wrote:
> Fix the array_size.cocci warning in tools/testing/selftests/bpf/
> 
> Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.
> 
> syscall.c and test_rdonly_maps.c don't contain header files which
> implement ARRAY_SIZE() macro. So I add `#include <linux/kernel.h>`,
> in which ARRAY_SIZE(arr) not only calculates the size of `arr`, but also
> checks that `arr` is really an array (using __must_be_array(arr)).
> 
> Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
> ---
>   .../selftests/bpf/prog_tests/cgroup_attach_autodetach.c     | 2 +-
>   .../testing/selftests/bpf/prog_tests/cgroup_attach_multi.c  | 2 +-
>   .../selftests/bpf/prog_tests/cgroup_attach_override.c       | 2 +-
>   tools/testing/selftests/bpf/prog_tests/global_data.c        | 6 +++---
>   tools/testing/selftests/bpf/prog_tests/obj_name.c           | 2 +-
>   tools/testing/selftests/bpf/progs/syscall.c                 | 3 ++-
>   tools/testing/selftests/bpf/progs/test_rdonly_maps.c        | 3 ++-
>   tools/testing/selftests/bpf/test_cgroup_storage.c           | 2 +-
>   tools/testing/selftests/bpf/test_lru_map.c                  | 4 ++--
>   tools/testing/selftests/bpf/test_sock_addr.c                | 6 +++---
>   tools/testing/selftests/bpf/test_sockmap.c                  | 4 ++--
>   11 files changed, 19 insertions(+), 17 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
> index 858916d11e2e..9367bd2f0ae1 100644
> --- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
> +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
> @@ -14,7 +14,7 @@ static int prog_load(void)
>   		BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */
>   		BPF_EXIT_INSN(),
>   	};
> -	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
> +	size_t insns_cnt = ARRAY_SIZE(prog);
>   
>   	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
>   			       prog, insns_cnt, "GPL", 0,
> diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
> index 38b3c47293da..db0b7bac78d1 100644
> --- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
> +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
> @@ -63,7 +63,7 @@ static int prog_load_cnt(int verdict, int val)
>   		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
>   		BPF_EXIT_INSN(),
>   	};
> -	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
> +	size_t insns_cnt = ARRAY_SIZE(prog);
>   	int ret;
>   
>   	ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
> diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
> index 356547e849e2..9421a5b7f4e1 100644
> --- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
> +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
> @@ -16,7 +16,7 @@ static int prog_load(int verdict)
>   		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
>   		BPF_EXIT_INSN(),
>   	};
> -	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
> +	size_t insns_cnt = ARRAY_SIZE(prog);
>   
>   	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
>   			       prog, insns_cnt, "GPL", 0,
> diff --git a/tools/testing/selftests/bpf/prog_tests/global_data.c b/tools/testing/selftests/bpf/prog_tests/global_data.c
> index 6fb3d3155c35..027685858925 100644
> --- a/tools/testing/selftests/bpf/prog_tests/global_data.c
> +++ b/tools/testing/selftests/bpf/prog_tests/global_data.c
> @@ -29,7 +29,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)
>   		{ "relocate .rodata reference", 10, ~0 },
>   	};
>   
> -	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
> +	for (i = 0; i < ARRAY_SIZE(tests); i++) {
>   		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &num);
>   		CHECK(err || num != tests[i].num, tests[i].name,
>   		      "err %d result %llx expected %llx\n",
> @@ -58,7 +58,7 @@ static void test_global_data_string(struct bpf_object *obj, __u32 duration)
>   		{ "relocate .bss reference",    4, "\0\0hello" },
>   	};
>   
> -	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
> +	for (i = 0; i < ARRAY_SIZE(tests); i++) {
>   		err = bpf_map_lookup_elem(map_fd, &tests[i].key, str);
>   		CHECK(err || memcmp(str, tests[i].str, sizeof(str)),
>   		      tests[i].name, "err %d result \'%s\' expected \'%s\'\n",
> @@ -92,7 +92,7 @@ static void test_global_data_struct(struct bpf_object *obj, __u32 duration)
>   		{ "relocate .data reference",   3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } },
>   	};
>   
> -	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
> +	for (i = 0; i < ARRAY_SIZE(tests); i++) {
>   		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val);
>   		CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)),
>   		      tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",
> diff --git a/tools/testing/selftests/bpf/prog_tests/obj_name.c b/tools/testing/selftests/bpf/prog_tests/obj_name.c
> index 6194b776a28b..7093edca6e08 100644
> --- a/tools/testing/selftests/bpf/prog_tests/obj_name.c
> +++ b/tools/testing/selftests/bpf/prog_tests/obj_name.c
> @@ -20,7 +20,7 @@ void test_obj_name(void)
>   	__u32 duration = 0;
>   	int i;
>   
> -	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
> +	for (i = 0; i < ARRAY_SIZE(tests); i++) {
>   		size_t name_len = strlen(tests[i].name) + 1;
>   		union bpf_attr attr;
>   		size_t ncopy;
> diff --git a/tools/testing/selftests/bpf/progs/syscall.c b/tools/testing/selftests/bpf/progs/syscall.c
> index e550f728962d..62e6fa49a4ab 100644
> --- a/tools/testing/selftests/bpf/progs/syscall.c
> +++ b/tools/testing/selftests/bpf/progs/syscall.c
> @@ -6,6 +6,7 @@
>   #include <bpf/bpf_tracing.h>
>   #include <../../../tools/include/linux/filter.h>
>   #include <linux/btf.h>
> +#include <bpf_util.h>
>   
>   char _license[] SEC("license") = "GPL";
>   
> @@ -82,7 +83,7 @@ int bpf_prog(struct args *ctx)
>   	static __u64 value = 34;
>   	static union bpf_attr prog_load_attr = {
>   		.prog_type = BPF_PROG_TYPE_XDP,
> -		.insn_cnt = sizeof(insns) / sizeof(insns[0]),
> +		.insn_cnt = ARRAY_SIZE(insns),
>   	};
>   	int ret;
>   
> diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> index fc8e8a34a3db..a500f2c15970 100644
> --- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> +++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> @@ -3,6 +3,7 @@
>   
>   #include <linux/ptrace.h>
>   #include <linux/bpf.h>
> +#include <bpf_util.h>
>   #include <bpf/bpf_helpers.h>
>   
>   const struct {
> @@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
>   {
>   	/* prevent compiler to optimize everything out */
>   	unsigned * volatile p = (void *)&rdonly_values.a;
> -	int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
> +	int i = ARRAY_SIZE(rdonly_values.a);
>   	unsigned iters = 0, sum = 0;
>   
>   	/* validate verifier can allow full loop as well */
> diff --git a/tools/testing/selftests/bpf/test_cgroup_storage.c b/tools/testing/selftests/bpf/test_cgroup_storage.c
> index 5b8314cd77fd..d6a1be4d8020 100644
> --- a/tools/testing/selftests/bpf/test_cgroup_storage.c
> +++ b/tools/testing/selftests/bpf/test_cgroup_storage.c
> @@ -36,7 +36,7 @@ int main(int argc, char **argv)
>   		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
>   		BPF_EXIT_INSN(),
>   	};
> -	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
> +	size_t insns_cnt = ARRAY_SIZE(prog);
>   	int error = EXIT_FAILURE;
>   	int map_fd, percpu_map_fd, prog_fd, cgroup_fd;
>   	struct bpf_cgroup_storage_key key;
> diff --git a/tools/testing/selftests/bpf/test_lru_map.c b/tools/testing/selftests/bpf/test_lru_map.c
> index 6e6235185a86..563bbe18c172 100644
> --- a/tools/testing/selftests/bpf/test_lru_map.c
> +++ b/tools/testing/selftests/bpf/test_lru_map.c
> @@ -878,11 +878,11 @@ int main(int argc, char **argv)
>   	assert(nr_cpus != -1);
>   	printf("nr_cpus:%d\n\n", nr_cpus);
>   
> -	for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
> +	for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
>   		unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ?
>   			PERCPU_FREE_TARGET : LOCAL_FREE_TARGET;
>   
> -		for (t = 0; t < sizeof(map_types) / sizeof(*map_types); t++) {
> +		for (t = 0; t < ARRAY_SIZE(map_types); t++) {
>   			test_lru_sanity0(map_types[t], map_flags[f]);
>   			test_lru_sanity1(map_types[t], map_flags[f], tgt_free);
>   			test_lru_sanity2(map_types[t], map_flags[f], tgt_free);
> diff --git a/tools/testing/selftests/bpf/test_sock_addr.c b/tools/testing/selftests/bpf/test_sock_addr.c
> index f0c8d05ba6d1..f3d5d7ac6505 100644
> --- a/tools/testing/selftests/bpf/test_sock_addr.c
> +++ b/tools/testing/selftests/bpf/test_sock_addr.c
> @@ -723,7 +723,7 @@ static int xmsg_ret_only_prog_load(const struct sock_addr_test *test,
>   		BPF_MOV64_IMM(BPF_REG_0, rc),
>   		BPF_EXIT_INSN(),
>   	};
> -	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
> +	return load_insns(test, insns, ARRAY_SIZE(insns));
>   }
>   
>   static int sendmsg_allow_prog_load(const struct sock_addr_test *test)
> @@ -795,7 +795,7 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
>   		BPF_EXIT_INSN(),
>   	};
>   
> -	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
> +	return load_insns(test, insns, ARRAY_SIZE(insns));
>   }
>   
>   static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test)
> @@ -858,7 +858,7 @@ static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
>   		BPF_EXIT_INSN(),
>   	};
>   
> -	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
> +	return load_insns(test, insns, ARRAY_SIZE(insns));
>   }
>   
>   static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
> diff --git a/tools/testing/selftests/bpf/test_sockmap.c b/tools/testing/selftests/bpf/test_sockmap.c
> index 1ba7e7346afb..dfb4f5c0fcb9 100644
> --- a/tools/testing/selftests/bpf/test_sockmap.c
> +++ b/tools/testing/selftests/bpf/test_sockmap.c
> @@ -1786,7 +1786,7 @@ static int populate_progs(char *bpf_file)
>   		i++;
>   	}
>   
> -	for (i = 0; i < sizeof(map_fd)/sizeof(int); i++) {
> +	for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
>   		maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
>   		map_fd[i] = bpf_map__fd(maps[i]);
>   		if (map_fd[i] < 0) {
> @@ -1867,7 +1867,7 @@ static int __test_selftests(int cg_fd, struct sockmap_options *opt)
>   	}
>   
>   	/* Tests basic commands and APIs */
> -	for (i = 0; i < sizeof(test)/sizeof(struct _test); i++) {
> +	for (i = 0; i < ARRAY_SIZE(test); i++) {
>   		struct _test t = test[i];
>   
>   		if (check_whitelist(&t, opt) != 0)
[PATCH v2] selftests/bpf: fix array_size.cocci warning
Posted by Guo Zhengkui 4 years, 3 months ago
Fix the array_size.cocci warning in tools/testing/selftests/bpf/

Use `ARRAY_SIZE(arr)` in bpf_util.h instead of forms like
`sizeof(arr)/sizeof(arr[0])`.

Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
---
 .../selftests/bpf/prog_tests/cgroup_attach_autodetach.c     | 2 +-
 .../testing/selftests/bpf/prog_tests/cgroup_attach_multi.c  | 2 +-
 .../selftests/bpf/prog_tests/cgroup_attach_override.c       | 2 +-
 tools/testing/selftests/bpf/prog_tests/global_data.c        | 6 +++---
 tools/testing/selftests/bpf/prog_tests/obj_name.c           | 2 +-
 tools/testing/selftests/bpf/progs/syscall.c                 | 3 ++-
 tools/testing/selftests/bpf/progs/test_rdonly_maps.c        | 3 ++-
 tools/testing/selftests/bpf/test_cgroup_storage.c           | 2 +-
 tools/testing/selftests/bpf/test_lru_map.c                  | 4 ++--
 tools/testing/selftests/bpf/test_sock_addr.c                | 6 +++---
 tools/testing/selftests/bpf/test_sockmap.c                  | 4 ++--
 11 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
index 858916d11e2e..9367bd2f0ae1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
@@ -14,7 +14,7 @@ static int prog_load(void)
 		BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 
 	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
 			       prog, insns_cnt, "GPL", 0,
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
index 38b3c47293da..db0b7bac78d1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
@@ -63,7 +63,7 @@ static int prog_load_cnt(int verdict, int val)
 		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	int ret;
 
 	ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
index 356547e849e2..9421a5b7f4e1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
@@ -16,7 +16,7 @@ static int prog_load(int verdict)
 		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 
 	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
 			       prog, insns_cnt, "GPL", 0,
diff --git a/tools/testing/selftests/bpf/prog_tests/global_data.c b/tools/testing/selftests/bpf/prog_tests/global_data.c
index 6fb3d3155c35..027685858925 100644
--- a/tools/testing/selftests/bpf/prog_tests/global_data.c
+++ b/tools/testing/selftests/bpf/prog_tests/global_data.c
@@ -29,7 +29,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)
 		{ "relocate .rodata reference", 10, ~0 },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &num);
 		CHECK(err || num != tests[i].num, tests[i].name,
 		      "err %d result %llx expected %llx\n",
@@ -58,7 +58,7 @@ static void test_global_data_string(struct bpf_object *obj, __u32 duration)
 		{ "relocate .bss reference",    4, "\0\0hello" },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, str);
 		CHECK(err || memcmp(str, tests[i].str, sizeof(str)),
 		      tests[i].name, "err %d result \'%s\' expected \'%s\'\n",
@@ -92,7 +92,7 @@ static void test_global_data_struct(struct bpf_object *obj, __u32 duration)
 		{ "relocate .data reference",   3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val);
 		CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)),
 		      tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",
diff --git a/tools/testing/selftests/bpf/prog_tests/obj_name.c b/tools/testing/selftests/bpf/prog_tests/obj_name.c
index 6194b776a28b..7093edca6e08 100644
--- a/tools/testing/selftests/bpf/prog_tests/obj_name.c
+++ b/tools/testing/selftests/bpf/prog_tests/obj_name.c
@@ -20,7 +20,7 @@ void test_obj_name(void)
 	__u32 duration = 0;
 	int i;
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		size_t name_len = strlen(tests[i].name) + 1;
 		union bpf_attr attr;
 		size_t ncopy;
diff --git a/tools/testing/selftests/bpf/progs/syscall.c b/tools/testing/selftests/bpf/progs/syscall.c
index e550f728962d..62e6fa49a4ab 100644
--- a/tools/testing/selftests/bpf/progs/syscall.c
+++ b/tools/testing/selftests/bpf/progs/syscall.c
@@ -6,6 +6,7 @@
 #include <bpf/bpf_tracing.h>
 #include <../../../tools/include/linux/filter.h>
 #include <linux/btf.h>
+#include <bpf_util.h>
 
 char _license[] SEC("license") = "GPL";
 
@@ -82,7 +83,7 @@ int bpf_prog(struct args *ctx)
 	static __u64 value = 34;
 	static union bpf_attr prog_load_attr = {
 		.prog_type = BPF_PROG_TYPE_XDP,
-		.insn_cnt = sizeof(insns) / sizeof(insns[0]),
+		.insn_cnt = ARRAY_SIZE(insns),
 	};
 	int ret;
 
diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
index fc8e8a34a3db..a500f2c15970 100644
--- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
+++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
@@ -3,6 +3,7 @@
 
 #include <linux/ptrace.h>
 #include <linux/bpf.h>
+#include <bpf_util.h>
 #include <bpf/bpf_helpers.h>
 
 const struct {
@@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
 {
 	/* prevent compiler to optimize everything out */
 	unsigned * volatile p = (void *)&rdonly_values.a;
-	int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
+	int i = ARRAY_SIZE(rdonly_values.a);
 	unsigned iters = 0, sum = 0;
 
 	/* validate verifier can allow full loop as well */
diff --git a/tools/testing/selftests/bpf/test_cgroup_storage.c b/tools/testing/selftests/bpf/test_cgroup_storage.c
index 5b8314cd77fd..d6a1be4d8020 100644
--- a/tools/testing/selftests/bpf/test_cgroup_storage.c
+++ b/tools/testing/selftests/bpf/test_cgroup_storage.c
@@ -36,7 +36,7 @@ int main(int argc, char **argv)
 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	int error = EXIT_FAILURE;
 	int map_fd, percpu_map_fd, prog_fd, cgroup_fd;
 	struct bpf_cgroup_storage_key key;
diff --git a/tools/testing/selftests/bpf/test_lru_map.c b/tools/testing/selftests/bpf/test_lru_map.c
index 6e6235185a86..563bbe18c172 100644
--- a/tools/testing/selftests/bpf/test_lru_map.c
+++ b/tools/testing/selftests/bpf/test_lru_map.c
@@ -878,11 +878,11 @@ int main(int argc, char **argv)
 	assert(nr_cpus != -1);
 	printf("nr_cpus:%d\n\n", nr_cpus);
 
-	for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
+	for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
 		unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ?
 			PERCPU_FREE_TARGET : LOCAL_FREE_TARGET;
 
-		for (t = 0; t < sizeof(map_types) / sizeof(*map_types); t++) {
+		for (t = 0; t < ARRAY_SIZE(map_types); t++) {
 			test_lru_sanity0(map_types[t], map_flags[f]);
 			test_lru_sanity1(map_types[t], map_flags[f], tgt_free);
 			test_lru_sanity2(map_types[t], map_flags[f], tgt_free);
diff --git a/tools/testing/selftests/bpf/test_sock_addr.c b/tools/testing/selftests/bpf/test_sock_addr.c
index f0c8d05ba6d1..f3d5d7ac6505 100644
--- a/tools/testing/selftests/bpf/test_sock_addr.c
+++ b/tools/testing/selftests/bpf/test_sock_addr.c
@@ -723,7 +723,7 @@ static int xmsg_ret_only_prog_load(const struct sock_addr_test *test,
 		BPF_MOV64_IMM(BPF_REG_0, rc),
 		BPF_EXIT_INSN(),
 	};
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int sendmsg_allow_prog_load(const struct sock_addr_test *test)
@@ -795,7 +795,7 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
 		BPF_EXIT_INSN(),
 	};
 
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test)
@@ -858,7 +858,7 @@ static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
 		BPF_EXIT_INSN(),
 	};
 
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
diff --git a/tools/testing/selftests/bpf/test_sockmap.c b/tools/testing/selftests/bpf/test_sockmap.c
index 1ba7e7346afb..dfb4f5c0fcb9 100644
--- a/tools/testing/selftests/bpf/test_sockmap.c
+++ b/tools/testing/selftests/bpf/test_sockmap.c
@@ -1786,7 +1786,7 @@ static int populate_progs(char *bpf_file)
 		i++;
 	}
 
-	for (i = 0; i < sizeof(map_fd)/sizeof(int); i++) {
+	for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
 		maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
 		map_fd[i] = bpf_map__fd(maps[i]);
 		if (map_fd[i] < 0) {
@@ -1867,7 +1867,7 @@ static int __test_selftests(int cg_fd, struct sockmap_options *opt)
 	}
 
 	/* Tests basic commands and APIs */
-	for (i = 0; i < sizeof(test)/sizeof(struct _test); i++) {
+	for (i = 0; i < ARRAY_SIZE(test); i++) {
 		struct _test t = test[i];
 
 		if (check_whitelist(&t, opt) != 0)
-- 
2.20.1
Re: [PATCH v2] selftests/bpf: fix array_size.cocci warning
Posted by Daniel Borkmann 4 years, 3 months ago
On 3/9/22 4:35 AM, Guo Zhengkui wrote:
> Fix the array_size.cocci warning in tools/testing/selftests/bpf/
> 
> Use `ARRAY_SIZE(arr)` in bpf_util.h instead of forms like
> `sizeof(arr)/sizeof(arr[0])`.
> 
> Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>

BPF CI fails with:

https://github.com/kernel-patches/bpf/runs/5498238267?check_suite_focus=true

   pahole: Multithreading requires elfutils >= 0.178. Continuing with a single thread...
   In file included from progs/test_rdonly_maps.c:7:
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:130:8: error: redefinition of 'bpf_map_def'
   struct bpf_map_def {
          ^
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:685:8: note: previous definition is here
   struct bpf_map_def {
          ^
   In file included from progs/test_rdonly_maps.c:7:
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:138:6: error: redefinition of 'libbpf_pin_type'
   enum libbpf_pin_type {
        ^
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:191:6: note: previous definition is here
   enum libbpf_pin_type {
        ^
   In file included from progs/test_rdonly_maps.c:7:
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:139:2: error: redefinition of enumerator 'LIBBPF_PIN_NONE'
           LIBBPF_PIN_NONE,
           ^
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:192:2: note: previous definition is here
           LIBBPF_PIN_NONE,
           ^
   In file included from progs/test_rdonly_maps.c:7:
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:141:2: error: redefinition of enumerator 'LIBBPF_PIN_BY_NAME'
           LIBBPF_PIN_BY_NAME,
           ^
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:194:2: note: previous definition is here
           LIBBPF_PIN_BY_NAME,
           ^
   In file included from progs/test_rdonly_maps.c:7:
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:144:6: error: redefinition of 'libbpf_tristate'
   enum libbpf_tristate {
        ^
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:1304:6: note: previous definition is here
   enum libbpf_tristate {
        ^
   In file included from progs/test_rdonly_maps.c:7:
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:145:2: error: redefinition of enumerator 'TRI_NO'
           TRI_NO = 0,
           ^
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:1305:2: note: previous definition is here
           TRI_NO = 0,
           ^
   In file included from progs/test_rdonly_maps.c:7:
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:146:2: error: redefinition of enumerator 'TRI_YES'
           TRI_YES = 1,
           ^
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:1306:2: note: previous definition is here
           TRI_YES = 1,
           ^
   In file included from progs/test_rdonly_maps.c:7:
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/bpf_helpers.h:147:2: error: redefinition of enumerator 'TRI_MODULE'
           TRI_MODULE = 2,
           ^
   /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:1307:2: note: previous definition is here
           TRI_MODULE = 2,
           ^
   8 errors generated.
   make: *** [Makefile:488: /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/test_rdonly_maps.o] Error 1
   make: *** Waiting for unfinished jobs....
   Error: Process completed with exit code 2.
[PATCH v3] selftests/bpf: fix array_size.cocci warning
Posted by Guo Zhengkui 4 years, 3 months ago
Fix the array_size.cocci warning in tools/testing/selftests/bpf/

Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.

tools/testing/selftests/bpf/test_cgroup_storage.c uses ARRAY_SIZE() defined
in tools/include/linux/kernel.h (sys/sysinfo.h -> linux/kernel.h), while
others use ARRAY_SIZE() in bpf_util.h.

Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
---
 .../selftests/bpf/prog_tests/cgroup_attach_autodetach.c     | 2 +-
 .../testing/selftests/bpf/prog_tests/cgroup_attach_multi.c  | 2 +-
 .../selftests/bpf/prog_tests/cgroup_attach_override.c       | 2 +-
 tools/testing/selftests/bpf/prog_tests/global_data.c        | 6 +++---
 tools/testing/selftests/bpf/prog_tests/obj_name.c           | 2 +-
 tools/testing/selftests/bpf/test_cgroup_storage.c           | 2 +-
 tools/testing/selftests/bpf/test_lru_map.c                  | 4 ++--
 tools/testing/selftests/bpf/test_sock_addr.c                | 6 +++---
 tools/testing/selftests/bpf/test_sockmap.c                  | 4 ++--
 9 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
index 858916d11e2e..9367bd2f0ae1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
@@ -14,7 +14,7 @@ static int prog_load(void)
 		BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 
 	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
 			       prog, insns_cnt, "GPL", 0,
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
index 38b3c47293da..db0b7bac78d1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
@@ -63,7 +63,7 @@ static int prog_load_cnt(int verdict, int val)
 		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	int ret;
 
 	ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
index 356547e849e2..9421a5b7f4e1 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
@@ -16,7 +16,7 @@ static int prog_load(int verdict)
 		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 
 	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
 			       prog, insns_cnt, "GPL", 0,
diff --git a/tools/testing/selftests/bpf/prog_tests/global_data.c b/tools/testing/selftests/bpf/prog_tests/global_data.c
index 6fb3d3155c35..027685858925 100644
--- a/tools/testing/selftests/bpf/prog_tests/global_data.c
+++ b/tools/testing/selftests/bpf/prog_tests/global_data.c
@@ -29,7 +29,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)
 		{ "relocate .rodata reference", 10, ~0 },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &num);
 		CHECK(err || num != tests[i].num, tests[i].name,
 		      "err %d result %llx expected %llx\n",
@@ -58,7 +58,7 @@ static void test_global_data_string(struct bpf_object *obj, __u32 duration)
 		{ "relocate .bss reference",    4, "\0\0hello" },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, str);
 		CHECK(err || memcmp(str, tests[i].str, sizeof(str)),
 		      tests[i].name, "err %d result \'%s\' expected \'%s\'\n",
@@ -92,7 +92,7 @@ static void test_global_data_struct(struct bpf_object *obj, __u32 duration)
 		{ "relocate .data reference",   3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } },
 	};
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val);
 		CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)),
 		      tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",
diff --git a/tools/testing/selftests/bpf/prog_tests/obj_name.c b/tools/testing/selftests/bpf/prog_tests/obj_name.c
index 6194b776a28b..7093edca6e08 100644
--- a/tools/testing/selftests/bpf/prog_tests/obj_name.c
+++ b/tools/testing/selftests/bpf/prog_tests/obj_name.c
@@ -20,7 +20,7 @@ void test_obj_name(void)
 	__u32 duration = 0;
 	int i;
 
-	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 		size_t name_len = strlen(tests[i].name) + 1;
 		union bpf_attr attr;
 		size_t ncopy;
diff --git a/tools/testing/selftests/bpf/test_cgroup_storage.c b/tools/testing/selftests/bpf/test_cgroup_storage.c
index 5b8314cd77fd..d6a1be4d8020 100644
--- a/tools/testing/selftests/bpf/test_cgroup_storage.c
+++ b/tools/testing/selftests/bpf/test_cgroup_storage.c
@@ -36,7 +36,7 @@ int main(int argc, char **argv)
 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	int error = EXIT_FAILURE;
 	int map_fd, percpu_map_fd, prog_fd, cgroup_fd;
 	struct bpf_cgroup_storage_key key;
diff --git a/tools/testing/selftests/bpf/test_lru_map.c b/tools/testing/selftests/bpf/test_lru_map.c
index 6e6235185a86..563bbe18c172 100644
--- a/tools/testing/selftests/bpf/test_lru_map.c
+++ b/tools/testing/selftests/bpf/test_lru_map.c
@@ -878,11 +878,11 @@ int main(int argc, char **argv)
 	assert(nr_cpus != -1);
 	printf("nr_cpus:%d\n\n", nr_cpus);
 
-	for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
+	for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
 		unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ?
 			PERCPU_FREE_TARGET : LOCAL_FREE_TARGET;
 
-		for (t = 0; t < sizeof(map_types) / sizeof(*map_types); t++) {
+		for (t = 0; t < ARRAY_SIZE(map_types); t++) {
 			test_lru_sanity0(map_types[t], map_flags[f]);
 			test_lru_sanity1(map_types[t], map_flags[f], tgt_free);
 			test_lru_sanity2(map_types[t], map_flags[f], tgt_free);
diff --git a/tools/testing/selftests/bpf/test_sock_addr.c b/tools/testing/selftests/bpf/test_sock_addr.c
index f0c8d05ba6d1..f3d5d7ac6505 100644
--- a/tools/testing/selftests/bpf/test_sock_addr.c
+++ b/tools/testing/selftests/bpf/test_sock_addr.c
@@ -723,7 +723,7 @@ static int xmsg_ret_only_prog_load(const struct sock_addr_test *test,
 		BPF_MOV64_IMM(BPF_REG_0, rc),
 		BPF_EXIT_INSN(),
 	};
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int sendmsg_allow_prog_load(const struct sock_addr_test *test)
@@ -795,7 +795,7 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
 		BPF_EXIT_INSN(),
 	};
 
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test)
@@ -858,7 +858,7 @@ static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
 		BPF_EXIT_INSN(),
 	};
 
-	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
+	return load_insns(test, insns, ARRAY_SIZE(insns));
 }
 
 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
diff --git a/tools/testing/selftests/bpf/test_sockmap.c b/tools/testing/selftests/bpf/test_sockmap.c
index 1ba7e7346afb..dfb4f5c0fcb9 100644
--- a/tools/testing/selftests/bpf/test_sockmap.c
+++ b/tools/testing/selftests/bpf/test_sockmap.c
@@ -1786,7 +1786,7 @@ static int populate_progs(char *bpf_file)
 		i++;
 	}
 
-	for (i = 0; i < sizeof(map_fd)/sizeof(int); i++) {
+	for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
 		maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
 		map_fd[i] = bpf_map__fd(maps[i]);
 		if (map_fd[i] < 0) {
@@ -1867,7 +1867,7 @@ static int __test_selftests(int cg_fd, struct sockmap_options *opt)
 	}
 
 	/* Tests basic commands and APIs */
-	for (i = 0; i < sizeof(test)/sizeof(struct _test); i++) {
+	for (i = 0; i < ARRAY_SIZE(test); i++) {
 		struct _test t = test[i];
 
 		if (check_whitelist(&t, opt) != 0)
-- 
2.20.1
Re: [PATCH v3] selftests/bpf: fix array_size.cocci warning
Posted by patchwork-bot+netdevbpf@kernel.org 4 years, 3 months ago
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Tue, 15 Mar 2022 21:01:26 +0800 you wrote:
> Fix the array_size.cocci warning in tools/testing/selftests/bpf/
> 
> Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.
> 
> tools/testing/selftests/bpf/test_cgroup_storage.c uses ARRAY_SIZE() defined
> in tools/include/linux/kernel.h (sys/sysinfo.h -> linux/kernel.h), while
> others use ARRAY_SIZE() in bpf_util.h.
> 
> [...]

Here is the summary with links:
  - [v3] selftests/bpf: fix array_size.cocci warning
    https://git.kernel.org/bpf/bpf-next/c/f98d6dd1e79d

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Re: [PATCH v2] selftests/bpf: fix array_size.cocci warning
Posted by Andrii Nakryiko 4 years, 3 months ago
On Tue, Mar 8, 2022 at 7:36 PM Guo Zhengkui <guozhengkui@vivo.com> wrote:
>
> Fix the array_size.cocci warning in tools/testing/selftests/bpf/
>
> Use `ARRAY_SIZE(arr)` in bpf_util.h instead of forms like
> `sizeof(arr)/sizeof(arr[0])`.
>
> Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
> ---
>  .../selftests/bpf/prog_tests/cgroup_attach_autodetach.c     | 2 +-
>  .../testing/selftests/bpf/prog_tests/cgroup_attach_multi.c  | 2 +-
>  .../selftests/bpf/prog_tests/cgroup_attach_override.c       | 2 +-
>  tools/testing/selftests/bpf/prog_tests/global_data.c        | 6 +++---
>  tools/testing/selftests/bpf/prog_tests/obj_name.c           | 2 +-
>  tools/testing/selftests/bpf/progs/syscall.c                 | 3 ++-
>  tools/testing/selftests/bpf/progs/test_rdonly_maps.c        | 3 ++-
>  tools/testing/selftests/bpf/test_cgroup_storage.c           | 2 +-
>  tools/testing/selftests/bpf/test_lru_map.c                  | 4 ++--
>  tools/testing/selftests/bpf/test_sock_addr.c                | 6 +++---
>  tools/testing/selftests/bpf/test_sockmap.c                  | 4 ++--
>  11 files changed, 19 insertions(+), 17 deletions(-)
>

[...]

> diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> index fc8e8a34a3db..a500f2c15970 100644
> --- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> +++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> @@ -3,6 +3,7 @@
>
>  #include <linux/ptrace.h>
>  #include <linux/bpf.h>
> +#include <bpf_util.h>

bpf_util.h isn't supposed to be included from BPF source code side. Is
this ARRAY_SIZE() use so important for BPF programs? Maybe just leave
existing code under progs/*.c as is?

>  #include <bpf/bpf_helpers.h>
>
>  const struct {
> @@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
>  {
>         /* prevent compiler to optimize everything out */
>         unsigned * volatile p = (void *)&rdonly_values.a;
> -       int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
> +       int i = ARRAY_SIZE(rdonly_values.a);
>         unsigned iters = 0, sum = 0;
>
>         /* validate verifier can allow full loop as well */

[...]
Re: [PATCH v2] selftests/bpf: fix array_size.cocci warning
Posted by Guo Zhengkui 4 years, 3 months ago
On 2022/3/12 2:38, Andrii Nakryiko wrote:
> On Tue, Mar 8, 2022 at 7:36 PM Guo Zhengkui <guozhengkui@vivo.com> wrote:
>>
>> Fix the array_size.cocci warning in tools/testing/selftests/bpf/
>>
>> Use `ARRAY_SIZE(arr)` in bpf_util.h instead of forms like
>> `sizeof(arr)/sizeof(arr[0])`.
>>
>> Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
>> ---
>>   .../selftests/bpf/prog_tests/cgroup_attach_autodetach.c     | 2 +-
>>   .../testing/selftests/bpf/prog_tests/cgroup_attach_multi.c  | 2 +-
>>   .../selftests/bpf/prog_tests/cgroup_attach_override.c       | 2 +-
>>   tools/testing/selftests/bpf/prog_tests/global_data.c        | 6 +++---
>>   tools/testing/selftests/bpf/prog_tests/obj_name.c           | 2 +-
>>   tools/testing/selftests/bpf/progs/syscall.c                 | 3 ++-
>>   tools/testing/selftests/bpf/progs/test_rdonly_maps.c        | 3 ++-
>>   tools/testing/selftests/bpf/test_cgroup_storage.c           | 2 +-
>>   tools/testing/selftests/bpf/test_lru_map.c                  | 4 ++--
>>   tools/testing/selftests/bpf/test_sock_addr.c                | 6 +++---
>>   tools/testing/selftests/bpf/test_sockmap.c                  | 4 ++--
>>   11 files changed, 19 insertions(+), 17 deletions(-)
>>
> 
> [...]
> 
>> diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
>> index fc8e8a34a3db..a500f2c15970 100644
>> --- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
>> +++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
>> @@ -3,6 +3,7 @@
>>
>>   #include <linux/ptrace.h>
>>   #include <linux/bpf.h>
>> +#include <bpf_util.h>
> 
> bpf_util.h isn't supposed to be included from BPF source code side. Is
> this ARRAY_SIZE() use so important for BPF programs? Maybe just leave
> existing code under progs/*.c as is?

I think so. Just leave progs/*.c unchanged. I'll commit PATCH v3.

> 
>>   #include <bpf/bpf_helpers.h>
>>
>>   const struct {
>> @@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
>>   {
>>          /* prevent compiler to optimize everything out */
>>          unsigned * volatile p = (void *)&rdonly_values.a;
>> -       int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
>> +       int i = ARRAY_SIZE(rdonly_values.a);
>>          unsigned iters = 0, sum = 0;
>>
>>          /* validate verifier can allow full loop as well */
> 
> [...]

Zhengkui