PDA

View Full Version : Strange behavior of EL at AV in DLL


Yams
14-Oct-2008, 09:18 AM
Hello.

I have a DLL and a function that called from main app. Main app is build without EL and I have no source code.
DLL is my project, developed in D2007, EL 6.0.17 is enabled.

the func roughly is like that:

var Run: procedure;

procedure gg; stdcall;
begin
try
...
Run; // here: Run = nil => EAccessViolation here.
...
except
on E: Exception do
ShowLastExceptionData;
end;
end;

When I run this stuff EL show me 'unknown error' with 'unknown message'. Expected is 'EAccessViolation' with 'Access violation at ...'.

I have traced this issue to:

procedure Call_HookedRtlUnwind(TargetFrame, TargetIp: Pointer;
ExceptionRecord: PExceptionRecord);
...
begin
...
Obj := OldExceptObjProc(ExceptionRecord);
try
GoNotify(Obj, Addr, False, True); // here Obj saved to LastExceptionObject
finally
Obj.Free; // and now we free it???
end;
...
end;


Here obj = EAccessViolation object, Addr = nil.
GoNotify sets LastExceptionObject with SetLastException.
So Obj will be saved in LastExceptionObject.
When we return from GoNotify object will be freed. But pointer in LastExceptionObject remains untouched and now points to garbage.

When later I call ShowLastExceptionData then IsValidObject inside GetLastExceptionObject detects that LastExceptionObject is real object. It is not true. Well, shit happens :) EL starts to generate error log and eventually revrite area to which LastExceptionObject points. So, when you call IsValidObject in GetExceptionType it returns False, since memory already rewritten. And now we got "unknown error" message.

Sorry, no demo today ;)

Yams
14-Oct-2008, 09:28 AM
BTW, if I run the function the second time, than memory for LastExceptionObject will not be overwritten (random again! :) ) and I will get normal message 'EAccessViolation'/'Access violation at ...' :D

admin
16-Oct-2008, 09:30 AM
Hi Yams,

I have just fixed this issue in the last 6.0.18 RC 1.

Try it and tell back to me your results, OK?

PS: thank you for your detailed feedback! ;)

Yams
17-Oct-2008, 09:21 AM
I have tested with 6.0.18 RC 1 and everything looks fine. But I did not digging this issue much (sorry, no much time).
I mean, this is floating error. May be changes in code just hide this error :) May be fixed :) I don't know, but it looks like it is working.
If I'll run into this issue again - I'll report.

admin
17-Oct-2008, 10:30 AM
OK, perfect! :)

Yams
22-Oct-2008, 07:02 AM
Hello.

Look like I have another problem with the same appearance.

I have a DLL and a function that called from main app. Main app is written in Delphi, builded without EL and I have no source code.
DLL is my project, developed in D2007, EL 6.0.18 RC 1 is enabled.

the func roughly is like that:

var Run: procedure;

procedure gg; stdcall;
begin
try
...
Run; // Run <> nil. Run = function in application (not in my DLL). Run raises an exception.
...
except
on E: Exception do //
ShowLastExceptionData;
end;
end;

When I run this stuff EL show me 'unknown error' with 'unknown message'. Expected is anything except it :) (actual message is in Russian).

Now the problem is that EL does not call SetLastExceptionData or SetLastExceptionObject at all.

If I will modify code as:

...
except
on E: Exception do
begin
Application.HandleException(Application); // Shows correct message
ShowLastExceptionData; // Shows "Unknown error"/"Unknown message"
end;
end;


Is it a bug or feature? :)

admin
29-Oct-2008, 09:07 AM
Good question! :)

I'm working on, please to be patient.

admin
03-Dec-2008, 01:40 PM
Hi Yams,

I think to have finally found and fixed this issue in the just released 6.0.18 version.

Try it! :)

Yams
04-Dec-2008, 09:46 AM
Okay, works fine now.

P.S. Actually, I forgot which one of my real projects run into this issue, so I create a demo for this problem.

admin
05-Dec-2008, 11:18 AM
Hi Yams,

perfect! :)