BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using DelphiSpeedUp hooks in package-based apps

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language BASM
View previous topic :: View next topic  
Author Message
Dave Nottage [TeamB]
Guest





PostPosted: Tue Apr 25, 2006 1:03 pm    Post subject: Using DelphiSpeedUp hooks in package-based apps Reply with quote



I've been fiddling around with Andreas Hausladen's DelphiSpeedUp code
to use it to hook FastCode functions into rtl.bpl in my own
package-based applications (as opposed to the IDE).

I've split the Utils unit out into FastCodeRTLUtils, because Utils has
dependencies on "visual" units such as StdCtrls etc, and made a
modification to FastRTL, since my app won't be hooking into the Delphi
IDE.

I've posted the resulting source to the .attachments group, including
the other required files, for completeness. RSFastCode.pas is a unit I
include in the project source (as the first named unit) to initialize
the rtl hooking.

I've had success with it in a Delphi 7 application, however a similar
app (the app is being restructured) in Delphi 2006 crashes when it
calls FastCodeStrLen.

Tracing the code, I have found the parameter being passed to
FastCodeStrLen is an empty string, and more notably, the value for eax
is 1, causing an access violation on the first line of code in
FastCodeStrLen.

I apologise for not having a complete test case, however I wondered
whether anyone might have a clues as to what is going wrong.

--
Dave Nottage [TeamB]
Have questions?: http://www.catb.org/~esr/faqs/smart-questions.html
Want answers?: http://support.borland.com
Back to top
Dave Nottage [TeamB]
Guest





PostPosted: Tue Apr 25, 2006 2:03 pm    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote



Dennis wrote:

Quote:
I think that this must be an illegal pointer.

One would assume so <g>

I've completed a test case, and posted it to .attachments. It includes
the files previously posted.

To test, you'll need to have a database to connect to with ADO, such as
SQL Server or MSDE, and modify the connection string in MainFrm.pas.

--
Dave Nottage [TeamB]
Have questions?: http://www.catb.org/~esr/faqs/smart-questions.html
Want answers?: http://support.borland.com
Back to top
Dennis
Guest





PostPosted: Tue Apr 25, 2006 2:03 pm    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote



Hi Dave

All functions pass this test

function TMainForm.Validate1 : Boolean;
var
I : Cardinal;
Str : PChar;
const
VALIDATENO : Cardinal = 1;

begin
Str := '';
try
Result := True;
I := StrLenFunction(Str);
if I <> 0 then
begin
ErrorTrap(VALIDATENO, Str);
Result := False;
end;
except
ErrorTrapException(VALIDATENO, Str);
Result := False
end;
end;

So all functions will handle an empty string correctly.

Quote:
Tracing the code, I have found the parameter being passed to
FastCodeStrLen is an empty string, and more notably, the value for eax
is 1, causing an access violation on the first line of code in
FastCodeStrLen.

Then you pass in a PChar pointing to address 1. I think that this must be an
illegal pointer.

Best regards
Dennis Kjaer Christensen
Back to top
Dennis
Guest





PostPosted: Tue Apr 25, 2006 3:03 pm    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

Hi Dave

Quote:
I think that this must be an illegal pointer.

One would assume so <g

And the bug must be in the code that calls StrLen and not in StrLen.

Quote:
To test, you'll need to have a database to connect to with ADO, such as
SQL Server or MSDE, and modify the connection string in MainFrm.pas.

To complicated for my taste ;-)

Best regards
Dennis Kjaer Christensen
Back to top
Dave Nottage [TeamB]
Guest





PostPosted: Tue Apr 25, 2006 10:03 pm    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

Dennis wrote:

Quote:
And the bug must be in the code that calls StrLen and not in StrLen.

I figured that, too.

Quote:
To complicated for my taste Wink

Thanks for listening anyway <g>

--
Dave Nottage [TeamB]
Have questions?: http://www.catb.org/~esr/faqs/smart-questions.html
Want answers?: http://support.borland.com
Back to top
Dennis
Guest





PostPosted: Wed Apr 26, 2006 8:03 am    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

Hi Dave

Let us hear what you find out.

Best regards
Dennis Kjaer Christensen
Back to top
Dan Downs
Guest





PostPosted: Wed Apr 26, 2006 7:03 pm    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

Here's where I've traced it to:

Inside: DBmsLPCn.ConnectionOpen

Before this call eax is "0012AE58"
74CD146A FF154830CD74 call dword ptr [$74cd3048] calls
kernel32.GetComputerNameA
after its "1"


74CD146A FF154830CD74 call dword ptr [$74cd3048] calls to
kernel32.GetComputerNameA
74CD1470 837D0C00 cmp dword ptr [ebp+$0c],$00
74CD1474 740A jz $74cd1480
74CD1476 8B4D0C mov ecx,[ebp+$0c]
74CD1479 0FBE11 movsx edx,[ecx]
74CD147C 85D2 test edx,edx
74CD147E 7519 jnz $74cd1499
74CD1480 683041CD74 push $74cd4130
74CD1485 8D857CF3FFFF lea eax,[ebp-$00000c84]
74CD148B 50 push eax
74CD148C E871150000 call $74cd2a02
74CD1491 83C408 add esp,$08
74CD1494 E970010000 jmp $74cd1609
74CD1499 8B4D0C mov ecx,[ebp+$0c]
74CD149C 51 push ecx
74CD149D E85A150000 call $74cd29fc calls FastCodeStrLen with eax = 1
and dies

DD
Back to top
Dan Downs
Guest





PostPosted: Wed Apr 26, 2006 8:03 pm    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

A little more searching, when removing the fastcodertl hooking from the test
app it calls the following function for StrLen. When entering the function
eax is still 1, but it looks like ecx is being used instead of eax, or at
least eax doesn't contain the pointer to the string variable. So I'm
wondering if the hooking is doing its job a little too well.

DD

msvcrt.strlen:
77BD8760 8B4C2404 mov ecx,[esp+$04]
77BD8764 F7C103000000 test ecx,$00000003
77BD876A 7424 jz $77bd8790
77BD876C 8A01 mov al,[ecx]
77BD876E 83C101 add ecx,$01
77BD8771 84C0 test al,al
77BD8773 744E jz $77bd87c3
77BD8775 F7C103000000 test ecx,$00000003
77BD877B 75EF jnz $77bd876c
77BD877D 0500000000 add eax,$00000000
77BD8782 8DA42400000000 lea esp,[esp+$0000]
77BD8789 8DA42400000000 lea esp,[esp+$0000]
77BD8790 8B01 mov eax,[ecx]
77BD8792 BAFFFEFE7E mov edx,$7efefeff
77BD8797 03D0 add edx,eax
77BD8799 83F0FF xor eax,-$01
77BD879C 33C2 xor eax,edx
77BD879E 83C104 add ecx,$04
77BD87A1 A900010181 test eax,$81010100
77BD87A6 74E8 jz $77bd8790
77BD87A8 8B41FC mov eax,[ecx-$04]
77BD87AB 84C0 test al,al
77BD87AD 7432 jz $77bd87e1
77BD87AF 84E4 test ah,ah
77BD87B1 7424 jz $77bd87d7
77BD87B3 A90000FF00 test eax,$00ff0000
77BD87B8 7413 jz $77bd87cd
77BD87BA A9000000FF test eax,$ff000000
77BD87BF 7402 jz $77bd87c3
77BD87C1 EBCD jmp $77bd8790
77BD87C3 8D41FF lea eax,[ecx-$01]
77BD87C6 8B4C2404 mov ecx,[esp+$04]
77BD87CA 2BC1 sub eax,ecx
77BD87CC C3 ret
77BD87CD 8D41FE lea eax,[ecx-$02]
77BD87D0 8B4C2404 mov ecx,[esp+$04]
77BD87D4 2BC1 sub eax,ecx
77BD87D6 C3 ret
77BD87D7 8D41FD lea eax,[ecx-$03]
77BD87DA 8B4C2404 mov ecx,[esp+$04]
77BD87DE 2BC1 sub eax,ecx
77BD87E0 C3 ret
77BD87E1 8D41FC lea eax,[ecx-$04]
77BD87E4 8B4C2404 mov ecx,[esp+$04]
77BD87E8 2BC1 sub eax,ecx
77BD87EA C3 ret
77BD87EB CC int 3
77BD87EC CC int 3
77BD87ED CC int 3
77BD87EE CC int 3
77BD87EF CC int 3
Back to top
Andreas Hausladen
Guest





PostPosted: Wed Apr 26, 2006 9:03 pm    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

Dan Downs wrote:

Quote:
msvcrt.strlen:

shoot at me. I mixed "register" with "cdecl".


--
Regards,

Andreas Hausladen
(http://andy.jgknet.de/blog)
Back to top
Dave Nottage [TeamB]
Guest





PostPosted: Thu Apr 27, 2006 1:03 am    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

Andreas Hausladen wrote:

Quote:
msvcrt.strlen:

shoot at me. I mixed "register" with "cdecl".

What exactly needs to be to changed? Some of the code is a little over
my head <g>

--
Dave Nottage [TeamB]
Have questions?: http://www.catb.org/~esr/faqs/smart-questions.html
Want answers?: http://support.borland.com
Back to top
Dave Nottage [TeamB]
Guest





PostPosted: Thu Apr 27, 2006 1:03 am    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

I wrote:

Quote:
What exactly needs to be to changed? Some of the code is a little over
my head <g

Never mind; I just read your post in .thirdpartytools.general. Thanks!

--
Dave Nottage [TeamB]
Have questions?: http://www.catb.org/~esr/faqs/smart-questions.html
Want answers?: http://support.borland.com
Back to top
Dave Nottage [TeamB]
Guest





PostPosted: Thu Apr 27, 2006 3:03 am    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

I wrote:

Quote:
I've been fiddling around with Andreas Hausladen's DelphiSpeedUp code
to use it to hook FastCode functions into rtl.bpl in my own
package-based applications (as opposed to the IDE).

OK.. now I have v1.6, however I can't even make the app run :-/

I've had this kind of problem before, however I'm at a loss as to what
the cause is/was.

Updated test case posted to .attachments.

--
Dave Nottage [TeamB]
Have questions?: http://www.catb.org/~esr/faqs/smart-questions.html
Want answers?: http://support.borland.com
Back to top
Dan Downs
Guest





PostPosted: Thu Apr 27, 2006 3:03 am    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

Quote:
OK.. now I have v1.6, however I can't even make the app run :-/

I've had this kind of problem before, however I'm at a loss as to what
the cause is/was.

I tried the new test app and it compiled and ran fine for me. I extracted it
to a new directory, loaded the project group, compiled the package, compiled
the app, ran it, then changed the connectstring recompiled ran and worked
fine.

Did the package get copied anywhere else on your system? Um...... I haven't
installed BDS2006 SP2 yet, wonder if there's a difference.

DD
Back to top
Dave Nottage [TeamB]
Guest





PostPosted: Thu Apr 27, 2006 5:03 am    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

Dan Downs wrote:

Quote:
Did the package get copied anywhere else on your system?

I have a feeling that's what it was :-/

If I run the app in the IDE, I get an AV on shutdown. Call stack:

MainPackage.Hooking.PeMapImgNtHeaders($76620000)

MainPackage.Hooking.TJclPeMapImgHooks.ReplaceImport($76620000,'msvcrt.dl
l',$3272EC,$77C472B0)
MainPackage.Hooking.TJclPeMapImgHookItem.InternalUnhook
MainPackage.Hooking.TJclPeMapImgHookItem.Destroy

at the line indicated:

function PeMapImgNtHeaders(const BaseAddress: Pointer): PImageNtHeaders;
begin
Result := nil;
if IsBadReadPtr(BaseAddress, SizeOf(TImageDosHeader)) then
Exit;
AV here ---> if (PImageDosHeader(BaseAddress)^.e_magic <>
IMAGE_DOS_SIGNATURE) or
(PImageDosHeader(BaseAddress)^._lfanew = 0) then
Exit;
Result := PImageNtHeaders(DWORD(BaseAddress) +
DWORD(PImageDosHeader(BaseAddress)^._lfanew));
if IsBadReadPtr(Result, SizeOf(TImageNtHeaders)) or
(Result^.Signature <> IMAGE_NT_SIGNATURE) then
Result := nil
end;

Since it doesn't happen outside the IDE, I'm not terribly concerned yet
<g>

--
Dave Nottage [TeamB]
Have questions?: http://www.catb.org/~esr/faqs/smart-questions.html
Want answers?: http://support.borland.com
Back to top
Dave Nottage [TeamB]
Guest





PostPosted: Thu Apr 27, 2006 5:03 am    Post subject: Re: Using DelphiSpeedUp hooks in package-based apps Reply with quote

I wrote:

Quote:
Did the package get copied anywhere else on your system?

I have a feeling that's what it was :-/

If I run the app in the IDE, I get an AV on shutdown.

Sheesh.. I don't get the AV in the "real" application. It's one of
those weeks, I think <g>

--
Dave Nottage [TeamB]
Have questions?: http://www.catb.org/~esr/faqs/smart-questions.html
Want answers?: http://support.borland.com
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language BASM All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.