输出重定向时保存堆栈指针会导致分段错误

发布于 2024-10-05 21:12:28 字数 177 浏览 0 评论 0原文

我正在编写一个 Sparc 编译器。我的一个测试用例正常运行良好,但当输出重定向到文件时崩溃。

使用GDB,我发现这是导致段错误的行:

save  %sp, -800, %sp

我的堆栈空间不足吗?这是怎么回事?为什么只有当我重定向输出时才会发生这种情况?

I'm writing a Sparc compiler. One of my test cases runs fine normally, but crashes when the output is redirected to a file.

Using GDB, I have found that this is the line that causes the segfault:

save  %sp, -800, %sp

Am I out of stack space? What's the deal? How come it only happens when I redirect the output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2024-10-12 21:12:28

SPARC 中的save 指令只能通过窗口溢出陷阱触发段错误。如果出现以下情况,就会发生这种情况:

  1. 您已经用完堆栈(和/或堆栈指针已损坏)
  2. 并且导致窗口溢出(即寄存器窗口写回刷新到堆栈)。

后者意味着事件的发生存在不可预测性。这是因为溢出的发生取决于先前的寄存器窗口使用情况 - 溢出的确切发生可能会通过分时共享同一 CPU 的其他进程所做的事情而改变。 Solaris 不会在每次上下文切换时自动溢出设置的整个注册表窗口,因为这会损害性能。例如,两个使用 8 个窗口(堆栈帧)的工作负载可能会愉快地互相抢占,并在具有 >= 16 个寄存器窗口的 CPU 上完全“无堆栈”运行。
我可以想象,由于输出重定向,溢出的可能性可能会增加(写入文件侧的堆栈比写入控制台侧更深的堆栈更有可能驱逐进程的注册表胜利)。

如果是这种情况,那么如果您将后台 CPU / 堆栈占用者(在循环中递归阶乘 200000,以永久废弃注册表窗口)绑定到测试用例正在处理的相同CPU。

A save instruction in SPARC can trigger a segfault only through window spill traps. That would happen if:

  1. you've run out of stack (and/or have a corrupted stackpointer)
  2. and cause a window spill (i.e. a register window writeback flush to stack).

The latter means there's an element of unpredictability in the occurance. That is because the occurance of spills depends on previous register window usage - the exact occurance of the spill can change through what other processes that timeshare the same CPU have been doing. Solaris will not auto-spill the entire reg window set on every context switch because that'd hurt performance. E.g. two workloads that use eight windows (stackframes) each might happily preempt each other and run fully "stack-free" on a CPU with >= 16 reg windows.
I can imagine that spill likelyhood might increase in due to output redirecting (deeper stacks on the write-to-file than write-to-console side are more likely to evict your process' reg wins).

If this is the case then you should be able to force consistent failure of your testcase even without the output redirect if you bind a background CPU / stack hogger (recursive factorial of 200000, in a loop, to permanently trash the reg windows) onto the same CPU where your testcase is processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文