Leaked source code of windows server 2003
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.

238 lines
7.1 KiB

  1. #include "EmulateHeap_kernel32.h"
  2. HANDLE hheapKernel = 0;
  3. PDB pdbCur;
  4. PDB *ppdbCur = &pdbCur;
  5. PDB **pppdbCur = &ppdbCur;
  6. /***SN PageCommit - commit physical pages to a specified linear address
  7. *
  8. * The entire target region must have been reserved by a single previous
  9. * call to PageReserve.
  10. *
  11. * If PC_LOCKED, PC_LOCKEDIFDP, or PC_FIXED are passed into PageCommit,
  12. * then all of the pages in the specified range must currently uncommitted.
  13. * If none of those flags are specified, then any existing
  14. * committed pages in the range will be unaffected by this call and an
  15. * error will not be returned. However, even though it is allowed,
  16. * calling PageCommit on a range containing already committed memory
  17. * should be avoided because it is waste of time.
  18. *
  19. * ENTRY: page - base virtual page number to start commit at
  20. * npages - number of pages to commit
  21. * hpd - handle to pager descriptor (returned from PagerRegister)
  22. * or one of these special value:
  23. * PD_ZEROINIT - swappable zero-initialized
  24. * PD_NOINIT - swappable uninitialized
  25. * PD_FIXED - fixed uninitialized (must also pass in
  26. * PC_FIXED flag)
  27. * PD_FIXEDZERO - fixed zero-initialized (must also pass
  28. * in PC_FIXED flag)
  29. * pagerdata - a single dword to be stored with the page(s) for
  30. * use by the pager. If one of the special pagers
  31. * listed above is used for the "hpd" parameter, then
  32. * this parameter is reserved and should be zero.
  33. * flags - PC_FIXED - page are created permanently locked
  34. * PC_LOCKED - pages are created present and locked
  35. * PC_LOCKEDIFDP - page are locked if swapping is via DOS
  36. * PC_STATIC - allow commit in AR_STATIC object
  37. * PC_USER - make the pages ring 3 accessible
  38. * PC_WRITEABLE - make the pages writeable
  39. * PC_INCR - increment "pagerdata" once for each page. If
  40. * one of the special pagers listed above is used
  41. * for the "hpd" parameter, then this flags
  42. * should not be specified.
  43. * PC_PRESENT - make the pages present as they are committed
  44. * (not needed with PC_FIXED or PC_LOCKED)
  45. * PC_DIRTY - mark the pages as dirty as they are committed
  46. * (ignored if PC_PRESENT, PC_FIXED or PC_LOCKED
  47. * isn't specified)
  48. * EXIT: non-zero if success, 0 if failure
  49. */
  50. ULONG EXTERNAL
  51. PageCommit(ULONG page, ULONG npages, ULONG hpd, ULONG pagerdata, ULONG flags)
  52. {
  53. return (ULONG_PTR) VirtualAlloc((LPVOID)(page * PAGESIZE), npages * PAGESIZE, MEM_COMMIT, PAGE_READWRITE);
  54. }
  55. /***SN PageDecommit - decommit physical pages from a specific address
  56. *
  57. * The pages must be within an address range previously allocated
  58. * by a single call to PageReserve. Though it is not an error to
  59. * call PageDecommit on a range including pages which are already
  60. * decommitted, such behavoir is discouraged because it is a waste of time.
  61. *
  62. * ENTRY: page - virtual page number of first page to decommit
  63. * npages - number of pages to decommit
  64. * flags - PC_STATIC - allow decommit in AR_STATIC object
  65. * EXIT: non-zero if success, else 0 if failure
  66. */
  67. ULONG EXTERNAL
  68. PageDecommit(ULONG page, ULONG npages, ULONG flags)
  69. {
  70. // PREFAST - This generates a PREFAST error asking us to use the MEM_RELEASE flag
  71. // We do not want that and hence this error can be ignored.
  72. return (ULONG) VirtualFree((LPVOID)(page * PAGESIZE), npages * PAGESIZE, MEM_DECOMMIT);
  73. }
  74. /***SN PageReserve - allocate linear address space in the current context
  75. *
  76. * The address range allocated by PageReserve is not backed by any
  77. * physical memory. PageCommit, PageCommitPhys, or PageCommitContig
  78. * should be called before actually touching a reserved region.
  79. *
  80. * Optionally, page permission flags (PC_WRITEABLE and PC_USER) may be
  81. * passed into this service. The flags are not acted on in any way
  82. * (because uncommitted memory is always inaccessible) but they are stored
  83. * internally by the memory manager. The PageQuery service returns these
  84. * permissions in the mbi_AllocationProtect field of its information
  85. * structure.
  86. *
  87. * ENTRY: page - requested base address of object (virtual page number)
  88. * or a special value:
  89. * PR_PRIVATE - anywhere in current ring 3 private region
  90. * PR_SHARED - anywhere in the ring 3 shared region
  91. * PR_SYSTEM - anywhere in the system region
  92. * npages - number of pages to reserve
  93. * flags - PR_FIXED - so PageReAllocate will not move object
  94. * PR_STATIC - don't allow commits, decommits or frees
  95. * unless *_STATIC flag is passed in
  96. * PR_4MEG - returned address must be 4mb aligned
  97. * (this flag is ignored if a specific address
  98. * is requested by the "page" parameter)
  99. * PC_WRITEABLE, PC_USER - optional, see above
  100. *
  101. * EXIT: linear address of allocated object or -1 if error
  102. */
  103. ULONG EXTERNAL
  104. PageReserve(ULONG page, ULONG npages, ULONG flags)
  105. {
  106. ULONG uRet;
  107. if ((page == PR_PRIVATE) ||
  108. (page == PR_SHARED) ||
  109. (page == PR_SYSTEM))
  110. {
  111. page = 0;
  112. }
  113. uRet = (ULONG) VirtualAlloc((LPVOID)(page * PAGESIZE), npages * PAGESIZE, MEM_RESERVE, PAGE_READWRITE);
  114. if (!uRet)
  115. {
  116. uRet = -1;
  117. }
  118. return uRet;
  119. }
  120. /***SO PageFree - De-reserved and de-commit an entire memory object
  121. *
  122. * ENTRY: laddr - linear address (handle) of base of object to free
  123. * flags - PR_STATIC - allow freeing of AR_STATIC object
  124. * EXIT: non-0 if success, 0 if failure
  125. *
  126. */
  127. ULONG EXTERNAL
  128. _PageFree(ULONG laddr, ULONG flags)
  129. {
  130. return VirtualFree((LPVOID) laddr, 0, MEM_RELEASE);
  131. }
  132. KERNENTRY
  133. HouseCleanLogicallyDeadHandles(VOID)
  134. {
  135. return 0;
  136. }
  137. CRITICAL_SECTION *
  138. NewCrst()
  139. {
  140. CRITICAL_SECTION *lpcs = (CRITICAL_SECTION *) VirtualAlloc(0, sizeof(CRITICAL_SECTION), MEM_COMMIT, PAGE_READWRITE);
  141. if (lpcs)
  142. {
  143. InitializeCriticalSection(lpcs);
  144. }
  145. return lpcs;
  146. }
  147. VOID
  148. DisposeCrst(CRITICAL_SECTION *lpcs)
  149. {
  150. if (lpcs)
  151. {
  152. DeleteCriticalSection(lpcs);
  153. VirtualFree(lpcs, 0, MEM_RELEASE);
  154. }
  155. }
  156. DWORD KERNENTRY
  157. GetAppCompatFlags(VOID)
  158. {
  159. return 0;
  160. }
  161. VOID APIENTRY
  162. MakeCriticalSectionGlobal(LPCRITICAL_SECTION lpcsCriticalSection)
  163. {
  164. }
  165. BOOL KERNENTRY
  166. ReadProcessMemoryFromPDB(
  167. PPDB ppdb,
  168. LPVOID lpBaseAddress,
  169. LPVOID lpBuffer,
  170. DWORD nSize,
  171. LPDWORD lpNumberOfBytesRead
  172. )
  173. {
  174. return ReadProcessMemory(
  175. GetCurrentProcess(),
  176. lpBaseAddress,
  177. lpBuffer,
  178. nSize,
  179. lpNumberOfBytesRead);
  180. }
  181. BOOL WINAPI
  182. vHeapFree(
  183. HANDLE hHeap,
  184. DWORD dwFlags,
  185. LPVOID lpMem
  186. )
  187. {
  188. return HeapFree((HHEAP)hHeap, dwFlags, (LPSTR) lpMem);
  189. }
  190. BOOL
  191. _HeapInit()
  192. {
  193. ZeroMemory(&pdbCur, sizeof(PDB));
  194. pdbCur.hheapLocal = _HeapCreate(HEAP_SHARED, 0, 0);
  195. hheapKernel = pdbCur.hheapLocal;
  196. return (BOOL)(pdbCur.hheapLocal);
  197. }
  198. HANDLE
  199. _GetProcessHeap(void)
  200. {
  201. return GetCurrentPdb()->hheapLocal;
  202. }
  203. BOOL
  204. _IsOurHeap(HANDLE hHeap)
  205. {
  206. if (!IsBadReadPtr(hHeap, sizeof(HANDLE)))
  207. {
  208. return ((struct heapinfo_s *) hHeap)->hi_signature == HI_SIGNATURE;
  209. }
  210. else
  211. {
  212. return FALSE;
  213. }
  214. }