forked from yavfast/dbg-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbgHookMemory.pas
286 lines (224 loc) · 6.42 KB
/
DbgHookMemory.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
unit DbgHookMemory;
interface
uses DbgHookTypes, DbgHookCS;
procedure InitMemoryHook(MemoryMgr: Pointer; MemoryCallStack: LongBool); stdcall;
procedure ResetMemoryHook; stdcall;
function _OutMemInfoBuf(const DbgInfoType: TDbgInfoType = dstMemInfo): LongBool;
var
MemInfoLock: TDbgCriticalSection = nil;
MemInfoList: PDbgMemInfoList = nil;
MemInfoListCnt: Integer = 0;
implementation
uses WinApi.Windows, System.SysUtils, DbgHookUtils;
var
_BaseMemoryMgr: TMemoryManagerEx;
_HookMemoryMgr: TMemoryManagerEx;
MemCallStack: LongBool = False;
MemLock: TDbgCriticalSection = nil;
_MemoryMgr: PMemoryManagerEx = nil;
type
TMemSize = NativeInt;
function _HookGetMem(Size: TMemSize): Pointer; forward;
function _HookFreeMem(P: Pointer): Integer; forward;
function _HookReallocMem(P: Pointer; Size: TMemSize): Pointer; forward;
function _HookAllocMem(Size: TMemSize): Pointer; forward;
type
PMemOutDbgInfo = ^TMemOutDbgInfo;
TMemOutDbgInfo = array[0..2] of NativeUInt;
threadvar
_MemOutDbgInfo: TMemOutDbgInfo;
procedure _MemOutInfo(const DbgInfoType: TDbgInfoType; Ptr: Pointer; const Count: NativeUInt);
var
MemOutDbgInfo: PMemOutDbgInfo;
begin
MemOutDbgInfo := @_MemOutDbgInfo;
MemOutDbgInfo[0] := NativeUInt(DbgInfoType);
MemOutDbgInfo[1] := NativeUInt(Ptr);
MemOutDbgInfo[2] := NativeUInt(Count);
RaiseException(DBG_EXCEPTION, 0, 3, @MemOutDbgInfo[0]);
end;
procedure _SetMemHookStatus(const Status: NativeUInt);
var
MemOutDbgInfo: PMemOutDbgInfo;
begin
MemOutDbgInfo := @_MemOutDbgInfo;
MemOutDbgInfo[0] := NativeUInt(dstMemHookStatus);
MemOutDbgInfo[1] := Status;
MemOutDbgInfo[2] := 0;
RaiseException(DBG_EXCEPTION, 0, 2, @MemOutDbgInfo[0]);
end;
function _OutMemInfoBuf(const DbgInfoType: TDbgInfoType = dstMemInfo): LongBool;
begin
Result := False;
if MemInfoList = Nil then Exit;
if MemInfoLock = Nil then Exit;
MemInfoLock.Enter;
try
if MemInfoListCnt > 0 then
begin
_MemOutInfo(DbgInfoType, @MemInfoList^[0], MemInfoListCnt);
MemInfoListCnt := 0; // ñáðîñ óêàçàòåëÿ íà íóëåâîé ýëåìåíò
Result := True;
end;
finally
MemInfoLock.Leave;
end;
end;
threadvar
_DbgMemInfo: TDbgMemInfo;
procedure _AddMemInfo(const _MemInfoType: TDbgMemInfoType; const _Ptr: Pointer; const _Size: Cardinal); stdcall;
var
DbgMemInfo: PDbgMemInfo;
begin
if MemInfoLock = Nil then Exit;
if MemInfoList = Nil then Exit;
DbgMemInfo := @_DbgMemInfo;
ZeroMemory(DbgMemInfo, SizeOf(TDbgMemInfo));
DbgMemInfo.Ptr := _Ptr;
DbgMemInfo.ThreadId := GetCurrentThreadId;
DbgMemInfo.MemInfoType := _MemInfoType;
case DbgMemInfo.MemInfoType of
miGetMem:
begin
DbgMemInfo.Size := _Size;
//GetCallStack(DbgMemInfo.Stack, -2);
GetCallStackOS(DbgMemInfo.Stack, 3);
end;
miFreeMem:
begin
DbgMemInfo.ObjClassType[0] := #0;
end;
end;
MemInfoLock.Enter;
if MemInfoList <> Nil then
begin
MemInfoList^[MemInfoListCnt] := DbgMemInfo^;
Inc(MemInfoListCnt);
if MemInfoListCnt = _DbgMemListLength then
_OutMemInfoBuf;
end;
MemInfoLock.Leave;
end;
{.$DEFINE MEMLOCK}
function _HookGetMem(Size: TMemSize): Pointer;
begin
{$IFDEF MEMLOCK}
MemLock.Enter;
try
{$ENDIF}
Result := _BaseMemoryMgr.GetMem(Size);
if Size >= SizeOf(Cardinal) then
PCardinal(Result)^ := $00000000; // Î÷èñòêà äëÿ TObject
_AddMemInfo(miGetMem, Result, Size);
{$IFDEF MEMLOCK}
finally
MemLock.Leave;
end;
{$ENDIF}
end;
function _HookFreeMem(P: Pointer): Integer;
begin
{$IFDEF MEMLOCK}
MemLock.Enter;
try
{$ENDIF}
_AddMemInfo(miFreeMem, P, 0);
// !!! Óêàçàòåëü ìîæåò áûòü óæå íåâàëèäíûì
//PCardinal(P)^ := $00000000; // Î÷èñòêà äëÿ TObject
Result := _BaseMemoryMgr.FreeMem(P);
{$IFDEF MEMLOCK}
finally
MemLock.Leave;
end;
{$ENDIF}
end;
function _HookReallocMem(P: Pointer; Size: TMemSize): Pointer;
begin
{$IFDEF MEMLOCK}
MemLock.Enter;
try
{$ENDIF}
_AddMemInfo(miFreeMem, P, 0);
Result := _BaseMemoryMgr.ReallocMem(P, Size);
_AddMemInfo(miGetMem, Result, Size);
{$IFDEF MEMLOCK}
finally
MemLock.Leave;
end;
{$ENDIF}
end;
function _HookAllocMem(Size: TMemSize): Pointer;
begin
{$IFDEF MEMLOCK}
MemLock.Enter;
try
{$ENDIF}
Result := _BaseMemoryMgr.AllocMem(Size);
_AddMemInfo(miGetMem, Result, Size);
{$IFDEF MEMLOCK}
finally
MemLock.Leave;
end;
{$ENDIF}
end;
procedure InitMemoryHook(MemoryMgr: Pointer; MemoryCallStack: LongBool); stdcall;
begin
_Log('Init memory hooks...');
MemInfoListCnt := 0;
MemCallStack := MemoryCallStack;
MemLock := TDbgCriticalSection.Create;
MemInfoList := AllocMem(SizeOf(TDbgMemInfoList));
MemInfoLock := TDbgCriticalSection.Create;
_HookMemoryMgr.GetMem := _HookGetMem;
_HookMemoryMgr.FreeMem := _HookFreeMem;
_HookMemoryMgr.ReallocMem := _HookReallocMem;
_HookMemoryMgr.AllocMem := _HookAllocMem;
_MemoryMgr := MemoryMgr;
_BaseMemoryMgr.GetMem := _MemoryMgr^.GetMem;
_BaseMemoryMgr.FreeMem := _MemoryMgr^.FreeMem;
_BaseMemoryMgr.ReallocMem := _MemoryMgr^.ReallocMem;
_BaseMemoryMgr.AllocMem := _MemoryMgr^.AllocMem;
MemLock.Enter;
MemInfoLock.Enter;
_MemoryMgr^.GetMem := _HookMemoryMgr.GetMem;
_MemoryMgr^.FreeMem := _HookMemoryMgr.FreeMem;
_MemoryMgr^.ReallocMem := _HookMemoryMgr.ReallocMem;
_MemoryMgr^.AllocMem := _HookMemoryMgr.AllocMem;
MemInfoLock.Leave;
MemLock.Leave;
// _SetMemHookStatus(0);
_Log('Init memory hooks - ok');
end;
procedure ResetMemoryHook; stdcall;
begin
try
if _MemoryMgr = nil then Exit;
if MemInfoLock = nil then Exit;
// Âîññòàíàâëèâàåì îðèãèíàëüíûé ìåíåäæåð ïàìÿòè
MemInfoLock.Enter;
try
_MemoryMgr^ := _BaseMemoryMgr;
_MemoryMgr := Nil;
_SetMemHookStatus(1);
finally
MemInfoLock.Leave;
end;
while not MemInfoLock.TryEnter do
SwitchToThread;
try
// Ñáðàñûâàåì áóôôåð
_OutMemInfoBuf(dstMemInfo);
FreeMemory(MemInfoList);
MemInfoList := Nil;
finally
MemInfoLock.Leave;
end;
FreeAndNil(MemInfoLock);
FreeAndNil(MemLock);
_Log('Reset memory hooks - ok');
except
on E: Exception do
_Log('Reset memory hooks fail: ' + E.Message);
end;
end;
end.