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.

249 lines
7.2 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. taststrs.c
  5. Abstract:
  6. Tasking stress test.
  7. Author:
  8. Mark Lucovsky (markl) 26-Sep-1990
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. HANDLE Semaphore, Event;
  19. VOID
  20. TestThread(
  21. LPVOID ThreadParameter
  22. )
  23. {
  24. DWORD st;
  25. (ReleaseSemaphore(Semaphore,1,NULL));
  26. st = WaitForSingleObject(Event,500);
  27. ExitThread(0);
  28. }
  29. VOID
  30. NewProcess()
  31. {
  32. PUCHAR buffer;
  33. buffer = VirtualAlloc (NULL, 600*1024, MEM_COMMIT, PAGE_READWRITE);
  34. Sleep(10000);
  35. TerminateProcess(GetCurrentProcess(),0);
  36. }
  37. TCHAR szCmdLine[] = "vmread.exe +";
  38. const TCHAR szAppName[] = "vmread.exe";
  39. DWORD
  40. _cdecl
  41. main(
  42. int argc,
  43. char *argv[],
  44. char *envp[]
  45. )
  46. {
  47. STARTUPINFO StartupInfo;
  48. PROCESS_INFORMATION ProcessInfo;
  49. SIZE_T st;
  50. DWORD ProcessCount;
  51. SMALL_RECT Window;
  52. MEMORY_BASIC_INFORMATION info;
  53. PUCHAR address;
  54. PUCHAR buffer;
  55. PUCHAR SystemRangeStart;
  56. ProcessCount = 0;
  57. if ( strchr(GetCommandLine(),'+') ) {
  58. NewProcess();
  59. }
  60. if (!NT_SUCCESS(NtQuerySystemInformation(SystemRangeStartInformation,
  61. &SystemRangeStart,
  62. sizeof(SystemRangeStart),
  63. NULL))) {
  64. // assume usermode is the low half of the address space
  65. SystemRangeStart = (PUCHAR)MAXLONG_PTR;
  66. }
  67. GetStartupInfo(&StartupInfo);
  68. if (CreateProcess(
  69. szAppName,
  70. szCmdLine,
  71. NULL,
  72. NULL,
  73. FALSE,
  74. CREATE_NEW_CONSOLE,
  75. NULL,
  76. NULL,
  77. &StartupInfo,
  78. &ProcessInfo
  79. ))
  80. {
  81. printf("Process Created\n");
  82. Sleep (1000);
  83. buffer = VirtualAlloc (NULL, 10*1000*1000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  84. if (!buffer) {
  85. printf("virtual alloc failed %ld.\n",GetLastError());
  86. return 1;
  87. }
  88. address = NULL;
  89. do {
  90. if (!VirtualQueryEx (ProcessInfo.hProcess,
  91. (PVOID)address,
  92. &info,
  93. sizeof(info)))
  94. {
  95. printf ("virtual query failed %ld.\n",GetLastError());
  96. break;
  97. } else {
  98. printf("address: %p size %lx state %lx protect %lx type %lx\n",
  99. address,
  100. info.RegionSize,
  101. info.State,
  102. info.Protect,
  103. info.Type);
  104. }
  105. if ((info.Protect != PAGE_NOACCESS) &&
  106. (info.Protect != 0) &&
  107. (!(info.Protect & PAGE_GUARD))) {
  108. if (!ReadProcessMemory (ProcessInfo.hProcess,
  109. address,
  110. buffer,
  111. 4,
  112. &st))
  113. {
  114. printf("read vm4 failed at %p error %ld. \n",
  115. address,
  116. GetLastError());
  117. return 1;
  118. }
  119. if (!ReadProcessMemory (ProcessInfo.hProcess,
  120. address,
  121. buffer,
  122. info.RegionSize,
  123. &st))
  124. {
  125. printf("read vm failed at %p error %ld. \n",
  126. address,
  127. GetLastError());
  128. return 1;
  129. }
  130. }
  131. address += info.RegionSize;
  132. } while (address < SystemRangeStart);
  133. address = NULL;
  134. do {
  135. if (!VirtualQueryEx (ProcessInfo.hProcess,
  136. (PVOID)address,
  137. &info,
  138. sizeof(info)))
  139. {
  140. printf ("virtual query failed %ld.\n",GetLastError());
  141. return 1;
  142. } else {
  143. printf("address: %p size %lx state %lx protect %lx type %lx\n",
  144. address,
  145. info.RegionSize,
  146. info.State,
  147. info.Protect,
  148. info.Type);
  149. }
  150. if ((info.Protect != PAGE_NOACCESS) &&
  151. (info.Protect != 0) &&
  152. (!(info.Protect & PAGE_GUARD)) &&
  153. (info.Protect & PAGE_READWRITE) &&
  154. (info.State != MEM_IMAGE)) {
  155. if (!ReadProcessMemory (ProcessInfo.hProcess,
  156. address,
  157. buffer,
  158. 4,
  159. &st) )
  160. {
  161. printf("read vm5 failed at %p error %ld. \n",
  162. address,
  163. GetLastError());
  164. return 1;
  165. }
  166. if (!WriteProcessMemory (ProcessInfo.hProcess,
  167. address,
  168. buffer,
  169. 4,
  170. &st))
  171. {
  172. printf("write vm4 failed at %p error %ld. \n",
  173. address,
  174. GetLastError());
  175. return 1;
  176. }
  177. if (!ReadProcessMemory (ProcessInfo.hProcess,
  178. address,
  179. buffer,
  180. info.RegionSize,
  181. &st))
  182. {
  183. printf("read 5 vm failed at %p error %ld. \n",
  184. address,
  185. GetLastError());
  186. return 1;
  187. }
  188. if (!WriteProcessMemory (ProcessInfo.hProcess,
  189. address,
  190. buffer,
  191. info.RegionSize,
  192. &st))
  193. {
  194. printf("write vm failed at %p error %ld. \n",
  195. address,
  196. GetLastError());
  197. return 1;
  198. }
  199. }
  200. address += info.RegionSize;
  201. } while (address < SystemRangeStart);
  202. CloseHandle(ProcessInfo.hProcess);
  203. CloseHandle(ProcessInfo.hThread);
  204. }
  205. return 0;
  206. }