All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Lobakin <aleksander.lobakin@intel.com>
To: Alexander Potapenko <glider@google.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Yury Norov <yury.norov@gmail.com>,
	Syed Nayyar Waris <syednwaris@gmail.com>,
	Kees Cook <keescook@chromium.org>,
	kernel test robot <lkp@intel.com>,
	<oe-kbuild-all@lists.linux.dev>,
	<linux-hardening@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: 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]'
Date: Tue, 7 Nov 2023 17:44:00 +0100	[thread overview]
Message-ID: <ca0ad357-727b-4f41-a3d6-2c48a9dd5836@intel.com> (raw)
In-Reply-To: <CAG_fn=VtA6e3oGm4WVnxmy9yMBab780-U_4VoHSqm4tcd2UTzQ@mail.gmail.com>

From: Alexander Potapenko <glider@google.com>
Date: Tue, 7 Nov 2023 17:33:56 +0100

> On Tue, Nov 7, 2023 at 2:23 PM Alexander Lobakin
> <aleksander.lobakin@intel.com> wrote:
>>
>> From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>> Date: Mon, 6 Nov 2023 20:23:52 +0200
>>
>>> On Mon, Nov 06, 2023 at 05:31:34PM +0100, Alexander Lobakin wrote:
>>>
>>>>> | Reported-by: kernel test robot <lkp@intel.com>
>>>>> | Closes: https://lore.kernel.org/oe-kbuild-all/202310170708.fJzLlgDM-lkp@intel.com/
>>>
>>>> Not sure how to approach this :z It was also captured on the version you
>>>> sent 2 weeks ago, so this could've been resolved already.
>>>
>>> Is it in the repository already? if so, we should revert it.
>>> Otherwise you have time to think and fix.
>>
>> Nah, neither Alex' series nor mine. And I'd say this should rather be
>> resolved in the functions Alex introduce.
>>
>> Thanks,
>> Olek
> 
> Sorry, I couldn't reproduce the problem using the instructions at
> https://download.01.org/0day-ci/archive/20231017/202310170708.fJzLlgDM-lkp@intel.com/reproduce
> locally, maybe that's because I only have gcc-11 and higher.
> 
> But if I'm understanding correctly what's going on, then GCC will be
> reporting the same issue in the following code:
> 
> =======================================================
> #include <stddef.h>
> #include <stdbool.h>
> 
> #define BITS_PER_LONG 64
> #define unlikely(x) x
> #define UL(x) (x##UL)
> #define GENMASK(h, l) \
>         (((~UL(0)) - (UL(1) << (l)) + 1) & \
>          (~UL(0) >> (BITS_PER_LONG - 1 - (h))))
> 
> #define BIT_WORD(nr)            ((nr) / BITS_PER_LONG)
> #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
> #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
> 
> inline void bitmap_write(unsigned long *map,
>                                 unsigned long value,
>                                 unsigned long start, unsigned long nbits)
> {
>         size_t index;
>         unsigned long offset;
>         unsigned long space;
>         unsigned long mask;
>         bool fit;
> 
>         if (unlikely(!nbits))
>                 return;
> 
>         mask = BITMAP_LAST_WORD_MASK(nbits);
>         value &= mask;
>         offset = start % BITS_PER_LONG;
>         space = BITS_PER_LONG - offset;
>         fit = space >= nbits;
>         index = BIT_WORD(start);
> 
>         map[index] &= (fit ? (~(mask << offset)) :
> ~BITMAP_FIRST_WORD_MASK(start));
>         map[index] |= value << offset;
>         if (fit)
>                 return;
> 
>         map[index + 1] &= BITMAP_FIRST_WORD_MASK(start + nbits);
>         map[index + 1] |= (value >> space);
> }
> 
> unsigned long foo(unsigned int n) {
>     unsigned long bm[1] = {0};
>     bitmap_write(bm, 1, n, 2);
>     return bm[0];
> }
> =======================================================
> (see also https://godbolt.org/z/GfGfYje53)
> 
> If so, the problem is not specific to GCC 9, trunk GCC also barks on this code:
> 
> =======================================================
> In function 'bitmap_write',
>     inlined from 'bitmap_write' at <source>:15:13,
>     inlined from 'foo' at <source>:47:7:
> <source>:40:12: warning: array subscript 1 is outside array bounds of
> 'long unsigned int[1]' [-Warray-bounds=]
>    40 |         map[index + 1] &= BITMAP_FIRST_WORD_MASK(start + nbits);
>       |         ~~~^~~~~~~~~~~
> =======================================================
> 
> If this is true for the code in drivers/gpio/gpio-pca953x.c,
> suppressing the report for GCC 9 won't help for other versions.
> Given that this report is isolated in a single file, we probably need

I tested it on GCC 9 using modified make.cross from lkp and it triggers
on one more file:

drivers/thermal/intel/intel_soc_dts_iosf.c: In function 'sys_get_curr_temp':
./include/linux/bitmap.h:601:18: error: array subscript [1,
288230376151711744] is outside array bounds of 'long unsigned int[1]'
[-Werror=array-bounds]

> to give the compiler some hints about the range of values passed to
> bitmap_write() rather than suppressing the optimizations.

OPTIMIZER_HIDE_VAR() doesn't disable optimizations if I get it
correctly, rather shuts up the compiler in cases like this one.

I've been thinking of using __member_size() from fortify-string.h, we
could probably optimize the object code even a bit more while silencing
this warning.
Adding Kees, maybe he'd like to participate in sorting this out as well.

Thanks,
Olek

  reply	other threads:[~2023-11-07 16:44 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-17  0:14 [alobakin:pfcp 11/19] include/linux/bitmap.h:642:17: warning: array subscript [1, 1024] is outside array bounds of 'long unsigned int[1]' kernel test robot
2023-11-06 16:31 ` Alexander Lobakin
2023-11-06 18:23   ` Andy Shevchenko
2023-11-07 13:21     ` Alexander Lobakin
2023-11-07 16:33       ` Alexander Potapenko
2023-11-07 16:44         ` Alexander Lobakin [this message]
2023-11-07 17:24           ` Alexander Lobakin
2023-11-07 18:32             ` Yury Norov
2023-11-07 18:52               ` Alexander Lobakin
2023-11-07 19:24                 ` Yury Norov
2023-11-08 10:07                   ` Alexander Potapenko
2023-11-08 12:28                   ` Alexander Lobakin
2023-11-07 23:25           ` Kees Cook
2023-11-08  0:48             ` Yury Norov
2023-11-07 13:22   ` Yury Norov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ca0ad357-727b-4f41-a3d6-2c48a9dd5836@intel.com \
    --to=aleksander.lobakin@intel.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=glider@google.com \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=oe-kbuild-all@lists.linux.dev \
    --cc=syednwaris@gmail.com \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.