为什么我们在函数的返回地址中使用堆栈?

huangapple go评论78阅读模式
英文:

Why do we use the stack for the return address of a function?

问题

我有点理解栈帧的工作原理。
为什么要用它们来存储返回地址?看起来这就是缓冲区溢出发生的原因。
分配一个特定的内存区域来仅存储返回地址,不与栈完全分离,会更安全吗?

英文:

I kind of understand how stack frames work.
Why do we use them to store the return address? It looks like that is why buffer overflows happen.
Wouldn't it be more secure to allocate a certain memory region to just keep return addresses, fully separated from the stack?

答案1

得分: 1

实际上,这是许多Forth实现的工作方式,它们有一个返回栈和一个数据栈。

然而,我不知道有哪些主流处理器在硬件上做到这一点(除了可能是很久以前的基于Forth的处理器)。

它们通常只有一个用于两种目的的

无论如何,栈溢出只是缓冲区溢出的一个可能后果。它并不是存储返回地址引起缓冲区溢出,而是后者损坏了前者。即使你保持返回地址分开,缓冲区溢出仍然会损坏与返回地址无关的数据。

有人会说,这甚至更糟糕,因为在堆栈破坏的情况下,你可能会很快崩溃,因为你的函数返回到某个随机的内存位置。

保护返回信息将阻止这种情况发生,然后那些受损的数据将自由地在后面的某个地方引发更多问题 为什么我们在函数的返回地址中使用堆栈?

英文:

Actually, that is the way many Forth implementations work, they have a return stack as well as a data stack.

However, I know of no mainstream processors that do this same thing in hardware (except possibly the Forth-based ones from many moons ago).

They tend to have just the one stack that is used for both purposes.

In any case, stack-smashing is only one possible consequence of buffer overflows. It's not the storing of the return addresses that causes buffer overflows, it's the latter that corrupts the former. Even if you kept return addresses separate, buffer overflows would still corrupt data unrelated to return addresses.

Some would say that was even worse since, with stack smashing, you probably crash quickly because your function returns to some random memory location.

Protecting the return information would stop this from happening and then that corrupted data would be free to cause you more issues down the line somewhere 为什么我们在函数的返回地址中使用堆栈?

huangapple
  • 本文由 发表于 2023年7月23日 20:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定