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

感情迁移 提交于 2019-12-13 12:29:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!