gcc 4.4.4 优化错误仅适用于 O1 或 O2+无严格别名

发布于 2024-12-13 05:58:10 字数 964 浏览 4 评论 0原文

接受后摘要:问题是使用了指向超出范围的堆栈变量的指针。它与优化无关。遗憾的是 valgrind 找不到堆栈错误...


我有一个段错误,只有在 gcc 4.4.4 (CentOS 5.5) 中启用 -O1 级别优化时才会出现。所有其他优化级别(0、2、3、s)都很好。我还没有设法为它创建一个简化的测试用例,但它似乎与导致堆栈被覆盖的数组偏移计算有关。

如果我启用 -O1 并禁用具有标志的所有优化 (http:// gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)该错误仍然存​​在。

如果我使用-O2(或任何其他级别)就没有问题。如果我使用 O2 并使用 -fno-strict-aliasing 禁用严格别名,则会返回段错误。

编辑:如果我将 -fstack-protector-all 添加到构建标志(O1O2 -fno-strict-aliasing)段错误消失。

因此,这似乎是由 O1 中默认发生的优化引起的,该优化被严格别名禁用。

我怀疑这是一个编译器错误(但如果没有减少的测试用例,我无法证明这一点)。这是一个需要快速周转的生产服务器。正常的优化级别是 O1,我不愿意将其更改为 O2,因为看起来修复可能比原始问题更危险。

我真的很感激一些建议。目前我正在考虑尝试编译 gcc 4.4.6 并看看是否可以修复它。然而,不确定是什么原因导致了这个问题,这有点令人担忧。

编辑:服务器是用-Wall -Werror(以及其他一些)编译的。它在 valgrind 中运行没有错误(valgrind 检查堆访问,这似乎是与堆栈相关的错误)。

Post-Answer-Acceptance Summary: The problem was the use of a pointer to a stack variable that had gone out of scope. It had nothing to do with optimization. It is a pity that valgrind can't find stack errors...


I have a segfault that appears only when enabling -O1 level optimization in gcc 4.4.4 (CentOS 5.5). All other optimization levels (0,2,3,s) are fine. I haven't managed to create a reduced test case for it yet, but it appears to be related to an array offset calculation causing the stack to be overwritten.

If I enable -O1 and disable all optimizations that have a flag (http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html) the bug still occurs.

If I use -O2 (or any other level) there is no problem. If I use O2 and disable strict-aliasing with -fno-strict-aliasing then the segfault returns.

Edit: If I add -fstack-protector-all to the build flags (either O1 or O2 -fno-strict-aliasing) the segfault disappears.

So it appears to be caused by an optimization that happens by default in O1 that is disabled by strict-aliasing.

I suspect that this is a compiler bug (but without a reduced testcase I can't prove it). This is a production server that needs a quick turn around. The normal optimization level is O1 and I'm loathe to just change it to O2 as it seems that the fix might be more dangerous than the original problem.

I would really appreciate some suggestions. Currently I'm thinking to try compiling gcc 4.4.6 and seeing if that fixes it. However not knowing for sure what is causing the problem is a little worrying.

Edit: the server is compiled with -Wall -Werror (and a few others). It runs without error in valgrind (valgrind checks heap accesses and this appears to be a stack related error).

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

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

发布评论

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

评论(1

独﹏钓一江月 2024-12-20 05:58:10

通常,编译器优化可能会暴露源代码中无效或未定义的行为,否则您很幸运能够正常工作。我会尝试一些事情:

  1. 使用在 valgrind 下运行的 -Wall -Wextra 进行编译,
  2. 看看是否可以获得更多关于错误在哪里
  3. 找到最小测试用例的提示!

Often, compiler optimizations can expose invalid or undefined behavior in source code, that you are lucky to get to work otherwise. A few things I would try:

  1. compiling with -Wall -Wextra
  2. running under valgrind to see if you can get more of a hint of where the error is
  3. finding that minimum testcase!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文