3 ms·
You need alloca in virtual machines to allocate local variable frames, if you have opted for using the native stack for doing so. These frames are dynamic; thei
by kazinator 7y ago
You need alloca in virtual machines to allocate local variable frames, if you have opted for using the native stack for doing so. These frames are dynamic; their size comes from some field pulled from a function object. Alloca is also useful in resuming stack-snapshot-based delimited continuations. These objects have to be planted into the stack to work properly. They also have a variable size: whatever it took to capture them up to their delimited prompt from.
Basically, C function calls are doing the equivalent of alloca all the time. The size of stack needed by a function call is not known in advance. It may be a constant in that particular function, but what function is called depends on run-time tests, and indirect calls. (*fptr)(foo) could call one of fifty possible functions, whose stack consumption ranges from 50 bytes to 50,000. That's kind of like calling alloca with a random number between 50 and 50,000. And like with alloca, there is no detection. C is not robust against the problem of "no more room for the next stack frame"; it is undefined behavior.
The simplified idea that "all alloca is bad" comes from a parochial, in-the-box thinking.
(Sure, alloca for, say, temporary storage for catenating strings that come from "read a line of any length" routines, whose inputs are controlled by a remote attacker, is undeniably bad. However, even without using alloca, we can cause the problem that some input available to an attacker controls the depth of some (non-tail-optimized) recursion.)