开发者

Debug stack corruption

Now I am debugging a large project, which has a stack corruption: the application fails.

I would like to know how to find (debug) such stack corruption code with Visual Studio 2010?

Here's an example of some code which causes stack problems, how would I find less obvious cases of this type of corruption?

void foo()
{
    int i = 10;
    int *p = &i;
    p[-2] = 100;
}

Update

Please note that this is just an example. I need to find such bad code in the current proj开发者_如何学Cect.


There's one technique that can be very effective with these kinds of bugs, but it'll only work on a subset of them that has a few characteristics:

  • the corrupting value must be stable (ie., as in your example, when the corruption occurs, it's always 100), or at least something that can be readily identified in a simple expression
  • the corruption has to occur at a particular address on the stack
  • the corrupting value is unusual enough that you won't be hit with a slew of false positives

Note that the second condition may seem unlikely at first glance because the stack can be used in so many different ways depending on the runtime actions. However, stack usage is generally pretty deterministic. The problem is that a particular stack location can be used for so many different things that the problem is really item #3.

Anyway, if your bug has these characteristics, you should identify the stack address (or one of them) that gets corrupted, then set a memory breakpoint for a write to that address with a condition that causes it to break only if the value written is the corrupting value. In visual Studio, you can do this by creating a "New Data Breakpoint..." in the Breakpoints window then right clicking the breakpoint to set the condition.

If you end up getting too many false positives, it might help to narrow the scope of the breakpoint by leaving it disabled until some point in the execution path that's closer to the bug (if you can identify such a time), or set the hit count high enough to remove most of the false positives.

An additional complication is the address of the stack may change from run to run - in this case, you'll have to take care to set the breakpoint on each run (the lower bits of the address should be the same).


I believe your Questions quotes an example of stack corruption and the question you are asking is not why it crashes.

If it is so, It crashes because it creates an Undefined Behavior because the index -2 points to an unknown memory location.

To answer the question on profiling your application:
You can use Rational Purify Plus for Visual studio to check for memory overrites and access errors.


This is UB: p[-2] = 100;

You can access p with the operator[] in this(p[i]) way, but in this case i is an invalid value. So p[-2] points to an invalid memory location and causes Undefined Behaviour.

To find it you should debug your app and find where it crashes, and hopefully, it'll be at a place where something is actually wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜