forked from Sovos-Compliance/convey-public-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uScope.pas
716 lines (620 loc) · 20.8 KB
/
uScope.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
unit uScope;
interface
{$i DelphiVersion_defines.inc}
uses
{$IFNDEF DELPHIXE}
CnvStrUtils,
{$ENDIF}
SyncObjs;
{$IFNDEF DELPHIXE}
type
// We must define NativeInt for platforms lesser than DelphiXE
// to make things even worse, in Delphi 2007 NativeInt is a 64 bits integer when on later
// versions this type mutates depending on the target platform x86 or x64
NativeInt = Integer;
NativeUInt = Cardinal;
{$ENDIF}
type
TEndThreadSubscriber = procedure (ExitCode: Integer);
PPointer = ^Pointer;
PCardinal = ^Cardinal;
TFinalizeScopeMethod = procedure of object;
TFinalizeScopeProcedure = procedure;
PObject = ^TObject;
IScope = interface
['{B3906162-927C-4BA7-BEC6-1F4920EC9833}']
function Add(AObject : TObject): Pointer; overload;
function Add(APointer : Pointer): Pointer; overload;
function Add(AHandle : Cardinal; AFreeProc : Pointer = nil): Cardinal; overload;
// The following methods don't work for local variables at a procedure level
// at the moment the Scope object is destroyed local variables are no longer valid, therefore references
// tracked by IScope will be invalid and will cause an access violation
procedure AddObjectReference(AObjectReference : PObject);
procedure AddPointerReference(APointerReference : PPointer);
procedure AddHandleReference(AHandleReference: PCardinal; AFreeProc: Pointer = nil);
procedure AssignObject(var Target; AObject : TObject);
procedure AssignPointer(var Target; APointer: Pointer);
procedure AssignHandle(var Target; Handle : Cardinal; AFreeProc : Pointer = nil); // The prototype for FreeProc is: procedure (AHandle : Cardinal)
function AllocMem(ASize : Cardinal) : pointer;
{$IFNDEF DELPHIXE}
function StrList(const AStringArray : array of string) : TAdvStringList; overload;
function StrList : TAdvStringList; overload;
{$ENDIF}
// Keep the following methods always at the bottom because otherwise will break generation of import file because
// These methods are excluded, they make PAX registrations blow up
procedure Assign(var Target; AObject : TObject); overload;
procedure Assign(var Target; APointer: Pointer); overload;
procedure Assign(var Target; Handle : Cardinal; AFreeProc : Pointer = nil); overload; // The prototype for FreeProc is: procedure (AHandle : Cardinal)
procedure AddFinalizeScopeMethod(AProc : TFinalizeScopeMethod);
procedure AutoFinalize(AInitProc, AFinalizeProc : TFinalizeScopeMethod);
procedure AddFinalizeScopeMethods(const AMethodReferences : array of TFinalizeScopeMethod);
procedure AddFinalizeScopeProc(AProc : TFinalizeScopeProcedure);
procedure AddFinalizeScopeProcs(const AProcReferences : array of TFinalizeScopeProcedure);
procedure AddObjectReferences(const AObjectReferences : array of PObject);
procedure AddPointerReferences(const APointerReferences : array of PPointer);
procedure AddHandleReferences(const AHandleReferences: array of PCardinal; AFreeProc: Pointer = nil);
end;
IAutoLocker = interface
['{E81B482E-5719-4C30-B37D-6F5D652E88CF}']
end;
function NewScope: IScope;
{ !!!! Important: ONLY use the following function to protect calls using synchronization objects
for calls that are in one procedure. AutoLocker objects allocate memory in a very light way
fashion on a piece of memory that works like a stack.
DON'T use it as a member of a class or bad things will happen }
function NewAutoLocker(ALockObject : TSynchroObject) : IAutoLocker;
procedure RegisterEndThreadSubscriber(ASubscriber: Pointer);
procedure UnRegisterEndThreadSubscriber(ASubscriber: Pointer);
procedure WaitForDLL_Attached_Event;
var
InitializingDll : Boolean; // Use this flag to know when a DLL is being attached, only used when ModuleIsPackage is True
ShuttingDownDll : Boolean; // Use this flag to know when a DLL is in DETACH mode, only used when ModuleIsPackage is True
implementation
uses
uLinkedList, iContainers, SysUtils, Windows, CnvGenUtils, Classes;
resourcestring
SEndOfAutolockerAllocationStackRe = 'End of autolocker allocation stack reached';
SOnlyLastAutoLockerCanBeDestroyed = 'Only last AutoLocker can be destroyed';
const
kernel32 = 'kernel32.dll';
AUTOLOCKER_STACKSIZE = 32;
type
TTrackedResource = class;
TScope = class(TInterfacedObject, IScope)
procedure FreeInstance; override;
private
FObjects : ILinkedList;
LastExceptionMsg : string;
LastExceptionClass : string;
procedure DestroyTrackedObjects;
protected
constructor Create; virtual;
procedure FinalizeTrackedResource(ATrackedResource: TTrackedResource);
function Add(AObject : TObject): Pointer; overload;
function Add(APointer : Pointer): Pointer; overload;
function Add(AHandle: Cardinal; AFreeProc: Pointer = nil): Cardinal; overload;
procedure AutoFinalize(AInitProc, AFinalizeProc : TFinalizeScopeMethod);
procedure AddFinalizeScopeMethod(AProc : TFinalizeScopeMethod);
procedure AddFinalizeScopeProc(AProc : TFinalizeScopeProcedure);
procedure Assign(var Target; AObject : TObject); overload;
procedure Assign(var Target; APointer: Pointer); overload;
procedure Assign(var Target; AHandle: Cardinal; AFreeProc: Pointer = nil); overload;
procedure AddObjectReferences(const AObjectReferences: array of PObject);
procedure AddPointerReferences(const APointerReferences: array of PPointer);
procedure AddHandleReferences(const AObjectReferences: array of PCardinal; AFreeProc: Pointer = nil);
procedure AddFinalizeScopeMethods(const AMethodReferences : array of TFinalizeScopeMethod);
procedure AddFinalizeScopeProcs(const AProcReferences: array of TFinalizeScopeProcedure);
procedure AddObjectReference(AObjectReference : PObject);
procedure AddPointerReference(APointerReference : PPointer);
procedure AddHandleReference(AHandleReference: PCardinal; AFreeProc: Pointer = nil);
procedure AssignObject(var Target; AObject : TObject);
procedure AssignPointer(var Target; APointer: Pointer);
procedure AssignHandle(var Target; Handle : Cardinal; AFreeProc : Pointer = nil);
function AllocMem(ASize : Cardinal): pointer;
{$IFNDEF DELPHIXE}
function StrList(const AStringArray : array of string): TAdvStringList; overload;
function StrList: TAdvStringList; overload;
{$ENDIF}
public
destructor Destroy; override;
procedure AfterConstruction; override;
end;
TFreeHandleProcedure = procedure (AHandle : Cardinal);
TTrackedResource = class
public
procedure FreeTrackedResource; virtual; abstract;
end;
TTrackedHandle = class(TTrackedResource)
private
FHandle : Cardinal;
FFreeProc : TFreeHandleProcedure;
FVarAddress: Pointer;
public
constructor Create(AHandle: Cardinal; AFreeProc: TFreeHandleProcedure;
AVarAddress: Pointer = nil);
procedure FreeTrackedResource; override;
end;
TTrackedHandleRefrence = class (TTrackedHandle)
public
procedure FreeTrackedResource; override;
end;
TTrackedObject = class(TTrackedResource)
private
FObject : TObject;
FVarAddress : Pointer;
public
constructor Create(AObject : TObject; AVarAddress : Pointer = nil);
procedure FreeTrackedResource; override;
end;
TTrackedObjectReference = class(TTrackedObject)
public
procedure FreeTrackedResource; override;
end;
TTrackedPointer = class(TTrackedResource)
private
FPointer : Pointer;
FVarAddress: Pointer;
public
constructor Create(APointer : Pointer; AVarAddress : Pointer = nil);
procedure FreeTrackedResource; override;
end;
TTrackedPointerReference = class(TTrackedPointer)
public
procedure FreeTrackedResource; override;
end;
TTrackedFinalizeScopeMethod = class(TTrackedResource)
private
FMethod : TFinalizeScopeMethod;
public
constructor Create(AMethod: TFinalizeScopeMethod);
procedure FreeTrackedResource; override;
end;
TTrackedFinalizeScopeProc = class(TTrackedResource)
private
FProc : TFinalizeScopeProcedure;
public
constructor Create(AProc: TFinalizeScopeProcedure);
procedure FreeTrackedResource; override;
end;
TAutoLocker = class(TInterfacedObject, IAutoLocker)
private
FLock : TSynchroObject;
public
constructor Create(ALock: TSynchroObject);
destructor Destroy; override;
procedure FreeInstance; override;
class function NewInstance: TObject; override;
end;
function CloseHandle(hObject: Cardinal): LongBool; stdcall; forward;
threadvar
AutoLockerStack : Pointer;
AutoLockerStackPtr : Pointer;
AutoLockerStackEnd : Pointer;
var
EndThreadSubscribers : ILinkedList;
function NewScope: IScope;
begin
Result := TScope.Create;
end;
constructor TScope.Create;
begin
inherited;
FObjects := LinkedListFactory.CreateObject (Self) as ILinkedList;
end;
destructor TScope.Destroy;
begin
DestroyTrackedObjects;
inherited;
end;
procedure TScope.DestroyTrackedObjects;
begin
if FObjects = nil then
exit;
with TIterator.CreateIterator(FObjects) do
while IterateBackwards do
try
FinalizeTrackedResource (GetAsObject as TTrackedResource);
except
on E : Exception do
begin
LastExceptionMsg := E.Message;
LastExceptionClass := E.ClassName;
end;
end;
end;
function TScope.Add(AObject : TObject): Pointer;
begin
Result := AObject;
FObjects.Insert(TTrackedObject.Create (AObject));
end;
function TScope.Add(APointer : Pointer): Pointer;
begin
Result := APointer;
FObjects.Insert(TTrackedPointer.Create (APointer));
end;
function TScope.Add(AHandle: Cardinal; AFreeProc: Pointer = nil): Cardinal;
begin
Result := AHandle;
FObjects.Insert(TTrackedHandle.Create (integer (AHandle), AFreeProc));
end;
procedure TScope.AddFinalizeScopeMethod(AProc : TFinalizeScopeMethod);
begin
FObjects.Insert(TTrackedFinalizeScopeMethod.Create(AProc));
end;
procedure TScope.AddFinalizeScopeMethods(const AMethodReferences : array of TFinalizeScopeMethod);
var
i : integer;
begin
for i := high (AMethodReferences) downto low (AMethodReferences) do
FObjects.Insert (TTrackedFinalizeScopeMethod.Create (AMethodReferences[i]));
end;
procedure TScope.AddFinalizeScopeProc(AProc : TFinalizeScopeProcedure);
begin
FObjects.Insert(TTrackedFinalizeScopeProc.Create(AProc));
end;
procedure TScope.AddFinalizeScopeProcs(const AProcReferences: array of TFinalizeScopeProcedure);
var
i : integer;
begin
for i := high (AProcReferences) downto low (AProcReferences) do
FObjects.Insert (TTrackedFinalizeScopeProc.Create (AProcReferences[i]));
end;
procedure TScope.AddHandleReference(AHandleReference: PCardinal; AFreeProc:
Pointer = nil);
begin
AddHandleReferences([AHandleReference], AFreeProc);
end;
procedure TScope.AddObjectReferences(const AObjectReferences: array of PObject);
var
i : integer;
begin
for i := low (AObjectReferences) to high (AObjectReferences) do
FObjects.Insert (TTrackedObjectReference.Create (AObjectReferences[i]^, AObjectReferences[i]));
end;
procedure TScope.AddHandleReferences(const AObjectReferences: array of PCardinal;
AFreeProc: Pointer = nil);
var
i : integer;
begin
for i := low (AObjectReferences) to high (AObjectReferences) do
FObjects.Insert (TTrackedHandleRefrence.Create (AObjectReferences[i]^, AFreeProc, AObjectReferences[i]));
end;
procedure TScope.AddObjectReference(AObjectReference : PObject);
begin
AddObjectReferences([AObjectReference]);
end;
procedure TScope.AddPointerReference(APointerReference : PPointer);
begin
AddPointerReferences([APointerReference]);
end;
procedure TScope.AddPointerReferences(const APointerReferences: array of
PPointer);
var
i : integer;
begin
for i := low (APointerReferences) to high (APointerReferences) do
FObjects.Insert (TTrackedPointerReference.Create (APointerReferences[i]^, APointerReferences[i]));
end;
procedure TScope.AfterConstruction;
begin
inherited;
if FObjects = nil
then raise Exception.Create ('Call NewScope to initialize a new Scope object')
end;
function TScope.AllocMem(ASize : Cardinal): pointer;
begin
GetMem(Result, ASize);
Add(Result);
end;
procedure TScope.Assign(var Target; AObject : TObject);
begin
TObject (Target) := AObject;
FObjects.Insert(TTrackedObject.Create (AObject, @Target));
end;
procedure TScope.Assign(var Target; AHandle: Cardinal; AFreeProc: Pointer =
nil);
begin
Cardinal (Target) := AHandle;
FObjects.Insert(TTrackedHandle.Create (integer (AHandle), AFreeProc, @Target));
end;
procedure TScope.Assign(var Target; APointer: Pointer);
begin
Pointer (Target) := APointer;
FObjects.Insert(TTrackedPointer.Create (APointer, @Target));
end;
procedure TScope.AssignHandle(var Target; Handle : Cardinal; AFreeProc :
Pointer = nil);
begin
Assign(Target, Handle, AFreeProc);
end;
procedure TScope.AssignObject(var Target; AObject : TObject);
begin
Assign(Target, AObject);
end;
procedure TScope.AssignPointer(var Target; APointer: Pointer);
begin
Assign(Target, APointer);
end;
procedure TScope.AutoFinalize(AInitProc, AFinalizeProc : TFinalizeScopeMethod);
begin
AInitProc;
AddFinalizeScopeMethod(AFinalizeProc);
end;
procedure TScope.FinalizeTrackedResource(ATrackedResource: TTrackedResource);
begin
with ATrackedResource do
begin
try
FreeTrackedResource;
finally
Free;
end;
end;
end;
procedure TScope.FreeInstance;
var
ALastExceptionMsg : String;
ALastExceptionClass : String;
begin
ALastExceptionMsg := LastExceptionMsg;
ALastExceptionClass := LastExceptionClass;
inherited;
if ALastExceptionMsg <> '' then
raise Exception.CreateFmt ('%s (%s class)', [ALastExceptionMsg, ALastExceptionClass]);
end;
{$IFNDEF DELPHIXE}
function TScope.StrList: TAdvStringList;
begin
Result := Add(TAdvStringList.Create);
end;
function TScope.StrList(const AStringArray : array of string): TAdvStringList;
var
i : integer;
begin
Result := StrList;
for i := low(AStringArray) to high(AStringArray) do
Result.Add(AStringArray[i]);
end;
{$ENDIF}
function CloseHandle; external kernel32 name 'CloseHandle';
constructor TTrackedHandle.Create(AHandle: Cardinal; AFreeProc:
TFreeHandleProcedure; AVarAddress: Pointer = nil);
begin
inherited Create;
FHandle := AHandle;
FFreeProc := AFreeProc;
FVarAddress := AVarAddress;
end;
procedure TTrackedHandle.FreeTrackedResource;
begin
try
if assigned (FFreeProc)
then FFreeProc (FHandle)
else CloseHandle (FHandle);
finally
if FVarAddress <> nil then
PCardinal (FVarAddress)^ := 0;
end;
end;
constructor TTrackedObject.Create(AObject : TObject; AVarAddress : Pointer =
nil);
begin
inherited Create;
FObject := AObject;
FVarAddress := AVarAddress;
end;
procedure TTrackedObject.FreeTrackedResource;
begin
try
FObject.Free;
finally
if FVarAddress <> nil then
PPointer(FVarAddress)^ := nil;
end;
end;
constructor TTrackedPointer.Create(APointer : Pointer; AVarAddress : Pointer =
nil);
begin
inherited Create;
FPointer := APointer;
FVarAddress := AVarAddress;
end;
procedure TTrackedPointer.FreeTrackedResource;
begin
try
FreeMem (FPointer);
finally
if FVarAddress <> nil then
PPointer(FVarAddress)^ := nil;
end;
end;
procedure TTrackedObjectReference.FreeTrackedResource;
begin
FObject := PPointer (FVarAddress)^;
if FObject <> nil
then inherited;
end;
procedure TTrackedHandleRefrence.FreeTrackedResource;
begin
FHandle := PCardinal (FVarAddress)^;
if FHandle <> 0
then inherited;
end;
procedure TTrackedPointerReference.FreeTrackedResource;
begin
FPointer := PPointer (FVarAddress)^;
if FPointer <> nil
then inherited;
end;
constructor TTrackedFinalizeScopeMethod.Create(AMethod: TFinalizeScopeMethod);
begin
inherited Create;
FMethod := AMethod;
end;
procedure TTrackedFinalizeScopeMethod.FreeTrackedResource;
begin
if assigned(FMethod) then
FMethod;
end;
constructor TTrackedFinalizeScopeProc.Create(AProc: TFinalizeScopeProcedure);
begin
inherited Create;
FProc := AProc;
end;
procedure TTrackedFinalizeScopeProc.FreeTrackedResource;
begin
if assigned(FProc) then
FProc;
end;
type
PJump = ^TJump;
TJump = packed record
OpCode:byte;
Distance:integer;
end;
{$IFNDEF DELPHI2007}
type
THookedDllProc = procedure (Reason: DWORD);
{$ENDIF}
var
DllAttachedEvent : TEvent;
OldCode : TJump;
NewCode : TJump;
{$IFDEF DELPHI2007}
OldDllProc : TDLLProc;
{$ELSE}
OldDllProc : Pointer;
{$ENDIF}
procedure WaitForDLL_Attached_Event;
begin
if DllAttachedEvent <> nil then
DllAttachedEvent.WaitFor(INFINITE);
end;
// Hooked DllProc used to flag when DLL is being detached
procedure HookedDllProc(Reason: DWORD);
begin
if not ShuttingDownDll then
ShuttingDownDll := Reason = DLL_PROCESS_DETACH;
if assigned(OldDllProc) then
{$IFNDEF DELPHI2007}THookedDllProc({$ENDIF}OldDllProc{$IFNDEF DELPHI2007}){$ENDIF}(Reason);
end;
procedure HookedEndThread(ExitCode: Integer);
var
i : IIterator;
begin
{$IFDEF DELPHI2007}
if Assigned(SystemThreadEndProc) then
SystemThreadEndProc(ExitCode);
{$ENDIF}
i := NewIterator(EndThreadSubscribers);
while i.IterateBackwards do
TEndThreadSubscriber(i.GetAsPointer)(ExitCode);
i := nil;
(* JSB: Special code to prevent deadlock when unloading a DLL and trying to finalize a thread
on the finalization section of units.
Read post http://www.verious.com/qa/exit-thread-upon-deleting-static-object-during-unload-dll-causes-deadlock/
it briefly touches on the subject that there seems to be a limitation (bug?) on trying to finalize a thread when already
entering on the dllMain call to perform a DLL_DETACH ( DLL_PROCESS_DETACH )
We need to know that module is going into detach mode before changing from ExitThread to TerminateThread.
This problem also seems to happen only with "normal" dlls, not with Borland Packages (BPLs)
*)
if (not IsLibrary) or (not ShuttingDownDll) then
ExitThread(ExitCode)
else TerminateThread(GetCurrentThread, ExitCode); // Forceful termination of thread if library mode and DLL_PROCESS_DETACH mode
end;
{ TAutoLocker }
procedure FreeLockerStackEndThreadSubscriber(ExitCode : integer);
begin
if AutoLockerStack <> nil then
FreeMem(AutoLockerStack);
end;
procedure InitAutoLockerStack;
begin
GetMem(AutoLockerStack, AUTOLOCKER_STACKSIZE * TAutoLocker.InstanceSize);
AutoLockerStackPtr := AutoLockerStack;
AutoLockerStackEnd := Pointer(Integer(AutoLockerStack) + AUTOLOCKER_STACKSIZE * TAutoLocker.InstanceSize);
end;
constructor TAutoLocker.Create(ALock: TSynchroObject);
begin
inherited Create;
FLock := ALock;
FLock.Acquire;
end;
destructor TAutoLocker.Destroy;
begin
FLock.Release;
inherited;
end;
procedure TAutoLocker.FreeInstance;
begin
if Self <> Pointer(Integer(AutoLockerStackPtr) - TAutoLocker.InstanceSize) then
raise Exception.Create(SOnlyLastAutoLockerCanBeDestroyed);
CleanupInstance;
dec(NativeUInt(AutoLockerStackPtr), TAutoLocker.InstanceSize);
end;
class function TAutoLocker.NewInstance: TObject;
begin
if AutoLockerStack = nil then
InitAutoLockerStack;
if Integer(AutoLockerStackPtr) >= Integer(AutoLockerStackEnd) then
raise Exception.Create(SEndOfAutolockerAllocationStackRe);
Result := pointer(AutoLockerStackPtr);
inc(NativeUInt(AutoLockerStackPtr), TAutoLocker.InstanceSize);
InitInstance(Result);
TAutoLocker(Result).FRefCount := 1;
end;
function NewAutoLocker(ALockObject : TSynchroObject) : IAutoLocker;
begin
Result := TAutoLocker.Create(ALockObject);
end;
procedure PatchEndThread;
begin
NewCode.Distance := NativeUInt(@HookedEndThread) - (NativeUInt(@EndThread) + 5);
PatchMemory (@EndThread, 5, @NewCode, @OldCode);
FlushInstructionCache (GetCurrentProcess, @EndThread, 5);
end;
procedure UnPatchEndThread;
begin
PatchMemory (@EndThread, 5, @OldCode);
FlushInstructionCache (GetCurrentProcess, @EndThread, 5);
end;
procedure RegisterEndThreadSubscriber(ASubscriber: Pointer);
begin
EndThreadSubscribers.Insert(ASubscriber);
end;
procedure UnRegisterEndThreadSubscriber(ASubscriber: Pointer);
begin
EndThreadSubscribers.Remove(ASubscriber);
end;
type
TSignalInitializedThread = class(TThread)
protected
procedure Execute; override;
end;
procedure TSignalInitializedThread.Execute;
begin
InitializingDll := False;
DllAttachedEvent.SetEvent;
end;
initialization
if IsLibrary and (not ModuleIsPackage) then
begin
InitializingDll := True;
DllAttachedEvent := TEvent.Create(nil, True, False, '');
with TSignalInitializedThread.Create(False) do
FreeOnTerminate := True;
end;
OldDllProc := DllProc;
DllProc := @HookedDllProc;
NewCode.OpCode := $E9;
NewCode.Distance := 0;
PatchEndThread;
EndThreadSubscribers := LinkedListFactory.CreateObject (nil) as ILinkedList;
RegisterEndThreadSubscriber(@FreeLockerStackEndThreadSubscriber);
finalization
UnRegisterEndThreadSubscriber(@FreeLockerStackEndThreadSubscriber);
UnPatchEndThread;
DllProc := OldDllProc;
if DllAttachedEvent <> nil then
FreeAndNil(DllAttachedEvent);
end.