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.

436 lines
10 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. vspace.c
  5. Abstract:
  6. This module implements verification functions for
  7. virtual address space management interfaces.
  8. Author:
  9. Silviu Calinoiu (SilviuC) 22-Feb-2001
  10. Revision History:
  11. --*/
  12. #include "pch.h"
  13. #include "verifier.h"
  14. #include "support.h"
  15. //NTSYSCALLAPI
  16. NTSTATUS
  17. NTAPI
  18. AVrfpNtAllocateVirtualMemory(
  19. IN HANDLE ProcessHandle,
  20. IN OUT PVOID *BaseAddress,
  21. IN ULONG_PTR ZeroBits,
  22. IN OUT PSIZE_T RegionSize,
  23. IN ULONG AllocationType,
  24. IN ULONG Protect
  25. )
  26. {
  27. NTSTATUS Status;
  28. SYSTEM_BASIC_INFORMATION SystemInfo;
  29. //
  30. // Allocate top-down for 64 bit systems or 3Gb systems.
  31. //
  32. try {
  33. if (*BaseAddress == NULL) {
  34. Status = NtQuerySystemInformation (SystemBasicInformation,
  35. &SystemInfo,
  36. sizeof SystemInfo,
  37. NULL);
  38. if (NT_SUCCESS(Status)) {
  39. if (SystemInfo.MaximumUserModeAddress - SystemInfo.MinimumUserModeAddress > (ULONG_PTR)0x80000000) {
  40. AllocationType |= MEM_TOP_DOWN;
  41. }
  42. }
  43. }
  44. }
  45. except (EXCEPTION_EXECUTE_HANDLER) {
  46. }
  47. Status = NtAllocateVirtualMemory (ProcessHandle,
  48. BaseAddress,
  49. ZeroBits,
  50. RegionSize,
  51. AllocationType,
  52. Protect);
  53. if (NT_SUCCESS(Status)) {
  54. VsLogCall (VsVirtualAlloc,
  55. *BaseAddress,
  56. *RegionSize,
  57. AllocationType,
  58. Protect);
  59. }
  60. return Status;
  61. }
  62. //NTSYSCALLAPI
  63. NTSTATUS
  64. NTAPI
  65. AVrfpNtFreeVirtualMemory(
  66. IN HANDLE ProcessHandle,
  67. IN OUT PVOID *BaseAddress,
  68. IN OUT PSIZE_T RegionSize,
  69. IN ULONG FreeType
  70. )
  71. {
  72. NTSTATUS Status;
  73. Status = NtFreeVirtualMemory (ProcessHandle,
  74. BaseAddress,
  75. RegionSize,
  76. FreeType);
  77. if (NT_SUCCESS(Status)) {
  78. VsLogCall (VsVirtualFree,
  79. *BaseAddress,
  80. *RegionSize,
  81. FreeType,
  82. 0);
  83. }
  84. return Status;
  85. }
  86. //NTSYSCALLAPI
  87. NTSTATUS
  88. NTAPI
  89. AVrfpNtMapViewOfSection(
  90. IN HANDLE SectionHandle,
  91. IN HANDLE ProcessHandle,
  92. IN OUT PVOID *BaseAddress,
  93. IN ULONG_PTR ZeroBits,
  94. IN SIZE_T CommitSize,
  95. IN OUT PLARGE_INTEGER SectionOffset OPTIONAL,
  96. IN OUT PSIZE_T ViewSize,
  97. IN SECTION_INHERIT InheritDisposition,
  98. IN ULONG AllocationType,
  99. IN ULONG Protect
  100. )
  101. {
  102. NTSTATUS Status;
  103. SYSTEM_BASIC_INFORMATION SystemInfo;
  104. //
  105. // Allocate top-down for 64 bit systems or 3Gb systems.
  106. //
  107. try {
  108. if (*BaseAddress == NULL) {
  109. Status = NtQuerySystemInformation (SystemBasicInformation,
  110. &SystemInfo,
  111. sizeof SystemInfo,
  112. NULL);
  113. if (NT_SUCCESS(Status)) {
  114. if (SystemInfo.MaximumUserModeAddress - SystemInfo.MinimumUserModeAddress > (ULONG_PTR)0x80000000) {
  115. AllocationType |= MEM_TOP_DOWN;
  116. }
  117. }
  118. }
  119. }
  120. except (EXCEPTION_EXECUTE_HANDLER) {
  121. }
  122. Status = NtMapViewOfSection (SectionHandle,
  123. ProcessHandle,
  124. BaseAddress,
  125. ZeroBits,
  126. CommitSize,
  127. SectionOffset,
  128. ViewSize,
  129. InheritDisposition,
  130. AllocationType,
  131. Protect);
  132. if (NT_SUCCESS(Status)) {
  133. VsLogCall (VsMapView,
  134. *BaseAddress,
  135. *ViewSize,
  136. AllocationType,
  137. Protect);
  138. }
  139. #if 0 // silviuc:temp
  140. if (NT_SUCCESS(Status)) {
  141. PAVRF_HANDLE Section;
  142. //
  143. // Check out the section handle used.
  144. //
  145. Section = HandleFind (SectionHandle);
  146. if (Section == NULL) {
  147. Section = HandleAdd (SectionHandle,
  148. HANDLE_TYPE_SECTION,
  149. TRUE,
  150. NULL,
  151. NULL);
  152. }
  153. DbgPrint ("AVRF: MapView (hndl: %X, size: %p) => addr: %p\n",
  154. HandleToUlong(SectionHandle),
  155. *ViewSize,
  156. *BaseAddress);
  157. }
  158. #endif
  159. return Status;
  160. }
  161. //NTSYSCALLAPI
  162. NTSTATUS
  163. NTAPI
  164. AVrfpNtUnmapViewOfSection(
  165. IN HANDLE ProcessHandle,
  166. IN PVOID BaseAddress
  167. )
  168. {
  169. NTSTATUS Status;
  170. Status = NtUnmapViewOfSection (ProcessHandle,
  171. BaseAddress);
  172. if (NT_SUCCESS(Status)) {
  173. VsLogCall (VsUnmapView,
  174. BaseAddress,
  175. 0,
  176. 0,
  177. 0);
  178. }
  179. return Status;
  180. }
  181. //NTSYSCALLAPI
  182. NTSTATUS
  183. NTAPI
  184. AVrfpNtCreateSection (
  185. OUT PHANDLE SectionHandle,
  186. IN ACCESS_MASK DesiredAccess,
  187. IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
  188. IN PLARGE_INTEGER MaximumSize OPTIONAL,
  189. IN ULONG SectionPageProtection,
  190. IN ULONG AllocationAttributes,
  191. IN HANDLE FileHandle OPTIONAL
  192. )
  193. {
  194. NTSTATUS Status;
  195. Status = NtCreateSection (SectionHandle,
  196. DesiredAccess,
  197. ObjectAttributes,
  198. MaximumSize,
  199. SectionPageProtection,
  200. AllocationAttributes,
  201. FileHandle);
  202. #if 0 // silviuc:temp
  203. if (NT_SUCCESS(Status)) {
  204. PAVRF_HANDLE Section;
  205. PAVRF_HANDLE File;
  206. PWSTR Name;
  207. CheckObjectAttributes (ObjectAttributes);
  208. Name = (ObjectAttributes && ObjectAttributes->ObjectName) ?
  209. (ObjectAttributes->ObjectName->Buffer) : NULL;
  210. Section = HandleAdd (*SectionHandle,
  211. HANDLE_TYPE_SECTION,
  212. FALSE,
  213. Name,
  214. NULL);
  215. if (FileHandle) {
  216. File = HandleFind (FileHandle);
  217. if (File == NULL) {
  218. HandleAdd (FileHandle,
  219. HANDLE_TYPE_FILE,
  220. TRUE,
  221. NULL,
  222. NULL);
  223. }
  224. }
  225. DbgPrint ("AVRF: CreateSection (file: %X) => hndl: %X \n\tname: %ws\n",
  226. HandleToUlong(FileHandle),
  227. HandleToUlong(*SectionHandle),
  228. HandleName(Section));
  229. }
  230. #endif
  231. return Status;
  232. }
  233. //NTSYSCALLAPI
  234. NTSTATUS
  235. NTAPI
  236. AVrfpNtOpenSection(
  237. OUT PHANDLE SectionHandle,
  238. IN ACCESS_MASK DesiredAccess,
  239. IN POBJECT_ATTRIBUTES ObjectAttributes
  240. )
  241. {
  242. NTSTATUS Status;
  243. Status = NtOpenSection (SectionHandle,
  244. DesiredAccess,
  245. ObjectAttributes);
  246. #if 0 // silviuc:temp
  247. if (NT_SUCCESS(Status)) {
  248. PAVRF_HANDLE Section;
  249. PWSTR Name;
  250. Name = (ObjectAttributes && ObjectAttributes->ObjectName) ?
  251. (ObjectAttributes->ObjectName->Buffer) : NULL;
  252. Section = HandleAdd (*SectionHandle,
  253. HANDLE_TYPE_SECTION,
  254. FALSE,
  255. Name,
  256. NULL);
  257. DbgPrint ("AVRF: OpenSection () => hndl: %X\n\tname: %ws\n",
  258. HandleToUlong (*SectionHandle),
  259. HandleName (Section));
  260. }
  261. #endif
  262. return Status;
  263. }
  264. //NTSYSCALLAPI
  265. NTSTATUS
  266. NTAPI
  267. AVrfpNtCreateFile(
  268. OUT PHANDLE FileHandle,
  269. IN ACCESS_MASK DesiredAccess,
  270. IN POBJECT_ATTRIBUTES ObjectAttributes,
  271. OUT PIO_STATUS_BLOCK IoStatusBlock,
  272. IN PLARGE_INTEGER AllocationSize OPTIONAL,
  273. IN ULONG FileAttributes,
  274. IN ULONG ShareAccess,
  275. IN ULONG CreateDisposition,
  276. IN ULONG CreateOptions,
  277. IN PVOID EaBuffer OPTIONAL,
  278. IN ULONG EaLength
  279. )
  280. {
  281. NTSTATUS Status;
  282. Status = NtCreateFile (FileHandle,
  283. DesiredAccess,
  284. ObjectAttributes,
  285. IoStatusBlock,
  286. AllocationSize,
  287. FileAttributes,
  288. ShareAccess,
  289. CreateDisposition,
  290. CreateOptions,
  291. EaBuffer,
  292. EaLength);
  293. #if 0 // silviuc:temp
  294. if (NT_SUCCESS(Status)) {
  295. PAVRF_HANDLE File;
  296. PWSTR Name;
  297. Name = (ObjectAttributes && ObjectAttributes->ObjectName) ?
  298. (ObjectAttributes->ObjectName->Buffer) : NULL;
  299. File = HandleAdd (*FileHandle,
  300. HANDLE_TYPE_FILE,
  301. FALSE,
  302. Name,
  303. NULL);
  304. DbgPrint ("AVRF: CreateFile () => hndl: %X\n\tname: %ws\n",
  305. HandleToUlong (*FileHandle),
  306. HandleName (File));
  307. }
  308. #endif
  309. return Status;
  310. }
  311. //NTSYSCALLAPI
  312. NTSTATUS
  313. NTAPI
  314. AVrfpNtOpenFile(
  315. OUT PHANDLE FileHandle,
  316. IN ACCESS_MASK DesiredAccess,
  317. IN POBJECT_ATTRIBUTES ObjectAttributes,
  318. OUT PIO_STATUS_BLOCK IoStatusBlock,
  319. IN ULONG ShareAccess,
  320. IN ULONG OpenOptions
  321. )
  322. {
  323. NTSTATUS Status;
  324. Status = NtOpenFile (FileHandle,
  325. DesiredAccess,
  326. ObjectAttributes,
  327. IoStatusBlock,
  328. ShareAccess,
  329. OpenOptions);
  330. #if 0 // silviuc:temp
  331. if (NT_SUCCESS(Status)) {
  332. PAVRF_HANDLE File;
  333. PWSTR Name;
  334. Name = (ObjectAttributes && ObjectAttributes->ObjectName) ?
  335. (ObjectAttributes->ObjectName->Buffer) : NULL;
  336. File = HandleAdd (*FileHandle,
  337. HANDLE_TYPE_FILE,
  338. FALSE,
  339. Name,
  340. NULL);
  341. DbgPrint ("AVRF: OpenFile () => hndl: %X\n\tname: %ws\n",
  342. HandleToUlong(*FileHandle),
  343. HandleName (File));
  344. }
  345. #endif
  346. return Status;
  347. }