-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUtils.pas
1262 lines (1053 loc) · 33.4 KB
/
NtUtils.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
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit NtUtils;
{
Base definitions for the NtUtils library.
}
interface
uses
Ntapi.WinNt, Ntapi.ntdef, Ntapi.ntseapi, Ntapi.WinError,
DelphiApi.Reflection, DelphiUtils.AutoObjects;
var
// Controls whether TNtxStatus should capture stack traces on failure.
// When enabled, you should also configure generation of debug symbols via
// Project -> Options -> Building -> Delphi Compiler -> Linking -> Map File.
// This switch controls creation of .map files which you can later convert
// into .dbg using the map2dbg tool. Optionally, you can go one step further
// and convert the .dbg file into a more modern .pdb via the cv2pdb tool.
// For more details, see https://stackoverflow.com/questions/9422703 and
// https://github.com/rainers/cv2pdb
// You can also configure the following post-build events for the project:
// map2dbg.exe $(OUTPUTPATH)
// cv2pdb64.exe -n -s. -p$(OUTPUTNAME).pdb $(OUTPUTPATH)
CaptureStackTraces: Boolean = False;
{ Forwarded definitions }
type
// Forward the types for automatic lifetime management
IAutoReleasable = DelphiUtils.AutoObjects.IAutoReleasable;
IAutoObject = DelphiUtils.AutoObjects.IAutoObject;
IAutoPointer = DelphiUtils.AutoObjects.IAutoPointer;
TMemory = DelphiUtils.AutoObjects.TMemory;
IMemory = DelphiUtils.AutoObjects.IMemory;
IHandle = DelphiUtils.AutoObjects.IHandle;
Auto = DelphiUtils.AutoObjects.Auto;
// Define commonly used IAutoPointer/IMemory aliases
IContext = IMemory<PContext>;
IEnvironment = IMemory<PEnvironment>;
ISecurityDescriptor = IAutoPointer<PSecurityDescriptor>;
IAcl = IAutoPointer<PAcl>;
ISid = IAutoPointer<PSid>;
TGroup = record
Sid: ISid;
Attributes: TGroupAttributes;
class function From(
const Sid: ISid;
Attributes: TGroupAttributes
): TGroup; static;
end;
{ Annotations }
// A few macros/aliases for checking bit flags and better expressing intent.
// Note: do not use with 64-bit or native integers!
BitTest = LongBool;
HasAny = LongBool;
// Forward SAL annotations
InAttribute = DelphiApi.Reflection.InAttribute;
OutAttribute = DelphiApi.Reflection.OutAttribute;
OptAttribute = DelphiApi.Reflection.OptAttribute;
MayReturnNilAttribute = DelphiApi.Reflection.MayReturnNilAttribute;
AccessAttribute = DelphiApi.Reflection.AccessAttribute;
ThreadSafeAttribute = DelphiApi.Reflection.ThreadSafeAttribute;
{ Error handling }
type
[NamingStyle(nsCamelCase, 'lc')]
TLastCallType = (lcOtherCall, lcOpenCall, lcQuerySetCall);
[NamingStyle(nsCamelCase, 'ic')]
TInfoClassOperation = (icUnknown, icQuery, icSet, icRead, icWrite, icControl,
icPerform, icParse, icMarshal);
TExpectedAccess = record
AccessMask: TAccessMask;
AccessMaskType: Pointer;
end;
TLastCallInfo = record
Location: String;
Parameter: String;
StackTrace: TArray<Pointer>;
ExpectedPrivilege: TSeWellKnownPrivilege;
ExpectedAccess: TArray<TExpectedAccess>;
procedure CaptureStackTrace;
procedure OpensForAccess<T>(Mask: T);
procedure Expects<T>(AccessMask: T);
procedure UsesInfoClass<T>(
InfoClassEnum: T;
Operation: TInfoClassOperation
);
case CallType: TLastCallType of
lcOpenCall: (
AccessMask: TAccessMask;
AccessMaskType: Pointer
);
lcQuerySetCall: (
InfoClassOperation: TInfoClassOperation;
InfoClass: Cardinal;
InfoClassType: Pointer
);
end;
// An enhanced NTSTATUS that stores additional information about the last
// operation and the location of failure.
TNtxStatus = record
private
FStatus: NTSTATUS;
function GetWin32Error: TWin32Error;
function GetHResult: HResult;
function GetLocation: String;
procedure FromWin32Error(const Value: TWin32Error);
procedure FromWin32ErrorOrSuccess(const Value: TWin32Error);
procedure FromLastWin32Error(const RetValue: Boolean);
procedure FromHResult(const Value: HResult);
procedure FromHResultAllowFalse(const Value: HResult);
procedure FromStatus(const Value: NTSTATUS);
procedure SetLocation(const Value: String); inline;
public
LastCall: TLastCallInfo;
// Note: setting location resets the rest of the last call information
property Location: String read GetLocation write SetLocation;
// Creation & conversion
property Status: NTSTATUS read FStatus write FromStatus;
property Win32Error: TWin32Error read GetWin32Error write FromWin32Error;
property Win32ErrorOrSuccess: TWin32Error write FromWin32ErrorOrSuccess;
property HResult: HResult read GetHResult write FromHResult;
property HResultAllowFalse: HResult write FromHResultAllowFalse;
property Win32Result: Boolean write FromLastWin32Error;
// Validation
function IsSuccess: Boolean; inline;
function IsWin32: Boolean;
function IsHResult: Boolean;
function Matches(Status: NTSTATUS; Location: String): Boolean; inline;
// Copy into another variable
function SaveTo(var Target: TNtxStatus): TNtxStatus;
// Returns boolean indicating whether iteration succeeded. Converts a status
// into success on graceful end of iteration but forwards other errors.
// Use: `while NtxGetNextSomething(Entry).HasEntry(Result) do`
function HasEntry(out Target: TNtxStatus): Boolean;
// Raise an unsuccessful status as an exception. When using, consider
// including NtUiLib.Exceptions for better integration with Delphi.
procedure RaiseOnError;
// A custom callback for raising exceptions (provided by NtUiLib.Exceptions)
class var NtxExceptionRaiser: procedure (const Status: TNtxStatus);
end;
PNtxStatus = ^TNtxStatus;
const
NtxSuccess: TNtxStatus = (FStatus: 0);
{ Stack tracing & exceptions }
// Get the address of the next instruction after the call
function RtlxNextInstruction: Pointer;
// Capture a stack trace of the current thread
function RtlxCaptureStackTrace(
FramesToSkip: Integer = 0
): TArray<Pointer>;
// Raise an external exception (when System.SysUtils is not available)
procedure RtlxRaiseException(
Status: NTSTATUS;
[in, opt] Address: Pointer
);
type
// An alternative base exception class when System.SysUtils is not available
ENoSysUtilsException = class
private
FExceptionCode: NTSTATUS;
FExceptionFlags: TExceptionFlags;
FExceptionAddress: Pointer;
FStackTrace: TArray<Pointer>;
public
property ExceptionCode: NTSTATUS read FExceptionCode;
property ExceptionFlags: TExceptionFlags read FExceptionFlags;
property ExceptionAddress: Pointer read FExceptionAddress;
property StackTrace: TArray<Pointer> read FStackTrace;
constructor Create(P: PExceptionRecord);
end;
// An access violation exception class when System.SysUtils is not available
ENoSysUtilsAccessViolation = class (ENoSysUtilsException)
private
FAccessOperation: TExceptionAccessViolationOperation;
FAccessAddress: Pointer;
public
property AccessOperation: TExceptionAccessViolationOperation read FAccessOperation;
property AccessAddress: Pointer read FAccessAddress;
constructor Create(P: PExceptionRecord);
end;
{ Buffer Expansion }
const
BUFFER_LIMIT = 1024 * 1024 * 1024; // 1 GiB
type
TBufferGrowthMethod = function (
const Memory: IMemory;
Required: NativeUInt
): NativeUInt;
// Slightly adjust required size with + 12% to mitigate fluctuations
function Grow12Percent(
const Memory: IMemory;
Required: NativeUInt
): NativeUInt;
// Re-allocate the buffer according to the required size
function NtxExpandBufferEx(
var Status: TNtxStatus;
var Memory: IMemory;
Required: NativeUInt;
[opt] GrowthMethod: TBufferGrowthMethod
): Boolean;
{ String functions }
type
// A macro for retrieving the buffer of a Delphi string via a typecast
RefStrOrEmtry = PWideChar;
// Reference the buffer of a Delphi string or nil, for empty input
[Result: MayReturnNil]
function RefStrOrNil(
[in, opt] const S: String
): PWideChar;
// Count the number of bytes required to store a string without terminating zero
[Result: NumberOfBytes]
function StringSizeNoZero(
[in, opt] const S: String
): NativeUInt;
// Count the number of bytes required to store a string with terminating zero
[Result: NumberOfBytes]
function StringSizeZero(
[in, opt] const S: String
): NativeUInt;
// Make a UNICODE_STRING that references a Delphi string
function RtlxInitUnicodeString(
out Destination: TNtUnicodeString;
[opt] const Source: String
): TNtxStatus;
// Make a ANSI_STRING that references a Delphi string
function RtlxInitAnsiString(
out Destination: TNtAnsiString;
[opt] const Source: AnsiString
): TNtxStatus;
// Write a string into a buffer
procedure MarshalString(
[in] const Source: String;
[out, WritesTo] Buffer: Pointer
);
// Write an NT unicode string into a buffer
function RtlxMarshalUnicodeString(
[in] const Source: String;
[out] out Target: TNtUnicodeString;
[out, WritesTo] Buffer: Pointer
): TNtxStatus;
{ Other helper functions }
// Get a handle value from IHandle or a defulat, when not provided
function HandleOrDefault(
[in, opt] const hxObject: IHandle;
[in, opt] Default: THandle = 0
): THandle;
// Pseudo-handles
function NtxCurrentProcess: IHandle;
function NtxCurrentThread: IHandle;
function NtxCurrentProcessToken: IHandle;
function NtxCurrentThreadToken: IHandle;
function NtxCurrentEffectiveToken: IHandle;
{ Object Attributes }
type
// A Delphi wrapper for a commonly used OBJECT_ATTRIBUTES type that allows
// building it with a simplified (fluent) syntax.
IObjectAttributes = interface
// Fluent builder
function UseRoot(const RootDirectory: IHandle): IObjectAttributes;
function UseName(const ObjectName: String): IObjectAttributes;
function UseAttributes(const Attributes: TObjectAttributesFlags): IObjectAttributes;
function UseSecurity(const SecurityDescriptor: ISecurityDescriptor): IObjectAttributes;
function UseImpersonation(const Level: TSecurityImpersonationLevel): IObjectAttributes;
function UseEffectiveOnly(const Enabled: Boolean = True): IObjectAttributes;
function UseContextTracking(const Enabled: Boolean = True): IObjectAttributes;
function UseDesiredAccess(const AccessMask: TAccessMask): IObjectAttributes;
// Accessor functions
function GetRoot: IHandle;
function GetName: String;
function GetAttributes: TObjectAttributesFlags;
function GetSecurity: ISecurityDescriptor;
function GetImpersonation: TSecurityImpersonationLevel;
function GetEffectiveOnly: Boolean;
function GetContextTracking: Boolean;
function GetDesiredAccess: TAccessMask;
// Accessors
property Root: IHandle read GetRoot;
property Name: String read GetName;
property Attributes: TObjectAttributesFlags read GetAttributes;
property Security: ISecurityDescriptor read GetSecurity;
property Impersonation: TSecurityImpersonationLevel read GetImpersonation;
property EffectiveOnly: Boolean read GetEffectiveOnly;
property ContextTracking: Boolean read GetContextTracking;
property DesiredAccess: TAccessMask read GetDesiredAccess;
// Finalize the builder and make a reference to the underlying structure.
// Note: the operation might fail because UNICODE_STRING for the name has a
// lower limit on the number of characters than Delphi strings.
function Build(out Reference: PObjectAttributes): TNtxStatus;
end;
// Make an instance of an object attribute builder
function AttributeBuilder(
[in, opt] const Template: IObjectAttributes = nil
): IObjectAttributes;
// Get an NT object attribute pointer from an interfaced object attributes
function AttributesRefOrNil(
[out, MayReturnNil] out Reference: PObjectAttributes;
[in, opt] const ObjAttributes: IObjectAttributes
): TNtxStatus;
// Prepare and reference security attributes from object attributes
function ReferenceSecurityAttributes(
[out] out SA: TSecurityAttributes;
[in, opt] const ObjectAttributes: IObjectAttributes
): PSecurityAttributes;
// Let the caller override the default access mask via Object Attributes when
// creating kernel objects.
function AccessMaskOverride(
[in] DefaultAccess: TAccessMask;
[in, opt] const ObjAttributes: IObjectAttributes
): TAccessMask;
{ Shared delayed free functions }
// Free a string buffer using RtlFreeUnicodeString after use
function RtlxDelayFreeUnicodeString(
[in] Buffer: PNtUnicodeString
): IAutoReleasable;
// Free a SID buffer using RtlFreeSid after use
function RtlxDelayFreeSid(
[in] Buffer: PSid
): IAutoReleasable;
// Free a buffer using LocalFree after use
function AdvxDelayLocalFree(
[in] Buffer: Pointer
): IAutoReleasable;
{ AutoObjects extensions }
type
TNtxOperation = reference to function : TNtxStatus;
TNtxEnumeratorProvider<T> = reference to function (out Next: T): TNtxStatus;
NtxAuto = class abstract
// Use an anonymous TNtxStatus-aware function as a for-in iterator
// Note: when the Status parameter is not provided, iteration will report
// errors via exceptions.
class function Iterate<T>(
[out, opt] Status: PNtxStatus;
[in] Provider: TNtxEnumeratorProvider<T>
): IEnumerable<T>; static;
// Same as above but with a one-time call to Prepare
class function IterateEx<T>(
[out, opt] Status: PNtxStatus;
[in] Prepare: TNtxOperation;
[in] Provider: TNtxEnumeratorProvider<T>
): IEnumerable<T>; static;
end;
// Internal; call NtxAuto.Iterate instead.
// A wrapper for anonymous TNtxStatus-aware for-in loop providers
TNtxAnonymousEnumerator<T> = class (TInterfacedObject, IEnumerator<T>,
IEnumerable<T>)
protected
FCurrent: T;
FIsPrepared: Boolean;
FPrepare: TNtxOperation;
FProvider: TNtxEnumeratorProvider<T>;
FStatus: PNtxStatus;
private
function GetCurrent: TObject; // legacy (untyped)
function GetEnumerator: IEnumerator; // legacy (untyped)
public
constructor Create(
[out, opt] Status: PNtxStatus;
[in, opt] const Prepare: TNtxOperation;
[in] const Provider: TNtxEnumeratorProvider<T>
);
procedure Reset;
function MoveNext: Boolean;
function GetCurrentT: T;
function GetEnumeratorT: IEnumerator<T>;
function IEnumerator<T>.GetCurrent = GetCurrentT;
function IEnumerable<T>.GetEnumerator = GetEnumeratorT;
end;
implementation
uses
Ntapi.ntrtl, Ntapi.ntstatus, Ntapi.ntpebteb, Ntapi.ntpsapi, Ntapi.WinBase,
NtUtils.Errors;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ TGroup }
class function TGroup.From;
begin
Result.Sid := Sid;
Result.Attributes := Attributes;
end;
{ TLastCallInfo }
procedure TLastCallInfo.CaptureStackTrace;
begin
StackTrace := RtlxCaptureStackTrace(3);
end;
procedure TLastCallInfo.Expects<T>;
var
Mask: TAccessMask absolute AccessMask;
begin
if Mask = 0 then
Exit;
// Add new access mask
SetLength(ExpectedAccess, Length(ExpectedAccess) + 1);
ExpectedAccess[High(ExpectedAccess)].AccessMask := Mask;
ExpectedAccess[High(ExpectedAccess)].AccessMaskType := TypeInfo(T);
end;
procedure TLastCallInfo.OpensForAccess<T>;
var
AsAccessMask: TAccessMask absolute Mask;
begin
CallType := lcOpenCall;
AccessMask := AsAccessMask;
AccessMaskType := TypeInfo(T);
end;
procedure TLastCallInfo.UsesInfoClass<T>;
var
AsByte: Byte absolute InfoClassEnum;
AsWord: Word absolute InfoClassEnum;
AsCardinal: Cardinal absolute InfoClassEnum;
begin
CallType := lcQuerySetCall;
InfoClassOperation := Operation;
InfoClassType := TypeInfo(T);
case SizeOf(T) of
SizeOf(Byte): InfoClass := AsByte;
SizeOf(Word): InfoClass := AsWord;
SizeOf(Cardinal): InfoClass := AsCardinal;
end;
end;
{ TNtxStatus }
procedure TNtxStatus.FromHResult;
begin
// S_FALSE is a controversial value that is successful, but indicates a
// failure. Its precise meaning depends on the context, so whenever we expect
// it as a result we should adjust the logic correspondingly. By default,
// consider it unsuccessful. For the opposite behavior, use HResultAllowFalse.
if Value = S_FALSE then
Status := System.HResult(S_FALSE_AS_ERROR).ToNtStatus
else
Status := Value.ToNtStatus;
end;
procedure TNtxStatus.FromHResultAllowFalse;
begin
// Note: if you want S_FALSE to be unsuccessful, see comments in FromHResult.
Status := Value.ToNtStatus;
end;
procedure TNtxStatus.FromLastWin32Error;
begin
if RetValue then
Status := STATUS_SUCCESS
else
Status := RtlxGetLastNtStatus(True);
end;
procedure TNtxStatus.FromStatus;
var
OldBeingDebugged: Boolean;
begin
// Note: all other methods of creation (from Win32 errors, HResults, etc.) end
// up in this function.
FStatus := Value;
// RtlSetLastWin32ErrorAndNtStatusFromNtStatus helps us to enhance debugging
// experience but it also has a side-effect of generating debug messages
// whenever it encounters an unrecognized values. Since we use custom
// NTSTATUSes to pack HRESULTs, these messages can become overwhelming.
// Suppress them by temporarily resetting the indicator flag in PEB.
OldBeingDebugged := RtlGetCurrentPeb.BeingDebugged;
RtlGetCurrentPeb.BeingDebugged := False;
RtlSetLastWin32ErrorAndNtStatusFromNtStatus(Value);
RtlGetCurrentPeb.BeingDebugged := OldBeingDebugged;
if not IsSuccess and CaptureStackTraces then
LastCall.CaptureStackTrace;
end;
procedure TNtxStatus.FromWin32Error;
begin
Status := Value.ToNtStatus;
end;
procedure TNtxStatus.FromWin32ErrorOrSuccess;
begin
if Value = ERROR_SUCCESS then
Status := STATUS_SUCCESS
else
Status := Value.ToNtStatus;
end;
function TNtxStatus.GetHResult;
begin
Result := Status.ToHResult;
end;
function TNtxStatus.GetLocation;
begin
Result := LastCall.Location;
end;
function TNtxStatus.GetWin32Error;
begin
Result := Status.ToWin32Error;
end;
function TNtxStatus.HasEntry;
begin
// When encountering a graceful end of iteration, set the result boolean to
// false to indicate that the caller should exit the loop but convert the
// target status to success to indicate that no unexpected errors occurred.
Result := IsSuccess;
case Status of
STATUS_NO_MORE_ENTRIES, STATUS_NO_MORE_FILES, STATUS_NO_MORE_MATCHES,
STATUS_NO_SUCH_FILE, STATUS_NO_MORE_EAS, STATUS_NO_EAS_ON_FILE,
STATUS_NONEXISTENT_EA_ENTRY:
Target := NtxSuccess;
else
Target := Self;
end;
end;
function TNtxStatus.IsHResult;
begin
Result := Status.IsHResult;
end;
function TNtxStatus.IsSuccess;
begin
Result := Integer(Status) >= 0; // inlined NT_SUCCESS / Succeeded
end;
function TNtxStatus.IsWin32;
begin
Result := Status.IsWin32Error;
end;
function TNtxStatus.Matches;
begin
Result := (Self.Status = Status) and (Self.Location = Location);
end;
procedure TNtxStatus.RaiseOnError;
begin
if IsSuccess then
Exit;
if Assigned(NtxExceptionRaiser) then
NtxExceptionRaiser(Self)
else
RtlxRaiseException(Status, ReturnAddress);
end;
function TNtxStatus.SaveTo;
begin
Target := Self;
Result := Self;
end;
procedure TNtxStatus.SetLocation;
begin
LastCall := Default(TLastCallInfo);
LastCall.Location := Value;
end;
{ Stack tracing & exceptions }
function RtlxNextInstruction;
begin
// Return address of a function is the next instruction for its caller
Result := ReturnAddress;
end;
function RtlxCaptureStackTrace;
var
Count, ReturnedCount: Cardinal;
begin
// Start with a reasonable depth
Count := 32;
Result := nil;
repeat
SetLength(Result, Count);
// Capture the trace
ReturnedCount := RtlCaptureStackBackTrace(FramesToSkip, Count, @Result[0],
nil);
if ReturnedCount < Count then
Break;
// Retry with twice the depth
Count := Count shl 1;
until False;
// Trim the output
SetLength(Result, ReturnedCount);
end;
procedure RtlxRaiseException;
var
ExceptionRecord: TExceptionRecord;
begin
ExceptionRecord := Default(TExceptionRecord);
ExceptionRecord.ExceptionCode := Status;
ExceptionRecord.ExceptionFlags := EXCEPTION_NONCONTINUABLE;
ExceptionRecord.ExceptionAddress := Address;
RtlRaiseException(ExceptionRecord);
end;
constructor ENoSysUtilsException.Create;
begin
FExceptionCode := P.ExceptionCode;
FExceptionFlags := P.ExceptionFlags;
FExceptionAddress := P.ExceptionAddress;
if CaptureStackTraces then
FStackTrace := RtlxCaptureStackTrace;
end;
constructor ENoSysUtilsAccessViolation.Create;
begin
inherited Create(P);
FAccessOperation := TExceptionAccessViolationOperation(
P.ExceptionInformation[0]);
FAccessAddress := Pointer(P.ExceptionInformation[1]);
end;
function NtUtilsExceptClsProc(P: PExceptionRecord): TClass;
begin
if P.ExceptionCode = STATUS_ACCESS_VIOLATION then
Result := ENoSysUtilsAccessViolation
else
Result := ENoSysUtilsException;
end;
function NtUtilsExceptObjProc(P: PExceptionRecord): TObject;
begin
if P.ExceptionCode = STATUS_ACCESS_VIOLATION then
Result := ENoSysUtilsAccessViolation.Create(P)
else
Result := ENoSysUtilsException.Create(P);
end;
{ Buffer expansion }
function Grow12Percent;
begin
Result := Required;
Inc(Result, Result shr 3);
end;
function NtxExpandBufferEx;
begin
// True means continue; False means break from the loop
Result := False;
if Status.IsWin32 then
case Status.Win32Error of
ERROR_INSUFFICIENT_BUFFER, ERROR_MORE_DATA,
ERROR_BAD_LENGTH: ; // Pass through
else
Exit;
end
else
case Status.Status of
STATUS_INFO_LENGTH_MISMATCH, STATUS_BUFFER_TOO_SMALL,
STATUS_BUFFER_OVERFLOW, STATUS_FLT_BUFFER_TOO_SMALL: ; // Pass through
else
Exit;
end;
// Grow the buffer with provided callback
if Assigned(GrowthMethod) then
Required := GrowthMethod(Memory, Required);
// The buffer should always grow, not shrink
if (Assigned(Memory) and (Required <= Memory.Size)) or (Required = 0) then
Exit(False);
// Check for the limitation
if Required > BUFFER_LIMIT then
begin
Status.Location := 'NtxExpandBufferEx';
Status.Status := STATUS_IMPLEMENTATION_LIMIT;
Exit(False);
end;
Memory := Auto.AllocateDynamic(Required);
Result := True;
end;
{ String functions }
function RefStrOrNil;
begin
if S <> '' then
Result := PWideChar(S)
else
Result := nil;
end;
function StringSizeNoZero;
begin
Result := Length(S) * SizeOf(WideChar);
end;
function StringSizeZero;
begin
Result := Succ(Length(S)) * SizeOf(WideChar);
end;
function RtlxInitUnicodeString;
begin
Destination.Buffer := PWideChar(Source);
if Length(Source) > MAX_UNICODE_STRING then
begin
// Truncate the length and return and an error
Destination.Length := MAX_UNICODE_STRING * SizeOf(WideChar);
Destination.MaximumLength := Destination.Length;
Result.Location := 'RtlxInitUnicodeString';
Result.Status := STATUS_NAME_TOO_LONG;
Exit;
end;
Result := NtxSuccess;
Destination.Length := StringSizeNoZero(Source);
// Make sure not to overflow the max length when addressing the longest string
if Length(Source) = MAX_UNICODE_STRING then
Destination.MaximumLength := Destination.Length
else
Destination.MaximumLength := StringSizeZero(Source)
end;
function RtlxInitAnsiString;
begin
Destination.Buffer := PAnsiChar(Source);
if Length(Source) > MAX_ANSI_STRING then
begin
// Truncate the length and return and an error
Destination.Length := MAX_ANSI_STRING * SizeOf(AnsiChar);
Destination.MaximumLength := Destination.Length;
Result.Location := 'RtlxInitAnsiString';
Result.Status := STATUS_NAME_TOO_LONG;
Exit;
end;
Result := NtxSuccess;
Destination.Length := Length(Source) * SizeOf(AnsiChar);
// Make sure not to overflow the max length when addressing the longest string
if Length(Source) = MAX_ANSI_STRING then
Destination.MaximumLength := Destination.Length
else
Destination.MaximumLength := Succ(Length(Source)) * SizeOf(AnsiChar)
end;
procedure MarshalString;
begin
Move(PWideChar(Source)^, Buffer^, StringSizeZero(Source));
end;
function RtlxMarshalUnicodeString;
begin
Result := RtlxInitUnicodeString(Target, Source);
if not Result.IsSuccess then
Exit;
Move(PWideChar(Source)^, Buffer^, Target.MaximumLength);
Target.Buffer := Buffer;
end;
function HandleOrDefault;
begin
if Assigned(hxObject) then
Result := hxObject.Handle
else
Result := Default;
end;
function NtxCurrentProcess;
begin
Result := Auto.RefHandle(NtCurrentProcess);
end;
function NtxCurrentThread;
begin
Result := Auto.RefHandle(NtCurrentThread);
end;
function NtxCurrentProcessToken;
begin
Result := Auto.RefHandle(NtCurrentProcessToken);
end;
function NtxCurrentThreadToken;
begin
Result := Auto.RefHandle(NtCurrentThreadToken);
end;
function NtxCurrentEffectiveToken;
begin
Result := Auto.RefHandle(NtCurrentEffectiveToken);
end;
{ Object Attributes }
type
TNtxObjectAttributes = class (TInterfacedObject, IObjectAttributes)
private
FObjAttr: TObjectAttributes;
FQoS: TSecurityQualityOfService;
FRoot: IHandle;
FName: String;
FNameStr: TNtUnicodeString;
FSecurity: ISecurityDescriptor;
FAccessMask: TAccessMask;
function SetRoot(const Value: IHandle): TNtxObjectAttributes;
function SetName(const Value: String): TNtxObjectAttributes;
function SetAttributes(const Value: TObjectAttributesFlags): TNtxObjectAttributes;
function SetSecurity(const Value: ISecurityDescriptor): TNtxObjectAttributes;
function SetImpersonation(const Value: TSecurityImpersonationLevel): TNtxObjectAttributes;
function SetEffectiveOnly(const Value: Boolean): TNtxObjectAttributes;
function SetContextTracking(const Value: Boolean): TNtxObjectAttributes;
function SetDesiredAccess(const Value: TAccessMask): TNtxObjectAttributes;
function Duplicate: TNtxObjectAttributes;
public
constructor Create;
function GetRoot: IHandle;
function GetName: String;
function GetAttributes: TObjectAttributesFlags;
function GetSecurity: ISecurityDescriptor;
function GetImpersonation: TSecurityImpersonationLevel;
function GetEffectiveOnly: Boolean;
function GetContextTracking: Boolean;
function GetDesiredAccess: TAccessMask;
function Build(out Reference: PObjectAttributes): TNtxStatus;
function UseRoot(const Value: IHandle): IObjectAttributes;
function UseName(const Value: String): IObjectAttributes;
function UseAttributes(const Value: TObjectAttributesFlags): IObjectAttributes;
function UseSecurity(const Value: ISecurityDescriptor): IObjectAttributes;
function UseImpersonation(const Value: TSecurityImpersonationLevel): IObjectAttributes;
function UseEffectiveOnly(const Value: Boolean): IObjectAttributes;
function UseContextTracking(const Value: Boolean): IObjectAttributes;
function UseDesiredAccess(const Value: TAccessMask): IObjectAttributes;
end;
function TNtxObjectAttributes.Build;
begin
Result := RtlxInitUnicodeString(FNameStr, FName);
if not Result.IsSuccess then
Exit;
FObjAttr.ObjectName := FNameStr.RefOrNil;
Reference := @FObjAttr;
end;
constructor TNtxObjectAttributes.Create;
begin
inherited;
FObjAttr.Length := SizeOf(TObjectAttributes);
FObjAttr.SecurityQualityOfService := @FQoS;
FObjAttr.Attributes := OBJ_CASE_INSENSITIVE;
FQoS.Length := SizeOf(TSecurityQualityOfService);
FQoS.ImpersonationLevel := SecurityImpersonation;
end;
function TNtxObjectAttributes.Duplicate;
begin
Result := TNtxObjectAttributes.Create
.SetRoot(GetRoot)
.SetName(GetName)
.SetAttributes(GetAttributes)
.SetSecurity(GetSecurity)
.SetImpersonation(GetImpersonation)
.SetEffectiveOnly(GetEffectiveOnly)
.SetContextTracking(GetContextTracking);
end;
function TNtxObjectAttributes.GetAttributes;
begin
Result := FObjAttr.Attributes;
end;
function TNtxObjectAttributes.GetContextTracking;
begin
Result := FQoS.ContextTrackingMode;
end;
function TNtxObjectAttributes.GetDesiredAccess;
begin
Result := FAccessMask;
end;
function TNtxObjectAttributes.GetEffectiveOnly;
begin
Result := FQoS.EffectiveOnly;
end;
function TNtxObjectAttributes.GetImpersonation;
begin
Result := FQoS.ImpersonationLevel;
end;
function TNtxObjectAttributes.GetName;
begin
Result := FName;
end;
function TNtxObjectAttributes.GetRoot;
begin
Result := FRoot;
end;
function TNtxObjectAttributes.GetSecurity;
begin
Result := FSecurity;
end;