How do functions like alloca allocate memory from the stack?

devblogs.microsoft.com

59 points by ingve 14 hours ago


uecker - 18 minutes ago

Nobody should use alloca. If you must allocate a buffer on the stack, use a VLA, which is standardized, has proper scope-bound lifetimes, and a type that remembers the exact size. Yes, I know MSVC does not upport it. Don't use this compiler. (where credit is due: MSVC had stack probing a lot ealier than GCC and clang, and clang was very late).

With gcc, you get stack probing with -fstack-clash-protection, which is similar to _chkstk but GCC inlines the stack probes.

VLA got a bad name because of stack clash attacks, but without stack clash protection these attacks can appear also without VLAs (and the first such attacks actually exploited fixed-size arrays), and if you activate this protection there is IMHO not much reason to avoid VLAs.

If you need a small variably-sized buffers, VLAs are almost always superior to any alternative. alloca is worse in every way (see above), a regular array with worst-case bound increases stack use relative to a VLA and does not encode the correct dynamic size which makes bounds checking weaker, and moving the buffer to the heap is slower and complicates the code.

If you can not properly account for the sizes of the things you put on your stack and worry about VLAs exceeding the limit (but again, regular arrays with worst-case size increase stack usage compared to VLAs), on GCC you can use -Wvla-larger-than to make sure the size of each VLA stays bounded.

pjmlp - 4 hours ago

One of those functions that isn't really implementable in standard C, requiring either compiler support, or being written in straight Assembly for stack registers manipulation, one of those "micro runtime" features for C.

From UNIX 7th edition all the way up to C99, when VLAs where introduced, only to be made optional in C11, and the C23 update still doesn't support automatic VLAs, only for function parameters, thus the point stands.

stkdump - 2 hours ago

So what if several functions that use less than 4KB each call each other before using the stack variables in a way that the first access skips over one page?

eska - 4 hours ago

Recommended related reading: https://nullprogram.com/blog/2024/02/05/

kjellsbells - 11 hours ago

One thing that scares me a little is whether there are younger developers, say, 25-40, who can and want to pick up the mantle of Windows internals gurus.

I mean, Chen has decades of winternals in his head. Microsoft has been gutting their staff for years now. When the Petzold/Chen generation hang up their spurs, does Microsoft still have a critical mass of people who understand Windows from the metal up?

jacknews - 8 hours ago

Only passingly related, some fun rust stack-allocation insanity by my 17yo son:

https://ogghostjelly.github.io/slog/alloca.html

rramadass - 4 hours ago

Related to the above, two important concepts to know w.r.t a stack are "Red Zone" and "Guard Pages".

Raymond Chen again;

Why do we even need to define a red zone? Can’t I just use my stack for anything? - https://devblogs.microsoft.com/oldnewthing/20190111-00/?p=10...

A closer look at the stack guard page - https://devblogs.microsoft.com/oldnewthing/20220203-00/?p=10...

ferrow - 9 hours ago

[flagged]