c++中处理堆栈损坏的工具

时间:2022-09-01 12:00:32

EDIT: Due to a comment that was right about my example I removed it and turn this into a generic question:

编辑:由于对我的例子的评论是正确的,我删除了它并把它变成了一个通用的问题:

Some times in my projects I come across stack corruption. No matter how much I fight to write code to avoid it, sometimes it is just unavoidable. But when it happens what are the ways to fight it?

在我的项目中,我有时会遇到堆栈损坏。无论我多么努力地编写代码以避免它,有时这是不可避免的。但是当它发生的时候,有什么方法可以对抗它呢?

I found one macro given by the good fellow in this blog: http://rxwen.blogspot.com/2009/04/detect-stack-corruption.html which reads the ebp register value to detect corruption.

我在这个博客中找到了一个好人给出的宏:http://rxwen.blogspot.com/2009/04/dete-stack-corrupt tion.html,它读取ebp寄存器值以检测腐败。

But there are bound to be more sophisticated tools to help with not shooting yourself on the foot. I am programming in Windows using Codeblocks and the gcc compiler. The reason I make this question is to find tools which I can use under my programming environment to help me detect such mistakes and correct them. Any suggestions?

但是一定会有更复杂的工具来帮助你不被脚击中。我正在使用代码库和gcc编译器在Windows中编程。我提出这个问题的原因是为了找到我可以在我的编程环境下使用的工具,以帮助我发现这些错误并改正它们。有什么建议吗?

Thanks for any answers and for taking the time to read my question.

谢谢你的回答,感谢你抽出时间来阅读我的问题。

2 个解决方案

#1


4  

It's far from unclear that you are having stack corruption. But I accept there is some data corruption.

目前还不清楚你是否存在堆栈腐败。但我承认存在一些数据损坏。

A reasonably effective technique is to add guard fields around the suspect field(s):

一种合理有效的技术是在可疑字段周围添加保护字段:

...
long   namecheck1;
Artist artist;
long   namecheck2;
...

Have the constructor initialize these to most anything, but without knowing the nature of the corruption something non-zero seems more satisfying.

让构造函数初始化它们到几乎所有的东西,但是在不知道损坏的本质的情况下,非零的东西似乎更令人满意。

myclass::myclass() : namecheck1(0x12345678), namcheck2(0x12345678) ...

Add a consistency check member function:

添加一致性检查成员函数:

void myclass::isokay()
{
       if (namecheck1 != namecheck2  ||
           namecheck2 != 0x12345678)
             cerr << "the object is corrupted";
         ... // maybe wait for input, cause core dump, etc.
}

Then pepper the code with calls to this, especially near suspicious logic. If you are comfortable with a debugger, place a breakpoint on the error message. By unraveling the stack, you can ascertain what the program has done recently and gather clues as to what bit of code is probably writing outside the proper bounds.

然后在代码中加上对这个的调用,特别是接近可疑逻辑的调用。如果您熟悉调试器,请在错误消息上放置断点。通过解析堆栈,您可以确定程序最近做了什么,并收集关于哪些代码可能在适当范围之外编写的线索。

#2


1  

Valgrind finds all kinds of memory corruption.

Valgrind发现了各种各样的内存损坏。

GCC has mudflap (-fmudflap and friends) and -fstack-protector to catch memory access problems. Other compilers probably do, too.

GCC有mudflap (-fmudflap和friends)和-fstack- protected来捕获内存访问问题。其他编译器可能也会这样做。

#1


4  

It's far from unclear that you are having stack corruption. But I accept there is some data corruption.

目前还不清楚你是否存在堆栈腐败。但我承认存在一些数据损坏。

A reasonably effective technique is to add guard fields around the suspect field(s):

一种合理有效的技术是在可疑字段周围添加保护字段:

...
long   namecheck1;
Artist artist;
long   namecheck2;
...

Have the constructor initialize these to most anything, but without knowing the nature of the corruption something non-zero seems more satisfying.

让构造函数初始化它们到几乎所有的东西,但是在不知道损坏的本质的情况下,非零的东西似乎更令人满意。

myclass::myclass() : namecheck1(0x12345678), namcheck2(0x12345678) ...

Add a consistency check member function:

添加一致性检查成员函数:

void myclass::isokay()
{
       if (namecheck1 != namecheck2  ||
           namecheck2 != 0x12345678)
             cerr << "the object is corrupted";
         ... // maybe wait for input, cause core dump, etc.
}

Then pepper the code with calls to this, especially near suspicious logic. If you are comfortable with a debugger, place a breakpoint on the error message. By unraveling the stack, you can ascertain what the program has done recently and gather clues as to what bit of code is probably writing outside the proper bounds.

然后在代码中加上对这个的调用,特别是接近可疑逻辑的调用。如果您熟悉调试器,请在错误消息上放置断点。通过解析堆栈,您可以确定程序最近做了什么,并收集关于哪些代码可能在适当范围之外编写的线索。

#2


1  

Valgrind finds all kinds of memory corruption.

Valgrind发现了各种各样的内存损坏。

GCC has mudflap (-fmudflap and friends) and -fstack-protector to catch memory access problems. Other compilers probably do, too.

GCC有mudflap (-fmudflap和friends)和-fstack- protected来捕获内存访问问题。其他编译器可能也会这样做。