All of lore.kernel.org
 help / color / mirror / Atom feed
* Arrays of variable length
@ 2017-03-05  9:44 Tomas Winkler
  2017-03-05 10:01 ` Al Viro
  2017-03-05 14:27 ` Måns Rullgård
  0 siblings, 2 replies; 18+ messages in thread
From: Tomas Winkler @ 2017-03-05  9:44 UTC (permalink / raw)
  To: linux-kernel, sparse

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-05  9:44 Arrays of variable length Tomas Winkler
@ 2017-03-05 10:01 ` Al Viro
  2017-03-05 14:27 ` Måns Rullgård
  1 sibling, 0 replies; 18+ messages in thread
From: Al Viro @ 2017-03-05 10:01 UTC (permalink / raw)
  To: Tomas Winkler; +Cc: linux-kernel, sparse

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.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-05  9:44 Arrays of variable length Tomas Winkler
  2017-03-05 10:01 ` Al Viro
@ 2017-03-05 14:27 ` Måns Rullgård
  2017-03-05 21:12   ` Henrique de Moraes Holschuh
  1 sibling, 1 reply; 18+ messages in thread
From: Måns Rullgård @ 2017-03-05 14:27 UTC (permalink / raw)
  To: Tomas Winkler; +Cc: linux-kernel, sparse

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-05 14:27 ` Måns Rullgård
@ 2017-03-05 21:12   ` Henrique de Moraes Holschuh
  2017-03-05 21:49     ` Richard Weinberger
  2017-03-06  0:31     ` Måns Rullgård
  0 siblings, 2 replies; 18+ messages in thread
From: Henrique de Moraes Holschuh @ 2017-03-05 21:12 UTC (permalink / raw)
  To: Måns Rullgård; +Cc: Tomas Winkler, linux-kernel, sparse

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-05 21:12   ` Henrique de Moraes Holschuh
@ 2017-03-05 21:49     ` Richard Weinberger
  2017-03-06  0:31     ` Måns Rullgård
  1 sibling, 0 replies; 18+ messages in thread
From: Richard Weinberger @ 2017-03-05 21:49 UTC (permalink / raw)
  To: Henrique de Moraes Holschuh
  Cc: Måns Rullgård, Tomas Winkler, linux-kernel, sparse

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-05 21:12   ` Henrique de Moraes Holschuh
  2017-03-05 21:49     ` Richard Weinberger
@ 2017-03-06  0:31     ` Måns Rullgård
  2017-03-09  7:54       ` Tomas Winkler
  1 sibling, 1 reply; 18+ messages in thread
From: Måns Rullgård @ 2017-03-06  0:31 UTC (permalink / raw)
  To: Henrique de Moraes Holschuh; +Cc: Tomas Winkler, linux-kernel, sparse

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-06  0:31     ` Måns Rullgård
@ 2017-03-09  7:54       ` Tomas Winkler
  2017-03-09 13:02           ` Måns Rullgård
  0 siblings, 1 reply; 18+ messages in thread
From: Tomas Winkler @ 2017-03-09  7:54 UTC (permalink / raw)
  To: Måns Rullgård
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-09  7:54       ` Tomas Winkler
@ 2017-03-09 13:02           ` Måns Rullgård
  0 siblings, 0 replies; 18+ messages in thread
From: Måns Rullgård @ 2017-03-09 13:02 UTC (permalink / raw)
  To: Tomas Winkler
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
@ 2017-03-09 13:02           ` Måns Rullgård
  0 siblings, 0 replies; 18+ messages in thread
From: Måns Rullgård @ 2017-03-09 13:02 UTC (permalink / raw)
  To: Tomas Winkler
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-09 13:02           ` Måns Rullgård
  (?)
@ 2017-03-09 13:40           ` Tomas Winkler
  2017-03-09 14:16               ` Måns Rullgård
  -1 siblings, 1 reply; 18+ messages in thread
From: Tomas Winkler @ 2017-03-09 13:40 UTC (permalink / raw)
  To: Måns Rullgård
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-09 13:40           ` Tomas Winkler
@ 2017-03-09 14:16               ` Måns Rullgård
  0 siblings, 0 replies; 18+ messages in thread
From: Måns Rullgård @ 2017-03-09 14:16 UTC (permalink / raw)
  To: Tomas Winkler
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
@ 2017-03-09 14:16               ` Måns Rullgård
  0 siblings, 0 replies; 18+ messages in thread
From: Måns Rullgård @ 2017-03-09 14:16 UTC (permalink / raw)
  To: Tomas Winkler
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-09 14:16               ` Måns Rullgård
  (?)
@ 2017-03-09 14:21               ` Tomas Winkler
  2017-03-09 14:26                   ` Måns Rullgård
  -1 siblings, 1 reply; 18+ messages in thread
From: Tomas Winkler @ 2017-03-09 14:21 UTC (permalink / raw)
  To: Måns Rullgård
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-09 14:21               ` Tomas Winkler
@ 2017-03-09 14:26                   ` Måns Rullgård
  0 siblings, 0 replies; 18+ messages in thread
From: Måns Rullgård @ 2017-03-09 14:26 UTC (permalink / raw)
  To: Tomas Winkler
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
@ 2017-03-09 14:26                   ` Måns Rullgård
  0 siblings, 0 replies; 18+ messages in thread
From: Måns Rullgård @ 2017-03-09 14:26 UTC (permalink / raw)
  To: Tomas Winkler
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-09 14:26                   ` Måns Rullgård
  (?)
@ 2017-03-09 14:29                   ` Tomas Winkler
  2017-03-09 14:38                       ` Måns Rullgård
  -1 siblings, 1 reply; 18+ messages in thread
From: Tomas Winkler @ 2017-03-09 14:29 UTC (permalink / raw)
  To: Måns Rullgård
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
  2017-03-09 14:29                   ` Tomas Winkler
@ 2017-03-09 14:38                       ` Måns Rullgård
  0 siblings, 0 replies; 18+ messages in thread
From: Måns Rullgård @ 2017-03-09 14:38 UTC (permalink / raw)
  To: Tomas Winkler
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Arrays of variable length
@ 2017-03-09 14:38                       ` Måns Rullgård
  0 siblings, 0 replies; 18+ messages in thread
From: Måns Rullgård @ 2017-03-09 14:38 UTC (permalink / raw)
  To: Tomas Winkler
  Cc: Henrique de Moraes Holschuh, linux-kernel, linux-sparse,
	Herbert Xu, Al Viro

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

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2017-03-09 14:45 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-05  9:44 Arrays of variable length Tomas Winkler
2017-03-05 10:01 ` Al Viro
2017-03-05 14:27 ` Måns Rullgård
2017-03-05 21:12   ` Henrique de Moraes Holschuh
2017-03-05 21:49     ` Richard Weinberger
2017-03-06  0:31     ` Måns Rullgård
2017-03-09  7:54       ` Tomas Winkler
2017-03-09 13:02         ` Måns Rullgård
2017-03-09 13:02           ` Måns Rullgård
2017-03-09 13:40           ` Tomas Winkler
2017-03-09 14:16             ` Måns Rullgård
2017-03-09 14:16               ` Måns Rullgård
2017-03-09 14:21               ` Tomas Winkler
2017-03-09 14:26                 ` Måns Rullgård
2017-03-09 14:26                   ` Måns Rullgård
2017-03-09 14:29                   ` Tomas Winkler
2017-03-09 14:38                     ` Måns Rullgård
2017-03-09 14:38                       ` Måns Rullgård

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.