Re: gcc-12: build errors: arch/arm64/kernel/setup.c:225:56: warning: array subscript -1 is outside array bounds of 'char[]' [-Warray-bounds]

From: Mark Rutland
Date: Mon Jun 06 2022 - 05:42:26 EST


On Fri, Jun 03, 2022 at 09:40:07AM +0200, Arnd Bergmann wrote:
> On Fri, Jun 3, 2022 at 4:03 AM Naresh Kamboju <naresh.kamboju@xxxxxxxxxx> wrote:
> > inlined from 'setup_arch' at arch/arm64/kernel/setup.c:350:2:
> > arch/arm64/kernel/setup.c:225:56: warning: array subscript -1 is
> > outside array bounds of 'char[]' [-Warray-bounds]
> > 225 | kernel_code.end = __pa_symbol(__init_begin - 1);
> >
>
> Is this the only warning of this type that you get for arm64?

There are a handful of those subscript warnings. Looking at v5.19-rc1
defconfig, using the kernel.org GCC 12.1.0 cross toolchain:

| [mark@lakrids:~/src/linux]% usekorg 12.1.0 make ARCH=arm64 CROSS_COMPILE=aarch64-linux- -j50 2>&1 | grep -A1 subscript
| arch/arm64/kernel/setup.c:225:56: warning: array subscript -1 is outside array bounds of 'char[]' [-Warray-bounds]
| 225 | kernel_code.end = __pa_symbol(__init_begin - 1);
| --
| arch/arm64/kernel/setup.c:227:48: warning: array subscript -1 is outside array bounds of 'char[]' [-Warray-bounds]
| 227 | kernel_data.end = __pa_symbol(_end - 1);
| --
| arch/arm64/kernel/hibernate.c:94:65: warning: array subscript -1 is outside array bounds of 'const void[]' [-Warray-bounds]
| 94 | unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1);

The last of those can't have the `- 1` pulled out, but we could stuff a
RELOC_HIDE() in there, as __pa_symbol() has internally.

Ideally we'd rework the section markers to not have this problem, either
rethinking the way we mark them as flexible arrays, or giving them accessors,
e.g.

#define va_init_begin() RELOC_HIDE((unsigned long)__init_begin)

... which'd be a pain, but at least it'd solve this generally.

> I think the easy fix would be to reword this line to
>
> kernel_code.end = __pa_symbol(__init_begin) - 1;
>

I agree that'd work for the __pa_symbol() cases.

For consistency it might be worth using RELOC_HIDE(), e.g.

kernel_code.end = __pa_symbol(RELOC_HIDE(__init_begin)) - 1);

... which IIUC should do the trick.

Thanks,
Mark.