这个问题的灵感来自 pthread_attr_setguardsize
和 pthread_attr_getguardsize
pthread API 中的方法。该方法的文档可以在 here 中找到。 .
在这些方法的文档中,我最接近于找到对实际防护大小的解释是这样的:
The guardsize attribute controls the size of the guard area for the created thread's stack. The guardsize attribute provides protection against overflow of the stack pointer. If a thread's stack is created with guard protection, the implementation allocates extra memory at the overflow end of the stack as a buffer against stack overflow of the stack pointer. If an application overflows into this buffer an error shall result (possibly in a SIGSEGV signal being delivered to the thread).
我相信这对某人来说很有意义,但我仍然有点迷茫。
我知道守卫是堆栈末尾的溢出缓冲区,旨在防止堆栈溢出。但是我还有两个问题:
我确信有一些简短的、概念性的东西可以回答这两个问题,我希望这里有人能够提供它。谢谢大家!
附言我终于可以用“堆栈溢出”标记一个问题,这也非常有趣。
最佳答案
下一个问题是线程堆栈之后是什么。
它可以是任何东西。它可能是另一个线程的栈顶,堆的一部分。一些内存映射文件等。
如果没有保护内存,线程可能会将其堆栈溢出到该区域,并且如果该内存是可写的,则不会阻止线程覆盖该内存而不会产生任何错误/陷阱/信号。
您可以分配更大的堆栈。但是多大才足够大?如果分配太大,则意味着其他任何东西的可用内存较少(尤其是在内存空间非常有限的 32 位系统上)
一个 guard 区试图解决这个问题。该区域将被标记为不可写,因此如果有人尝试写入该区域(即线程溢出其堆栈),内核将在硬件协助下收到通知,向进程发送信号并(默认情况下)终止它。
与冒着让线程默默地破坏不应该破坏的内存的风险相比,杀死进程通常更好。
关于memory - Posix 中的防护尺寸是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34802140/