Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.7 KiB

4 years ago
  1. //
  2. // heappagi.h
  3. //
  4. // The following definitions are internal to the debug heap manager,
  5. // but are placed in this include file so that debugger extensions
  6. // can reference the same structure definitions. The following
  7. // definitions are not intended to be referenced externally except
  8. // by debugger extensions.
  9. //
  10. #ifndef _HEAP_PAGE_I_
  11. #define _HEAP_PAGE_I_
  12. #ifdef DEBUG_PAGE_HEAP
  13. #include "heap.h"
  14. #define DPH_INTERNAL_DEBUG 0 // change to 0 or #undef for production code
  15. #define DPH_MAX_STACK_LENGTH 20
  16. #if defined(_X86_) // RtlCaptureStackBackTrace() only implemented on x86
  17. #if DBG // RtlCaptureStackBackTrace() only consistent with no FPO
  18. #define DPH_CAPTURE_STACK_TRACE 1
  19. #endif // DBG
  20. #endif // _X86_
  21. #if DPH_CAPTURE_STACK_TRACE
  22. typedef struct _DPH_STACK_TRACE_NODE DPH_STACK_TRACE_NODE, *PDPH_STACK_TRACE_NODE;
  23. struct _DPH_STACK_TRACE_NODE {
  24. PDPH_STACK_TRACE_NODE Left; // B-tree on Hash
  25. PDPH_STACK_TRACE_NODE Right; // B-tree on Hash
  26. ULONG Hash; // simple sum of PVOIDs in stack trace
  27. ULONG Length; // number of PVOIDs in stack trace
  28. ULONG BusyCount; // number of busy allocations
  29. ULONG BusyBytes; // total user size of busy allocations
  30. PVOID Address[ 0 ]; // variable length array of addresses
  31. };
  32. #endif // DPH_CAPTURE_STACK_TRACE
  33. typedef struct _DPH_HEAP_ALLOCATION DPH_HEAP_ALLOCATION, *PDPH_HEAP_ALLOCATION;
  34. struct _DPH_HEAP_ALLOCATION {
  35. //
  36. // Singly linked list of allocations (pNextAlloc must be
  37. // first member in structure).
  38. //
  39. PDPH_HEAP_ALLOCATION pNextAlloc;
  40. //
  41. // | PAGE_READWRITE | PAGE_NOACCESS |
  42. // |____________________|___||_________________________|
  43. //
  44. // ^pVirtualBlock ^pUserAllocation
  45. //
  46. // |---------------- nVirtualBlockSize ----------------|
  47. //
  48. // |---nVirtualAccessSize----|
  49. //
  50. // |---| nUserRequestedSize
  51. //
  52. // |----| nUserActualSize
  53. //
  54. PUCHAR pVirtualBlock;
  55. ULONG nVirtualBlockSize;
  56. ULONG nVirtualAccessSize;
  57. PUCHAR pUserAllocation;
  58. ULONG nUserRequestedSize;
  59. ULONG nUserActualSize;
  60. PVOID UserValue;
  61. ULONG UserFlags;
  62. #if DPH_CAPTURE_STACK_TRACE
  63. PDPH_STACK_TRACE_NODE pStackTrace;
  64. #endif
  65. };
  66. typedef struct _DPH_HEAP_ROOT DPH_HEAP_ROOT, *PDPH_HEAP_ROOT;
  67. struct _DPH_HEAP_ROOT {
  68. //
  69. // Maintain a signature (DPH_HEAP_ROOT_SIGNATURE) as the
  70. // first value in the heap root structure.
  71. //
  72. ULONG Signature;
  73. ULONG HeapFlags;
  74. //
  75. // Access to this heap is synchronized with a critical section.
  76. //
  77. PRTL_CRITICAL_SECTION HeapCritSect;
  78. ULONG nRemoteLockAcquired;
  79. //
  80. // The "VirtualStorage" list only uses the pVirtualBlock,
  81. // nVirtualBlockSize, and nVirtualAccessSize fields of the
  82. // HEAP_ALLOCATION structure. This is the list of virtual
  83. // allocation entries that all the heap allocations are
  84. // taken from.
  85. //
  86. PDPH_HEAP_ALLOCATION pVirtualStorageListHead;
  87. PDPH_HEAP_ALLOCATION pVirtualStorageListTail;
  88. ULONG nVirtualStorageRanges;
  89. ULONG nVirtualStorageBytes;
  90. //
  91. // The "Busy" list is the list of active heap allocations.
  92. // It is stored in LIFO order to improve temporal locality
  93. // for linear searches since most initial heap allocations
  94. // tend to remain permanent throughout a process's lifetime.
  95. //
  96. PDPH_HEAP_ALLOCATION pBusyAllocationListHead;
  97. PDPH_HEAP_ALLOCATION pBusyAllocationListTail;
  98. ULONG nBusyAllocations;
  99. ULONG nBusyAllocationBytesCommitted;
  100. //
  101. // The "Free" list is the list of freed heap allocations, stored
  102. // in FIFO order to increase the length of time a freed block
  103. // remains on the freed list without being used to satisfy an
  104. // allocation request. This increases the odds of catching
  105. // a reference-after-freed bug in an app.
  106. //
  107. PDPH_HEAP_ALLOCATION pFreeAllocationListHead;
  108. PDPH_HEAP_ALLOCATION pFreeAllocationListTail;
  109. ULONG nFreeAllocations;
  110. ULONG nFreeAllocationBytesCommitted;
  111. //
  112. // The "Available" list is stored in address-sorted order to facilitate
  113. // coalescing. When an allocation request cannot be satisfied from the
  114. // "Available" list, it is attempted from the free list. If it cannot
  115. // be satisfied from the free list, the free list is coalesced into the
  116. // available list. If the request still cannot be satisfied from the
  117. // coalesced available list, new VM is added to the available list.
  118. //
  119. PDPH_HEAP_ALLOCATION pAvailableAllocationListHead;
  120. PDPH_HEAP_ALLOCATION pAvailableAllocationListTail;
  121. ULONG nAvailableAllocations;
  122. ULONG nAvailableAllocationBytesCommitted;
  123. //
  124. // The "UnusedNode" list is simply a list of available node
  125. // entries to place "Busy", "Free", or "Virtual" entries.
  126. // When freed nodes get coalesced into a single free node,
  127. // the other "unused" node goes on this list. When a new
  128. // node is needed (like an allocation not satisfied from the
  129. // free list), the node comes from this list if it's not empty.
  130. //
  131. PDPH_HEAP_ALLOCATION pUnusedNodeListHead;
  132. PDPH_HEAP_ALLOCATION pUnusedNodeListTail;
  133. ULONG nUnusedNodes;
  134. ULONG nBusyAllocationBytesAccessible;
  135. //
  136. // Node pools need to be tracked so they can be protected
  137. // from app scribbling on them.
  138. //
  139. PDPH_HEAP_ALLOCATION pNodePoolListHead;
  140. PDPH_HEAP_ALLOCATION pNodePoolListTail;
  141. ULONG nNodePools;
  142. ULONG nNodePoolBytes;
  143. //
  144. // Doubly linked list of DPH heaps in process is tracked through this.
  145. //
  146. PDPH_HEAP_ROOT pNextHeapRoot;
  147. PDPH_HEAP_ROOT pPrevHeapRoot;
  148. ULONG nUnProtectionReferenceCount;
  149. ULONG InsideAllocateNode; // only for debugging
  150. #if DPH_CAPTURE_STACK_TRACE
  151. PUCHAR pStackTraceStorage;
  152. ULONG nStackTraceStorage;
  153. PDPH_STACK_TRACE_NODE pStackTraceRoot; // B-tree root
  154. PDPH_STACK_TRACE_NODE pStackTraceCreator;
  155. ULONG nStackTraceBytesCommitted;
  156. ULONG nStackTraceBytesWasted;
  157. ULONG nStackTraceBNodes;
  158. ULONG nStackTraceBDepth;
  159. ULONG nStackTraceBHashCollisions;
  160. #endif // DPH_CAPTURE_STACK_TRACE
  161. };
  162. #endif // DEBUG_PAGE_HEAP
  163. #endif // _HEAP_PAGE_I_