Jump to content
Mahdi Safsafi

DDetours v2.2

Recommended Posts

Hi,

DDetours v2.2 is released. 

version 2.2(Jun 9, 2020):
  +Added support for older Delphi version: Now the minimal supported Delphi version is D7.
  +Added support for FPC.
  +Added recursive section feature: EnterRecursiveSection/ExitRecursiveSection.
  +Added param/tag feature for all InterceptCreate functions.
  +Added GetTrampolineParam function to get user param.
  +Added GetCreatorThreadIdFromTrampoline function to get thread id that created the hook/trampoline.
  +Added detection for non valid trampoline pointer.
  +Added unittest.
  +Replaced BeginHooks/BeginUnHooks by BeginTransaction.
  +Replaced EndHooks/EndUnHooks by EndTransaction.
  +Replaced GetNHook by GetHookCount.
  +Replaced TDetours<T> by TIntercept<T,U>/TIntercept<T>
  +Fixed many bugs related to MultiBytesNop.
  +Fixed wrong displacement value for some branch instructions on x64.
  +Fixed wrong offset size on x86 for GetJmpType function.
  +Removed v1 compatibility.
  +Now the library does not rely on Object.
  +Code refactoring.
https://github.com/MahdiSafsafi/DDetours

Some of the above features were planed for v3 that I started working on years ago. But I never get a chance to finish it. What a pity I'm rolling v2.2 instead of v3.2. :classic_sad:

  • Like 9
  • Thanks 2

Share this post


Link to post

Hi,

 

After implementing v2.2 and executing my own DUnitX tests I get a Memory Leak in Berlin.

I can reproduce this with Sydney and MadExcept, oddly no Memory Leak by simply setting `ReportMemoryLeaksOnShutdown := True` in Sydney..
In addition, turning on MadExcept Leak Test causes exceptions..

 

These changes where made to uTest to duplicate the issue:

var
  TDDetoursTrampolineAdd: TAdd = nil;
  
procedure TDDetours.Setup;
var LTransaction : THandle;
begin
  ReportMemoryLeaksOnShutdown := True;
  LTransaction := DDetours.BeginTransaction;
  TDDetoursTrampolineAdd := InterceptCreate(@Add, @InterceptAdd);
  DDetours.EndTransaction(LTransaction);
end;

procedure TDDetours.TearDown;
var LTransaction : THandle;
begin
  LTransaction := DDetours.BeginTransaction;
  InterceptRemove(@TDDetoursTrampolineAdd);
  DDetours.EndTransaction(LTransaction);
end;

 

allocation number: 11072
program up time: 1.07 s
type: GetMem
address: $2c9f820
size: 24
access rights: read/write

main thread ($b74):
671a5da7 madExcept32.dll madExceptDbg       1743 GetMemCallback
0041463e Test.exe        System            39806 GetMemory
006e4ce1 Test.exe        DDetours           2595 BeginTransaction
006ab856 Test.exe        DUnitX.TestRunner       TDUnitXTestRunner.Loggers_TeardownTest
006e5836 Test.exe        uTest               100 TDDetours.TearDown
006ab0ca Test.exe        DUnitX.TestRunner       TDUnitXTestRunner.ExecuteTestTearDown
006aadc7 Test.exe        DUnitX.TestRunner       TDUnitXTestRunner.ExecuteTests
006aa37e Test.exe        DUnitX.TestRunner       TDUnitXTestRunner.ExecuteFixtures
006aa3ab Test.exe        DUnitX.TestRunner       TDUnitXTestRunner.ExecuteFixtures
006aa010 Test.exe        DUnitX.TestRunner       TDUnitXTestRunner.Execute
006ee2ca Test.exe        Test                 48 initialization
7507343b kernel32.dll                            BaseThreadInitThunk

memory dump: 
02c9f820  34 0c 00 00 30 0e 00 00 - 9b 9e 73 f3 50 f8 f2 e9  4...0.....s.P...
02c9f830  9a 53 61 bd 93 e4 94 be  

 

Share this post


Link to post

@FredS Thanks man ! 

Indeed there is a leak and is not being reported by ReportMemoryLeaksOnShutdown. I don't use madExcept, but a quick debug suggested that there was no call to FreeMemory to clean up SuspendedThread array. 

I just committed a patch to fix the leak. 
Thanks again.

Share this post


Link to post
35 minutes ago, Mahdi Safsafi said:

a patch to fix the leak

 

Thanks, Works for me..

 

Unfortunately when I turn on MadExcept 'Instantly Crash on Over/Under-Run' I still get exceptions:

DUnitX - [DUnitX.TestDFX.exe] - Starting Tests.

.E.E.E.E.....E.E.E.E..

Tests Found   : 11
Tests Ignored : 0
Tests Passed  : 3
Tests Leaked  : 0
Tests Failed  : 0
Tests Errored : 8

Tests With Errors

  uTest.TDDetours.Test1
  Message: Access violation at address 0000000067104A2F in module 'madExcept64.dll'. Read of address 0000000000000000

  uTest.TDDetours.Test2
  Message: Access violation at address 0000000067104A2F in module 'madExcept64.dll'. Read of address 0000000000000000

  uTest.TDDetours.Test3
  Message: Access violation at address 0000000067104A2F in module 'madExcept64.dll'. Read of address 0000000000000000

  uTest.TDDetours.Test4
  Message: Access violation at address 0000000067104A2F in module 'madExcept64.dll'. Read of address 0000000000000000

  DFX.Win.Hooks.DUnitX.Tests.THooksTests.TestHookPublicMethod
  Message: Access violation at address 0000000067104A2F in module 'madExcept64.dll'. Read of address 0000000000000000

  DFX.Win.Hooks.DUnitX.Tests.THooksTests.TestHookPublicVirtualMethod.UseRtti
  Message: Access violation at address 0000000067104A2F in module 'madExcept64.dll'. Read of address 0000000000000000

  DFX.Win.Hooks.DUnitX.Tests.THooksTests.TestHookPublicVirtualMethod.UseMockup
  Message: Access violation at address 0000000067104A2F in module 'madExcept64.dll'. Read of address 0000000000000000

  DFX.Win.Hooks.DUnitX.Tests.THooksTests.TestHookPrivateVirtualMethod
  Message: Access violation at address 0000000067104A2F in module 'madExcept64.dll'. Read of address 0000000000000000

 

 

Edited by FredS

Share this post


Link to post
Quote

Unfortunately when I turn on MadExcept 'Instantly Crash on Over/Under-Run' I still get exceptions:

The issue was fired a long time ago. And its related to MadExcept and not to DDetours. DDetours on its current implementation rely on Delphi memory management (DMM). MadExcept hooks DMM and for some reason it reports fake buffer overrun/underrun when a memory page is marked as execute(at least, this is what I found when I installed MadExcept and did some investigation). 
For now, I made a temporary solution to work around. You need to update your DDetours version and define FIX_MADEXCEPT in DDetours.pas 

v3 would eventually have a new custom memory management that lets it run independently on DDM. 

Share this post


Link to post
1 hour ago, Mahdi Safsafi said:

define FIX_MADEXCEPT

 

You're like Superman.. faster than a speeding bullet 🙂

 

Thanks. Perfect!

  • Like 1

Share this post


Link to post

Doesn't compile with Delphi 7, 2005, 2006 and 2007. I have added issues with fixes to github for these. It's mostly that PNativeInt isn't declared for these older Delphi versions.

 

Also doesn't compile with Delphi 2009 due to a different issue, but I have not yet found a fix.

  • Like 1

Share this post


Link to post

The good news is that it compiles for Delphi XE2 and later (and the Demo works). Older versions need changes. I have added issues with proposed fixes on Github for them.

 

Unfortunately I didn't get it to compile with Delphi 6. I get an internal compiler error and I have no idea what to do about it.

Share this post


Link to post
21 hours ago, dummzeuch said:

I have added issues with proposed fixes on Github for them.

Thanks 🙂 

Quote

Unfortunately I didn't get it to compile with Delphi 6. I get an internal compiler error and I have no idea what to do about it.

It happened several time for me under different IDE with different libraries, I just noticed that it happens often when code is using .inc files. I used to do a clean and rebuild and that worked in many time. Sometime I need to restart the IDE.

Share this post


Link to post
4 minutes ago, Mahdi Safsafi said:

It happened several time for me under different IDE with different libraries, I just noticed that it happens often when code is using .inc files. I used to do a clean and rebuild and that worked in many time. Sometime I need to restart the IDE.

Unfortunately neither a rebuild nor restarting the IDE worked in this case. Even a command line compile with DCC failed.

I'm not that interested in Delphi 6 support, so I give up.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×