-
Notifications
You must be signed in to change notification settings - Fork 11
/
mlBPLLoader.pas
445 lines (409 loc) · 14.2 KB
/
mlBPLLoader.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
{*******************************************************************************
* Created by Vladimir Georgiev, 2014 *
* *
* Description: *
* Unit implementing the TBPLLoader class that provides additional methods for *
* loading BPLs. The code for the methods is based on the source of LoadPackage*
* and UnloadPackage from the Embarcadero SysUtils unit *
* The code handles the internal dependency of one package on another and *
* loading of the dependencies *
* *
* References: *
* http://stackoverflow.com/questions/7566954/why-code-in-any-unit-finalization-section-of-a-package-is-not-executed-at-start
* http://hallvards.blogspot.com/2005/08/ultimate-delphi-ide-start-up-hack.html*
*******************************************************************************}
{$I mlDefines.inc}
unit mlBPLLoader;
interface
uses
Windows,
SysUtils,
SysConst,
Classes,
HashTrie,
mlBaseLoader,
mlTypes;
type
PPackageInfoHeader = ^TPackageInfoHeader;
TPackageInfoHeader = packed record
Flags: Cardinal;
RequiresCount: Integer;
end;
const
cBucketSize = 1021; // better distribution than 1024
type
PPkgName = ^TPkgName;
TPkgName = packed record
HashCode: Byte;
Name: array[0..255] of Char;
end;
PUnitName = ^TUnitName;
TUnitName = packed record
Flags : Byte;
HashCode: Byte;
Name: array[0..255] of Char;
end;
PUnitHashEntry = ^TUnitHashEntry;
TUnitHashEntry = record
Next, Prev: PUnitHashEntry;
LibModule: PLibModule;
UnitName: PChar;
DupsAllowed: Boolean;
FullHash: Cardinal;
end;
TUnitHashArray = array of TUnitHashEntry;
TUnitHashBuckets = array[0..cBucketSize-1] of PUnitHashEntry;
PModuleInfo = ^TModuleInfo;
TModuleInfo = record
Validated: Boolean;
UnitHashArray: TUnitHashArray;
end;
type
TBPLLoader = class(TMlBaseLoader)
private
fPackageInitialized: Boolean;
function HashName(Name: PChar): Cardinal;
function FindLibModule(Module: HModule): PLibModule;
function PackageInfoTable(Module: HMODULE; aSelfCheck: Boolean): PPackageInfoHeader;
procedure CheckForDuplicateUnits(Module: HMODULE; aValidatePackage: TValidatePackageProc);
procedure InitializePackage(Module: HMODULE; aValidatePackage: TValidatePackageProc);
procedure FinalizePackage(Module: HMODULE);
procedure ModuleUnloaded(Module: Longword);
public
constructor Create; overload;
constructor Create(aMem: TStream; const aLibFileName: String; aValidatePackage: TValidatePackageProc = nil); overload;
destructor Destroy; override;
procedure LoadFromStream(aMem: TStream; const aLibFileName: String;
aValidatePackage: TValidatePackageProc = nil; aHandleHash: TIntegerHashTrie
= nil; aNamesHash: TStringHashTrie = nil);
procedure Unload; overload;
end;
implementation
uses
mlLibrary;
var
SysInitHC: Cardinal;
ValidatedUnitHashBuckets: TUnitHashBuckets;
UnitHashBuckets: TUnitHashBuckets;
{$IFDEF VER130}
function GetModuleName(Module: HMODULE): string;
var
ModName: array[0..MAX_PATH] of Char;
begin
SetString(Result, ModName, GetModuleFileName(Module, ModName, SizeOf(ModName)));
end;
{$ENDIF VER130}
function TBPLLoader.HashName(Name: PChar): Cardinal;
asm
PUSH ESI
PUSH EBX
MOV ESI, Name
XOR EAX, EAX
@@loop:
ROL EAX, 5
MOV BL, [ESI]
CMP BL, 0
JE @@done
CMP BL, 'A'
JL @@LowerCased
CMP BL, 'Z'
JG @@LowerCased
OR BL, 20H // make lower case
@@LowerCased:
XOR AL, BL
INC ESI
JMP @@loop
@@done:
POP EBX
POP ESI
RET
end;
function TBPLLoader.FindLibModule(Module: HModule): PLibModule;
begin
Result := LibModuleList;
while Result <> nil do
begin
if Result.Instance = Cardinal(Module) then
Exit;
Result := Result.Next;
end;
end;
/// Try to find the PACKAGEINFO resource to check the dependencies. Check the current module,
/// then a disk loaded, and then a mem loaded one
function TBPLLoader.PackageInfoTable(Module: HMODULE; aSelfCheck: Boolean): PPackageInfoHeader;
var
ResInfo: HRSRC;
Data: THandle;
begin
Result := nil;
try
if aSelfCheck then
begin
ResInfo := FindResourceMl('PACKAGEINFO', RT_RCDATA);
Data := LoadResourceMl(ResInfo);
end else
begin
ResInfo := Windows.FindResource(Module, 'PACKAGEINFO', RT_RCDATA);
if ResInfo <> 0 then
Data := Windows.LoadResource(Module, ResInfo)
else
begin
{$IFDEF MLHOOKED}
ResInfo := FindResource(Module, 'PACKAGEINFO', RT_RCDATA);
Data := LoadResource(Module, ResInfo);
{$ELSE}
ResInfo := FindResourceMem(Module, 'PACKAGEINFO', RT_RCDATA);
Data := LoadResourceMem(Module, ResInfo);
{$ENDIF MLHOOKED}
end;
end;
except
Data := 0;
end;
if Data <> 0 then
Result := PPackageInfoHeader(Data);
end;
procedure TBPLLoader.CheckForDuplicateUnits(Module: HMODULE; aValidatePackage: TValidatePackageProc);
var
ModuleFlags: Cardinal;
function IsUnitPresent(UnitName: PChar; HashCode: Cardinal; Module: HMODULE; const Buckets: TUnitHashBuckets; const
ModuleName: string; var UnitPackage: string; ModuleHashCode: Cardinal): Boolean;
var
HashEntry: PUnitHashEntry;
{$IFDEF MLHOOKED}
Buf: array[0..MAX_PATH + 1] of Char;
{$ENDIF MLHOOKED}
begin
if ((HashCode <> SysInitHC) or (StrIComp(UnitName, 'SysInit') <> 0)) and
((HashCode <> ModuleHashCode) or (StrIComp(UnitName, PChar(ModuleName)) <> 0)) then
begin
HashEntry := Buckets[HashCode mod cBucketSize];
while HashEntry <> nil do
begin
if (HashEntry.DupsAllowed = (ModuleFlags and pfIgnoreDupUnits <> 0)) and
((HashEntry.FullHash = HashCode) and (StrIComp(UnitName, HashEntry.UnitName) = 0)) then
begin
// Get the module name that contains the duplicate unit. It could be either a disk or mem loaded one so try both
UnitPackage := ChangeFileExt(ExtractFileName(GetModuleName(HMODULE(HashEntry.LibModule.Instance))), '');
if UnitPackage = '' then
try
{$IFDEF MLHOOKED}
if GetModuleFileName(HashEntry.LibModule.Instance, Buf, Length(Buf)) <> 0 then
UnitPackage := Buf
else
UnitPackage := '';
{$ELSE}
UnitPackage := ChangeFileExt(ExtractFileName(GetModuleFileNameMem(TLibHandle(HashEntry.LibModule.Instance))), '');
{$ENDIF MLHOOKED}
except
UnitPackage := '';
end;
Result := True;
Exit;
end;
HashEntry := HashEntry.Next;
end;
end;
Result := False;
end;
procedure InternalUnitCheck(Module: HModule; aSelfCheck: Boolean);
var
I, J: Integer;
InfoTable: PPackageInfoHeader;
UnitPackage: string;
ModuleName: string;
PkgName: PPkgName;
UName: PUnitName;
Count: Integer;
LibModule: PLibModule;
Validated: Boolean;
HC: Cardinal;
ModuleInfo: PModuleInfo;
Buckets: ^TUnitHashBuckets;
ModuleHC: Cardinal;
begin
InfoTable := PackageInfoTable(Module, aSelfCheck);
if (InfoTable <> nil) and (InfoTable.Flags and pfModuleTypeMask = pfPackageModule) then
begin
if ModuleFlags = 0 then
ModuleFlags := InfoTable.Flags;
LibModule := FindLibModule(Module);
if (LibModule <> nil) and (LibModule.Reserved <> 0) then
Exit;
Validated := Assigned(AValidatePackage) and AValidatePackage(Module);
if aSelfCheck then
ModuleName := ChangeFileExt(ExtractFileName(Name), '')
else
ModuleName := ChangeFileExt(ExtractFileName(GetExternalLibraryName(Module)), '');
PkgName := PPkgName(Integer(InfoTable) + SizeOf(InfoTable^));
Count := InfoTable.RequiresCount;
for I := 0 to Count - 1 do
begin
InternalUnitCheck(GetExternalLibraryHandle(PChar(ChangeFileExt(PkgName^.Name, '.bpl'))), false);
Inc(Integer(PkgName), StrLen(PkgName.Name) + 2);
end;
Count := Integer(Pointer(PkgName)^);
UName := PUnitName(Integer(PkgName) + 4);
if LibModule <> nil then
begin
New(ModuleInfo);
ModuleInfo.Validated := Validated;
if Validated then
Buckets := @ValidatedUnitHashBuckets
else
Buckets := @UnitHashBuckets;
LibModule.Reserved := Integer(ModuleInfo);
// don't include SysInit;
SetLength(ModuleInfo.UnitHashArray, Count - 1);
J := 0;
ModuleHC := HashName(PChar(Pointer(ModuleName)));
for I := 0 to Count - 1 do
begin
HC := HashName(UName.Name);
// Test Flags to ignore weak package units
if ((HC <> SysInitHC) or (StrIComp(UName^.Name, 'SysInit') <> 0)) and
(UName^.Flags and ufWeakPackageUnit = 0) then
begin
// Always check against the unvalidated packages
if IsUnitPresent(UName^.Name, HC, Module, UnitHashBuckets, ModuleName, UnitPackage, ModuleHC) or
// if the package is not validateed also check it against the validated ones
(not Validated and IsUnitPresent(UName^.Name, HC, Module, ValidatedUnitHashBuckets, ModuleName, UnitPackage, ModuleHC)) then
raise EPackageError.CreateResFmt(@SDuplicatePackageUnit, [ModuleName, UName^.Name, UnitPackage]);
ModuleInfo.UnitHashArray[J].UnitName := @UName.Name;
ModuleInfo.UnitHashArray[J].LibModule := LibModule;
ModuleInfo.UnitHashArray[J].DupsAllowed := InfoTable.Flags and pfIgnoreDupUnits <> 0;
ModuleInfo.UnitHashArray[J].Prev := nil;
ModuleInfo.UnitHashArray[J].FullHash := HC;
HC := HC mod cBucketSize;
ModuleInfo.UnitHashArray[J].Next := Buckets[HC];
Buckets[HC] := @ModuleInfo.UnitHashArray[J];
if ModuleInfo.UnitHashArray[J].Next <> nil then
ModuleInfo.UnitHashArray[J].Next.Prev := Buckets[HC];
Inc(J);
end;
Inc(Integer(UName), StrLen(UName.Name) + 3);
end;
end;
end;
end;
begin
if SysInitHC = 0 then
SysInitHC := HashName('SysInit');
ModuleFlags := 0;
InternalUnitCheck(Module, true);
end;
procedure TBPLLoader.InitializePackage(Module: HMODULE; aValidatePackage: TValidatePackageProc);
type
TPackageLoad = procedure;
var
PackageLoad: TPackageLoad;
begin
CheckForDuplicateUnits(Module, aValidatePackage);
@PackageLoad := GetFunctionAddress('Initialize'); //Do not localize
if Assigned(PackageLoad) then
PackageLoad
else
raise EPackageError.CreateFmt(sInvalidPackageFile, [Name]);
end;
procedure TBPLLoader.FinalizePackage(Module: HMODULE);
type
TPackageUnload = procedure;
var
PackageUnload: TPackageUnload;
begin
@PackageUnload := GetFunctionAddress('Finalize'); //Do not localize
if Assigned(PackageUnload) then
PackageUnload
else
raise EPackageError.CreateRes(@sInvalidPackageHandle);
end;
procedure TBPLLoader.ModuleUnloaded(Module: Longword);
var
LibModule: PLibModule;
ModuleInfo: PModuleInfo;
I: Integer;
HC: Cardinal;
Buckets: ^TUnitHashBuckets;
begin
LibModule := FindLibModule(Module);
if (LibModule <> nil) and (LibModule.Reserved <> 0) then
begin
ModuleInfo := PModuleInfo(LibModule.Reserved);
if ModuleInfo.Validated then
Buckets := @ValidatedUnitHashBuckets
else
Buckets := @UnitHashBuckets;
for I := Low(ModuleInfo.UnitHashArray) to High(ModuleInfo.UnitHashArray) do
begin
if ModuleInfo.UnitHashArray[I].Prev <> nil then
ModuleInfo.UnitHashArray[I].Prev.Next := ModuleInfo.UnitHashArray[I].Next
else if ModuleInfo.UnitHashArray[I].UnitName <> nil then
begin
HC := HashName(ModuleInfo.UnitHashArray[I].UnitName) mod cBucketSize;
if Buckets[HC] = @ModuleInfo.UnitHashArray[I] then
Buckets[HC] := ModuleInfo.UnitHashArray[I].Next;
end;
if ModuleInfo.UnitHashArray[I].Next <> nil then
ModuleInfo.UnitHashArray[I].Next.Prev := ModuleInfo.UnitHashArray[I].Prev;
end;
Dispose(ModuleInfo);
LibModule.Reserved := 0;
end;
end;
constructor TBPLLoader.Create;
begin
inherited;
end;
constructor TBPLLoader.Create(aMem: TStream; const aLibFileName: String; aValidatePackage: TValidatePackageProc = nil);
begin
Create;
LoadFromStream(aMem, aLibFileName); // Load the passed stream. Validation is done during loading
end;
destructor TBPLLoader.Destroy;
begin
try
// Unload can raise exceptions if forcefully unloading the libs in wrong order, so make sure the inherited
// destructor is called to free the memory allocated
if Loaded then
Unload;
finally
inherited;
end;
end;
procedure TBPLLoader.LoadFromStream(aMem: TStream; const aLibFileName: String;
aValidatePackage: TValidatePackageProc = nil; aHandleHash: TIntegerHashTrie
= nil; aNamesHash: TStringHashTrie = nil);
begin
if aLibFileName = '' then
raise EMlLibraryLoadError.Create('The package file name can not be empty');
if GetModuleHandle(PChar(aLibFileName)) <> 0 then
raise EMlLibraryLoadError.CreateFmt('The %s package is already loaded from disk with the regular LoadPackage.' + #13#10 +
' Loading it again from memory will result in unpredicted behaviour.', [aLibFileName]);
inherited LoadFromStream(aMem, aLibFileName, aHandleHash, aNamesHash);
try
Assert(Handle <> 0, 'The Handle of a package must be assigned before loading it from a stream. It is used in RegisterModule');
InitializePackage(Cardinal(Handle), aValidatePackage);
fPackageInitialized := true;
except
Unload;
raise;
end;
end;
procedure TBPLLoader.Unload;
var
LibModule: PLibModule;
begin
Assert(Handle <> 0, 'The Handle of a package must not be 0 when calling UnregisterModule');
try
// FinalizePackage can raise exceptions if forcefully unloading the libs in wrong order, so make sure the rest
// of the unloading is complete to avoid classes remaining registered
if fPackageInitialized then
FinalizePackage(Handle);
finally
LibModule := FindLibModule(Handle);
if Assigned(LibModule) then
ModuleUnloaded(Handle);
inherited;
end;
end;
end.