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.

165 lines
5.3 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. typedef unsigned long *POINTER_64 PULONG64;
  12. __cdecl main()
  13. {
  14. LONG i, j;
  15. PVOID64 p1;
  16. PVOID64 p2;
  17. PVOID64 p3;
  18. PULONG64 long64;
  19. ULONG Size2, Size3;
  20. ULONGLONG Size1;
  21. NTSTATUS status, alstatus;
  22. HANDLE CurrentProcessHandle;
  23. HANDLE GiantSection;
  24. HANDLE Section2, Section4;
  25. MEMORY_BASIC_INFORMATION MemInfo;
  26. ULONG OldProtect;
  27. STRING Name3;
  28. HANDLE Section1;
  29. OBJECT_ATTRIBUTES ObjectAttributes;
  30. OBJECT_ATTRIBUTES Object1Attributes;
  31. ULONG ViewSize;
  32. LARGE_INTEGER Offset;
  33. LARGE_INTEGER SectionSize;
  34. UNICODE_STRING Unicode;
  35. CurrentProcessHandle = NtCurrentProcess();
  36. DbgPrint(" 64-bit Memory Management Tests - AllocVm, FreeVm, ProtectVm, QueryVm\n");
  37. Size1 = 1*1024*1024;
  38. p1 = NULL;
  39. alstatus = NtAllocateVirtualMemory64 (CurrentProcessHandle,
  40. (PVOID *)&p1,
  41. 0,
  42. &Size1,
  43. MEM_RESERVE | MEM_COMMIT,
  44. PAGE_READWRITE);
  45. if (!NT_SUCCESS(alstatus)) {
  46. DbgPrint("failed first created vm status %X start %lx size %lx\n",
  47. alstatus, (ULONG)p1, Size1);
  48. DbgPrint("******** FAILED TEST 1 **************\n");
  49. return 1;
  50. }
  51. printf("starting va %lx %08lx\n", (ULONG)((ULONGLONG)p1 >> 32),(ULONG)((ULONGLONG)p1));
  52. printf("touching va %lx %08lx\n", (ULONG)((ULONGLONG)p1 >> 32),(ULONG)(ULONGLONG)p1);
  53. long64 = p1;
  54. *long64 = 77;
  55. p2 = NULL;
  56. alstatus = NtAllocateVirtualMemory64 (CurrentProcessHandle,
  57. (PVOID *)&p2,
  58. 0,
  59. &Size1,
  60. MEM_RESERVE | MEM_COMMIT,
  61. PAGE_READWRITE);
  62. if (!NT_SUCCESS(alstatus)) {
  63. DbgPrint("failed first created vm status %X start %lx size %lx\n",
  64. alstatus, (ULONG)p2, Size1);
  65. DbgPrint("******** FAILED TEST 1 **************\n");
  66. return 1;
  67. }
  68. printf("starting va %lx %08lx\n", (ULONG)((ULONGLONG)p2 >> 32),(ULONG)((ULONGLONG)p2));
  69. p3 = NULL;
  70. alstatus = NtAllocateVirtualMemory64 (CurrentProcessHandle,
  71. (PVOID *)&p3,
  72. 0,
  73. &Size1,
  74. MEM_RESERVE | MEM_COMMIT,
  75. PAGE_READWRITE);
  76. if (!NT_SUCCESS(alstatus)) {
  77. DbgPrint("failed first created vm status %X start %lx size %lx\n",
  78. alstatus, (ULONG)p3, Size1);
  79. DbgPrint("******** FAILED TEST 1 **************\n");
  80. return 1;
  81. }
  82. printf("starting va %lx %08lx\n", (ULONG)((ULONGLONG)p3 >> 32),(ULONG)((ULONGLONG)p3));
  83. printf("freeing va at %lx %08lx\n", (ULONG)((ULONGLONG)p2 >> 32),(ULONG)(ULONGLONG)p2);
  84. Size1 = 0;
  85. alstatus = NtFreeVirtualMemory64 (CurrentProcessHandle,
  86. (PVOID *)&p2,
  87. &Size1,
  88. MEM_RELEASE);
  89. if (!NT_SUCCESS(alstatus)) {
  90. DbgPrint("failed first free vm status %X start %lx size %lx\n",
  91. alstatus, (ULONG)p2, Size1);
  92. DbgPrint("******** FAILED TEST 1 **************\n");
  93. return 1;
  94. }
  95. printf("decommit va at %lx %08lx\n", (ULONG)((ULONGLONG)p3 >> 32),(ULONG)(ULONGLONG)p3);
  96. Size1 = 4096;
  97. alstatus = NtFreeVirtualMemory64 (CurrentProcessHandle,
  98. (PVOID *)&p3,
  99. &Size1,
  100. MEM_DECOMMIT);
  101. if (!NT_SUCCESS(alstatus)) {
  102. DbgPrint("failed first delete vm status %X start %lx size %lx\n",
  103. alstatus, (ULONG)p3, Size1);
  104. DbgPrint("******** FAILED TEST 1 **************\n");
  105. return 1;
  106. }
  107. // return 0;
  108. #if 0
  109. status = NtQueryVirtualMemory (CurrentProcessHandle, p1,
  110. MemoryBasicInformation,
  111. &MemInfo, sizeof (MEMORY_BASIC_INFORMATION),
  112. NULL);
  113. if (!NT_SUCCESS(status)) {
  114. DbgPrint("******** FAILED TEST 2 **************\n");
  115. DbgPrint("FAILURE query vm status %X address %lx Base %lx size %lx\n",
  116. status,
  117. p1,
  118. MemInfo.BaseAddress,
  119. MemInfo.RegionSize);
  120. DbgPrint(" state %lx protect %lx type %lx\n",
  121. MemInfo.State,
  122. MemInfo.Protect,
  123. MemInfo.Type);
  124. }
  125. if ((MemInfo.RegionSize != Size1) || (MemInfo.BaseAddress != p1) ||
  126. (MemInfo.Protect != PAGE_READWRITE) || (MemInfo.Type != MEM_PRIVATE) ||
  127. (MemInfo.State != MEM_COMMIT)) {
  128. DbgPrint("******** FAILED TEST 3 **************\n");
  129. DbgPrint("FAILURE query vm status %X address %lx Base %lx size %lx\n",
  130. status,
  131. p1,
  132. MemInfo.BaseAddress,
  133. MemInfo.RegionSize);
  134. }
  135. return 0;
  136. #endif //0
  137. }