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.

1250 lines
37 KiB

  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <memory.h>
  5. #include <nt.h>
  6. #include <ntrtl.h>
  7. #include <nturtl.h>
  8. #include <windows.h>
  9. #define DbgPrint printf
  10. #define NtTerminateProcess(a,b) ExitProcess(b)
  11. __cdecl
  12. main(
  13. )
  14. {
  15. SIZE_T size, Size;
  16. PVOID BaseAddress;
  17. LONG i, j;
  18. PULONG p4, p3, p2, p1, oldp1, vp1;
  19. SIZE_T Size1, Size2, Size3;
  20. NTSTATUS status, alstatus;
  21. HANDLE CurrentProcessHandle;
  22. HANDLE GiantSection;
  23. HANDLE Section2, Section4;
  24. MEMORY_BASIC_INFORMATION MemInfo;
  25. ULONG OldProtect;
  26. STRING Name3;
  27. HANDLE Section1;
  28. OBJECT_ATTRIBUTES ObjectAttributes;
  29. OBJECT_ATTRIBUTES Object1Attributes;
  30. SIZE_T ViewSize;
  31. LARGE_INTEGER Offset;
  32. LARGE_INTEGER SectionSize;
  33. UNICODE_STRING Unicode;
  34. LOGICAL Os64Bit;
  35. SYSTEM_PROCESSOR_INFORMATION SysInfo;
  36. Os64Bit = FALSE;
  37. //
  38. // If we're running on a 64-bit OS, make large memory calls.
  39. //
  40. status = NtQuerySystemInformation (SystemProcessorInformation,
  41. &SysInfo,
  42. sizeof(SYSTEM_PROCESSOR_INFORMATION),
  43. NULL);
  44. if (NT_SUCCESS(status)) {
  45. if (SysInfo.ProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
  46. SysInfo.ProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) {
  47. Os64Bit = TRUE;
  48. }
  49. }
  50. DbgPrint("****Memory Management Tests (%d-bit) - AllocVm, FreeVm, ProtectVm, QueryVm\n", Os64Bit == TRUE ? 64 : 32);
  51. CurrentProcessHandle = NtCurrentProcess();
  52. p1 = (PULONG)0x20020000;
  53. Size1 = 0xbc0000;
  54. DbgPrint(" Test 1 - ");
  55. alstatus = NtAllocateVirtualMemory(CurrentProcessHandle,
  56. (PVOID *)&p1,
  57. 0,
  58. &Size1,
  59. MEM_RESERVE | MEM_COMMIT,
  60. PAGE_READWRITE);
  61. if (!NT_SUCCESS(alstatus)) {
  62. DbgPrint("Failed allocate with status %lx start %p size %p\n",
  63. alstatus,
  64. p1,
  65. (void *)Size1);
  66. } else {
  67. DbgPrint("Succeeded\n");
  68. }
  69. DbgPrint(" Test 2 - ");
  70. status = NtQueryVirtualMemory(CurrentProcessHandle,
  71. p1,
  72. MemoryBasicInformation,
  73. &MemInfo,
  74. sizeof(MEMORY_BASIC_INFORMATION),
  75. NULL);
  76. if (!NT_SUCCESS(status) ||
  77. (MemInfo.RegionSize != Size1) ||
  78. (MemInfo.BaseAddress != p1) ||
  79. (MemInfo.Protect != PAGE_READWRITE) ||
  80. (MemInfo.Type != MEM_PRIVATE) ||
  81. (MemInfo.State != MEM_COMMIT)) {
  82. DbgPrint("Failed query with status %lx address %p Base %p size %p\n",
  83. status,
  84. p1,
  85. MemInfo.BaseAddress,
  86. (void *)MemInfo.RegionSize);
  87. DbgPrint(" state %lx protect %lx type %lx\n",
  88. MemInfo.State,
  89. MemInfo.Protect,
  90. MemInfo.Type);
  91. } else {
  92. DbgPrint("Succeeded\n");
  93. }
  94. DbgPrint(" Test 3 - ");
  95. p2 = NULL;
  96. Size2 = 0x100000;
  97. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  98. (PVOID *)&p2,
  99. 3,
  100. &Size2,
  101. MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT,
  102. PAGE_READWRITE);
  103. if (!NT_SUCCESS(status)) {
  104. DbgPrint("Failed allocate with status %lx start %p size %p\n",
  105. status,
  106. p2,
  107. (void *)Size2);
  108. } else {
  109. DbgPrint("Succeeded\n");
  110. }
  111. //
  112. // Touch every other page.
  113. //
  114. DbgPrint(" Test 4 - ");
  115. try {
  116. vp1 = p2 + 3000;
  117. while (vp1 < (p2 + (Size2 / sizeof(ULONG)))) {
  118. *vp1 = 938;
  119. vp1 += 3000;
  120. }
  121. DbgPrint("Succeeded\n");
  122. } except(EXCEPTION_EXECUTE_HANDLER) {
  123. DbgPrint("Failed with an exception\n");
  124. }
  125. //
  126. // Decommit pages.
  127. //
  128. DbgPrint(" Test 5 - ");
  129. Size3 = Size2 - 5044;
  130. vp1 = p2 + 3000;
  131. status = NtFreeVirtualMemory(CurrentProcessHandle,
  132. (PVOID *)&p2,
  133. &Size3,
  134. MEM_DECOMMIT);
  135. if (!(NT_SUCCESS(status))) {
  136. DbgPrint("Failed free with status %lx\n", status);
  137. } else {
  138. DbgPrint("Succeeded\n");
  139. }
  140. //
  141. // Split the memory block using MEM_RELEASE.
  142. //
  143. DbgPrint(" Test 6 - ");
  144. vp1 = p2 + 5000;
  145. Size3 = Size2 - 50000;
  146. status = NtFreeVirtualMemory(CurrentProcessHandle,
  147. (PVOID *)&vp1,
  148. &Size3,
  149. MEM_RELEASE);
  150. if (!(NT_SUCCESS(status))) {
  151. DbgPrint("Failed free with status %lx\n", status);
  152. } else {
  153. DbgPrint("Succeeded\n");
  154. }
  155. DbgPrint(" Test 7 - ");
  156. vp1 = p2 + 3000;
  157. Size3 = 41;
  158. status = NtFreeVirtualMemory(CurrentProcessHandle,
  159. (PVOID *)&vp1,
  160. &Size3,
  161. MEM_RELEASE);
  162. if (!(NT_SUCCESS(status))) {
  163. DbgPrint("Failed free with status %lx\n", status);
  164. } else {
  165. DbgPrint("Succeeded\n");
  166. }
  167. //
  168. // free every page, ignore the status.
  169. //
  170. vp1 = p2;
  171. Size3 = 30;
  172. while (vp1 < (p2 + (Size2 / sizeof(ULONG)))) {
  173. status = NtFreeVirtualMemory(CurrentProcessHandle,
  174. (PVOID *)&vp1,
  175. &Size3,
  176. MEM_RELEASE);
  177. vp1 += 128;
  178. }
  179. DbgPrint(" Test 8 - ");
  180. p2 = NULL;
  181. Size2 = 0x10000;
  182. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  183. (PVOID *)&p2,
  184. 3,
  185. &Size2,
  186. MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT,
  187. PAGE_READWRITE);
  188. if (!NT_SUCCESS(status)) {
  189. DbgPrint("Failed allocate with status %lx start %p size %p\n",
  190. status,
  191. p2,
  192. (void *)Size1);
  193. } else {
  194. if (p2 < (PULONG)0x1fff0000) {
  195. DbgPrint("Failed allocate at top of memory at %p\n", p2);
  196. }
  197. status = NtFreeVirtualMemory(CurrentProcessHandle,
  198. (PVOID *)&p2,
  199. &Size2,
  200. MEM_RELEASE);
  201. if (!(NT_SUCCESS(status))) {
  202. DbgPrint("Failed free with status %lx\n", status);
  203. } else {
  204. DbgPrint("Succeeded with allocation at %p\n", p2);
  205. }
  206. }
  207. DbgPrint(" Test 9 - ");
  208. if (NT_SUCCESS(alstatus)) {
  209. status = NtFreeVirtualMemory(CurrentProcessHandle,
  210. (PVOID *)&p1,
  211. &Size1,
  212. MEM_RELEASE);
  213. if (!(NT_SUCCESS(status))) {
  214. DbgPrint("Failed free with status %lx\n", status);
  215. } else {
  216. DbgPrint("Succeeded\n");
  217. }
  218. } else {
  219. DbgPrint("Failed allocate with status %lx\n", alstatus);
  220. }
  221. DbgPrint(" Test 10 - ");
  222. p1 = NULL;
  223. Size1 = 16 * 4096;
  224. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  225. (PVOID *)&p1,
  226. 0,
  227. &Size1,
  228. MEM_RESERVE, PAGE_READWRITE | PAGE_GUARD);
  229. if (!NT_SUCCESS(status)) {
  230. DbgPrint("Failed created with status %lx start %p size %p\n",
  231. status,
  232. p1,
  233. (void *)Size1);
  234. } else {
  235. DbgPrint("Succeeded\n");
  236. }
  237. DbgPrint(" Test 11 - ");
  238. status = NtQueryVirtualMemory(CurrentProcessHandle,
  239. p1,
  240. MemoryBasicInformation,
  241. &MemInfo,
  242. sizeof(MEMORY_BASIC_INFORMATION),
  243. NULL);
  244. if (!NT_SUCCESS(status) ||
  245. (MemInfo.RegionSize != Size1) ||
  246. (MemInfo.BaseAddress != p1) ||
  247. (MemInfo.AllocationProtect != (PAGE_READWRITE | PAGE_GUARD)) ||
  248. (MemInfo.Protect != 0) ||
  249. (MemInfo.Type != MEM_PRIVATE) ||
  250. (MemInfo.State != MEM_RESERVE)) {
  251. DbgPrint("Failed query with status %lx address %p Base %p size %p\n",
  252. status,
  253. p1,
  254. MemInfo.BaseAddress,
  255. (void *)MemInfo.RegionSize);
  256. DbgPrint(" state %lx protect %lx alloc_protect %lx type %lx\n",
  257. MemInfo.State,
  258. MemInfo.Protect,
  259. MemInfo.AllocationProtect,
  260. MemInfo.Type);
  261. } else {
  262. DbgPrint("Succeeded\n");
  263. }
  264. DbgPrint(" Test 12 - ");
  265. Size2 = 8192;
  266. oldp1 = p1;
  267. p1 = p1 + 14336; // 64k -8k /4
  268. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  269. (PVOID *)&p1,
  270. 0,
  271. &Size2,
  272. MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  273. if (!NT_SUCCESS(status)) {
  274. DbgPrint("Failed creat with status %lx start %p size %p\n",
  275. status,
  276. p1,
  277. (void *)Size1);
  278. } else {
  279. DbgPrint("Succeeded\n");
  280. }
  281. DbgPrint(" Test 13 - ");
  282. status = NtQueryVirtualMemory(CurrentProcessHandle,
  283. oldp1,
  284. MemoryBasicInformation,
  285. &MemInfo,
  286. sizeof(MEMORY_BASIC_INFORMATION),
  287. NULL);
  288. if (!NT_SUCCESS(status) ||
  289. (MemInfo.RegionSize != 56*1024) ||
  290. (MemInfo.BaseAddress != oldp1) ||
  291. (MemInfo.AllocationProtect != (PAGE_READWRITE | PAGE_GUARD)) ||
  292. (MemInfo.Protect != 0) ||
  293. (MemInfo.Type != MEM_PRIVATE) ||
  294. (MemInfo.State != MEM_RESERVE)) {
  295. DbgPrint("Failed query with status %lx address %p Base %p size %p\n",
  296. status,
  297. oldp1,
  298. MemInfo.BaseAddress,
  299. (void *)MemInfo.RegionSize);
  300. DbgPrint(" state %lx protect %lx alloc_protect %lx type %lx\n",
  301. MemInfo.State,
  302. MemInfo.Protect,
  303. MemInfo.AllocationProtect,
  304. MemInfo.Type);
  305. } else {
  306. DbgPrint("Succeeded\n");
  307. }
  308. DbgPrint(" Test 14 - ");
  309. status = NtQueryVirtualMemory(CurrentProcessHandle,
  310. p1,
  311. MemoryBasicInformation,
  312. &MemInfo,
  313. sizeof(MEMORY_BASIC_INFORMATION),
  314. NULL);
  315. if (!NT_SUCCESS(status) ||
  316. (MemInfo.RegionSize != Size2) || (MemInfo.BaseAddress != p1) ||
  317. (MemInfo.Protect != PAGE_EXECUTE_READWRITE) || (MemInfo.Type != MEM_PRIVATE) ||
  318. (MemInfo.State != MEM_COMMIT)
  319. || (MemInfo.AllocationBase != oldp1)) {
  320. DbgPrint("Failed query with status %lx address %p Base %p size %p\n",
  321. status,
  322. oldp1,
  323. MemInfo.BaseAddress,
  324. (void *)MemInfo.RegionSize);
  325. DbgPrint(" state %lx protect %lx type %lx\n",
  326. MemInfo.State,
  327. MemInfo.Protect,
  328. MemInfo.Type);
  329. } else {
  330. DbgPrint("Succeeded\n");
  331. }
  332. DbgPrint(" Test 15 - ");
  333. Size1 = Size2;
  334. status = NtProtectVirtualMemory(CurrentProcessHandle,
  335. (PVOID *)&p1,
  336. &Size1,
  337. PAGE_READONLY | PAGE_NOCACHE, &OldProtect);
  338. if ((!NT_SUCCESS(status)) ||
  339. (OldProtect != PAGE_EXECUTE_READWRITE)) {
  340. DbgPrint("Failed protect with status %lx base %p size %p old protect %lx\n",
  341. status,
  342. p1,
  343. (void *)Size1,
  344. OldProtect);
  345. } else {
  346. DbgPrint("Succeeded\n");
  347. }
  348. DbgPrint(" Test 16 - ");
  349. status = NtQueryVirtualMemory(CurrentProcessHandle,
  350. p1,
  351. MemoryBasicInformation,
  352. &MemInfo,
  353. sizeof(MEMORY_BASIC_INFORMATION),
  354. NULL);
  355. if ((!NT_SUCCESS(status)) ||
  356. (MemInfo.Protect != (PAGE_NOCACHE | PAGE_READONLY))) {
  357. DbgPrint("Failed query with status %lx address %p Base %p size %p\n",
  358. status,
  359. p1,
  360. MemInfo.BaseAddress,
  361. (void *)MemInfo.RegionSize);
  362. DbgPrint(" state %lx protect %lx type %lx\n",
  363. MemInfo.State,
  364. MemInfo.Protect,
  365. MemInfo.Type);
  366. } else {
  367. DbgPrint("Succeeded\n");
  368. }
  369. DbgPrint(" Test 17 - ");
  370. i = *p1;
  371. status = NtProtectVirtualMemory(CurrentProcessHandle,
  372. (PVOID *)&p1,
  373. &Size1,
  374. PAGE_NOACCESS | PAGE_NOCACHE, &OldProtect);
  375. if (status != STATUS_INVALID_PAGE_PROTECTION) {
  376. DbgPrint("Failed protect with status %lx, base %p, size %p, old protect %lx\n",
  377. status,
  378. p1,
  379. (void *)Size1,
  380. OldProtect);
  381. } else {
  382. DbgPrint("Succeeded\n");
  383. }
  384. DbgPrint(" Test 18 - ");
  385. status = NtProtectVirtualMemory(CurrentProcessHandle,
  386. (PVOID *)&p1,
  387. &Size1,
  388. PAGE_READONLY,
  389. &OldProtect);
  390. if ((!NT_SUCCESS(status)) ||
  391. (OldProtect != (PAGE_NOCACHE | PAGE_READONLY))) {
  392. DbgPrint("Failed protect with status %lx base %p size %p old protect %lx\n",
  393. status,
  394. p1,
  395. (void *)Size1,
  396. OldProtect);
  397. } else {
  398. DbgPrint("Succeeded\n");
  399. }
  400. DbgPrint(" Test 19 - ");
  401. status = NtProtectVirtualMemory(CurrentProcessHandle,
  402. (PVOID *)&p1,
  403. &Size1,
  404. PAGE_READWRITE,
  405. &OldProtect);
  406. if ((!NT_SUCCESS(status)) ||
  407. (OldProtect != (PAGE_READONLY))) {
  408. DbgPrint("Failed protect with status %lx base %p size %p old protect %lx\n",
  409. status,
  410. p1,
  411. (void *)Size1,
  412. OldProtect);
  413. } else {
  414. DbgPrint("Succeeded\n");
  415. }
  416. DbgPrint(" Test 20 - ");
  417. for (i = 1; i < 12; i++) {
  418. p2 = NULL;
  419. Size2 = i * 4096;
  420. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  421. (PVOID *)&p2,
  422. 0,
  423. &Size2,
  424. MEM_COMMIT,
  425. PAGE_READWRITE);
  426. if (!NT_SUCCESS(status)) {
  427. DbgPrint("Failed creat with status %lx start %p size %p\n",
  428. status,
  429. p2,
  430. (void *)Size2);
  431. break;
  432. }
  433. if (i == 4) {
  434. p3 = p2;
  435. }
  436. if (i == 8) {
  437. Size3 = 12000;
  438. status = NtFreeVirtualMemory(CurrentProcessHandle,
  439. (PVOID *)&p3,
  440. &Size3,
  441. MEM_RELEASE);
  442. if (!NT_SUCCESS(status)) {
  443. DbgPrint("Failed free with status %lx start %p size %p\n",
  444. status,
  445. p3,
  446. (void *)Size3);
  447. break;
  448. }
  449. }
  450. }
  451. if (i == 12) {
  452. DbgPrint("Succeeded\n");
  453. }
  454. DbgPrint(" Test 21 - ");
  455. p3 = p1 + 8 * 1024;
  456. status = NtQueryVirtualMemory(CurrentProcessHandle,
  457. p3,
  458. MemoryBasicInformation,
  459. &MemInfo,
  460. sizeof(MEMORY_BASIC_INFORMATION),
  461. NULL);
  462. if (!NT_SUCCESS(status)) {
  463. DbgPrint("Failed query with status %lx address %p Base %p size %p\n",
  464. status,
  465. p3,
  466. MemInfo.BaseAddress,
  467. (void *)MemInfo.RegionSize);
  468. DbgPrint(" state %lx protect %lx type %lx\n",
  469. MemInfo.State,
  470. MemInfo.Protect,
  471. MemInfo.Type);
  472. } else {
  473. DbgPrint("Succeeded\n");
  474. }
  475. DbgPrint(" Test 22 - ");
  476. p3 = p1 - 8 * 1024;
  477. status = NtQueryVirtualMemory(CurrentProcessHandle,
  478. p3,
  479. MemoryBasicInformation,
  480. &MemInfo,
  481. sizeof(MEMORY_BASIC_INFORMATION),
  482. NULL);
  483. if (!NT_SUCCESS(status)) {
  484. DbgPrint("Failed query with status %lx address %p Base %p size %p\n",
  485. status,
  486. p3,
  487. MemInfo.BaseAddress,
  488. (void *)MemInfo.RegionSize);
  489. DbgPrint(" state %lx protect %lx type %lx\n",
  490. MemInfo.State,
  491. MemInfo.Protect,
  492. MemInfo.Type);
  493. } else {
  494. DbgPrint("Succeeded\n");
  495. }
  496. DbgPrint(" Test 23 - ");
  497. Size3 = 16 * 4096;
  498. status = NtFreeVirtualMemory(CurrentProcessHandle,
  499. (PVOID *)&p3,
  500. &Size3,
  501. MEM_RELEASE);
  502. if (status != STATUS_UNABLE_TO_FREE_VM) {
  503. DbgPrint("Failed free with status %lx start %p size %p\n",
  504. status,
  505. p3,
  506. (void *)Size3);
  507. } else {
  508. DbgPrint("Succeeded\n");
  509. }
  510. DbgPrint(" Test 24 - ");
  511. Size3 = 1 * 4096;
  512. status = NtFreeVirtualMemory(CurrentProcessHandle,
  513. (PVOID *)&p3,
  514. &Size3,
  515. MEM_RELEASE);
  516. if (!NT_SUCCESS(status)) {
  517. DbgPrint("Failed free with status %lx start %p size %p\n",
  518. status,
  519. p3,
  520. (void *)Size3);
  521. } else {
  522. DbgPrint("Succeeded\n");
  523. }
  524. DbgPrint(" Test 25 - ");
  525. p3 = NULL;
  526. Size3 = 300 * 4096;
  527. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  528. (PVOID *)&p3,
  529. 0,
  530. &Size3,
  531. MEM_COMMIT, PAGE_READWRITE);
  532. if (!NT_SUCCESS(status)) {
  533. DbgPrint("Failed creat with status %lx start %p size %p\n",
  534. status,
  535. p3,
  536. (void *)Size3);
  537. } else {
  538. DbgPrint("Succeeded\n");
  539. }
  540. DbgPrint(" Test 26 - ");
  541. p1 = p3;
  542. p2 = ((PULONG)((PUCHAR)p3 + Size3));
  543. p4 = p1;
  544. j = 0;
  545. while (p3 < p2) {
  546. j += 1;
  547. if (j % 8 == 0) {
  548. if (*p4 != (ULONG)((ULONG_PTR)p4)) {
  549. DbgPrint("Failed bad value in xcell %p value is %lx\n", p4, *p4);
  550. break;
  551. }
  552. p4 += 1;
  553. *p4 = (ULONG)((ULONG_PTR)p4);
  554. p4 = p4 + 1026;
  555. }
  556. *p3 = (ULONG)((ULONG_PTR)p3);
  557. p3 += 1027;
  558. }
  559. if (p3 >= p2) {
  560. DbgPrint("Succeeded\n");
  561. }
  562. DbgPrint(" Test 27 - ");
  563. status = NtQueryVirtualMemory(CurrentProcessHandle,
  564. p3,
  565. MemoryBasicInformation,
  566. &MemInfo,
  567. sizeof(MEMORY_BASIC_INFORMATION),
  568. NULL);
  569. if (!NT_SUCCESS(status)) {
  570. DbgPrint("Failed query with status %lx address %p Base %p size %p\n",
  571. status,
  572. p3,
  573. MemInfo.BaseAddress,
  574. (void *)MemInfo.RegionSize);
  575. DbgPrint(" state %lx protect %lx type %lx\n",
  576. MemInfo.State,
  577. MemInfo.Protect,
  578. MemInfo.Type);
  579. } else {
  580. DbgPrint("Succeeded\n");
  581. }
  582. DbgPrint(" Test 28 - ");
  583. p3 = p1;
  584. while (p3 < p2) {
  585. if (*p3 != (ULONG)((ULONG_PTR)p3)) {
  586. DbgPrint("Failed bad value in 1cell %p value is %lx\n", p3, *p3);
  587. break;
  588. }
  589. p3 += 1027;
  590. }
  591. if (p3 >= p2) {
  592. DbgPrint("Succeeded\n");
  593. }
  594. DbgPrint(" Test 29 - ");
  595. p3 = p1;
  596. while (p3 < p2) {
  597. if (*p3 != (ULONG)((ULONG_PTR)p3)) {
  598. DbgPrint("Failed bad value in 2cell %p value is %lx\n", p3, *p3);
  599. break;
  600. }
  601. p3 += 1027;
  602. }
  603. if (p3 >= p2) {
  604. DbgPrint("Succeeded\n");
  605. }
  606. DbgPrint(" Test 30 - ");
  607. p3 = p1;
  608. while (p3 < p2) {
  609. if (*p3 != (ULONG)((ULONG_PTR)p3)) {
  610. DbgPrint("Failed bad value in 3cell %p value is %lx\n", p3, *p3);
  611. break;
  612. }
  613. p3 += 1027;
  614. }
  615. if (p3 >= p2) {
  616. DbgPrint("Succeeded\n");
  617. }
  618. DbgPrint(" Test 31 - ");
  619. p3 = p1;
  620. while (p3 < p2) {
  621. if (*p3 != (ULONG)((ULONG_PTR)p3)) {
  622. DbgPrint("Failed bad value in 4cell %p value is %lx\n", p3, *p3);
  623. break;
  624. }
  625. p3 += 1027;
  626. }
  627. if (p3 >= p2) {
  628. DbgPrint("Succeeded\n");
  629. }
  630. DbgPrint(" Test 32 - ");
  631. p3 = p1;
  632. while (p3 < p2) {
  633. if (*p3 != (ULONG)((ULONG_PTR)p3)) {
  634. DbgPrint("Failed bad value in 5cell %p value is %lx\n", p3, *p3);
  635. break;
  636. }
  637. p3 += 1027;
  638. }
  639. if (p3 >= p2) {
  640. DbgPrint("Succeeded\n");
  641. }
  642. DbgPrint(" Test 33 - ");
  643. p3 = p1;
  644. while (p3 < p2) {
  645. if (*p3 != (ULONG)((ULONG_PTR)p3)) {
  646. DbgPrint("Failed bad value in cell %p value is %lx\n", p3, *p3);
  647. break;
  648. }
  649. p3 += 1027;
  650. }
  651. if (p3 >= p2) {
  652. DbgPrint("Succeeded\n");
  653. }
  654. //
  655. // Check physical frame mapping.
  656. //
  657. DbgPrint(" Test 34 - ");
  658. RtlInitAnsiString(&Name3, "\\Device\\PhysicalMemory");
  659. RtlAnsiStringToUnicodeString(&Unicode, &Name3, TRUE);
  660. InitializeObjectAttributes(&ObjectAttributes,
  661. &Unicode,
  662. OBJ_CASE_INSENSITIVE,
  663. NULL,
  664. NULL);
  665. status = NtOpenSection(&Section1,
  666. SECTION_MAP_READ | SECTION_MAP_WRITE,
  667. &ObjectAttributes);
  668. RtlFreeUnicodeString(&Unicode);
  669. if (!NT_SUCCESS(status)) {
  670. DbgPrint("Failed open physical section with status %lx\n", status);
  671. DbgPrint(" skipping test 35\n");
  672. goto Test36;
  673. } else {
  674. DbgPrint("Succeeded\n");
  675. }
  676. DbgPrint(" Test 35 - ");
  677. p1 = NULL;
  678. Offset.QuadPart = 0x810ff033;
  679. ViewSize = 300 * 4096;
  680. status = NtMapViewOfSection(Section1,
  681. NtCurrentProcess(),
  682. (PVOID *)&p1,
  683. 0,
  684. ViewSize,
  685. &Offset,
  686. &ViewSize,
  687. ViewUnmap,
  688. 0,
  689. PAGE_READWRITE);
  690. if (!NT_SUCCESS(status)) {
  691. DbgPrint("Failed map physical section %lx offset %p base %p\n",
  692. status,
  693. Offset.QuadPart,
  694. p1);
  695. } else {
  696. DbgPrint("Succeeded\n");
  697. }
  698. Test36:
  699. DbgPrint(" Test 36 - ");
  700. p1 = NULL;
  701. Size1 = 8 * 1024 * 1024;
  702. alstatus = NtAllocateVirtualMemory(CurrentProcessHandle,
  703. (PVOID *)&p1,
  704. 0,
  705. &Size1,
  706. MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  707. if (!NT_SUCCESS(alstatus)) {
  708. DbgPrint("Failed created with status %lx start %p size %p\n",
  709. alstatus,
  710. p1,
  711. Size1);
  712. } else {
  713. DbgPrint("Succeeded\n");
  714. }
  715. try {
  716. RtlZeroMemory(p1, Size1);
  717. } except(EXCEPTION_EXECUTE_HANDLER) {
  718. }
  719. DbgPrint(" Test 37 - ");
  720. Size1 -= 20000;
  721. (PUCHAR)p1 += 5000;
  722. status = NtFreeVirtualMemory(CurrentProcessHandle,
  723. (PVOID *)&p1,
  724. &Size1 ,
  725. MEM_DECOMMIT);
  726. if (!(NT_SUCCESS(status))) {
  727. DbgPrint("Failed free with status %lx\n", status);
  728. } else {
  729. DbgPrint("Succeeded\n");
  730. }
  731. DbgPrint(" Test 38 - ");
  732. Size1 -= 20000;
  733. (PUCHAR)p1 += 5000;
  734. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  735. (PVOID *)&p1,
  736. 0,
  737. &Size1,
  738. MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  739. if (!NT_SUCCESS(status)) {
  740. DbgPrint("Failed create with status %lx start %p size %p\n",
  741. status,
  742. p1,
  743. Size1);
  744. } else {
  745. DbgPrint("Succeeded\n");
  746. }
  747. try {
  748. RtlZeroMemory(p1, Size1);
  749. } except(EXCEPTION_EXECUTE_HANDLER) {
  750. }
  751. DbgPrint(" Test 39 - ");
  752. Size1 = 28 * 4096;
  753. p1 = NULL;
  754. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  755. (PVOID *)&p1,
  756. 0,
  757. &Size1,
  758. MEM_COMMIT, PAGE_READWRITE | PAGE_GUARD);
  759. if (!NT_SUCCESS(status)) {
  760. DbgPrint("Failed create with status %lx start %p size %p\n",
  761. status,
  762. p1,
  763. Size1);
  764. } else {
  765. DbgPrint("Succeeded\n");
  766. }
  767. DbgPrint(" Test 40 - ");
  768. try {
  769. //
  770. // attempt to write the guard page.
  771. //
  772. *p1 = 973;
  773. DbgPrint("Failed guard page exception did not occur\n");
  774. } except (EXCEPTION_EXECUTE_HANDLER) {
  775. status = GetExceptionCode();
  776. if (status != STATUS_GUARD_PAGE_VIOLATION) {
  777. DbgPrint("Failed incorrect guard exception code %lx\n", status);
  778. } else {
  779. DbgPrint("Succeeded\n");
  780. }
  781. }
  782. DbgPrint(" Test 41 - ");
  783. p2 = NULL;
  784. Size2 = 200 * 1024 * 1024; //200MB
  785. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  786. (PVOID *)&p2,
  787. 0,
  788. &Size2,
  789. MEM_COMMIT, PAGE_READWRITE);
  790. if (!NT_SUCCESS(status)) {
  791. DbgPrint("Failed allocate with status %lx\n", status);
  792. } else {
  793. status = NtFreeVirtualMemory(CurrentProcessHandle,
  794. (PVOID *)&p2,
  795. &Size2,
  796. MEM_RELEASE);
  797. if (!NT_SUCCESS(status)) {
  798. DbgPrint("Failed free with status %lx\n", status);
  799. } else {
  800. DbgPrint("Succeeded\n");
  801. }
  802. }
  803. //
  804. // Create a giant section 2gb on 32-bit system, 4gb on 64_bit system.
  805. //
  806. DbgPrint(" Test 42 - ");
  807. InitializeObjectAttributes(&Object1Attributes,
  808. NULL,
  809. 0,
  810. NULL,
  811. NULL);
  812. #if defined(_WIN64)
  813. SectionSize.QuadPart = 0xffffffff;
  814. #else
  815. SectionSize.QuadPart = 0x7f000000;
  816. #endif
  817. status = NtCreateSection(&GiantSection,
  818. SECTION_MAP_READ | SECTION_MAP_WRITE,
  819. &Object1Attributes,
  820. &SectionSize,
  821. PAGE_READWRITE,
  822. SEC_RESERVE,
  823. NULL);
  824. if (!NT_SUCCESS(status)) {
  825. DbgPrint("Failed create big section with status %lx\n", status);
  826. DbgPrint(" skipping test 43\n");
  827. goto Test44;
  828. } else {
  829. DbgPrint("Succeeded\n");
  830. }
  831. //
  832. // Attempt to map the section (this should fail).
  833. //
  834. DbgPrint(" Test 43 - ");
  835. p1 = NULL;
  836. ViewSize = 0;
  837. status = NtMapViewOfSection(GiantSection,
  838. CurrentProcessHandle,
  839. (PVOID *)&p1,
  840. 0L,
  841. 0,
  842. 0,
  843. &ViewSize,
  844. ViewUnmap,
  845. 0,
  846. PAGE_READWRITE );
  847. if (status != STATUS_NO_MEMORY) {
  848. DbgPrint("Failed map big section status %lx\n", status);
  849. } else {
  850. DbgPrint("Succeeded\n");
  851. }
  852. Test44:
  853. DbgPrint(" Test 44 - ");
  854. i = 0;
  855. #if defined(_WIN64)
  856. if (Os64Bit == TRUE) {
  857. Size2 = (SIZE_T)(32i64 * 1024 * 1024 * 1024 + 9938);
  858. }
  859. else {
  860. Size2 = 8 * 1024 * 1024 + 9938;
  861. }
  862. #else
  863. Size2 = 8 * 1024 * 1024 + 9938;
  864. #endif
  865. do {
  866. p2 = NULL;
  867. i += 1;
  868. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  869. (PVOID *)&p2,
  870. 0,
  871. &Size2,
  872. MEM_RESERVE, PAGE_READWRITE);
  873. } while (NT_SUCCESS(status));
  874. if (status != STATUS_NO_MEMORY) {
  875. DbgPrint("Failed with status %lx after %d allocations\n", status, i);
  876. } else {
  877. DbgPrint("Succeeded with %d allocations\n", i);
  878. }
  879. //
  880. // we pass an address of 1, so mm will round it down to 0. if we
  881. // passed 0, it looks like a not present argument
  882. //
  883. // N.B. We have to make two separate calls to allocatevm, because
  884. // we want a specific virtual address. If we don't first reserve
  885. // the address, the mm fails the commit call.
  886. //
  887. DbgPrint(" Test 45 - ");
  888. Size = 50 * 1024;
  889. size = Size - 1;
  890. BaseAddress = (PVOID)1;
  891. status = NtAllocateVirtualMemory(NtCurrentProcess(),
  892. &BaseAddress,
  893. 0L,
  894. &size,
  895. MEM_RESERVE,
  896. PAGE_READWRITE );
  897. if (!NT_SUCCESS(status)) {
  898. DbgPrint("Failed reserve with status = %lx\n", status);
  899. } else {
  900. DbgPrint("Succeeded\n");
  901. }
  902. DbgPrint(" Test 46 - ");
  903. size = Size - 1;
  904. BaseAddress = (PVOID)1;
  905. status = NtAllocateVirtualMemory(NtCurrentProcess(),
  906. &BaseAddress,
  907. 0L,
  908. &size,
  909. MEM_COMMIT,
  910. PAGE_READWRITE );
  911. if (!NT_SUCCESS(status)) {
  912. DbgPrint("Failed commit with status = %lx\n", status);
  913. } else {
  914. DbgPrint("Succeeded\n");
  915. }
  916. //
  917. // Test MEM_DOS_LIM support.
  918. //
  919. #ifdef i386
  920. DbgPrint(" Test 47 - ");
  921. InitializeObjectAttributes(&Object1Attributes,
  922. NULL,
  923. OBJ_CASE_INSENSITIVE,
  924. NULL,
  925. NULL);
  926. SectionSize.QuadPart = 1575757,
  927. status = NtCreateSection(&Section4,
  928. SECTION_MAP_READ | SECTION_MAP_WRITE,
  929. &Object1Attributes,
  930. &SectionSize,
  931. PAGE_READWRITE,
  932. SEC_COMMIT,
  933. NULL);
  934. if (!NT_SUCCESS(status)) {
  935. DbgPrint("Failed create section with status %lx section handle %lx\n",
  936. status,
  937. (ULONG)Section4);
  938. } else {
  939. DbgPrint("Succeeded\n");
  940. }
  941. DbgPrint(" Test 48 - ");
  942. p3 = (PVOID)0x9001000;
  943. ViewSize = 8000;
  944. status = NtMapViewOfSection(Section4,
  945. CurrentProcessHandle,
  946. (PVOID *)&p3,
  947. 0L,
  948. 0,
  949. 0,
  950. &ViewSize,
  951. ViewUnmap,
  952. MEM_DOS_LIM,
  953. PAGE_READWRITE );
  954. if (!NT_SUCCESS(status)) {
  955. DbgPrint("Failed map section with status %lx base %lx size %lx\n",
  956. status,
  957. (ULONG)p3,
  958. ViewSize);
  959. } else {
  960. DbgPrint("Succeeded\n");
  961. }
  962. DbgPrint(" Test 49 - ");
  963. p2 = (PVOID)0x9003000;
  964. ViewSize = 8000;
  965. status = NtMapViewOfSection(Section4,
  966. CurrentProcessHandle,
  967. (PVOID *)&p2,
  968. 0L,
  969. 0,
  970. 0,
  971. &ViewSize,
  972. ViewUnmap,
  973. MEM_DOS_LIM,
  974. PAGE_READWRITE );
  975. if (!NT_SUCCESS(status)) {
  976. DbgPrint("Failed map section with status %lx base %lx size %lx\n",
  977. status,
  978. (ULONG)p3,
  979. ViewSize);
  980. } else {
  981. DbgPrint("Succeeded\n");
  982. }
  983. DbgPrint(" Test 50 - ");
  984. status = NtQueryVirtualMemory(CurrentProcessHandle,
  985. p3,
  986. MemoryBasicInformation,
  987. &MemInfo,
  988. sizeof(MEMORY_BASIC_INFORMATION),
  989. NULL);
  990. if (!NT_SUCCESS(status)) {
  991. DbgPrint("Failed query with status %lx address %lx Base %lx size %lx\n",
  992. status,
  993. p1,
  994. MemInfo.BaseAddress,
  995. MemInfo.RegionSize);
  996. DbgPrint(" state %lx protect %lx type %lx\n",
  997. MemInfo.State,
  998. MemInfo.Protect,
  999. MemInfo.Type);
  1000. } else {
  1001. DbgPrint("Succeeded\n");
  1002. }
  1003. DbgPrint(" Test 51 - ");
  1004. *p3 = 98;
  1005. if (*p3 != *p2) {
  1006. DbgPrint("Failed compare with %lx %lx\n", *p3, *p2);
  1007. } else {
  1008. DbgPrint("Succeeded\n");
  1009. }
  1010. DbgPrint(" Test 52 - ");
  1011. Size2 = 8;
  1012. p1 = (PVOID)((ULONG)p2 - 0x3000);
  1013. status = NtAllocateVirtualMemory(CurrentProcessHandle,
  1014. (PVOID *)&p1,
  1015. 0,
  1016. &Size2,
  1017. MEM_COMMIT,
  1018. PAGE_EXECUTE_READWRITE);
  1019. if (NT_SUCCESS(status)) {
  1020. DbgPrint("Failed create with status %lx start %lx size %lx\n",
  1021. status,
  1022. p1,
  1023. Size1);
  1024. } else {
  1025. DbgPrint("Succeeded\n");
  1026. }
  1027. #endif
  1028. DbgPrint("****End of Memory Management Tests\n");
  1029. return 0;
  1030. }