-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPythonFunction.c
More file actions
1418 lines (1076 loc) · 37.4 KB
/
Copy pathPythonFunction.c
File metadata and controls
1418 lines (1076 loc) · 37.4 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
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
/*++
Copyright (c) 2016 Trent Nelson <trent@trent.me>
Module Name:
PythonFunction.c
Abstract:
This module implements functionality related to the PYTHON_FUNCTION
structure, which is based on the PYTHON_PATH_TABLE_ENTRY structure,
additionally capturing Python function information such as the code
object, number of lines, etc.
This module is consumed by Python tracing components, which interface
to it via the RegisterFrame method, which returns the corresponding
PYTHON_FUNCTION structure for a given Python frame object.
--*/
#include "stdafx.h"
_Use_decl_annotations_
BOOL
RegisterFrame(
PPYTHON Python,
PPYFRAMEOBJECT FrameObject,
PYTHON_EVENT_TRAITS EventTraits,
PPYOBJECT ArgObject,
PPPYTHON_FUNCTION FunctionPointer
)
/*++
Routine Description:
This routine returns a PYTHON_FUNCTION structure for a given Python frame
object, a process referred to as "frame registration". Its primary goal
is to provide rich function information with low overhead, such that it
can be called for every single line trace event of a potentially long
running Python program without adversely affecting runtime too much.
The frame object's underlying code object is tracked in a splaying table
(RTL_GENERIC_TABLE), which allows us to exploit the temporal locality of
trace events and code objects. Thus, the fast path through this routine
is the most common path: being called with the same underlying code object
as the previous call. This ensures the code object's pointer is at the
root of the splay tree, and can be returned with minimal pointer chasing
overhead and good cache locality. (This is the same approach taken by the
cProfile module within the Python interpreter core.)
If we're seeing a new code object for the first time, GetPathEntryFromFrame
will be called. This routine extracts the PYTHON_PATH_TABLE_ENTRY structure
for the filename object associated with the code object, with the amount of
work required to satisfy such a request dependent upon how many things we've
seen before.
If the filename hasn't been seen before, and the directory hasn't been seen
before, the routine will need to enumerate each directory, from longest to
shortest, seeing if it is a module directory. That is, whether or not the
directory contains an __init__.py[co] file. We stop once we've found our
first non-module directory, which is also referred to as a root directory.
Once our root directory is registered, module directories can be registered
for each intermediate directory between the root and the directory that the
filename resides in.
Once we have a PYTHON_PATH_TABLE_ENTRY for the filename, we can finalize
the creation of a PYTHON_FUNCTION structure, ensuring that is has the right
module and class name information if applicable.
The code object is stored in the new PYTHON_FUNCTION's CodeObject field,
and then inserted into the PYTHON structure's PYTHON_FUNCTION_TABLE, which
is the RTL_GENERIC_TABLE-based structure referred to earlier. This will
also splay the tree such that the newly-inserted function will be at the
root of the table, ensuring fast subsequent access.
This routine relies heavily on the PythonPathTableEntry component. The
pseudo code of the overall flow for frame registration looks like this:
RegisterFrame():
CodeObject = Frame.CodeObject
Function = Python.FunctionTable[CodeObject]
if not Function:
//
// GetPathEntryForFrame():
//
Filename = CodeObject.Filename
FilePathEntry = Python.PathTable[Filename]
if not FilePathEntry:
//
// RegisterFile():
//
DirectoryName = Filename.DirectoryPart
DirectoryPathEntry = Python.PathTable[DirectoryName]
if not DirectoryPathEntry:
//
// GetPathEntryForDirectory():
//
If a prefix match was found for the part of the directory
name, register each intermediate directory from the prefix
onward.
Otherwise, if no part of the directory has been seen before,
add our parent, then our parent's parent, etc, until we find
the first non-module directory (i.e. the first directory
without an __init__.py file).
(A non-module directory is registered via the routine
RegisterNonModuleDirectory(), a module directory via the
routine RegisterModuleDirectory().)
return the DirectoryPathEntry
FilePathEntry = new PythonPathTableEntry(DirectoryPathEntry)
Python.PathTable[Filename] = FilePathEntry
Function = new PythonFunction(FilePathEntry)
return Function
Thus, the most expensive path through the code is typically visited the
least; i.e. registering a new function, filename, directory and all ancestor
directories as necessary. Once each intermediate piece has been done, the
extensive use of prefix trees ensures we don't need to keep repeating the
expensive operations.
Arguments:
Python - Supplies a pointer to a PYTHON structure.
FrameObject - Supplies a pointer to a Python frame object structure. That
is, the PyFrameObject * passed to the Python C trace function. As the
layout of this structure has changed over different Python versions,
the Python->CodeObjectOffsets table is used initially to resolve the
code object pointer from the frame object.
EventTraits - Supplies a PYTHON_EVENT_TRAITS value that describes the event.
ArgObject - Supplies a pointer to a PYOBJECT structure that was provided as
a parameter to the trace function.
FunctionPointer - Supplies a pointer to a variable that receives the address
of a PYTHON_FUNCTION structure for the FrameObject's underlying code
object. If the routine fails to resolve a function for the frame, this
will be set to NULL.
Return Value:
TRUE on success, FALSE on failure.
--*/
{
BOOL Success;
BOOL IsValid;
BOOL IsC;
BOOL IsCall;
BOOL NewPathEntry;
BOOLEAN NewFunction;
PRTL Rtl;
PPYFRAMEOBJECT Frame = (PPYFRAMEOBJECT)FrameObject;
PPYOBJECT CodeObject;
PPYCFUNCTIONOBJECT PyCFunctionObject;
PPYMETHODDEF MethodDef;
PPYCFUNCTION PyCFunctionPointer;
STRING FilenameString;
PYTHON_FUNCTION FunctionRecord;
PPYTHON_FUNCTION Function;
PPYTHON_FUNCTION_TABLE FunctionTable;
PPYTHON_PATH_TABLE_ENTRY ParentPathEntry;
FILENAME_FLAGS FilenameFlags;
//
// Clear the caller's function pointer up front if present.
//
if (ARGUMENT_PRESENT(FunctionPointer)) {
*FunctionPointer = NULL;
}
//
// Verify the code object type is something sane.
//
CodeObject = Frame->Code;
if (CodeObject->Type != Python->PyCode.Type) {
return FALSE;
}
//
// Initialize aliases.
//
IsC = EventTraits.IsC;
IsCall = EventTraits.IsCall;
Rtl = Python->Rtl;
FunctionTable = Python->FunctionTable;
if (IsC) {
//
// Initialize local variables specific to Python C functions.
//
PyCFunctionObject = (PPYCFUNCTIONOBJECT)ArgObject;
//
// Make sure the underlying type object is what we expect.
//
if (PyCFunctionObject->Type != Python->PyCFunction.Type) {
return FALSE;
}
MethodDef = PyCFunctionObject->MethodDef;
PyCFunctionPointer = MethodDef->FunctionPointer;
//
// Use the function pointer as the key.
//
FunctionRecord.Key = (ULONG_PTR)PyCFunctionPointer;
} else {
//
// Use the code object as the key.
//
FunctionRecord.Key = (ULONG_PTR)CodeObject;
}
//
// Attempt to insert the function into the function table.
//
Function = Rtl->RtlInsertElementGenericTable(
&FunctionTable->GenericTable,
&FunctionRecord,
sizeof(FunctionRecord),
&NewFunction
);
if (!NewFunction) {
//
// We've already seen this function. Increment the call count if it's
// a valid function and this is a call event.
//
IsValid = IsValidFunction(Function);
if (IsValid) {
if (IsCall) {
Function->CallCount++;
}
}
goto End;
}
//
// This is a new function. RtlInsertElementGenericTable() will have copied
// the stack-backed memory of the FunctionRecord structure over to the new
// structure, so zero the entire structure now, then reset the code object
// and key fields.
//
SecureZeroMemory(Function, sizeof(*Function));
Function->CodeObject = CodeObject;
Function->Key = FunctionRecord.Key;
//
// Clear the filename flags.
//
FilenameFlags.AsLong = 0;
//
// We now need to get the filename to pass to GetPathEntryFromFrame(). How
// we do this depends on whether or not this is normal Python user code, or
// a Python C function.
//
if (IsC) {
//
// We're a Python C function. Get the underlying DLL handle for the
// C function pointer, then get the DLL's file name.
//
CHAR Path[_MAX_PATH];
HMODULE Handle;
ULONG Flags;
DWORD BufferSizeInChars = sizeof(Path);
DWORD ActualSizeInChars;
LPCWSTR Method = (LPCWSTR)PyCFunctionPointer;
Flags = (
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
);
if (!GetModuleHandleExW(Flags, Method, &Handle)) {
return FALSE;
}
if (!Handle) {
return FALSE;
}
ActualSizeInChars = GetModuleFileNameA(Handle,
(LPSTR)&Path,
BufferSizeInChars);
if (ActualSizeInChars == 0) {
DWORD LastError = GetLastError();
if (LastError == ERROR_INSUFFICIENT_BUFFER) {
//
// XXX todo: allocate string buffer.
//
__debugbreak();
FilenameFlags.WeOwnPathBuffer = TRUE;
}
return FALSE;
}
//
// N.B. ActualSizeInChars includes the NUL-terminator, so we don't need
// to subtract 1 from the length here.
//
FilenameString.Length = (USHORT)ActualSizeInChars;
FilenameString.MaximumLength = (USHORT)BufferSizeInChars;
FilenameString.Buffer = Path;
FilenameFlags.IsFullyQualified = TRUE;
FilenameFlags.IsDll = TRUE;
Function->ModuleHandle = Handle;
} else {
BOOL IsSpecialName;
PSTRING Path;
PPYOBJECT FilenameObject;
PCPYCODEOBJECTOFFSETS CodeObjectOffsets;
CodeObjectOffsets = Python->PyCodeObjectOffsets;
FilenameObject = *(
(PPPYOBJECT)RtlOffsetToPointer(
FrameObject->Code,
Python->PyCodeObjectOffsets->Filename
)
);
Path = &FilenameString;
Success = WrapPythonFilenameStringAsString(
Python,
FilenameObject,
Path,
&IsSpecialName
);
if (!Success) {
__debugbreak();
return FALSE;
}
if (IsSpecialName) {
FilenameFlags.IsSpecialName = TRUE;
}
}
//
// Attempt to get a path entry for this frame and filename object.
//
Success = GetPathEntryFromFrame(Python,
FrameObject,
EventTraits,
ArgObject,
&FilenameString,
&FilenameFlags,
&ParentPathEntry,
&NewPathEntry);
if (!Success || !ParentPathEntry || !ParentPathEntry->IsValid) {
goto Error;
}
Function->ParentPathEntry = ParentPathEntry;
//
// Finish registration of the function.
//
if (IsC) {
Function->PyCFunctionObject = PyCFunctionObject;
Success = RegisterPyCFunction(Python,
Function,
FrameObject);
} else {
Success = RegisterPythonFunction(Python,
Function,
FrameObject);
}
if (!Success) {
goto Error;
}
//
// If this was the first time we saw the filename (represented by the
// parent path entry), invoke the callback now if one is present.
//
if (NewPathEntry && Python->RegisterNewPathEntry) {
Success = Python->RegisterNewPathEntry(
Python->RegisterNewPathEntryContext,
ParentPathEntry
);
}
if (Success) {
IsValid = TRUE;
//
// Mark our 'seen' bit in the relevant target object's reference count.
//
if (IsC) {
PyCFunctionObject->RefCountEx.Seen = TRUE;
} else {
CodeObject->RefCountEx.Seen = TRUE;
}
//
// Initialize the linked-list head.
//
InitializeListHead(&Function->ListEntry);
goto End;
}
//
// Intentional follow-on to Error.
//
Error:
IsValid = Function->PathEntry.IsValid = FALSE;
End:
if (IsValid) {
if (!Function->PathEntry.IsValid) {
__debugbreak();
}
if (ARGUMENT_PRESENT(FunctionPointer)) {
*FunctionPointer = Function;
}
}
return IsValid;
}
_Use_decl_annotations_
BOOL
RegisterPythonFunction(
PPYTHON Python,
PPYTHON_FUNCTION Function,
PPYFRAMEOBJECT FrameObject
)
/*++
Routine Description:
This method is responsible for finalizing details about a function, such
as the function name, class name (if any), module name and first line
number. A frame object is provided to assist with resolution of names.
It is called once per function after a new PYTHON_PATH_TABLE_ENTRY has been
inserted into the path prefix table.
Arguments:
Python - Supplies a pointer to a PYTHON structure.
Function - Supplies a pointer to a PYTHON_FUNCTION structure to be
registered by this routine.
FrameObject - Supplies a pointer to the PYFRAMEOBJECT structure for which
this function is being registered. This is required in order to assist
with the resolution of names.
Return Value:
TRUE on success, FALSE on failure.
--*/
{
BOOL Success;
PRTL Rtl;
PCHAR Dest;
PCHAR Start;
PPYOBJECT Self = NULL;
PPYOBJECT FunctionNameObject;
PPYOBJECT CodeObject = Function->CodeObject;
PCPYCODEOBJECTOFFSETS CodeObjectOffsets = Python->PyCodeObjectOffsets;
PSTRING Path;
PSTRING FunctionName;
PSTRING ClassName;
PSTRING FullName;
PSTRING ModuleName;
PCHAR ClassNameBuffer = NULL;
USHORT FullNameAllocSize;
USHORT FullNameLength;
PSTRING ParentModuleName;
PSTRING ParentName;
PPYTHON_PATH_TABLE_ENTRY PathEntry;
PPYTHON_PATH_TABLE_ENTRY ParentPathEntry;
//
// Initialize aliases and set the PathEntry to invalid.
//
PathEntry = (PPYTHON_PATH_TABLE_ENTRY)Function;
ParentPathEntry = Function->ParentPathEntry;
PathEntry->IsValid = FALSE;
//
// Resolve the first line number and function name object using the code
// object offsets structure.
//
Function->FirstLineNumber = (USHORT)*(
(PULONG)RtlOffsetToPointer(
CodeObject,
CodeObjectOffsets->FirstLineNumber
)
);
FunctionNameObject = *(
(PPPYOBJECT)RtlOffsetToPointer(
CodeObject,
CodeObjectOffsets->Name
)
);
//
// Wrap the PyString/PyUnicode string in a STRING struct.
//
Rtl = Python->Rtl;
FunctionName = &PathEntry->Name;
Success = WrapPythonStringAsString(Python,
FunctionNameObject,
FunctionName);
if (!Success) {
return FALSE;
}
//
// Attempt to get the "self" object from the current frame, if present.
//
Success = GetSelf(Python, Function, FrameObject, &Self);
if (!Success) {
//
// The GetSelf() method failed. Note that this indicates an actual
// failure -- not simply that we couldn't resolve the "self" object
// from the frame. We test for that below.
//
return FALSE;
}
//
// Clear the class name string.
//
ClassName = &PathEntry->ClassName;
ClearString(ClassName);
if (Self) {
//
// We found a "self" object within the frame, which means we're an
// instance object and we have a class. So, let's try and extract
// the class name.
//
USHORT ClassNameLength;
Success = GetClassNameFromSelf(Python,
Function,
FrameObject,
Self,
FunctionName,
&ClassNameBuffer);
if (!Success) {
return FALSE;
}
if (ClassNameBuffer) {
//
// We were able to extract a class name. The C Python API will have
// provided us with a pointer to a NULL-terminated C (char) string,
// which we'll need to take a copy of, so, record the relevant
// details here regarding length and buffer.
//
ClassNameLength = (USHORT)strlen((PCSZ)ClassNameBuffer);
//
// Ensure we've got a NUL-terminated string.
//
if (ClassNameBuffer[ClassNameLength] != '\0') {
__debugbreak();
}
ClassName->Length = ClassNameLength;
ClassName->MaximumLength = ClassNameLength;
ClassName->Buffer = ClassNameBuffer;
}
}
//
// Initialize the module name, initially pointing at our parent's
// buffer.
//
ParentModuleName = &ParentPathEntry->ModuleName;
ModuleName = &PathEntry->ModuleName;
ModuleName->Length = ParentModuleName->Length;
ModuleName->MaximumLength = ParentModuleName->Length;
ModuleName->Buffer = ParentModuleName->Buffer;
ParentName = &ParentPathEntry->Name;
//
// Calculate the length of the full name. The extra +1s are accounting
// for joining slashes and the final trailing NUL.
//
FullNameLength = (
(ModuleName->Length ? ModuleName->Length + 1 : 0) +
(ParentName->Length ? ParentName->Length + 1 : 0) +
(ClassName->Length ? ClassName->Length + 1 : 0) +
FunctionName->Length +
1
);
//
// Ensure we don't exceed name lengths.
//
if (FullNameLength > MAX_STRING) {
return FALSE;
}
FullNameAllocSize = ALIGN_UP_USHORT_TO_POINTER_SIZE(FullNameLength);
if (FullNameAllocSize > MAX_STRING) {
FullNameAllocSize = (USHORT)MAX_STRING;
}
//
// Construct the final full name. After each part has been copied, update
// the corresponding Buffer pointer to the relevant point within the newly-
// allocated buffer for the full name.
//
FullName = &PathEntry->FullName;
Success = AllocateStringBuffer(Python, FullNameAllocSize, FullName);
if (!Success) {
return FALSE;
}
Dest = FullName->Buffer;
if (ModuleName->Length) {
__movsb(Dest, (PBYTE)ModuleName->Buffer, ModuleName->Length);
Dest += ModuleName->Length;
*Dest++ = '\\';
}
//
// Point the module name to the base of the full name buffer now that we've
// created and copied the original buffer.
//
ModuleName->Buffer = FullName->Buffer;
//
// Copy the parent's name if it exists and is not __init__.
//
if (ParentName->Length && !ParentPathEntry->IsInitPy) {
__movsb(Dest, (PBYTE)ParentName->Buffer, ParentName->Length);
Dest += ParentName->Length;
*Dest++ = '\\';
if (!ModuleName->Length) {
//
// There's no module name currently set, so the parent name
// becomes our module name. Inherit the length.
//
ModuleName->Length = ParentName->Length;
ModuleName->MaximumLength = ParentName->Length;
} else if (ParentPathEntry->IsFile) {
//
// The parent is a file, so the module name will be a concatenation
// of the parent name plus the module name. Update the module name
// length to account for the parent name, plus one for the joining
// slash.
//
if (ParentPathEntry->IsFile) {
ModuleName->Length += ParentName->Length + 1;
ModuleName->MaximumLength = ModuleName->Length;
}
}
}
if (ClassName->Length) {
Start = Dest;
__movsb(Dest, (PBYTE)ClassName->Buffer, ClassName->Length);
ClassName->Buffer = Start;
Dest += ClassName->Length;
*Dest++ = '\\';
}
Start = Dest;
__movsb(Dest, (PBYTE)FunctionName->Buffer, FunctionName->Length);
FunctionName->Buffer = Start;
Dest += FunctionName->Length;
*Dest++ = '\0';
//
// Omit trailing NUL from Length.
//
FullName->Length = FullNameLength - 1;
FullName->MaximumLength = FullNameAllocSize;
//
// Point our path at our parent.
//
Path = &PathEntry->Path;
Path->Length = ParentPathEntry->Path.Length;
Path->MaximumLength = Path->Length;
Path->Buffer = ParentPathEntry->Path.Buffer;
//
// Initialize the call count.
//
Function->CallCount = 1;
//
// Indicate that the path entry is a) valid, and b) a function.
//
PathEntry->IsFunction = TRUE;
PathEntry->IsValid = TRUE;
//
// Resolve line numbers. This will set the FirstLineNumber and
// NumberOfCodeLines fields in the Function structure.
//
ResolveLineNumbers(Python, Function);
//
// Calculate the code object's hash.
//
Function->CodeObjectHash = Python->PyObject_Hash(CodeObject);
//
// Hash the strings.
//
HashString(Python, &PathEntry->Name);
HashString(Python, &PathEntry->Path);
HashString(Python, &PathEntry->FullName);
HashString(Python, &PathEntry->ModuleName);
if (ClassName->Length) {
HashString(Python, &PathEntry->ClassName);
} else {
PathEntry->ClassName.Hash = 0;
}
//
// Calculate a final hash value and use that as the function signature.
//
Function->Signature = (ULONG_PTR)(
PathEntry->PathEntryTypeFlags ^
PathEntry->PathHash ^
PathEntry->FullNameHash ^
Function->CodeObjectHash ^
Function->NumberOfCodeLines
);
return TRUE;
}
_Use_decl_annotations_
BOOL
GetSelf(
PPYTHON Python,
PPYTHON_FUNCTION Function,
PPYFRAMEOBJECT FrameObject,
PPPYOBJECT SelfPointer
)
/*++
Routine Description:
This routine attempts to extract the Python "self" object from a frame
object.
Arguments:
Python - Supplies a pointer to a PYTHON structure.
Function - Supplies a pointer to a PYTHON_FUNCTION structure that has been
created for the frame object.
FrameObject - Supplies a pointer to the PYFRAMEOBJECT structure for which
the "self" object is to be extracted from.
SelfPointer - Supplies a pointer to a variable that receives the address
of a PYOBJECT structure representing the "self" variable if one could
be found. If no "self" variable could be found, this is set to NULL.
Return Value:
TRUE on success, FALSE on failure. Note that TRUE doesn't necessarily mean
the self variable was found; check the value of SelfPointer for that.
--*/
{
BOOL Success = FALSE;
PPYOBJECT Self = NULL;
PPYOBJECT Locals = FrameObject->Locals;
PPYOBJECT CodeObject = Function->CodeObject;
LONG ArgumentCount;
PPYTUPLEOBJECT ArgumentNames;
//
// Attempt to resolve self from the locals dictionary.
//
if (Locals && Locals->Type == Python->PyDict.Type) {
if ((Self = Python->PyDict_GetItemString(Locals, SELF_A.Buffer))) {
Success = TRUE;
goto End;
}
}
//
// If that didn't work, see if the first argument's name was "self", and
// if so, use that.
//
ArgumentCount = *(
(PLONG)RtlOffsetToPointer(
CodeObject,
Python->PyCodeObjectOffsets->ArgumentCount
)
);
ArgumentNames = *(
(PPPYTUPLEOBJECT)RtlOffsetToPointer(
CodeObject,
Python->PyCodeObjectOffsets->LocalVariableNames
)
);
if (ArgumentCount != 0 && ArgumentNames->Type == Python->PyTuple.Type) {
PRTL_EQUAL_STRING EqualString = Python->Rtl->RtlEqualString;
STRING ArgumentName;
Success = WrapPythonStringAsString(
Python,
ArgumentNames->Item[0],
&ArgumentName
);
if (!Success) {
return FALSE;
}
if (EqualString(&ArgumentName, &SELF_A, FALSE)) {
Self = *(
(PPPYOBJECT)RtlOffsetToPointer(
FrameObject,
Python->PyFrameObjectOffsets->LocalsPlusStack
)
);
}
}
Success = TRUE;
End:
*SelfPointer = Self;
return Success;
}
_Use_decl_annotations_
BOOL
GetClassNameFromSelf(
PPYTHON Python,
PPYTHON_FUNCTION Function,
PPYFRAMEOBJECT FrameObject,
PPYOBJECT Self,
PSTRING FunctionName,
PPCHAR ClassNameBuffer
)
/*++
Routine Description:
This routine extracts a class name from a Python "self" object.
Arguments:
Python - Supplies a pointer to a PYTHON structure.
Function - Supplies a pointer to a PYTHON_FUNCTION structure for the frame
object containing the self object for which the class name will be
extracted.
FrameObject - Supplies a pointer to the PYFRAMEOBJECT structure where
the "self" object resides.
Self - Supplies a pointer to a PYOBJECT structure representing the self
object for the current frame. Derived by GetSelf().
FunctionName - Supplies a pointer to a STRING structure representing the
name of the function.
ClassNameBuffer - Supplies a pointer to a variable that receives the
address of the NULL-terminated character array representing the class
name.
Return Value:
TRUE on success, FALSE on failure. Note that TRUE doesn't necessarily mean
the class name was found, check the value of ClassNameBuffer for that.
--*/
{
BOOL Success;
ULONG Index;
PPYTUPLEOBJECT Mro;
PPYTYPEOBJECT TypeObject;
PPYOBJECT TypeDict;