Is there a reliable way to learn that a memory page or a range of pages belongs to a specific DLL inside the address space of a process?
Depends on the type of page. The address of the code pages, etc of a dll are known when loaded, and viewable by looking at the "loaded modules" window or equivalent in any debugger.
If you're talking about a general read/write memory page, and I think you are, then I don't know of a way to find out what dll it "belongs" to - nor do I think there is a strict concept of ownership here.
Hello!
There are a method known as API hooking. Well known BugslayerUtil.DLL from John Robbins (see his book "Debugging Applications") war used originally as API hooking inside own process. I mean that all memory allocation can be allocated with respect of small number of well known functions like LocalAlloc
, GlobalAlloc
, VirtualAlloc
etc. One can overwrite start addresses of this functions inside of process address space. You can do this either somewhere at the beginning of the process or use DLL Injection to make this (like it do Dependency Walker in profiling mode). So you will be able to log (trace) every memory allocation attempt, forward the call to the original function, see the resulting return values log (trace) one more time and gives results back. Inside of every call attempt you can see all functions which called this one on the call stack. So the contain of the call stack together with the address of allocated memory and the size gives you full information for which your are looking for. You will see all in the dynamic.
You should not implement all stuffs yourself. Just search in internet for "API hooking" or "DLL injection" and you will find enough working examples. For examining of the call stack you can use documented StackWalk64 function (see http://msdn.microsoft.com/en-us/library/ms680650(VS.85).aspx) from imagehlp.dll / dbghelp.dll (for example see http://www.codeproject.com/KB/threads/StackWalker.aspx).
So it seems to me that your problem could be solved.