问题
Does Linux provide an inaccessible memory area below the lower stack end that has a guaranteed minimum size? And if such a guaranteed minimum size exists, what is it?
Or in other words, when should I start to worry about alloca()
or so giving me pointers into valid, non-stack memory?
回答1:
As the alloca man page says:
There is no error indication if the stack frame cannot be extended. (However, after a failed allocation, the program is likely to receive a SIGSEGV signal if it attempts to access the unallocated space.)
So there is no indication at all and it also says:
If the allocation causes stack overflow, program behavior is undefined.
The stack overflow problem is a general issue with recursion and not really particular to alloca
or let's say variable length arrays. Typically you either need to find a way to limit the depth of the recursion, refactor to an iterative solution or use your own dynamic stack(probably does not apply to this case).
Update
As the OP discovered Linux does provide an after the fact indication using a guard page after the stack of stack overflow by generating a SIGBUS
signal, which addresses the first part of the question.
回答2:
Thanks to @ElliottFrisch for making me google this with the proper name... whoops.
Looks like the answer is "in newer kernels: one page, in older kernels: no such protection".
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=320b2b8de12698082609ebbc1a17165727f4c893
来源:https://stackoverflow.com/questions/22540918/does-linux-provide-a-guaranteed-inaccessible-memory-area-below-the-lower-stack-e