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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxx>
[...]
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.



[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux