-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJclHookExcept.pas
More file actions
864 lines (782 loc) · 26.2 KB
/
Copy pathJclHookExcept.pas
File metadata and controls
864 lines (782 loc) · 26.2 KB
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
{**************************************************************************************************}
{ }
{ Project JEDI Code Library (JCL) }
{ }
{ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
{ you may not use this file except in compliance with the License. You may obtain a copy of the }
{ License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF }
{ ANY KIND, either express or implied. See the License for the specific language governing rights }
{ and limitations under the License. }
{ }
{ The Original Code is JclHookExcept.pas. }
{ }
{ The Initial Developer of the Original Code is Petr Vones. Portions created by Petr Vones are }
{ Copyright (C) Petr Vones. All Rights Reserved. }
{ }
{ Contributor(s): }
{ Petr Vones (pvones) }
{ Robert Marquardt (marquardt) }
{ Andreas Hausladen (ahuser) }
{ }
{**************************************************************************************************}
{ }
{ Exception hooking routines }
{ }
{**************************************************************************************************}
{ }
{ Last modified: $Date:: $ }
{ Revision: $Rev:: $ }
{ Author: $Author:: $ }
{ }
{**************************************************************************************************}
unit JclHookExcept;
interface
{$I jcl.inc}
{$I windowsonly.inc}
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
{$IFDEF HAS_UNITSCOPE}
Winapi.Windows, System.SysUtils, System.Classes;
{$ELSE ~HAS_UNITSCOPE}
Windows, SysUtils, Classes;
{$ENDIF ~HAS_UNITSCOPE}
type
// Exception hooking notifiers routines
{$IFDEF BORLAND}
TJclExceptFilterProc = function(ExceptRecord: PExceptionRecord): Exception;
{$ENDIF BORLAND}
TJclExceptNotifyProc = procedure(ExceptObj: TObject; ExceptAddr: Pointer; OSException: Boolean);
TJclExceptNotifyProcEx = procedure(ExceptObj: TObject; ExceptAddr: Pointer; OSException: Boolean; StackPointer: Pointer);
TJclExceptNotifyMethod = procedure(ExceptObj: TObject; ExceptAddr: Pointer; OSException: Boolean) of object;
TJclExceptNotifyPriority = (npNormal, npFirstChain);
{$IFDEF BORLAND}
function JclAddExceptFilter(const FilterProc: TJclExceptFilterProc; Priority: TJclExceptNotifyPriority = npNormal): Boolean;
{$ENDIF BORLAND}
function JclAddExceptNotifier(const NotifyProc: TJclExceptNotifyProc; Priority: TJclExceptNotifyPriority = npNormal): Boolean; overload;
function JclAddExceptNotifier(const NotifyProc: TJclExceptNotifyProcEx; Priority: TJclExceptNotifyPriority = npNormal): Boolean; overload;
function JclAddExceptNotifier(const NotifyMethod: TJclExceptNotifyMethod; Priority: TJclExceptNotifyPriority = npNormal): Boolean; overload;
{$IFDEF BORLAND}
function JclRemoveExceptFilter(const FilterProc: TJclExceptFilterProc): Boolean;
{$ENDIF BORLAND}
function JclRemoveExceptNotifier(const NotifyProc: TJclExceptNotifyProc): Boolean; overload;
function JclRemoveExceptNotifier(const NotifyProc: TJclExceptNotifyProcEx): Boolean; overload;
function JclRemoveExceptNotifier(const NotifyMethod: TJclExceptNotifyMethod): Boolean; overload;
procedure JclReplaceExceptObj(NewExceptObj: Exception);
// Exception hooking routines
function JclHookExceptions: Boolean;
function JclUnhookExceptions: Boolean;
function JclExceptionsHooked: Boolean;
function JclHookExceptionsInModule(Module: HMODULE): Boolean;
function JclUnhookExceptionsInModule(Module: HMODULE): Boolean;
// Exceptions hooking in libraries
type
TJclModuleArray = array of HMODULE;
function JclInitializeLibrariesHookExcept: Boolean;
function JclHookedExceptModulesList(out ModulesList: TJclModuleArray): Boolean;
// Hooking routines location info helper
function JclBelongsHookedCode(Address: Pointer): Boolean;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$URL$';
Revision: '$Revision$';
Date: '$Date$';
LogPath: 'JCL\source\windows';
Extra: '';
Data: nil
);
{$ENDIF UNITVERSIONING}
implementation
uses
JclBase,
JclPeImage,
JclSysInfo, JclSysUtils;
type
PExceptionArguments = ^TExceptionArguments;
TExceptionArguments = record
ExceptAddr: Pointer;
ExceptObj: Exception;
end;
{$IFDEF BORLAND}
TFilterItem = class(TObject)
private
FExceptFilterProc: TJclExceptFilterProc;
FPriority: TJclExceptNotifyPriority;
public
constructor Create(const ExceptFilterProc: TJclExceptFilterProc; APriority: TJclExceptNotifyPriority);
function DoFilterException(ExceptRecord: PExceptionRecord; out ExceptObj: Exception): Boolean;
property Priority: TJclExceptNotifyPriority read FPriority;
end;
{$ENDIF BORLAND}
TNotifierItem = class(TObject)
private
FNotifyMethod: TJclExceptNotifyMethod;
FNotifyProc: TJclExceptNotifyProc;
FNotifyProcEx: TJclExceptNotifyProcEx;
FPriority: TJclExceptNotifyPriority;
public
constructor Create(const NotifyProc: TJclExceptNotifyProc; Priority: TJclExceptNotifyPriority); overload;
constructor Create(const NotifyProc: TJclExceptNotifyProcEx; Priority: TJclExceptNotifyPriority); overload;
constructor Create(const NotifyMethod: TJclExceptNotifyMethod; Priority: TJclExceptNotifyPriority); overload;
procedure DoNotify(ExceptObj: TObject; ExceptAddr: Pointer; OSException: Boolean; StackPointer: Pointer);
property Priority: TJclExceptNotifyPriority read FPriority;
end;
var
ExceptionsHooked: Boolean;
Kernel32_RaiseException: procedure (dwExceptionCode, dwExceptionFlags,
nNumberOfArguments: DWORD; lpArguments: PDWORD); stdcall;
{$IFDEF BORLAND}
SysUtils_ExceptObjProc: function (P: PExceptionRecord): Exception;
{$ENDIF BORLAND}
{$IFDEF FPC}
SysUtils_ExceptProc: TExceptProc;
{$ENDIF FPC}
Notifiers: TThreadList;
{$IFDEF BORLAND}
Filters: TThreadList;
{$ENDIF BORLAND}
{$IFDEF HOOK_DLL_EXCEPTIONS}
const
JclHookExceptDebugHookName = '__JclHookExcept';
type
TJclHookExceptDebugHook = procedure(Module: HMODULE; Hook: Boolean); stdcall;
TJclHookExceptModuleList = class(TObject)
private
FModules: TThreadList;
protected
procedure HookStaticModules;
public
constructor Create;
destructor Destroy; override;
class function JclHookExceptDebugHookAddr: Pointer;
procedure HookModule(Module: HMODULE);
procedure List(out ModulesList: TJclModuleArray);
procedure UnhookModule(Module: HMODULE);
end;
var
HookExceptModuleList: TJclHookExceptModuleList;
JclHookExceptDebugHook: Pointer;
exports
JclHookExceptDebugHook name JclHookExceptDebugHookName;
{$ENDIF HOOK_DLL_EXCEPTIONS}
{$STACKFRAMES OFF}
threadvar
Recursive: Boolean;
NewResultExc: Exception;
//=== Helper routines ========================================================
function RaiseExceptionAddress: Pointer;
begin
Result := GetProcAddress(GetModuleHandle(kernel32), 'RaiseException');
Assert(Result <> nil);
end;
procedure FreeThreadObjList(var TheList: TThreadList);
var
I: Integer;
begin
with TheList.LockList do
try
for I := 0 to Count - 1 do
TObject(Items[I]).Free;
finally
TheList.UnlockList;
end;
FreeAndNil(TheList);
end;
//=== { TFilterItem } ========================================================
{$IFDEF BORLAND}
constructor TFilterItem.Create(const ExceptFilterProc: TJclExceptFilterProc; APriority: TJclExceptNotifyPriority);
begin
FExceptFilterProc := ExceptFilterProc;
FPriority := APriority;
end;
function TFilterItem.DoFilterException(ExceptRecord: PExceptionRecord; out ExceptObj: Exception): Boolean;
begin
if Assigned(FExceptFilterProc) then
begin
ExceptObj := FExceptFilterProc(ExceptRecord);
Result := ExceptObj <> nil;
end
else
Result := False;
end;
{$ENDIF BORLAND}
//=== { TNotifierItem } ======================================================
constructor TNotifierItem.Create(const NotifyProc: TJclExceptNotifyProc; Priority: TJclExceptNotifyPriority);
begin
inherited Create;
FNotifyProc := NotifyProc;
FPriority := Priority;
end;
constructor TNotifierItem.Create(const NotifyProc: TJclExceptNotifyProcEx; Priority: TJclExceptNotifyPriority);
begin
inherited Create;
FNotifyProcEx := NotifyProc;
FPriority := Priority;
end;
constructor TNotifierItem.Create(const NotifyMethod: TJclExceptNotifyMethod; Priority: TJclExceptNotifyPriority);
begin
inherited Create;
FNotifyMethod := NotifyMethod;
FPriority := Priority;
end;
procedure TNotifierItem.DoNotify(ExceptObj: TObject; ExceptAddr: Pointer;
OSException: Boolean; StackPointer: Pointer);
begin
if Assigned(FNotifyProc) then
FNotifyProc(ExceptObj, ExceptAddr, OSException)
else
if Assigned(FNotifyProcEx) then
FNotifyProcEx(ExceptObj, ExceptAddr, OSException, StackPointer)
else
if Assigned(FNotifyMethod) then
FNotifyMethod(ExceptObj, ExceptAddr, OSException);
end;
function GetFramePointer: Pointer;
asm
{$IFDEF CPU32}
MOV EAX, EBP
{$ENDIF CPU32}
{$IFDEF CPU64}
MOV RAX, RBP
{$ENDIF CPU64}
end;
{$STACKFRAMES ON}
{$IFDEF BORLAND}
function DoExceptFilter(ExceptRecord: PExceptionRecord): Exception;
var
Priorities: TJclExceptNotifyPriority;
I: Integer;
begin
if Recursive then
Exit;
if Assigned(Filters) then
begin
Recursive := True;
try
with Filters.LockList do
try
for Priorities := High(Priorities) downto Low(Priorities) do
for I := 0 to Count - 1 do
with TFilterItem(Items[I]) do
if Priority = Priorities then
if DoFilterException(ExceptRecord, Result) then
Exit;
finally
Filters.UnlockList;
end;
// Nobody wanted to handle the external exception. Call the default handler.
Result := SysUtils_ExceptObjProc(ExceptRecord);
finally
Recursive := False;
end;
end;
end;
{$ENDIF BORLAND}
procedure DoExceptNotify(ExceptObj: TObject; ExceptAddr: Pointer; OSException: Boolean; StackPointer: Pointer);
var
Priorities: TJclExceptNotifyPriority;
I: Integer;
begin
if Recursive then
Exit;
if Assigned(Notifiers) then
begin
Recursive := True;
NewResultExc := nil;
try
with Notifiers.LockList do
try
if Count = 1 then
begin
with TNotifierItem(Items[0]) do
DoNotify( ExceptObj, ExceptAddr, OSException, StackPointer);
end
else
begin
for Priorities := High(Priorities) downto Low(Priorities) do
for I := 0 to Count - 1 do
with TNotifierItem(Items[I]) do
if Priority = Priorities then
DoNotify(ExceptObj, ExceptAddr, OSException, StackPointer);
end;
finally
Notifiers.UnlockList;
end;
finally
Recursive := False;
end;
end;
end;
procedure HookedRaiseException(ExceptionCode, ExceptionFlags, NumberOfArguments: DWORD;
Arguments: PExceptionArguments); stdcall;
const
MS_VC_EXCEPTION = $406D1388;
cDelphiException = $0EEDFADE;
cNonContinuable = 1; // Delphi exceptions
cNonContinuableException = $C0000025; // C++Builder exceptions (sounds like a bug)
DelphiNumberOfArguments = 7;
CBuilderNumberOfArguments = 8;
begin
if ((ExceptionFlags = cNonContinuable) or (ExceptionFlags = cNonContinuableException)) and
(ExceptionCode = cDelphiException) and
(NumberOfArguments in [DelphiNumberOfArguments, CBuilderNumberOfArguments])
//TODO: The difference for Win64 is bigger than 100 Byte and the comment of JVCS revision 0.3 of
// JclDebug.pas, where HookedRaiseException has been added by Petr, isn't very informative
{$IFDEF CPU32}
and (TJclAddr(Arguments) = TJclAddr(@Arguments) + SizeOf(Pointer))
{$ENDIF CPU32}
and (ExceptionCode <> MS_VC_EXCEPTION) // ignore TThread.NameThreadForDebugging
then
begin
DoExceptNotify(Arguments.ExceptObj, Arguments.ExceptAddr, False, GetFramePointer);
end;
Kernel32_RaiseException(ExceptionCode, ExceptionFlags, NumberOfArguments, PDWORD(Arguments));
end;
{$IFDEF BORLAND}
function HookedExceptObjProc(P: PExceptionRecord): Exception;
const
MS_VC_EXCEPTION = $406D1388;
var
NewResultExcCache: Exception; // TLS optimization
begin
if P.ExceptionCode <> MS_VC_EXCEPTION then
begin
Result := DoExceptFilter(P);
DoExceptNotify(Result, P^.ExceptionAddress, True, GetFramePointer);
NewResultExcCache := NewResultExc;
if NewResultExcCache <> nil then
Result := NewResultExcCache;
end
else
Result := SysUtils_ExceptObjProc(P);
end;
{$ENDIF BORLAND}
{$IFDEF FPC}
procedure HookedExceptProc(Obj : TObject; Addr : Pointer; FrameCount:Longint; Frame: PPointer);
var
NewResultExcCache: Exception; // TLS optimization
begin
DoExceptNotify(Obj, Addr, True, GetFramePointer);
NewResultExcCache := NewResultExc;
if NewResultExcCache <> nil then
SysUtils_ExceptProc(NewResultExcCache, Addr, FrameCount, Frame)
else
SysUtils_ExceptProc(Obj, Addr, FrameCount, Frame)
end;
{$ENDIF FPC}
{$IFNDEF STACKFRAMES_ON}
{$STACKFRAMES OFF}
{$ENDIF ~STACKFRAMES_ON}
// Do not change ordering of HookedRaiseException, HookedExceptObjProc and JclBelongsHookedCode routines
function JclBelongsHookedCode(Address: Pointer): Boolean;
begin
Result := (TJclAddr(@HookedRaiseException) < TJclAddr(@JclBelongsHookedCode)) and
(TJclAddr(@HookedRaiseException) <= TJclAddr(Address)) and
(TJclAddr(@JclBelongsHookedCode) > TJclAddr(Address));
end;
{$IFDEF BORLAND}
function JclAddExceptFilter(const FilterProc: TJclExceptFilterProc; Priority: TJclExceptNotifyPriority = npNormal): Boolean;
begin
Result := Assigned(FilterProc);
if Result then
with Filters.LockList do
try
Add(TFilterItem.Create(FilterProc, Priority));
finally
Filters.UnlockList;
end;
end;
{$ENDIF BORLAND}
function JclAddExceptNotifier(const NotifyProc: TJclExceptNotifyProc; Priority: TJclExceptNotifyPriority): Boolean;
begin
Result := Assigned(NotifyProc);
if Result then
with Notifiers.LockList do
try
Add(TNotifierItem.Create(NotifyProc, Priority));
finally
Notifiers.UnlockList;
end;
end;
function JclAddExceptNotifier(const NotifyProc: TJclExceptNotifyProcEx; Priority: TJclExceptNotifyPriority): Boolean;
begin
Result := Assigned(NotifyProc);
if Result then
with Notifiers.LockList do
try
Add(TNotifierItem.Create(NotifyProc, Priority));
finally
Notifiers.UnlockList;
end;
end;
function JclAddExceptNotifier(const NotifyMethod: TJclExceptNotifyMethod; Priority: TJclExceptNotifyPriority): Boolean;
begin
Result := Assigned(NotifyMethod);
if Result then
with Notifiers.LockList do
try
Add(TNotifierItem.Create(NotifyMethod, Priority));
finally
Notifiers.UnlockList;
end;
end;
{$IFDEF BORLAND}
function JclRemoveExceptFilter(const FilterProc: TJclExceptFilterProc): Boolean;
var
O: TFilterItem;
I: Integer;
begin
Result := Assigned(FilterProc);
if Result then
with Filters.LockList do
try
for I := 0 to Count - 1 do
begin
O := TFilterItem(Items[I]);
if @O.FExceptFilterProc = @FilterProc then
begin
O.Free;
Items[I] := nil;
end;
end;
Pack;
finally
Filters.UnlockList;
end;
end;
{$ENDIF BORLAND}
function JclRemoveExceptNotifier(const NotifyProc: TJclExceptNotifyProc): Boolean;
var
O: TNotifierItem;
I: Integer;
begin
Result := Assigned(NotifyProc);
if Result then
with Notifiers.LockList do
try
for I := 0 to Count - 1 do
begin
O := TNotifierItem(Items[I]);
if @O.FNotifyProc = @NotifyProc then
begin
O.Free;
Items[I] := nil;
end;
end;
Pack;
finally
Notifiers.UnlockList;
end;
end;
function JclRemoveExceptNotifier(const NotifyProc: TJclExceptNotifyProcEx): Boolean;
var
O: TNotifierItem;
I: Integer;
begin
Result := Assigned(NotifyProc);
if Result then
with Notifiers.LockList do
try
for I := 0 to Count - 1 do
begin
O := TNotifierItem(Items[I]);
if @O.FNotifyProcEx = @NotifyProc then
begin
O.Free;
Items[I] := nil;
end;
end;
Pack;
finally
Notifiers.UnlockList;
end;
end;
function JclRemoveExceptNotifier(const NotifyMethod: TJclExceptNotifyMethod): Boolean;
var
O: TNotifierItem;
I: Integer;
begin
Result := Assigned(NotifyMethod);
if Result then
with Notifiers.LockList do
try
for I := 0 to Count - 1 do
begin
O := TNotifierItem(Items[I]);
if (TMethod(O.FNotifyMethod).Code = TMethod(NotifyMethod).Code) and
(TMethod(O.FNotifyMethod).Data = TMethod(NotifyMethod).Data) then
begin
O.Free;
Items[I] := nil;
end;
end;
Pack;
finally
Notifiers.UnlockList;
end;
end;
procedure JclReplaceExceptObj(NewExceptObj: Exception);
begin
Assert(Recursive);
NewResultExc := NewExceptObj;
end;
{$IFDEF BORLAND}
function GetCppRtlBase: Pointer;
const
{$IFDEF COMPILER6} { Delphi/C++Builder 6 }
CppRtlVersion = 60;
{$ELSE ~COMPILER6}
{$IFDEF RTL185} { Delphi/C++Builder 2007 were aiming for
binary compatibility with BDS2006, which
complicates things a bit }
CppRtlVersion = 80;
{$ELSE ~RTL185}
{ Successive RTLDLL version numbers in the remaining cases: CB2006 has cc3270mt.dll,
CB2009 (= CB2006 + 2 releases) has cc3290mt.dll, CB2010 has cc32100mt.dll etc. }
CppRtlVersion = 70 + Trunc(RtlVersion - 18.0) * 10;
{$ENDIF ~RTL185}
{$ENDIF ~COMPILER6}
begin
Result := Pointer(GetModuleHandle(PChar(Format('cc32%dmt.dll', [CppRtlVersion]))));
{ 'Result = nil' means that the C++ RTL has been linked statically or is not available at all;
in this case TJclPeMapImgHooks.ReplaceImport() is a no-op. The base module is also being
hooked separately, so we're covered. }
end;
function HasCppRtl: Boolean;
begin
Result := GetCppRtlBase <> TJclPeMapImgHooks.SystemBase;
end;
{$ENDIF BORLAND}
function JclHookExceptions: Boolean;
var
RaiseExceptionAddressCache: Pointer;
begin
RaiseExceptionAddressCache := RaiseExceptionAddress;
{ Detect C++Builder applications and C++ packages loaded into Delphi applications.
Hook the C++ RTL regardless of ExceptionsHooked so that users can call JclHookException() after
loading a C++ package which might pull in the C++ RTL DLL. }
{$IFDEF BORLAND}
if HasCppRtl then
TJclPeMapImgHooks.ReplaceImport(GetCppRtlBase, kernel32, RaiseExceptionAddressCache, @HookedRaiseException);
{$ENDIF BORLAND}
if not ExceptionsHooked then
begin
Recursive := False;
with TJclPeMapImgHooks do
Result := ReplaceImport(SystemBase, kernel32, RaiseExceptionAddressCache, @HookedRaiseException);
if Result then
begin
@Kernel32_RaiseException := RaiseExceptionAddressCache;
{$IFDEF BORLAND}
SysUtils_ExceptObjProc := System.ExceptObjProc;
System.ExceptObjProc := @HookedExceptObjProc;
{$ENDIF BORLAND}
{$IFDEF FPC}
SysUtils_ExceptProc := System.ExceptProc;
System.ExceptProc := @HookedExceptProc;
{$ENDIF FPC}
end;
ExceptionsHooked := Result;
end
else
Result := True;
end;
function JclUnhookExceptions: Boolean;
begin
{$IFDEF BORLAND}
if HasCppRtl then
TJclPeMapImgHooks.ReplaceImport (GetCppRtlBase, kernel32, @HookedRaiseException, @Kernel32_RaiseException);
{$ENDIF BORLAND}
if ExceptionsHooked then
begin
with TJclPeMapImgHooks do
ReplaceImport(SystemBase, kernel32, @HookedRaiseException, @Kernel32_RaiseException);
{$IFDEF BORLAND}
System.ExceptObjProc := @SysUtils_ExceptObjProc;
@SysUtils_ExceptObjProc := nil;
{$ENDIF BORLAND}
{$IFDEF FPC}
System.ExceptProc := @SysUtils_ExceptProc;
@SysUtils_ExceptProc := nil;
{$ENDIF FPC}
@Kernel32_RaiseException := nil;
Result := True;
ExceptionsHooked := False;
end
else
Result := True;
end;
function JclExceptionsHooked: Boolean;
begin
Result := ExceptionsHooked;
end;
function JclHookExceptionsInModule(Module: HMODULE): Boolean;
begin
Result := ExceptionsHooked and
TJclPeMapImgHooks.ReplaceImport(Pointer(Module), kernel32, RaiseExceptionAddress, @HookedRaiseException);
end;
function JclUnhookExceptionsInModule(Module: HMODULE): Boolean;
begin
Result := ExceptionsHooked and
TJclPeMapImgHooks.ReplaceImport(Pointer(Module), kernel32, @HookedRaiseException, @Kernel32_RaiseException);
end;
{$IFDEF HOOK_DLL_EXCEPTIONS}
// Exceptions hooking in libraries
procedure JclHookExceptDebugHookProc(Module: HMODULE; Hook: Boolean); stdcall;
begin
if Hook then
HookExceptModuleList.HookModule(Module)
else
HookExceptModuleList.UnhookModule(Module);
end;
function CallExportedHookExceptProc(Module: HMODULE; Hook: Boolean): Boolean;
var
HookExceptProcPtr: PPointer;
HookExceptProc: TJclHookExceptDebugHook;
begin
HookExceptProcPtr := TJclHookExceptModuleList.JclHookExceptDebugHookAddr;
Result := Assigned(HookExceptProcPtr);
if Result then
begin
@HookExceptProc := HookExceptProcPtr^;
if Assigned(HookExceptProc) then
HookExceptProc(Module, True);
end;
end;
{$ENDIF HOOK_DLL_EXCEPTIONS}
function JclInitializeLibrariesHookExcept: Boolean;
begin
{$IFDEF HOOK_DLL_EXCEPTIONS}
if IsLibrary then
Result := CallExportedHookExceptProc(SystemTObjectInstance, True)
else
begin
if not Assigned(HookExceptModuleList) then
HookExceptModuleList := TJclHookExceptModuleList.Create;
Result := True;
end;
{$ELSE HOOK_DLL_EXCEPTIONS}
Result := True;
{$ENDIF HOOK_DLL_EXCEPTIONS}
end;
function JclHookedExceptModulesList(out ModulesList: TJclModuleArray): Boolean;
begin
{$IFDEF HOOK_DLL_EXCEPTIONS}
Result := Assigned(HookExceptModuleList);
if Result then
HookExceptModuleList.List(ModulesList);
{$ELSE HOOK_DLL_EXCEPTIONS}
Result := False;
SetLength(ModulesList, 0);
{$ENDIF HOOK_DLL_EXCEPTIONS}
end;
{$IFDEF HOOK_DLL_EXCEPTIONS}
procedure FinalizeLibrariesHookExcept;
begin
FreeAndNil(HookExceptModuleList);
if IsLibrary then
CallExportedHookExceptProc(SystemTObjectInstance, False);
end;
//=== { TJclHookExceptModuleList } ===========================================
constructor TJclHookExceptModuleList.Create;
begin
inherited Create;
FModules := TThreadList.Create;
HookStaticModules;
JclHookExceptDebugHook := @JclHookExceptDebugHookProc;
end;
destructor TJclHookExceptModuleList.Destroy;
begin
JclHookExceptDebugHook := nil;
FreeAndNil(FModules);
inherited Destroy;
end;
procedure TJclHookExceptModuleList.HookModule(Module: HMODULE);
begin
with FModules.LockList do
try
if IndexOf(Pointer(Module)) = -1 then
begin
Add(Pointer(Module));
JclHookExceptionsInModule(Module);
end;
finally
FModules.UnlockList;
end;
end;
procedure TJclHookExceptModuleList.HookStaticModules;
var
ModulesList: TStringList;
I: Integer;
Module: HMODULE;
begin
ModulesList := nil;
with FModules.LockList do
try
ModulesList := TStringList.Create;
if LoadedModulesList(ModulesList, GetCurrentProcessId, True) then
for I := 0 to ModulesList.Count - 1 do
begin
Module := HMODULE(ModulesList.Objects[I]);
if GetProcAddress(Module, JclHookExceptDebugHookName) <> nil then
HookModule(Module);
end;
finally
FModules.UnlockList;
ModulesList.Free;
end;
end;
class function TJclHookExceptModuleList.JclHookExceptDebugHookAddr: Pointer;
var
HostModule: HMODULE;
begin
HostModule := GetModuleHandle(nil);
Result := GetProcAddress(HostModule, JclHookExceptDebugHookName);
end;
procedure TJclHookExceptModuleList.List(out ModulesList: TJclModuleArray);
var
I: Integer;
begin
with FModules.LockList do
try
SetLength(ModulesList, Count);
for I := 0 to Count - 1 do
ModulesList[I] := HMODULE(Items[I]);
finally
FModules.UnlockList;
end;
end;
procedure TJclHookExceptModuleList.UnhookModule(Module: HMODULE);
begin
with FModules.LockList do
try
Remove(Pointer(Module));
finally
FModules.UnlockList;
end;
end;
{$ENDIF HOOK_DLL_EXCEPTIONS}
initialization
Notifiers := TThreadList.Create;
{$IFDEF BORLAND}
Filters := TThreadList.Create;
{$ENDIF BORLAND}
{$IFDEF UNITVERSIONING}
RegisterUnitVersion(HInstance, UnitVersioning);
{$ENDIF UNITVERSIONING}
finalization
{$IFDEF UNITVERSIONING}
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}
{$IFDEF HOOK_DLL_EXCEPTIONS}
FinalizeLibrariesHookExcept;
{$ENDIF HOOK_DLL_EXCEPTIONS}
FreeThreadObjList(Notifiers);
{$IFDEF BORLAND}
FreeThreadObjList(Filters);
{$ENDIF BORLAND}
end.