为什么堆栈溢出会导致分段错误,而不是Linux中的堆栈溢出? [英] Why stack overflow causes segmentation fault instead of stack overflow in Linux?

查看:275
本文介绍了为什么堆栈溢出会导致分段错误,而不是Linux中的堆栈溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
分段错误之间有什么区别和堆栈溢出?

Possible Duplicate:
What is the difference between a segmentation fault and a stack overflow?

我只是想知道,为什么堆栈溢出会导致分段错误而不是堆栈溢出.

I was just wondering, why stack overflow results in segmentation fault instead of stack overflow.

是因为越过了堆栈限制的边界导致了SIGSEGV?为什么我们在Linux中没有遇到堆栈溢出,而是分段错误?

Is it because the boundary of stack limit is crossed which causes SIGSEGV? Why we don't encounter stack overflow in Linux, and rather a segmentation fault?

int foo()
{
  return foo();
}

这个小代码应该会导致堆栈溢出,但是会导致Linux中的分段错误.

This small code should cause stack overflow but rather it causes segmentation fault in Linux.

推荐答案

堆栈溢出会导致几种不同类型的硬件错误.

A stack overflow can cause several different kinds of hardware errors.

  • 这可能会导致尝试访问该程序没有适当权限的内存→内核将为该进程发出一个SIGSEGV(违反分段)信号.
  • 这可能会导致尝试执行一条非法指令(例如:您重写了返回地址以指向无效指令)→内核将发出一个SIGILL(非法指令)信号.
  • 在某些平台上可能是SIGBUS(例如对齐异常).
  • It may lead to an attempt to access memory for which the program has no appropriate permissions → the kernel will raise a SIGSEGV (segmentation violation) signal for the process.
  • It may lead to an attempt to execute an illegal instruction (e.g: you overwrote the return address to point to an invalid instruction) → the kernel will raise a SIGILL (illegal instruction) signal.
  • Probably SIGBUS on some platforms (e.g: alignment exception).

所有这些错误在堆栈溢出后 发生.一种选择是添加堆栈溢出保护(ProPolice,...),以便在堆栈溢出引起更严重的问题之前捕获它们.

All these errors occur after the stack overflow. An option is to add stack overflow protections (ProPolice, ...), so as to catch stack overflows before they cause more serious problems.

您的意思是实际堆栈溢出".好吧,这种情况由SEGV覆盖(尝试访问该进程没有权限的内存),因此它获得了SEGV,而不是对更普通的SEGV的每种情况都进行特殊包装.

You mean a "real stack overflow". Well, this case is covered by SEGV (trying to access memory for which the process has no permissions), so it gets a SEGV, instead of special-casing every single case of the more general SEGV.

这篇关于为什么堆栈溢出会导致分段错误,而不是Linux中的堆栈溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆