2022-06-03 15:50:19

by Arnd Bergmann

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

On Fri, Jun 3, 2022 at 4:03 AM Naresh Kamboju <[email protected]> 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?

I think the easy fix would be to reword this line to

kernel_code.end = __pa_symbol(__init_begin) - 1;


Arnd


2022-06-06 09:25:33

by Maxime Ripard

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

Hi,

On Fri, Jun 03, 2022 at 09:40:07AM +0200, Arnd Bergmann wrote:
> On Fri, Jun 3, 2022 at 4:03 AM Naresh Kamboju <[email protected]> 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?

In that function, both kernel_code.end and kernel_data_end show a
similar warning in 5.19-rc1

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

Doing this for both fix the warnings.

Maxime


Attachments:
(No filename) (773.00 B)
signature.asc (235.00 B)
Download all attachments

2022-06-06 10:09:12

by Mark Rutland

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

On Fri, Jun 03, 2022 at 09:40:07AM +0200, Arnd Bergmann wrote:
> On Fri, Jun 3, 2022 at 4:03 AM Naresh Kamboju <[email protected]> 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.

2022-06-08 08:38:19

by Arnd Bergmann

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

On Mon, Jun 6, 2022 at 11:41 AM Mark Rutland <[email protected]> wrote:
> On Fri, Jun 03, 2022 at 09:40:07AM +0200, Arnd Bergmann wrote:
>
> #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);
>asm-gener
> ... which IIUC should do the trick.
>

I see we have similar logic on each architecture, and they probably
all have the same
issue now, so maybe we can just do a helper function in include/linux/ioport.h
(which has all the struct resource logic) that can be called like

resource_set_pa(&kernel_code, _stext, __init_begin);
resource_set_pa(&kernel_data, _sdata, _end);

Arnd