简体   繁体   English

当我堆栈溢出时,操作系统如何防止崩溃?

[英]How do OSes keep themselves from crashing when I stack overflow?

当我的一个程序意外泄漏内存或堆栈溢出时,操作系统使用哪些方法来防止崩溃或不稳定的行为?

Briefly: Memory management. 简而言之:内存管理。

Typically each process is allocated a limited (but usually adjustable) amount of stack space, so a single process can't use up enough to cause problems for the system as a whole. 通常,每个进程都分配有限(但通常是可调整的)堆栈空间量,因此单个进程不能用尽足以导致整个系统出现问题。

And if a process attempts to access memory outside what's been allocated for it, that will (at worst) crash the process itself; 如果一个进程试图在为其分配的内存之外访问内存,那么(最坏的情况)会使进程本身崩溃; this frees up the resources allocated for that process without stepping on other processes. 这样可以释放为该进程分配的资源,而无需踩到其他进程。

OSes don´t generally protect from memory leaks in your program; 操作系统通常不会防止程序中的内存泄漏; but once your application ends all its memory is reclaimed. 但是一旦你的应用程序结束,它的所有内存都会被回收。 If your application never ended, then the OS would eventually get into trouble when it runs out of memory. 如果您的应用程序永远不会结束,那么当内存不足时,操作系统最终会遇到麻烦。

Regarding stack overflows, they can detect that you have gone through your stack size. 关于堆栈溢出,它们可以检测到您已经完成了堆栈大小。 A posibility is to flag a few pages after the stack as protected memory, if you try to access it then you will get a segfault and your program will be terminated. 一个可能性是在堆栈之后标记几页作为受保护的内存,如果您尝试访问它,那么您将得到段错误并且您的程序将被终止。

Very good question, thanks for asking. 非常好的问题,谢谢你的提问。 There are three issues that I can think of off the bat. 我可以想到三个问题。 And, for each issue, there are two cases. 而且,对于每个问题,有两种情况。

Stack overflows: If your program is written in anything but assembly language, the OS can detect stack overflow because all stack operations are software operations . 堆栈溢出:如果程序是用汇编语言编写的,则操作系统可以检测堆栈溢出,因为所有堆栈操作都是软件操作 The run-time system manages the software stack and knows when overflow happens. 运行时系统管理软件堆栈并知道何时发生溢出。

If you have taken the trouble to write your program in assembly language and you pop the hardware stack in error, well, the OS can't save you. 如果您已经用汇编语言编写程序并且错误地弹出硬件堆栈 ,那么操作系统无法保存您。 Bad things can happen. 坏事可能发生。

Out-of-bounds memory accesses: When your C++ program starts, the OS sets memory bounds on your behalf into the CPU. 越界内存访问:当您的C ++程序启动时,操作系统会代表您将内存限制设置到CPU中。 If your program tries to access memory outside those bounds, the CPU raises a hardware interrupt . 如果程序试图访问这些边界之外的内存,则CPU会引发硬件中断 The OS, as it handles the interrupt, can tell you that your program has misbehaved. 操作系统在处理中断时可以告诉您程序出现异常。 This is what happens when you try to dereference a NULL pointer, for example. 例如,当您尝试取消引用NULL指针时会发生这种情况。

Your assembly-language program, though, can try to read or write from/into whatever memory it feels like. 但是,您的汇编语言程序可以尝试从/读入任何内存中读取或写入。 If your program is polite and was started by the OS in the usual way, then the OS can catch that error. 如果您的程序是礼貌的并且由操作系统以通常的方式启动,则操作系统可以捕获该错误。 But if your program is evil and somehow started outside the purview of the OS, it can do some real damage. 但是如果你的程序是邪恶的并且以某种方式开始在操作系统的范围之外,它可以做一些真正的损害。

Memory Leaks: Sorry, nobody can help you here. 内存泄漏:对不起,没有人可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM