Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

c99 - How many memory pages do C compilers on desktop OSes use to detect stack overflows?

This question is related to but different from this one about variable length arrays in C99.

The answers point out that one danger with allocating variable length arrays (or just large arrays of a fixed size) in the stack is that the allocation may fail silently, as opposed to, say, calling malloc, which explicitly tells the caller whether allocation succeeded.

Modern non-embedded compilation platforms use an invalid memory zone to detect some stack overflows at no additional cost (the checks are only the checks already made for free by the MMU). This doesn't protect at 100% from the above problem because a very large local array may cause the stack pointer to jump over the invalid area.

Does any one know how many pages are typically allocated for this detection? I guess it would be at least 4KiB, but it could be more. Is that a choice made by the compiler or the OS, and in either case, is there a way to change it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm pretty sure the most common practice is using just one page, usually 4k. A good compiler, however, will sequentially attempt to access each page of a stack frame larger than the page size on function entry (or on VLA/alloca allocation) to ensure that a guard page is hit. GCC can optionally do this; see: http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options and the -fstack-check option.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...