Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does linux provide a guaranteed inaccessible memory area below the lower stack end?

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?

like image 270
thejh Avatar asked Feb 21 '26 22:02

thejh


1 Answers

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.

like image 103
Shafik Yaghmour Avatar answered Feb 24 '26 10:02

Shafik Yaghmour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!