Dedecting stack corruption tools

delphi package - automated exception handling
Post Reply
mattg
Posts: 17
Joined: Thu Apr 27, 2017 11:25 pm

Dedecting stack corruption tools

Post by mattg »

Anyone have any tools to help with detecting stack errors. Compilers other than delphi have options to help with this which seems to be very useful.

Having guard variables (a known byte pattern) before and after each stack variable would be one way of knowing if you are getting overwrites on the stack. Seems like an obvious thing to do but cannot find any examples of this for delphi. Sure we could do this manually but it seems like a low level option should be available.

Is it possible to hook the creation of a variable in the stack?

Looks like it was requested some time ago, but Delphi is lacking here:

http://qc.embarcadero.com/wc/qcmain.aspx?d=82567
mattg
Posts: 17
Joined: Thu Apr 27, 2017 11:25 pm

Re: Dedecting stack corruption tools

Post by mattg »

well after much soul searching and code banging tracked the issue down to:

Result.AddPair('EmailAddresses',Self.EmailAddresses.CommaText);

Where "EmailAddresses" is a TStringList and was nil, the "Result" object is a TJSONObject.

It generated an "access violation" error, this exception was caught internally ok but smashed some memory somewhere, i think on the stack which is near impossible to fault find with Delphi.

I thought an "access violation" meant that protected or unaddressed (nill pointer) memory was attempted to be accessed but failed - but not slay other variables in this situation.

As this was reading a property would not have thought this would lead to Heap or Stack corruption, what was worse is the effect would be delayed (thinking the issue then moved to the heap somehow), the app would freeze indefinitely some time after depending on the users actions (upto a day or more later with no other faults) even with madexcept in place and monitoring the main thread it would lock up the app with no bug report.

Wish there was a way of adding a couple of bytes before and after each variable that could be checked when they are freed on the heap or better still the stack, at least we have some chance of picking up an error.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Dedecting stack corruption tools

Post by madshi »

Detecting stack corruption is not easily possible in Delphi. However, as you say, a *read* access violation should not normally corrupt anything. I'm not sure what happened there in your situation. Something weird going on.

You say the AV was caught internally ok. So what did you do with this exception? Did you silently surpress it? Or why did not have to do much soul searching to find the problem? You should never silently surpress access violations, for obvious reasons.
Post Reply