If a large const array or struct is on the stack, make it static const so that the data does not have to be populated on the stack each time a function is entered. I keep on finding these in the kernel.
-
-
Replying to @colinianking
Why doesn't the compiler do this already? Putting it on the stack violates "const" intentions too.
1 reply 0 retweets 0 likes -
Replying to @kees_cook
If one looks at the object code size after adding the static one can see the data segment size increases and text segment decreases, so probably not. Looks iffy to me.
1 reply 0 retweets 0 likes -
Replying to @colinianking
https://gist.github.com/kees/4a72681d1b56c08e294872260233243c … This is what I mean: only the "static const" is actually read-only. I think locally declared const should be automatically made static. Maybe I'm missing something where this would break things?
2 replies 0 retweets 1 like -
Replying to @kees_cook
K&R 2nd edition, page 211, final paragraph "The purpose of const is to announce objects that may be placed in read-only memory, and perhaps to increase opportunities for optimization". So it's a hint and implementation defined.
2 replies 0 retweets 1 like -
Replying to @colinianking
Right -- it's a hint, but the expectation these days is that is must be read-only. Similar things were fixed for PPC recently https://lkml.org/lkml/2018/2/28/1409 …
1 reply 0 retweets 0 likes -
-
Replying to @colinianking @kees_cook
You can have const register declarations, that's legal C, but not many CPUs have read-only registers. Const is a misnomer, it does not mean it is constant, it means the compiler should warning if it is written to. const register int x = 1; x++; /* compiler picks this up */
2 replies 0 retweets 1 like -
Replying to @colinianking
Yup, I don't disagree -- but where it's _possible_, it should be done. There's no reason that a local const structure or array should live anywhere except in .rodata.
1 reply 0 retweets 0 likes -
Replying to @kees_cook
I agree too. The expectation is to do the "safe" thing. Just shoving auto const variables on the stack is sometimes more performance optimal because it's cache hot and still complies with the spec, so I guess maybe why it's done that way.
1 reply 0 retweets 1 like
Can we throw Coccinelle at this?
-
-
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.