Source code of Windows XP (NT5)
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.

397 lines
9.7 KiB

  1. /*++
  2. Copyright (c) 1994-2000 Microsoft Corporation
  3. Module Name:
  4. heappage.h
  5. Abstract:
  6. External interface for page heap manager.
  7. Author:
  8. Tom McGuire (TomMcg) 06-Jan-1995
  9. Silviu Calinoiu (SilviuC) 22-Feb-2000
  10. Revision History:
  11. --*/
  12. #ifndef _HEAP_PAGE_H_
  13. #define _HEAP_PAGE_H_
  14. //
  15. // #defining DEBUG_PAGE_HEAP will cause the page heap manager
  16. // to be compiled. Only #define this flag if NOT kernel mode.
  17. // Probably want to define this just for checked-build (DBG).
  18. //
  19. #ifndef NTOS_KERNEL_RUNTIME
  20. #define DEBUG_PAGE_HEAP 1
  21. #endif
  22. //silviuc: #include "heappagi.h"
  23. #ifndef DEBUG_PAGE_HEAP
  24. //
  25. // These macro-based hooks should be defined to nothing so they
  26. // simply "go away" during compile if the debug heap manager is
  27. // not desired (retail builds).
  28. //
  29. #define IS_DEBUG_PAGE_HEAP_HANDLE( HeapHandle ) FALSE
  30. #define IF_DEBUG_PAGE_HEAP_THEN_RETURN( Handle, ReturnThis )
  31. #define IF_DEBUG_PAGE_HEAP_THEN_CALL( Handle, CallThis )
  32. #define IF_DEBUG_PAGE_HEAP_THEN_BREAK( Handle, Text, ReturnThis )
  33. #define HEAP_FLAG_PAGE_ALLOCS 0
  34. #define RtlpDebugPageHeapValidate( HeapHandle, Flags, Address ) TRUE
  35. #else // DEBUG_PAGE_HEAP
  36. //
  37. // The following definitions and prototypes are the external interface
  38. // for hooking the debug heap manager in the retail heap manager.
  39. //
  40. #define HEAP_FLAG_PAGE_ALLOCS 0x01000000
  41. #define HEAP_PROTECTION_ENABLED 0x02000000
  42. #define HEAP_BREAK_WHEN_OUT_OF_VM 0x04000000
  43. #define HEAP_NO_ALIGNMENT 0x08000000
  44. #define IS_DEBUG_PAGE_HEAP_HANDLE( HeapHandle ) \
  45. (((PHEAP)(HeapHandle))->ForceFlags & HEAP_FLAG_PAGE_ALLOCS )
  46. #define IF_DEBUG_PAGE_HEAP_THEN_RETURN( Handle, ReturnThis ) \
  47. { \
  48. if ( IS_DEBUG_PAGE_HEAP_HANDLE( Handle )) \
  49. { \
  50. return ReturnThis; \
  51. } \
  52. }
  53. #define IF_DEBUG_PAGE_HEAP_THEN_CALL( Handle, CallThis ) \
  54. { \
  55. if ( IS_DEBUG_PAGE_HEAP_HANDLE( Handle )) \
  56. { \
  57. CallThis; \
  58. return; \
  59. } \
  60. }
  61. #define IF_DEBUG_PAGE_HEAP_THEN_BREAK( Handle, Text, ReturnThis ) \
  62. { \
  63. if ( IS_DEBUG_PAGE_HEAP_HANDLE( Handle )) \
  64. { \
  65. RtlpDebugPageHeapBreak( Text ); \
  66. return ReturnThis; \
  67. } \
  68. }
  69. PVOID
  70. RtlpDebugPageHeapCreate(
  71. IN ULONG Flags,
  72. IN PVOID HeapBase,
  73. IN SIZE_T ReserveSize,
  74. IN SIZE_T CommitSize,
  75. IN PVOID Lock,
  76. IN PRTL_HEAP_PARAMETERS Parameters
  77. );
  78. PVOID
  79. RtlpDebugPageHeapAllocate(
  80. IN PVOID HeapHandle,
  81. IN ULONG Flags,
  82. IN SIZE_T Size
  83. );
  84. BOOLEAN
  85. RtlpDebugPageHeapFree(
  86. IN PVOID HeapHandle,
  87. IN ULONG Flags,
  88. IN PVOID Address
  89. );
  90. PVOID
  91. RtlpDebugPageHeapReAllocate(
  92. IN PVOID HeapHandle,
  93. IN ULONG Flags,
  94. IN PVOID Address,
  95. IN SIZE_T Size
  96. );
  97. PVOID
  98. RtlpDebugPageHeapDestroy(
  99. IN PVOID HeapHandle
  100. );
  101. SIZE_T
  102. RtlpDebugPageHeapSize(
  103. IN PVOID HeapHandle,
  104. IN ULONG Flags,
  105. IN PVOID Address
  106. );
  107. ULONG
  108. RtlpDebugPageHeapGetProcessHeaps(
  109. ULONG NumberOfHeaps,
  110. PVOID *ProcessHeaps
  111. );
  112. ULONG
  113. RtlpDebugPageHeapCompact(
  114. IN PVOID HeapHandle,
  115. IN ULONG Flags
  116. );
  117. BOOLEAN
  118. RtlpDebugPageHeapValidate(
  119. IN PVOID HeapHandle,
  120. IN ULONG Flags,
  121. IN PVOID Address
  122. );
  123. NTSTATUS
  124. RtlpDebugPageHeapWalk(
  125. IN PVOID HeapHandle,
  126. IN OUT PRTL_HEAP_WALK_ENTRY Entry
  127. );
  128. BOOLEAN
  129. RtlpDebugPageHeapLock(
  130. IN PVOID HeapHandle
  131. );
  132. BOOLEAN
  133. RtlpDebugPageHeapUnlock(
  134. IN PVOID HeapHandle
  135. );
  136. BOOLEAN
  137. RtlpDebugPageHeapSetUserValue(
  138. IN PVOID HeapHandle,
  139. IN ULONG Flags,
  140. IN PVOID Address,
  141. IN PVOID UserValue
  142. );
  143. BOOLEAN
  144. RtlpDebugPageHeapGetUserInfo(
  145. IN PVOID HeapHandle,
  146. IN ULONG Flags,
  147. IN PVOID Address,
  148. OUT PVOID* UserValue,
  149. OUT PULONG UserFlags
  150. );
  151. BOOLEAN
  152. RtlpDebugPageHeapSetUserFlags(
  153. IN PVOID HeapHandle,
  154. IN ULONG Flags,
  155. IN PVOID Address,
  156. IN ULONG UserFlagsReset,
  157. IN ULONG UserFlagsSet
  158. );
  159. BOOLEAN
  160. RtlpDebugPageHeapSerialize(
  161. IN PVOID HeapHandle
  162. );
  163. NTSTATUS
  164. RtlpDebugPageHeapExtend(
  165. IN PVOID HeapHandle,
  166. IN ULONG Flags,
  167. IN PVOID Base,
  168. IN SIZE_T Size
  169. );
  170. NTSTATUS
  171. RtlpDebugPageHeapZero(
  172. IN PVOID HeapHandle,
  173. IN ULONG Flags
  174. );
  175. NTSTATUS
  176. RtlpDebugPageHeapReset(
  177. IN PVOID HeapHandle,
  178. IN ULONG Flags
  179. );
  180. NTSTATUS
  181. RtlpDebugPageHeapUsage(
  182. IN PVOID HeapHandle,
  183. IN ULONG Flags,
  184. IN OUT PRTL_HEAP_USAGE Usage
  185. );
  186. BOOLEAN
  187. RtlpDebugPageHeapIsLocked(
  188. IN PVOID HeapHandle
  189. );
  190. VOID
  191. RtlpDebugPageHeapBreak(
  192. PCH Text
  193. );
  194. //
  195. // Page Heap Global Flags
  196. //
  197. // These flags are kept in a global variable that can be set from
  198. // debugger. During heap creation these flags are stored in a per heap
  199. // structure and control the behavior of that particular heap.
  200. //
  201. // PAGE_HEAP_ENABLE_PAGE_HEAP
  202. //
  203. // This flag is set by default. It means that page heap allocations
  204. // should be used always. The flag is useful if we want to use page
  205. // heap only for certain heaps and stick with normal heaps for the
  206. // others. It can be changed on the fly (after heap creation) to direct
  207. // allocations in one heap or another.
  208. //
  209. // PAGE_HEAP_CATCH_BACKWARD_OVERRUNS
  210. //
  211. // Places the N/A page at the beginning of the block.
  212. //
  213. // PAGE_HEAP_UNALIGNED_ALLOCATIONS
  214. //
  215. // For historical reasons (related to RPC) by default page heap
  216. // aligns allocations at 8 byte boundaries. With this flag set
  217. // this does not happen and we can catch instantly off by one
  218. // errors for unaligned allocations.
  219. //
  220. // PAGE_HEAP_SMART_MEMORY_USAGE
  221. //
  222. // This flag reduces the committed memory consumption in half
  223. // by using decommitted ranges (reserved virtual space) instead
  224. // of N/A committed pages. This flag is disabled by catch backward
  225. // overruns.
  226. //
  227. // PAGE_HEAP_USE_SIZE_RANGE
  228. //
  229. // Use page heap for allocations in the size range specified by:
  230. // RtlpDphSizeRangeStart..RtlpDphSizeRangeEnd.
  231. //
  232. // PAGE_HEAP_USE_DLL_RANGE
  233. //
  234. // Use page heap for allocations in the address range specified by:
  235. // RtlpDphDllRangeStart..RtlpDphDllRangeEnd. If the stack trace
  236. // of the allocation contains one address in this range then
  237. // allocation will be made from page heap.
  238. //
  239. // PAGE_HEAP_USE_RANDOM_DECISION
  240. //
  241. // Use page heap if we randomly decide so.
  242. //
  243. // PAGE_HEAP_USE_DLL_NAMES
  244. //
  245. // Use page heap if allocation call was generated from on of the
  246. // target dlls.
  247. //
  248. // PAGE_HEAP_USE_FAULT_INJECTION
  249. //
  250. // Fault inject heap allocation calls based on a simple
  251. // probabilistic model (see FaultProbability and FaultTimeOut).
  252. //
  253. // PAGE_HEAP_PROTECT_META_DATA
  254. //
  255. // Keep page heap metadata read only if we are not executing inside
  256. // the page heap code.
  257. //
  258. #define PAGE_HEAP_ENABLE_PAGE_HEAP 0x0001
  259. #define PAGE_HEAP_COLLECT_STACK_TRACES 0x0002
  260. #define PAGE_HEAP_RESERVED_04 0x0004
  261. #define PAGE_HEAP_RESERVED_08 0x0008
  262. #define PAGE_HEAP_CATCH_BACKWARD_OVERRUNS 0x0010
  263. #define PAGE_HEAP_UNALIGNED_ALLOCATIONS 0x0020
  264. #define PAGE_HEAP_SMART_MEMORY_USAGE 0x0040
  265. #define PAGE_HEAP_USE_SIZE_RANGE 0x0080
  266. #define PAGE_HEAP_USE_DLL_RANGE 0x0100
  267. #define PAGE_HEAP_USE_RANDOM_DECISION 0x0200
  268. #define PAGE_HEAP_USE_DLL_NAMES 0x0400
  269. #define PAGE_HEAP_USE_FAULT_INJECTION 0x0800
  270. #define PAGE_HEAP_PROTECT_META_DATA 0x1000
  271. //
  272. // Is page heap enabled for this process?
  273. //
  274. extern BOOLEAN RtlpDebugPageHeap;
  275. //
  276. // `RtlpDphGlobalFlags' stores the global page heap flags.
  277. // The value of this variable is copied into the per heap
  278. // flags (ExtraFlags field) during heap creation. This variable
  279. // might get its value from the `PageHeap' ImageFileOptions
  280. // registry key.
  281. //
  282. extern ULONG RtlpDphGlobalFlags;
  283. //
  284. // Page heap global flags. They might be read from the
  285. // `ImageFileOptions' registry key.
  286. //
  287. extern ULONG RtlpDphSizeRangeStart;
  288. extern ULONG RtlpDphSizeRangeEnd;
  289. extern ULONG RtlpDphDllRangeStart;
  290. extern ULONG RtlpDphDllRangeEnd;
  291. extern ULONG RtlpDphRandomProbability;
  292. extern WCHAR RtlpDphTargetDlls[];
  293. //
  294. // If not zero controls the probability with which
  295. // allocations will be failed on purpose by page heap
  296. // manager. Timeout represents the initial period during
  297. // process initialization when faults are not allowed.
  298. //
  299. extern ULONG RtlpDphFaultProbability;
  300. extern ULONG RtlpDphFaultTimeOut;
  301. //
  302. // Stuff needed for per dll logic implemented in the loader
  303. //
  304. const WCHAR *
  305. RtlpDphIsDllTargeted (
  306. const WCHAR * Name
  307. );
  308. VOID
  309. RtlpDphTargetDllsLoadCallBack (
  310. PUNICODE_STRING Name,
  311. PVOID Address,
  312. ULONG Size
  313. );
  314. //
  315. // Functions needed to turn on/off fault injection.
  316. // They are needed in the loader so that allocations
  317. // succeed while in LdrLoadDll code path.
  318. //
  319. VOID
  320. RtlpDphDisableFaultInjection (
  321. );
  322. VOID
  323. RtlpDphEnableFaultInjection (
  324. );
  325. #endif // DEBUG_PAGE_HEAP
  326. #endif // _HEAP_PAGE_H_