Re: [alobakin:pfcp 11/19] include/linux/bitmap.h:642:17: warning: array subscript [1, 1024] is outside array bounds of 'long unsigned int[1]'

Yury Norov
2023-11-07T13:22:29Z

On Mon, Nov 06, 2023 at 05:31:34PM +0100, Alexander Lobakin wrote:
> BTW, I have this in my inbox:
>
> From: Kernel Test Robot <lkp@intel.com>
> Date: Tue, 17 Oct 2023 08:14:51 +0800
>
> > tree: https://github.com/alobakin/linux pfcp
> > head: 9183a3eb639912169a3d3e2be4f25556b465919b
> > commit: c8a652cdcc0964510f108726b3da0784d1bc0cd2 [11/19] bitmap: make bitmap_{get,set}_value8() use bitmap_{read,write}()
>
> So it happened after I converted bitmap_{get,set}_value8() so that they
> use bitmap_{read,write}().
>
> > config: x86_64-randconfig-004-20231017 (https://download.01.org/0day-ci/archive/20231017/202310170708.fJzLlgDM-lkp@intel.com/config)
> > compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
> > reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231017/202310170708.fJzLlgDM-lkp@intel.com/reproduce)

[...]

> > 643 | map[index + 1] |= (value >> space);
> > | ^~
> > In file included from include/linux/kasan-checks.h:5,
> > from include/asm-generic/rwonce.h:26,
> > from ./arch/x86/include/generated/asm/rwonce.h:1,
> > from include/linux/compiler.h:246,
> > from include/linux/build_bug.h:5,
> > from include/linux/bits.h:21,
> > from include/linux/ioport.h:13,
> > from include/linux/acpi.h:12,
> > from drivers/gpio/gpio-pca953x.c:11:
> > drivers/gpio/gpio-pca953x.c:1032:17: note: while referencing 'val'

It looks like a gcc-9 false-positive. I tried gcc-12 and gcc-13, and
they both looks OK. Below is the fix that works for me.

Can you please test the following patch and add it to your series?

Thanks,
Yury

From 2a883221ddbd18796ddef0125e9e3022a56edd7b Mon Sep 17 00:00:00 2001
From: Yury Norov <yury.norov@gmail.com>
Date: Tue, 7 Nov 2023 05:05:17 -0800
Subject: [PATCH] bitmap: suppress false-positive -Warray-bounds in
bitmap_{read,write}

bitmap_{read,write} conditionally accesses map[index + 1], carefully
checking before that it's a safe dereference. But still, gcc-9 emits
-Warray-bounds.

Gcc-12 and gcc-13 are both OK with this code. So fix it for gcc-9 with
OPTIMIZER_HIDE_VAR().

Reported-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202310170708.fJzLlgDM-lkp@intel.com/
Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
include/linux/bitmap.h | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 945a680816cc..0b1a07ff1080 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -592,6 +592,11 @@ static inline unsigned long bitmap_read(const unsigned long *map,
if (unlikely(!nbits || nbits > BITS_PER_LONG))
return 0;

+#if CONFIG_GCC_VERSION < 100000
+ /* Suppress gcc-9 false-positive -Warray-bounds */
+ OPTIMIZER_HIDE_VAR(map);
+#endif
+
if (space >= nbits)
return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);

@@ -634,6 +639,11 @@ static inline void bitmap_write(unsigned long *map, unsigned long value,
fit = space >= nbits;
index = BIT_WORD(start);

+#if CONFIG_GCC_VERSION < 100000
+ /* Suppress gcc-9 false-positive -Warray-bounds */
+ OPTIMIZER_HIDE_VAR(map);
+#endif
+
map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start));
map[index] |= value << offset;
if (fit)
--
2.39.2