Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1592713 > unrolled thread

Arrays of variable length

Started byTomas Winkler <tomasw@gmail.com>
First post2017-03-05 10:50 +0100
Last post2017-03-09 15:50 +0100
Articles 14 — 5 participants

Back to article view | Back to linux.kernel


Contents

  Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-05 10:50 +0100
    Re: Arrays of variable length Al Viro <viro@ZenIV.linux.org.uk> - 2017-03-05 11:10 +0100
    Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-05 16:50 +0100
      Re: Arrays of variable length Henrique de Moraes Holschuh <hmh@hmh.eng.br> - 2017-03-05 22:20 +0100
        Re: Arrays of variable length Richard Weinberger <richard.weinberger@gmail.com> - 2017-03-05 23:00 +0100
        Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-06 01:40 +0100
          Re: Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-09 09:10 +0100
            Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-09 14:10 +0100
              Re: Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-09 15:00 +0100
                Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-09 15:20 +0100
                  Re: Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-09 15:30 +0100
                    Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-09 15:50 +0100
                  Re: Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-09 15:30 +0100
                    Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-09 15:50 +0100

#1592713 — Arrays of variable length

FromTomas Winkler <tomasw@gmail.com>
Date2017-03-05 10:50 +0100
SubjectArrays of variable length
Message-ID<thAXE-6JA-19@gated-at.bofh.it>
Sparse complains for arrays declared with variable length

'warning: Variable length array is used'

Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
And also Linux kernel compilation with W=1 doesn't complain.

Since sparse is used extensively would like to ask what is the correct
usage of arrays of variable length
within Linux Kernel.


Thanks
Tomas

[toc] | [next] | [standalone]


#1592720

FromAl Viro <viro@ZenIV.linux.org.uk>
Date2017-03-05 11:10 +0100
Message-ID<thBgZ-77S-3@gated-at.bofh.it>
In reply to#1592713
On Sun, Mar 05, 2017 at 11:44:33AM +0200, Tomas Winkler wrote:
> Sparse complains for arrays declared with variable length
> 
> 'warning: Variable length array is used'
> 
> Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
> with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
> And also Linux kernel compilation with W=1 doesn't complain.
> 
> Since sparse is used extensively would like to ask what is the correct
> usage of arrays of variable length
> within Linux Kernel.

That depends.  For structure members the answer is simply "don't, it's
not a valid C to start with".  Note that this is about actual VLA, not
struct foo {
	int bar;
	struct baz[];
}
- that is valid C99 and sparse is just fine with it.  For local variables...
keep in mind that kernel stack is _small_, so any VLA there needs to be
done very carefully.  For heap it's more or less usable, but keep in mind
that gcc support of VLA (and variably-modified types in general) has
seriously unpleasant corner cases, especially when combined with the ({...}) 
thing.  IOW, "doesn't have problem" is overoptimistic; use with care.

[toc] | [prev] | [next] | [standalone]


#1592785

FromMåns Rullgård <mans@mansr.com>
Date2017-03-05 16:50 +0100
Message-ID<thGA1-2jU-3@gated-at.bofh.it>
In reply to#1592713
Tomas Winkler <tomasw@gmail.com> writes:

> Sparse complains for arrays declared with variable length
>
> 'warning: Variable length array is used'
>
> Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
> with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
> And also Linux kernel compilation with W=1 doesn't complain.
>
> Since sparse is used extensively would like to ask what is the correct
> usage of arrays of variable length
> within Linux Kernel.

Variable-length arrays are a very bad idea.  Don't use them, ever.
If the size has a sane upper bound, just use that value statically.
Otherwise, you have a stack overflow waiting to happen and should be
using some kind of dynamic allocation instead.

Furthermore, use of VLAs generally results in less efficient code.  For
instance, it forces gcc to waste a register for the frame pointer, and
it often prevents inlining.

-- 
Måns Rullgård

[toc] | [prev] | [next] | [standalone]


#1592840

FromHenrique de Moraes Holschuh <hmh@hmh.eng.br>
Date2017-03-05 22:20 +0100
Message-ID<thLJo-6ax-23@gated-at.bofh.it>
In reply to#1592785
On Sun, 05 Mar 2017, Måns Rullgård wrote:
> Tomas Winkler <tomasw@gmail.com> writes:
> > Sparse complains for arrays declared with variable length
> >
> > 'warning: Variable length array is used'
> >
> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
> > And also Linux kernel compilation with W=1 doesn't complain.
> >
> > Since sparse is used extensively would like to ask what is the correct
> > usage of arrays of variable length
> > within Linux Kernel.
> 
> Variable-length arrays are a very bad idea.  Don't use them, ever.
> If the size has a sane upper bound, just use that value statically.
> Otherwise, you have a stack overflow waiting to happen and should be
> using some kind of dynamic allocation instead.
> 
> Furthermore, use of VLAs generally results in less efficient code.  For
> instance, it forces gcc to waste a register for the frame pointer, and
> it often prevents inlining.

Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
system should call gcc with -Werror=vla to get that point across early,
and flush out any offenders.

-- 
  Henrique Holschuh

[toc] | [prev] | [next] | [standalone]


#1592847

FromRichard Weinberger <richard.weinberger@gmail.com>
Date2017-03-05 23:00 +0100
Message-ID<thMm6-6qR-23@gated-at.bofh.it>
In reply to#1592840
On Sun, Mar 5, 2017 at 10:12 PM, Henrique de Moraes Holschuh
<hmh@hmh.eng.br> wrote:
> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>> Tomas Winkler <tomasw@gmail.com> writes:
>> > Sparse complains for arrays declared with variable length
>> >
>> > 'warning: Variable length array is used'
>> >
>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>> > And also Linux kernel compilation with W=1 doesn't complain.
>> >
>> > Since sparse is used extensively would like to ask what is the correct
>> > usage of arrays of variable length
>> > within Linux Kernel.
>>
>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>> If the size has a sane upper bound, just use that value statically.
>> Otherwise, you have a stack overflow waiting to happen and should be
>> using some kind of dynamic allocation instead.
>>
>> Furthermore, use of VLAs generally results in less efficient code.  For
>> instance, it forces gcc to waste a register for the frame pointer, and
>> it often prevents inlining.
>
> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
> system should call gcc with -Werror=vla to get that point across early,
> and flush out any offenders.

First we'd have to fix all existing offenders which are a few...

-- 
Thanks,
//richard

[toc] | [prev] | [next] | [standalone]


#1592867

FromMåns Rullgård <mans@mansr.com>
Date2017-03-06 01:40 +0100
Message-ID<thOQV-8jX-1@gated-at.bofh.it>
In reply to#1592840
Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:

> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>> Tomas Winkler <tomasw@gmail.com> writes:
>> > Sparse complains for arrays declared with variable length
>> >
>> > 'warning: Variable length array is used'
>> >
>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>> > And also Linux kernel compilation with W=1 doesn't complain.
>> >
>> > Since sparse is used extensively would like to ask what is the correct
>> > usage of arrays of variable length
>> > within Linux Kernel.
>> 
>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>> If the size has a sane upper bound, just use that value statically.
>> Otherwise, you have a stack overflow waiting to happen and should be
>> using some kind of dynamic allocation instead.
>> 
>> Furthermore, use of VLAs generally results in less efficient code.  For
>> instance, it forces gcc to waste a register for the frame pointer, and
>> it often prevents inlining.
>
> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
> system should call gcc with -Werror=vla to get that point across early,
> and flush out any offenders.

If it were up to me, that's exactly what I'd do.

-- 
Måns Rullgård

[toc] | [prev] | [next] | [standalone]


#1595773

FromTomas Winkler <tomasw@gmail.com>
Date2017-03-09 09:10 +0100
Message-ID<tj1j5-2og-69@gated-at.bofh.it>
In reply to#1592867
On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@mansr.com> wrote:
> Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:
>
>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>> Tomas Winkler <tomasw@gmail.com> writes:
>>> > Sparse complains for arrays declared with variable length
>>> >
>>> > 'warning: Variable length array is used'
>>> >
>>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>>> > And also Linux kernel compilation with W=1 doesn't complain.
>>> >
>>> > Since sparse is used extensively would like to ask what is the correct
>>> > usage of arrays of variable length
>>> > within Linux Kernel.
>>>
>>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>>> If the size has a sane upper bound, just use that value statically.
>>> Otherwise, you have a stack overflow waiting to happen and should be
>>> using some kind of dynamic allocation instead.
>>>
>>> Furthermore, use of VLAs generally results in less efficient code.  For
>>> instance, it forces gcc to waste a register for the frame pointer, and
>>> it often prevents inlining.
>>
>> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
>> system should call gcc with -Werror=vla to get that point across early,
>> and flush out any offenders.
>
> If it were up to me, that's exactly what I'd do.

>
Some parts of the kernel depends on VLA such as ___ON_STACK macros in
include/crypto/hash.h
It's actually pretty neat implementation, maybe it's too harsh to
disable  VLA completely.

Tomas

[toc] | [prev] | [next] | [standalone]


#1596042

FromMåns Rullgård <mans@mansr.com>
Date2017-03-09 14:10 +0100
Message-ID<tj5Zn-5Ce-7@gated-at.bofh.it>
In reply to#1595773
Tomas Winkler <tomasw@gmail.com> writes:

> On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@mansr.com> wrote:
>> Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:
>>
>>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>> > Sparse complains for arrays declared with variable length
>>>> >
>>>> > 'warning: Variable length array is used'
>>>> >
>>>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>>>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>>>> > And also Linux kernel compilation with W=1 doesn't complain.
>>>> >
>>>> > Since sparse is used extensively would like to ask what is the correct
>>>> > usage of arrays of variable length
>>>> > within Linux Kernel.
>>>>
>>>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>>>> If the size has a sane upper bound, just use that value statically.
>>>> Otherwise, you have a stack overflow waiting to happen and should be
>>>> using some kind of dynamic allocation instead.
>>>>
>>>> Furthermore, use of VLAs generally results in less efficient code.  For
>>>> instance, it forces gcc to waste a register for the frame pointer, and
>>>> it often prevents inlining.
>>>
>>> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
>>> system should call gcc with -Werror=vla to get that point across early,
>>> and flush out any offenders.
>>
>> If it were up to me, that's exactly what I'd do.
>
>>
> Some parts of the kernel depends on VLA such as ___ON_STACK macros in
> include/crypto/hash.h
> It's actually pretty neat implementation, maybe it's too harsh to
> disable  VLA completely.

And what happens if the requested size is insane?

-- 
Måns Rullgård

[toc] | [prev] | [next] | [standalone]


#1596076

FromTomas Winkler <tomasw@gmail.com>
Date2017-03-09 15:00 +0100
Message-ID<tj6LL-5W9-1@gated-at.bofh.it>
In reply to#1596042
On Thu, Mar 9, 2017 at 3:02 PM, Måns Rullgård <mans@mansr.com> wrote:
> Tomas Winkler <tomasw@gmail.com> writes:
>
>> On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@mansr.com> wrote:
>>> Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:
>>>
>>>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>> > Sparse complains for arrays declared with variable length
>>>>> >
>>>>> > 'warning: Variable length array is used'
>>>>> >
>>>>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>>>>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>>>>> > And also Linux kernel compilation with W=1 doesn't complain.
>>>>> >
>>>>> > Since sparse is used extensively would like to ask what is the correct
>>>>> > usage of arrays of variable length
>>>>> > within Linux Kernel.
>>>>>
>>>>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>>>>> If the size has a sane upper bound, just use that value statically.
>>>>> Otherwise, you have a stack overflow waiting to happen and should be
>>>>> using some kind of dynamic allocation instead.
>>>>>
>>>>> Furthermore, use of VLAs generally results in less efficient code.  For
>>>>> instance, it forces gcc to waste a register for the frame pointer, and
>>>>> it often prevents inlining.
>>>>
>>>> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
>>>> system should call gcc with -Werror=vla to get that point across early,
>>>> and flush out any offenders.
>>>
>>> If it were up to me, that's exactly what I'd do.
>>
>>>
>> Some parts of the kernel depends on VLA such as ___ON_STACK macros in
>> include/crypto/hash.h
>> It's actually pretty neat implementation, maybe it's too harsh to
>> disable  VLA completely.
>
> And what happens if the requested size is insane?

One option is to add '-Wvla-larger-than=n' other option is to selectively
shut down the warning on ON_STACK macros using #pragma
warning(disable:) though this looks rather ugly.
Just a thought

Tomas

[toc] | [prev] | [next] | [standalone]


#1596091

FromMåns Rullgård <mans@mansr.com>
Date2017-03-09 15:20 +0100
Message-ID<tj758-6kn-15@gated-at.bofh.it>
In reply to#1596076
Tomas Winkler <tomasw@gmail.com> writes:

> On Thu, Mar 9, 2017 at 3:02 PM, Måns Rullgård <mans@mansr.com> wrote:
>> Tomas Winkler <tomasw@gmail.com> writes:
>>
>>> On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@mansr.com> wrote:
>>>> Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:
>>>>
>>>>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>>> > Sparse complains for arrays declared with variable length
>>>>>> >
>>>>>> > 'warning: Variable length array is used'
>>>>>> >
>>>>>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>>>>>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>>>>>> > And also Linux kernel compilation with W=1 doesn't complain.
>>>>>> >
>>>>>> > Since sparse is used extensively would like to ask what is the correct
>>>>>> > usage of arrays of variable length
>>>>>> > within Linux Kernel.
>>>>>>
>>>>>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>>>>>> If the size has a sane upper bound, just use that value statically.
>>>>>> Otherwise, you have a stack overflow waiting to happen and should be
>>>>>> using some kind of dynamic allocation instead.
>>>>>>
>>>>>> Furthermore, use of VLAs generally results in less efficient code.  For
>>>>>> instance, it forces gcc to waste a register for the frame pointer, and
>>>>>> it often prevents inlining.
>>>>>
>>>>> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
>>>>> system should call gcc with -Werror=vla to get that point across early,
>>>>> and flush out any offenders.
>>>>
>>>> If it were up to me, that's exactly what I'd do.
>>>
>>>>
>>> Some parts of the kernel depends on VLA such as ___ON_STACK macros in
>>> include/crypto/hash.h
>>> It's actually pretty neat implementation, maybe it's too harsh to
>>> disable  VLA completely.
>>
>> And what happens if the requested size is insane?
>
> One option is to add '-Wvla-larger-than=n'

If you know the upper bound, why use VLAs in the first place?

-- 
Måns Rullgård

[toc] | [prev] | [next] | [standalone]


#1596097

FromTomas Winkler <tomasw@gmail.com>
Date2017-03-09 15:30 +0100
Message-ID<tj7eO-6nW-11@gated-at.bofh.it>
In reply to#1596091
On Thu, Mar 9, 2017 at 4:26 PM, Måns Rullgård <mans@mansr.com> wrote:
> Tomas Winkler <tomasw@gmail.com> writes:
>
>> On Thu, Mar 9, 2017 at 4:16 PM, Måns Rullgård <mans@mansr.com> wrote:
>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>
>>>> On Thu, Mar 9, 2017 at 3:02 PM, Måns Rullgård <mans@mansr.com> wrote:
>>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>>
>>>>>> On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@mansr.com> wrote:
>>>>>>> Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:
>>>>>>>
>>>>>>>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>>>>>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>>>>>> > Sparse complains for arrays declared with variable length
>>>>>>>>> >
>>>>>>>>> > 'warning: Variable length array is used'
>>>>>>>>> >
>>>>>>>>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>>>>>>>>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>>>>>>>>> > And also Linux kernel compilation with W=1 doesn't complain.
>>>>>>>>> >
>>>>>>>>> > Since sparse is used extensively would like to ask what is the correct
>>>>>>>>> > usage of arrays of variable length
>>>>>>>>> > within Linux Kernel.
>>>>>>>>>
>>>>>>>>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>>>>>>>>> If the size has a sane upper bound, just use that value statically.
>>>>>>>>> Otherwise, you have a stack overflow waiting to happen and should be
>>>>>>>>> using some kind of dynamic allocation instead.
>>>>>>>>>
>>>>>>>>> Furthermore, use of VLAs generally results in less efficient code.  For
>>>>>>>>> instance, it forces gcc to waste a register for the frame pointer, and
>>>>>>>>> it often prevents inlining.
>>>>>>>>
>>>>>>>> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
>>>>>>>> system should call gcc with -Werror=vla to get that point across early,
>>>>>>>> and flush out any offenders.
>>>>>>>
>>>>>>> If it were up to me, that's exactly what I'd do.
>>>>>>
>>>>>>>
>>>>>> Some parts of the kernel depends on VLA such as ___ON_STACK macros in
>>>>>> include/crypto/hash.h
>>>>>> It's actually pretty neat implementation, maybe it's too harsh to
>>>>>> disable  VLA completely.
>>>>>
>>>>> And what happens if the requested size is insane?
>>>>
>>>> One option is to add '-Wvla-larger-than=n'
>>>
>>> If you know the upper bound, why use VLAs in the first place?
>>
>> This is a water mark and not  actual usage, but maybe I didn't
>> understand your comment.
>
> If there is an upper bound known at compile time, why not simply use
> that size statically?  If there is no upper bound, well, then you have a
> problem.

If the compiler can do the job, why not to use this flexibility ?

Tomas

[toc] | [prev] | [next] | [standalone]


#1596121

FromMåns Rullgård <mans@mansr.com>
Date2017-03-09 15:50 +0100
Message-ID<tj7ya-6vj-35@gated-at.bofh.it>
In reply to#1596097
Tomas Winkler <tomasw@gmail.com> writes:

> On Thu, Mar 9, 2017 at 4:26 PM, Måns Rullgård <mans@mansr.com> wrote:
>> Tomas Winkler <tomasw@gmail.com> writes:
>>
>>> On Thu, Mar 9, 2017 at 4:16 PM, Måns Rullgård <mans@mansr.com> wrote:
>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>
>>>>> On Thu, Mar 9, 2017 at 3:02 PM, Måns Rullgård <mans@mansr.com> wrote:
>>>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>>>
>>>>>>> On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@mansr.com> wrote:
>>>>>>>> Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:
>>>>>>>>
>>>>>>>>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>>>>>>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>>>>>>> > Sparse complains for arrays declared with variable length
>>>>>>>>>> >
>>>>>>>>>> > 'warning: Variable length array is used'
>>>>>>>>>> >
>>>>>>>>>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>>>>>>>>>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>>>>>>>>>> > And also Linux kernel compilation with W=1 doesn't complain.
>>>>>>>>>> >
>>>>>>>>>> > Since sparse is used extensively would like to ask what is the correct
>>>>>>>>>> > usage of arrays of variable length
>>>>>>>>>> > within Linux Kernel.
>>>>>>>>>>
>>>>>>>>>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>>>>>>>>>> If the size has a sane upper bound, just use that value statically.
>>>>>>>>>> Otherwise, you have a stack overflow waiting to happen and should be
>>>>>>>>>> using some kind of dynamic allocation instead.
>>>>>>>>>>
>>>>>>>>>> Furthermore, use of VLAs generally results in less efficient code.  For
>>>>>>>>>> instance, it forces gcc to waste a register for the frame pointer, and
>>>>>>>>>> it often prevents inlining.
>>>>>>>>>
>>>>>>>>> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
>>>>>>>>> system should call gcc with -Werror=vla to get that point across early,
>>>>>>>>> and flush out any offenders.
>>>>>>>>
>>>>>>>> If it were up to me, that's exactly what I'd do.
>>>>>>>
>>>>>>>>
>>>>>>> Some parts of the kernel depends on VLA such as ___ON_STACK macros in
>>>>>>> include/crypto/hash.h
>>>>>>> It's actually pretty neat implementation, maybe it's too harsh to
>>>>>>> disable  VLA completely.
>>>>>>
>>>>>> And what happens if the requested size is insane?
>>>>>
>>>>> One option is to add '-Wvla-larger-than=n'
>>>>
>>>> If you know the upper bound, why use VLAs in the first place?
>>>
>>> This is a water mark and not  actual usage, but maybe I didn't
>>> understand your comment.
>>
>> If there is an upper bound known at compile time, why not simply use
>> that size statically?  If there is no upper bound, well, then you have a
>> problem.
>
> If the compiler can do the job, why not to use this flexibility ?

Because, as I already said, there are security implications if the size
is unbounded, and even with safely bounded size, using VLAs interferes
with compiler optimisations.  Ensuring VLAs are used safely is usually
more work than simply avoiding them in the first place.

-- 
Måns Rullgård

[toc] | [prev] | [next] | [standalone]


#1596101

FromTomas Winkler <tomasw@gmail.com>
Date2017-03-09 15:30 +0100
Message-ID<tj7eO-6nW-13@gated-at.bofh.it>
In reply to#1596091
On Thu, Mar 9, 2017 at 4:16 PM, Måns Rullgård <mans@mansr.com> wrote:
> Tomas Winkler <tomasw@gmail.com> writes:
>
>> On Thu, Mar 9, 2017 at 3:02 PM, Måns Rullgård <mans@mansr.com> wrote:
>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>
>>>> On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@mansr.com> wrote:
>>>>> Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:
>>>>>
>>>>>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>>>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>>>> > Sparse complains for arrays declared with variable length
>>>>>>> >
>>>>>>> > 'warning: Variable length array is used'
>>>>>>> >
>>>>>>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>>>>>>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>>>>>>> > And also Linux kernel compilation with W=1 doesn't complain.
>>>>>>> >
>>>>>>> > Since sparse is used extensively would like to ask what is the correct
>>>>>>> > usage of arrays of variable length
>>>>>>> > within Linux Kernel.
>>>>>>>
>>>>>>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>>>>>>> If the size has a sane upper bound, just use that value statically.
>>>>>>> Otherwise, you have a stack overflow waiting to happen and should be
>>>>>>> using some kind of dynamic allocation instead.
>>>>>>>
>>>>>>> Furthermore, use of VLAs generally results in less efficient code.  For
>>>>>>> instance, it forces gcc to waste a register for the frame pointer, and
>>>>>>> it often prevents inlining.
>>>>>>
>>>>>> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
>>>>>> system should call gcc with -Werror=vla to get that point across early,
>>>>>> and flush out any offenders.
>>>>>
>>>>> If it were up to me, that's exactly what I'd do.
>>>>
>>>>>
>>>> Some parts of the kernel depends on VLA such as ___ON_STACK macros in
>>>> include/crypto/hash.h
>>>> It's actually pretty neat implementation, maybe it's too harsh to
>>>> disable  VLA completely.
>>>
>>> And what happens if the requested size is insane?
>>
>> One option is to add '-Wvla-larger-than=n'
>
> If you know the upper bound, why use VLAs in the first place?

This is a water mark and not  actual usage, but maybe I didn't
understand your comment.

Tomas

[toc] | [prev] | [next] | [standalone]


#1596116

FromMåns Rullgård <mans@mansr.com>
Date2017-03-09 15:50 +0100
Message-ID<tj7eO-6nW-15@gated-at.bofh.it>
In reply to#1596101
Tomas Winkler <tomasw@gmail.com> writes:

> On Thu, Mar 9, 2017 at 4:16 PM, Måns Rullgård <mans@mansr.com> wrote:
>> Tomas Winkler <tomasw@gmail.com> writes:
>>
>>> On Thu, Mar 9, 2017 at 3:02 PM, Måns Rullgård <mans@mansr.com> wrote:
>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>
>>>>> On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@mansr.com> wrote:
>>>>>> Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:
>>>>>>
>>>>>>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>>>>>>> Tomas Winkler <tomasw@gmail.com> writes:
>>>>>>>> > Sparse complains for arrays declared with variable length
>>>>>>>> >
>>>>>>>> > 'warning: Variable length array is used'
>>>>>>>> >
>>>>>>>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>>>>>>>> > with that  https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>>>>>>>> > And also Linux kernel compilation with W=1 doesn't complain.
>>>>>>>> >
>>>>>>>> > Since sparse is used extensively would like to ask what is the correct
>>>>>>>> > usage of arrays of variable length
>>>>>>>> > within Linux Kernel.
>>>>>>>>
>>>>>>>> Variable-length arrays are a very bad idea.  Don't use them, ever.
>>>>>>>> If the size has a sane upper bound, just use that value statically.
>>>>>>>> Otherwise, you have a stack overflow waiting to happen and should be
>>>>>>>> using some kind of dynamic allocation instead.
>>>>>>>>
>>>>>>>> Furthermore, use of VLAs generally results in less efficient code.  For
>>>>>>>> instance, it forces gcc to waste a register for the frame pointer, and
>>>>>>>> it often prevents inlining.
>>>>>>>
>>>>>>> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
>>>>>>> system should call gcc with -Werror=vla to get that point across early,
>>>>>>> and flush out any offenders.
>>>>>>
>>>>>> If it were up to me, that's exactly what I'd do.
>>>>>
>>>>>>
>>>>> Some parts of the kernel depends on VLA such as ___ON_STACK macros in
>>>>> include/crypto/hash.h
>>>>> It's actually pretty neat implementation, maybe it's too harsh to
>>>>> disable  VLA completely.
>>>>
>>>> And what happens if the requested size is insane?
>>>
>>> One option is to add '-Wvla-larger-than=n'
>>
>> If you know the upper bound, why use VLAs in the first place?
>
> This is a water mark and not  actual usage, but maybe I didn't
> understand your comment.

If there is an upper bound known at compile time, why not simply use
that size statically?  If there is no upper bound, well, then you have a
problem.

-- 
Måns Rullgård

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web