Re: Modifying default Win32 heap management.



"MJ" <art.oriented@xxxxxxxxx> wrote in message
news:1160637190.324654.304140@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello,

I'm doing some experiment about heap management on Windows. My goal
is intercepting Win32 default heap management calls and modifying its
behavior.

In Linux, what I have to do are rewriting and modifying
malloc/realloc/free functions, and intercepting these calls using
LD_PRELOAD. (Although, statically linked malloc call cannot be
intercepted.) However, such trial on Windows is quite challenge.

First, I tried to hook malloc/free calls in MSVCR*.dll by Detours of MS
Research. Then, in order to check the integrity of the hooking, I've
replaced these calls with another verified malloc implementation, Doug
Lea's 2.8.4 malloc sources, which is widely used in libc on Linux and
works on Win32 as well. Fortunately, this replacement worked without
crash (and it have to be). After, such verification, I will modify
malloc implementation for my purpose.

Applications using dynamically linked malloc/free calls can be hooked
by the above approach; however, otherwise I cannot hook the
application. There is no way to hook statically linked malloc call or
direct HeapAlloc call.

Thus, I tried to directly hook
Heap{Create/Destroy/Alloc/Free/ReAlloc/Size/Validate} functions in
Kernel32.dll in order to have more comprehensive solution.

Hooking was fine, however, mapping these Heap* interfaces to malloc one
is really difficult. The hooked application never runs successfully. It
crashed with "The application failed to initialize properly
(0xC0000142)" message.

In Heap*, there are HeapCreate/Destroy functions which seem a little
bit wired. These kind calls does not exist in malloc call. Thus, I just
ignored to hook these functions, and only intercept
HeapAlloc/ReAlloc/Free/Size/Validate calls. In these calls, I ignored
given "HANDLE hHeap", and carefully mapped HEAP_* flags semantic.
Also, in HeapFree/Validate, I used another data structure to check
given pointer. Only after confirming this given parameter was allocated
by my implementation, I freed the pointer. (The data structure does not
use hooked memory allocation calls.)

Desperately, I debugged for much time, but I cannot find why it does
not work. I also hooked related Rtl* calls and Global* calls and tried
to tweak HeapCreate/Destroy. But failed.


Is there anything that I missed or mistaken? I'd really appreciate
any advice and suggestion.


ps. Now, I'm trying to use codes from WINE project, which implemented
ntdll.dll for Linux environment. I really hope this trial would give
success. If this also fails, I only have to hook malloc/free calls.


Depending on CRT implementation, malloc/free may, or not, map to
HeapAlloc/Free. In other words, don't assume that malloc calls HeapAlloc. It
may go direct to virtual-memory (VirtualAlloc) and bypass the win32 heap
altogether. In fact, on my compiler, it does exactly that.

--
James Brown
Microsoft MVP - Windows SDK
www.catch22.net
Free Win32 Tutorials and Sourcecode





.



Relevant Pages

  • Re: trap in malloc on 2000 terminal server
    ... For the error in the debugger extension, looks like you have an old debugger ... some operation that has an undefined behavior beforehand. ... On average, heap corruption shows up as crashes in random places, ... What is the logic of malloc? ...
    (microsoft.public.win32.programmer.kernel)
  • Re: trap in malloc on 2000 terminal server
    ... For the error in the debugger extension, looks like you have an old debugger ... some operation that has an undefined behavior beforehand. ... On average, heap corruption shows up as crashes in random places, ... What is the logic of malloc? ...
    (microsoft.public.win32.programmer.tools)
  • Re: What does SZ on the ps output mean?
    ... originally assumed that no matter how many malloc and frees were done, ... there shouldn't be an upward trend. ... hope of spotting any memory leak. ... examine the java heap as mgd by the JVM. ...
    (comp.unix.solaris)
  • Malloc, free and thread-safety
    ... I get intermittent exceptions such as: ... HEAP[recv.exe]: HEAP: Free Heap block 316d978 modified at 316d988 after it ... I've checked carefully that malloc() and freeare ... The exceptions are infrequent and intermittent for basically the same traces ...
    (microsoft.public.dotnet.languages.vc)
  • Re: segmentation fault on calloc
    ... the heap is used when you use malloc (or ... making care of knowing that), since when I want to use memory, that is not ... > the end of an allocated block, it is almost certain to overwrite some ...
    (comp.unix.programmer)