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.

599 lines
14 KiB

  1. //
  2. // Template Driver
  3. // Copyright (c) Microsoft Corporation, 1999.
  4. //
  5. // Module: SectMap.c
  6. // Author: Daniel Mihai (DMihai)
  7. // Created: 6/19/1999 2:39pm
  8. //
  9. // This module contains tests for MmMapViewOfSection & MmMapViewInSystemSpace.
  10. //
  11. // --- History ---
  12. //
  13. // 6/19/1999 (DMihai): initial version.
  14. //
  15. #include <ntddk.h>
  16. #include <wchar.h>
  17. #include "active.h"
  18. #include "ContMem.h"
  19. #if !SECTMAP_ACTIVE
  20. void
  21. TdSectionMapTestProcessSpace(
  22. PVOID NotUsed
  23. )
  24. {
  25. DbgPrint ("Buggy: sectmap test is disabled \n");
  26. }
  27. void
  28. TdSectionMapTestSystemSpace(
  29. PVOID NotUsed
  30. )
  31. {
  32. DbgPrint ("Buggy: sectmap test is disabled \n");
  33. }
  34. #else
  35. NTKERNELAPI
  36. NTSTATUS
  37. MmCreateSection (
  38. OUT PVOID *SectionObject,
  39. IN ACCESS_MASK DesiredAccess,
  40. IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
  41. IN PLARGE_INTEGER MaximumSize,
  42. IN ULONG SectionPageProtection,
  43. IN ULONG AllocationAttributes,
  44. IN HANDLE FileHandle OPTIONAL,
  45. IN PFILE_OBJECT File OPTIONAL
  46. );
  47. NTKERNELAPI
  48. NTSTATUS
  49. MmMapViewOfSection(
  50. IN PVOID SectionToMap,
  51. IN PEPROCESS Process,
  52. IN OUT PVOID *CapturedBase,
  53. IN ULONG_PTR ZeroBits,
  54. IN SIZE_T CommitSize,
  55. IN OUT PLARGE_INTEGER SectionOffset,
  56. IN OUT PSIZE_T CapturedViewSize,
  57. IN SECTION_INHERIT InheritDisposition,
  58. IN ULONG AllocationType,
  59. IN ULONG Protect
  60. );
  61. NTKERNELAPI
  62. NTSTATUS
  63. MmUnmapViewOfSection(
  64. IN PEPROCESS Process,
  65. IN PVOID BaseAddress
  66. );
  67. /////////////////////////////////////////////////////////////////////////
  68. //
  69. // macros
  70. //
  71. #define SECTMAP_TEST_FILE_SIZE (4 * 1024 * 1024)
  72. #ifndef SEC_COMMIT
  73. #define SEC_COMMIT 0x8000000
  74. #endif
  75. /////////////////////////////////////////////////////////////////////////
  76. //
  77. // test variations
  78. //
  79. void
  80. TdSectionMapTestProcessSpace(
  81. PVOID NotUsed
  82. )
  83. {
  84. NTSTATUS Status;
  85. ULONG uCrtThreadId;
  86. ULONG uPagesNo;
  87. PULONG puCrtUlong;
  88. PEPROCESS pEProcess;
  89. PVOID pSectionObject;
  90. PVOID pViewBase;
  91. PVOID pAfterLastValidPage;
  92. HANDLE hFile;
  93. SIZE_T sizeView;
  94. LARGE_INTEGER liMaxSize;
  95. LARGE_INTEGER CurrentTime;
  96. LARGE_INTEGER liSectionOffset;
  97. UNICODE_STRING ustrFileName;
  98. OBJECT_ATTRIBUTES ObjAttrib;
  99. IO_STATUS_BLOCK IoStatusBlock;
  100. WCHAR strFileName[ 64 ] = L"\\DosDevices\\c:\\maptest";
  101. WCHAR strThreadId[ 16 ];
  102. uCrtThreadId = PtrToUlong( PsGetCurrentThreadId() );
  103. //
  104. // generate the file name
  105. //
  106. swprintf( strThreadId, L"%u", uCrtThreadId );
  107. wcscat( strFileName, strThreadId );
  108. /*
  109. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, using file %ws\n",
  110. uCrtThreadId,
  111. strFileName );
  112. */
  113. //
  114. // make it a UNICODE_STRING
  115. //
  116. RtlInitUnicodeString(
  117. &ustrFileName,
  118. strFileName
  119. );
  120. InitializeObjectAttributes(
  121. &ObjAttrib,
  122. &ustrFileName,
  123. OBJ_CASE_INSENSITIVE,
  124. 0,
  125. 0
  126. );
  127. //
  128. // open the file
  129. //
  130. liMaxSize.QuadPart = SECTMAP_TEST_FILE_SIZE;
  131. Status = ZwCreateFile(
  132. &hFile,
  133. GENERIC_READ | GENERIC_WRITE,
  134. &ObjAttrib,
  135. &IoStatusBlock,
  136. &liMaxSize,
  137. FILE_ATTRIBUTE_NORMAL,
  138. FILE_SHARE_READ,
  139. FILE_OPEN_IF,
  140. FILE_WRITE_THROUGH |
  141. FILE_NO_INTERMEDIATE_BUFFERING |
  142. FILE_SYNCHRONOUS_IO_NONALERT,
  143. NULL,
  144. 0
  145. );
  146. if( ! NT_SUCCESS( Status ) )
  147. {
  148. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, ZwCreateFile failed %X\n",
  149. uCrtThreadId,
  150. (ULONG)Status );
  151. DbgBreakPoint();
  152. return;
  153. }
  154. /*
  155. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, file opened\n",
  156. uCrtThreadId );
  157. */
  158. ASSERT( IoStatusBlock.Information == FILE_CREATED || IoStatusBlock.Information == FILE_OPENED );
  159. ASSERT( hFile != (HANDLE)-1 );
  160. ASSERT( liMaxSize.QuadPart == SECTMAP_TEST_FILE_SIZE );
  161. //
  162. // create the section
  163. //
  164. Status = MmCreateSection(
  165. &pSectionObject,
  166. STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE,
  167. 0,
  168. &liMaxSize,
  169. PAGE_READWRITE,
  170. SEC_COMMIT,
  171. hFile,
  172. NULL
  173. );
  174. ZwClose(hFile);
  175. if( ! NT_SUCCESS( Status ) )
  176. {
  177. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, MmCreateSection failed %X\n",
  178. uCrtThreadId,
  179. (ULONG)Status );
  180. DbgBreakPoint();
  181. return;
  182. }
  183. /*
  184. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, section %p created\n",
  185. uCrtThreadId,
  186. pSectionObject );
  187. */
  188. //
  189. // map the section
  190. //
  191. sizeView = (SIZE_T)liMaxSize.LowPart;
  192. liSectionOffset.QuadPart = 0;
  193. pEProcess = PsGetCurrentProcess();
  194. /*
  195. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, mapping section %p in process %p\n",
  196. uCrtThreadId,
  197. pSectionObject,
  198. pEProcess );
  199. */
  200. pViewBase = NULL;
  201. Status = MmMapViewOfSection(
  202. pSectionObject,
  203. pEProcess,
  204. &pViewBase,
  205. 0,
  206. 0,
  207. &liSectionOffset,
  208. &sizeView,
  209. ViewUnmap,
  210. 0, // allocation type
  211. PAGE_READWRITE
  212. );
  213. if( ! NT_SUCCESS( Status ) )
  214. {
  215. //
  216. // dereference the section object
  217. //
  218. ObDereferenceObject( pSectionObject );
  219. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, MmMapViewOfSection failed %X\n",
  220. uCrtThreadId,
  221. (ULONG)Status );
  222. DbgBreakPoint();
  223. return;
  224. }
  225. /*
  226. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, section mapped, pViewBase = %p\n",
  227. uCrtThreadId,
  228. pViewBase );
  229. */
  230. // DbgBreakPoint();
  231. ASSERT( liSectionOffset.QuadPart == 0 );
  232. ASSERT( sizeView == SECTMAP_TEST_FILE_SIZE );
  233. ASSERT( pViewBase != NULL );
  234. //
  235. // touch some of the pages
  236. //
  237. uPagesNo = (ULONG)sizeView / PAGE_SIZE;
  238. pAfterLastValidPage = (PVOID)( (ULONG_PTR)pViewBase + uPagesNo * PAGE_SIZE );
  239. KeQuerySystemTime (&CurrentTime);
  240. puCrtUlong = (PULONG)( (ULONG_PTR)pViewBase + (CurrentTime.LowPart % 5) * PAGE_SIZE );
  241. while( (ULONG_PTR)puCrtUlong < (ULONG_PTR)pAfterLastValidPage )
  242. {
  243. /*
  244. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, touching page %p\n",
  245. uCrtThreadId,
  246. puCrtUlong );
  247. */
  248. *puCrtUlong = CurrentTime.LowPart;
  249. KeQuerySystemTime (&CurrentTime);
  250. puCrtUlong = (PULONG)( (ULONG_PTR)puCrtUlong + (CurrentTime.LowPart % 5 + 1) * PAGE_SIZE );
  251. }
  252. //
  253. // clean-up
  254. //
  255. //
  256. // un-map the section
  257. //
  258. /*
  259. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, MmUnmapViewOfSection process %p, pViewBase = %p\n",
  260. uCrtThreadId,
  261. pEProcess,
  262. pViewBase );
  263. */
  264. Status = MmUnmapViewOfSection(
  265. pEProcess,
  266. pViewBase );
  267. if( ! NT_SUCCESS( Status ) )
  268. {
  269. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, MmUnmapViewOfSection failed %X\n",
  270. uCrtThreadId,
  271. (ULONG)Status );
  272. DbgBreakPoint();
  273. }
  274. //
  275. // dereference the section object
  276. //
  277. /*
  278. DbgPrint( "buggy: TdSectionMapTestProcessSpace: thread %u, dereference section at %p\n",
  279. uCrtThreadId,
  280. pSectionObject );
  281. */
  282. ObDereferenceObject( pSectionObject );
  283. }
  284. /////////////////////////////////////////////////////////////////////////
  285. void
  286. TdSectionMapTestSystemSpace(
  287. PVOID NotUsed
  288. )
  289. {
  290. NTSTATUS Status;
  291. ULONG uCrtThreadId;
  292. ULONG uPagesNo;
  293. PULONG puCrtUlong;
  294. PVOID pSectionObject;
  295. PVOID pViewBase;
  296. PVOID pAfterLastValidPage;
  297. HANDLE hFile;
  298. SIZE_T sizeView;
  299. LARGE_INTEGER liMaxSize;
  300. LARGE_INTEGER CurrentTime;
  301. LARGE_INTEGER liSectionOffset;
  302. UNICODE_STRING ustrFileName;
  303. OBJECT_ATTRIBUTES ObjAttrib;
  304. IO_STATUS_BLOCK IoStatusBlock;
  305. WCHAR strFileName[ 64 ] = L"\\DosDevices\\c:\\maptest";
  306. WCHAR strThreadId[ 16 ];
  307. uCrtThreadId = PtrToUlong( PsGetCurrentThreadId() );
  308. //
  309. // generate the file name
  310. //
  311. swprintf( strThreadId, L"%u", uCrtThreadId );
  312. wcscat( strFileName, strThreadId );
  313. /*
  314. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, using file %ws\n",
  315. uCrtThreadId,
  316. strFileName );
  317. */
  318. //
  319. // make it a UNICODE_STRING
  320. //
  321. RtlInitUnicodeString(
  322. &ustrFileName,
  323. strFileName
  324. );
  325. InitializeObjectAttributes(
  326. &ObjAttrib,
  327. &ustrFileName,
  328. OBJ_CASE_INSENSITIVE,
  329. 0,
  330. 0
  331. );
  332. //
  333. // open the file
  334. //
  335. liMaxSize.QuadPart = SECTMAP_TEST_FILE_SIZE;
  336. Status = ZwCreateFile(
  337. &hFile,
  338. GENERIC_READ | GENERIC_WRITE,
  339. &ObjAttrib,
  340. &IoStatusBlock,
  341. &liMaxSize,
  342. FILE_ATTRIBUTE_NORMAL,
  343. FILE_SHARE_READ,
  344. FILE_OPEN_IF,
  345. FILE_WRITE_THROUGH |
  346. FILE_NO_INTERMEDIATE_BUFFERING |
  347. FILE_SYNCHRONOUS_IO_NONALERT,
  348. NULL,
  349. 0
  350. );
  351. if( ! NT_SUCCESS( Status ) )
  352. {
  353. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, ZwCreateFile failed %X\n",
  354. uCrtThreadId,
  355. (ULONG)Status );
  356. DbgBreakPoint();
  357. return;
  358. }
  359. /*
  360. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, file opened\n",
  361. uCrtThreadId );
  362. */
  363. ASSERT( IoStatusBlock.Information == FILE_CREATED || IoStatusBlock.Information == FILE_OPENED );
  364. ASSERT( hFile != (HANDLE)-1 );
  365. ASSERT( liMaxSize.QuadPart == SECTMAP_TEST_FILE_SIZE );
  366. //
  367. // create the section
  368. //
  369. Status = MmCreateSection(
  370. &pSectionObject,
  371. STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE,
  372. 0,
  373. &liMaxSize,
  374. PAGE_READWRITE,
  375. SEC_COMMIT,
  376. hFile,
  377. NULL
  378. );
  379. ZwClose(hFile);
  380. if( ! NT_SUCCESS( Status ) )
  381. {
  382. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, MmCreateSection failed %X\n",
  383. uCrtThreadId,
  384. (ULONG)Status );
  385. DbgBreakPoint();
  386. return;
  387. }
  388. /*
  389. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, section %p created\n",
  390. uCrtThreadId,
  391. pSectionObject );
  392. */
  393. //
  394. // map the section
  395. //
  396. sizeView = (SIZE_T)liMaxSize.LowPart;
  397. liSectionOffset.QuadPart = 0;
  398. /*
  399. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, mapping section %p system space\n",
  400. uCrtThreadId,
  401. pSectionObject );
  402. */
  403. pViewBase = NULL;
  404. Status = MmMapViewInSystemSpace(
  405. pSectionObject,
  406. &pViewBase,
  407. &sizeView
  408. );
  409. if( ! NT_SUCCESS( Status ) )
  410. {
  411. //
  412. // dereference the section object
  413. //
  414. ObDereferenceObject( pSectionObject );
  415. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, MmMapViewInSystemSpace failed %X\n",
  416. uCrtThreadId,
  417. (ULONG)Status );
  418. DbgBreakPoint();
  419. return;
  420. }
  421. /*
  422. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, section mapped, pViewBase = %p\n",
  423. uCrtThreadId,
  424. pViewBase );
  425. */
  426. // DbgBreakPoint();
  427. ASSERT( liSectionOffset.QuadPart == 0 );
  428. ASSERT( sizeView == SECTMAP_TEST_FILE_SIZE );
  429. ASSERT( pViewBase != NULL );
  430. //
  431. // touch some of the pages
  432. //
  433. uPagesNo = (ULONG)sizeView / PAGE_SIZE;
  434. pAfterLastValidPage = (PVOID)( (ULONG_PTR)pViewBase + uPagesNo * PAGE_SIZE );
  435. KeQuerySystemTime (&CurrentTime);
  436. puCrtUlong = (PULONG)( (ULONG_PTR)pViewBase + (CurrentTime.LowPart % 5) * PAGE_SIZE );
  437. while( (ULONG_PTR)puCrtUlong < (ULONG_PTR)pAfterLastValidPage )
  438. {
  439. /*
  440. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, touching page %p\n",
  441. uCrtThreadId,
  442. puCrtUlong );
  443. */
  444. *puCrtUlong = CurrentTime.LowPart;
  445. KeQuerySystemTime (&CurrentTime);
  446. puCrtUlong = (PULONG)( (ULONG_PTR)puCrtUlong + (CurrentTime.LowPart % 5 + 1) * PAGE_SIZE );
  447. }
  448. //
  449. // clean-up
  450. //
  451. //
  452. // un-map the section
  453. //
  454. /*
  455. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, MmUnmapViewInSystemSpace pViewBase = %p\n",
  456. uCrtThreadId,
  457. pViewBase );
  458. */
  459. Status = MmUnmapViewInSystemSpace(
  460. pViewBase );
  461. if( ! NT_SUCCESS( Status ) )
  462. {
  463. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, MmUnmapViewInSystemSpace failed %X\n",
  464. uCrtThreadId,
  465. (ULONG)Status );
  466. DbgBreakPoint();
  467. }
  468. //
  469. // dereference the section object
  470. //
  471. /*
  472. DbgPrint( "buggy: TdSectionMapTestSystemSpace: thread %u, dereference section at %p\n",
  473. uCrtThreadId,
  474. pSectionObject );
  475. */
  476. ObDereferenceObject( pSectionObject );
  477. }
  478. #endif // #if !SECTMAP_ACTIVE