桌面操作系统上的 C 编译器使用多少内存页来检测堆栈溢出?
这个问题与this one有关C99中的可变长度数组相关但不同。
答案指出,在堆栈中分配可变长度数组(或只是固定大小的大数组)的一个危险是分配可能会默默失败,而不是调用malloc
,后者明确告诉调用者分配是否成功。
现代非嵌入式编译平台使用无效内存区域来检测某些堆栈溢出,而无需额外成本(这些检查只是 MMU 已经免费进行的检查)。这并不能 100% 避免上述问题,因为非常大的本地数组可能会导致堆栈指针跳过无效区域。
有谁知道通常为该检测分配多少页?我猜它至少是 4KiB,但也可能更多。这是编译器还是操作系统做出的选择,在这两种情况下,有没有办法改变它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定最常见的做法是仅使用一页,通常是 4k。然而,一个好的编译器会顺序尝试访问大于函数入口(或 VLA/alloca 分配)页面大小的堆栈帧的每个页面,以确保命中保护页面。 GCC 可以选择这样做;请参阅: http://gcc.gnu.org /onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options 和
-fstack-check
选项。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.在 Windows 上,它是一个 4KB 页面(至少在 x86 上):请参阅基于 Windows NT 的应用程序的堆栈检查说明 。
对于 GCC,GCC 堆栈检查
我不确定 C99 的 VLA 如何/是否会改变 WinNT行为。
On Windows, it's one 4KB page (at least on x86): See Description of the stack checking for Windows NT-based applications.
For GCC, GCC Stack checking
I'm not sure how/if C99's VLA's would change the WinNT behaviour.