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.

1292 lines
27 KiB

  1. //
  2. // Template Driver
  3. // Copyright (c) Microsoft Corporation, 2000.
  4. //
  5. // Module: MapView.c
  6. // Author: Daniel Mihai (DMihai)
  7. // Created: 4/6/2000
  8. //
  9. // This module contains tests for MmMapViewInSystemSpace & MmMapViewInSessionSpace.
  10. // Note that SystemViewSize & SessionViewSize are configurable using the registry.
  11. //
  12. // SessionPoolTest is exercising the paged pool allocated with SESSION_POOL_MASK.
  13. // The size of this pool can be also configured using the SsessionViewSize registry
  14. // value.
  15. //
  16. // --- History ---
  17. //
  18. // 4/6/2000 (DMihai): initial version.
  19. //
  20. #include <ntddk.h>
  21. #include "active.h"
  22. #include "MapView.h"
  23. #include "tdriver.h"
  24. #if !MAPVIEW_ACTIVE
  25. //
  26. // Dummy implementation if the module is inactive
  27. //
  28. VOID MmMapViewInSystemSpaceLargest (
  29. PVOID NotUsed
  30. )
  31. {
  32. DbgPrint ("Buggy: MapView module is disabled from active.h\n");
  33. }
  34. VOID MmMapViewInSystemSpaceTotal (
  35. PVOID NotUsed
  36. )
  37. {
  38. DbgPrint ("Buggy: MapView module is disabled from active.h\n");
  39. }
  40. VOID MmMapViewInSessionSpaceLargest (
  41. PVOID NotUsed
  42. )
  43. {
  44. DbgPrint ("Buggy: MapView module is disabled from active.h\n");
  45. }
  46. VOID MmMapViewInSessionSpaceTotal (
  47. PVOID NotUsed
  48. )
  49. {
  50. DbgPrint ("Buggy: MapView module is disabled from active.h\n");
  51. }
  52. VOID SessionPoolTest (
  53. PVOID NotUsed
  54. )
  55. {
  56. DbgPrint ("Buggy: MapView module is disabled from active.h\n");
  57. }
  58. #else
  59. const LARGE_INTEGER BuggyFiveSeconds = {(ULONG)(-5 * 1000 * 1000 * 10), -1};
  60. //
  61. // Real implementation if the module is active
  62. //
  63. #ifndef SESSION_POOL_MASK
  64. #define SESSION_POOL_MASK 32
  65. #endif //#ifndef SESSION_POOL_MASK
  66. #ifndef SEC_COMMIT
  67. #define SEC_COMMIT 0x8000000
  68. #endif //#ifndef SEC_COMMIT
  69. #ifndef ZwDeleteFile
  70. NTSYSCALLAPI
  71. NTSTATUS
  72. NTAPI
  73. ZwDeleteFile(
  74. IN POBJECT_ATTRIBUTES ObjectAttributes
  75. ); //#ifndef ZwDeleteFile
  76. #endif
  77. #ifndef MmCreateSection
  78. NTKERNELAPI
  79. NTSTATUS
  80. MmCreateSection (
  81. OUT PVOID *SectionObject,
  82. IN ACCESS_MASK DesiredAccess,
  83. IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
  84. IN PLARGE_INTEGER MaximumSize,
  85. IN ULONG SectionPageProtection,
  86. IN ULONG AllocationAttributes,
  87. IN HANDLE FileHandle OPTIONAL,
  88. IN PFILE_OBJECT File OPTIONAL
  89. );
  90. NTKERNELAPI
  91. NTSTATUS
  92. MmMapViewInSystemSpace (
  93. IN PVOID Section,
  94. OUT PVOID *MappedBase,
  95. IN PSIZE_T ViewSize
  96. );
  97. NTKERNELAPI
  98. NTSTATUS
  99. MmUnmapViewInSystemSpace (
  100. IN PVOID MappedBase
  101. );
  102. // begin_ntosp
  103. NTKERNELAPI
  104. NTSTATUS
  105. MmMapViewInSessionSpace (
  106. IN PVOID Section,
  107. OUT PVOID *MappedBase,
  108. IN OUT PSIZE_T ViewSize
  109. );
  110. NTKERNELAPI
  111. NTSTATUS
  112. MmUnmapViewInSessionSpace (
  113. IN PVOID MappedBase
  114. );
  115. #endif //#ifndef MmCreateSection
  116. #define BUGGY_TEMPORARY_FILE1 L"\\SystemRoot\\Buggy1.tmp"
  117. #define BUGGY_TEMPORARY_FILE2 L"\\SystemRoot\\Buggy2.tmp"
  118. #define BUGGY_TEMPORARY_FILE3 L"\\SystemRoot\\Buggy3.tmp"
  119. #define BUGGY_TEMPORARY_FILE4 L"\\SystemRoot\\Buggy4.tmp"
  120. #define BUGGY_MAX_SECTIONS_TO_MAP ( 8 * 1024 )
  121. //
  122. // global array for mapped sections
  123. //
  124. /////////////////////////////////////////////////////////////////////////////
  125. //
  126. // verify a mapping
  127. //
  128. VOID VerifyMapping( PVOID pBase, SIZE_T uSize )
  129. {
  130. SIZE_T *puCrtPage;
  131. SIZE_T uCrtPageIndex;
  132. SIZE_T uPages;
  133. if( uSize > 100 * 1024 * 1024 )
  134. {
  135. DbgPrint ( "Buggy: VerifyMapping: don't try to touch all the %p size to avoid deadlock\n",
  136. uSize );
  137. return;
  138. }
  139. /*
  140. DbgPrint ( "\nBuggy: Verifying mapping at address %p, size %p...\n",
  141. pBase,
  142. (PVOID) uSize );
  143. */
  144. uPages = uSize / PAGE_SIZE;
  145. puCrtPage = (SIZE_T *)pBase;
  146. for( uCrtPageIndex = 0; uCrtPageIndex < uPages; uCrtPageIndex++ )
  147. {
  148. *puCrtPage = uCrtPageIndex;
  149. puCrtPage = (SIZE_T *) ( ( (CHAR*) puCrtPage ) + PAGE_SIZE );
  150. }
  151. while( uCrtPageIndex > 0 )
  152. {
  153. uCrtPageIndex --;
  154. puCrtPage = (SIZE_T *) ( ( (CHAR*) puCrtPage ) - PAGE_SIZE );
  155. if( *puCrtPage != uCrtPageIndex )
  156. {
  157. DbgPrint ( "\nBuggy: Wrong mapping at address %p\n",
  158. puCrtPage );
  159. DbgBreakPoint();
  160. }
  161. }
  162. //DbgPrint ( "done\n" );
  163. }
  164. /////////////////////////////////////////////////////////////////////////////
  165. VOID MmMapViewInSystemSpaceLargest (
  166. PVOID NotUsed
  167. )
  168. {
  169. NTSTATUS Status;
  170. HANDLE hFile = NULL;
  171. SIZE_T SizeToMap;
  172. SIZE_T SizeToGrow;
  173. PVOID pMappedBase = NULL;
  174. PVOID pSection = NULL;
  175. LARGE_INTEGER MaxSectionSize;
  176. OBJECT_ATTRIBUTES ObjectAttributes;
  177. UNICODE_STRING FileName;
  178. IO_STATUS_BLOCK IoStatusBlock;
  179. //
  180. // Create BUGGY_TEMPORARY_FILE1
  181. //
  182. RtlInitUnicodeString(
  183. &FileName,
  184. BUGGY_TEMPORARY_FILE1
  185. );
  186. InitializeObjectAttributes(
  187. &ObjectAttributes,
  188. &FileName,
  189. OBJ_CASE_INSENSITIVE ,
  190. NULL,
  191. NULL
  192. );
  193. Status = ZwCreateFile(
  194. &hFile,
  195. GENERIC_READ | GENERIC_WRITE,
  196. &ObjectAttributes,
  197. &IoStatusBlock,
  198. NULL,
  199. FILE_ATTRIBUTE_NORMAL,
  200. 0,
  201. FILE_OVERWRITE_IF,
  202. FILE_NON_DIRECTORY_FILE,
  203. NULL,
  204. 0 );
  205. /*
  206. Status = ZwCreateFile(
  207. &hFile,
  208. GENERIC_READ | GENERIC_WRITE,
  209. &ObjectAttributes,
  210. &IoStatusBlock,
  211. NULL,
  212. FILE_ATTRIBUTE_NORMAL,
  213. FILE_SHARE_READ,
  214. FILE_OPEN_IF,
  215. FILE_WRITE_THROUGH |
  216. FILE_NO_INTERMEDIATE_BUFFERING |
  217. FILE_SYNCHRONOUS_IO_NONALERT,
  218. NULL,
  219. 0
  220. );
  221. */
  222. if( ! NT_SUCCESS( Status ) )
  223. {
  224. DbgPrint ("Buggy: ZwCreateFile failed - status %X\n",
  225. Status );
  226. goto cleanup;
  227. }
  228. else
  229. {
  230. DbgPrint ( "Buggy: created file, handle %p\n",
  231. hFile );
  232. }
  233. //
  234. // Create a section for the temp file
  235. //
  236. #ifdef _WIN64
  237. MaxSectionSize.QuadPart = (LONGLONG)0x40000000 * PAGE_SIZE;
  238. #else
  239. MaxSectionSize.QuadPart = 0xFFFFFFFF;
  240. #endif //#ifdef _WIN64
  241. do
  242. {
  243. Status = MmCreateSection(
  244. &pSection,
  245. STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE,
  246. NULL,
  247. &MaxSectionSize,
  248. PAGE_READWRITE,
  249. SEC_COMMIT,
  250. hFile,
  251. NULL );
  252. if( ! NT_SUCCESS( Status ) )
  253. {
  254. if( Status == STATUS_DISK_FULL )
  255. {
  256. MaxSectionSize.QuadPart /= 2;
  257. DbgPrint ("Buggy: MmCreateSection returned STATUS_DISK_FULL, re-trying with max section size = %I64X\n",
  258. MaxSectionSize.QuadPart );
  259. }
  260. else
  261. {
  262. DbgPrint ("Buggy: MmCreateSection failed - status %X\n",
  263. Status );
  264. goto cleanup;
  265. }
  266. }
  267. else
  268. {
  269. DbgPrint ( "Buggy: created section with max size %I64X\n",
  270. MaxSectionSize.QuadPart );
  271. break;
  272. }
  273. } while( MaxSectionSize.QuadPart > PAGE_SIZE );
  274. DbgPrint ( "Buggy: Using section at %p\n",
  275. pSection );
  276. //
  277. // try to map the max size section
  278. //
  279. SizeToMap = (SIZE_T) MaxSectionSize.QuadPart;
  280. while( SizeToMap > PAGE_SIZE )
  281. {
  282. Status = MmMapViewInSystemSpace(
  283. pSection,
  284. &pMappedBase,
  285. &SizeToMap );
  286. if( ! NT_SUCCESS( Status ) )
  287. {
  288. DbgPrint ( "Buggy: MmMapViewInSystemSpace failed for size %p, status %X\n",
  289. SizeToMap,
  290. Status );
  291. SizeToMap /= 2;
  292. }
  293. else
  294. {
  295. DbgPrint ( "\n\nFirst result of the test:\n\n" );
  296. DbgPrint ( "Buggy: MmMapViewInSystemSpace succeeded for size %p, mapped base %p\n",
  297. SizeToMap,
  298. pMappedBase );
  299. //DbgPrint ( "\n\n" );
  300. VerifyMapping( pMappedBase, SizeToMap );
  301. break;
  302. }
  303. }
  304. //
  305. // try to grow the size
  306. //
  307. while( pMappedBase != NULL )
  308. {
  309. //
  310. // unmap the old one
  311. //
  312. Status = MmUnmapViewInSystemSpace(
  313. pMappedBase );
  314. if( ! NT_SUCCESS( Status ) )
  315. {
  316. DbgPrint ( "Buggy: MmUnmapViewInSystemSpace failed, status %X\n",
  317. Status );
  318. DbgBreakPoint();
  319. break;
  320. }
  321. pMappedBase = NULL;
  322. //
  323. // grow the size
  324. //
  325. SizeToGrow = SizeToMap / 10;
  326. if( SizeToGrow < 10 * PAGE_SIZE )
  327. {
  328. //
  329. // don't bother
  330. //
  331. break;
  332. }
  333. SizeToMap += SizeToGrow;
  334. //
  335. // try to use this bigger size
  336. //
  337. Status = MmMapViewInSystemSpace(
  338. pSection,
  339. &pMappedBase,
  340. &SizeToMap );
  341. if( ! NT_SUCCESS( Status ) )
  342. {
  343. /*
  344. DbgPrint ( "Buggy: MmMapViewInSystemSpace failed for size %p, status %X\n",
  345. SizeToMap,
  346. Status );
  347. */
  348. //
  349. // can't grow the size anymore
  350. //
  351. break;
  352. }
  353. else
  354. {
  355. DbgPrint ( "\n\nBetter result of the test:\n\n" );
  356. DbgPrint ( "Buggy: MmMapViewInSystemSpace succeeded for size %p, mapped base %p\n",
  357. SizeToMap,
  358. pMappedBase );
  359. //DbgPrint ( "\n\n" );
  360. VerifyMapping( pMappedBase, SizeToMap );
  361. }
  362. }
  363. //
  364. // clean-up
  365. //
  366. cleanup:
  367. if( pMappedBase != NULL )
  368. {
  369. Status = MmUnmapViewInSystemSpace(
  370. pMappedBase );
  371. if( ! NT_SUCCESS( Status ) )
  372. {
  373. DbgPrint ( "Buggy: MmUnmapViewInSystemSpace failed, status %X\n",
  374. Status );
  375. DbgBreakPoint();
  376. }
  377. }
  378. if( pSection != NULL )
  379. {
  380. ObDereferenceObject(
  381. pSection );
  382. }
  383. if( hFile != NULL )
  384. {
  385. ZwClose( hFile );
  386. Status = ZwDeleteFile(
  387. &ObjectAttributes );
  388. if( ! NT_SUCCESS( Status ) )
  389. {
  390. DbgPrint ("Buggy: ZwDeleteFile failed - status %X\n",
  391. Status );
  392. }
  393. else
  394. {
  395. // DbgPrint ("Buggy: temporary file deleted\n" );
  396. }
  397. }
  398. }
  399. ///////////////////////////////////////////////////////////////////////////
  400. PVOID *MappedSections;
  401. VOID MmMapViewInSystemSpaceTotal (
  402. PVOID NotUsed
  403. )
  404. {
  405. NTSTATUS Status;
  406. HANDLE hFile = NULL;
  407. SIZE_T SizeToMap;
  408. PVOID pSection = NULL;
  409. SIZE_T CrtMap;
  410. LARGE_INTEGER MaxSectionSize;
  411. OBJECT_ATTRIBUTES ObjectAttributes;
  412. UNICODE_STRING FileName;
  413. IO_STATUS_BLOCK IoStatusBlock;
  414. MappedSections = ExAllocatePoolWithTag(
  415. NonPagedPool,
  416. BUGGY_MAX_SECTIONS_TO_MAP * sizeof( PVOID ),
  417. TD_POOL_TAG );
  418. if( MappedSections == NULL )
  419. {
  420. DbgPrint ("Buggy: ExAllocatePoolWithTag failed - bail\n" );
  421. return;
  422. }
  423. RtlZeroMemory( MappedSections, BUGGY_MAX_SECTIONS_TO_MAP * sizeof( PVOID ) );
  424. //
  425. // Create BUGGY_TEMPORARY_FILE2
  426. //
  427. RtlInitUnicodeString(
  428. &FileName,
  429. BUGGY_TEMPORARY_FILE2
  430. );
  431. InitializeObjectAttributes(
  432. &ObjectAttributes,
  433. &FileName,
  434. OBJ_CASE_INSENSITIVE,
  435. NULL,
  436. NULL
  437. );
  438. Status = ZwCreateFile(
  439. &hFile,
  440. GENERIC_READ | GENERIC_WRITE,
  441. &ObjectAttributes,
  442. &IoStatusBlock,
  443. NULL,
  444. FILE_ATTRIBUTE_NORMAL,
  445. 0,
  446. FILE_OVERWRITE_IF,
  447. FILE_NON_DIRECTORY_FILE,
  448. NULL,
  449. 0 );
  450. if( ! NT_SUCCESS( Status ) )
  451. {
  452. /*
  453. DbgPrint ("Buggy: ZwCreateFile failed - status %X\n",
  454. Status );
  455. */
  456. goto cleanup;
  457. }
  458. else
  459. {
  460. //DbgPrint ( "Buggy: created file\n" );
  461. }
  462. //
  463. // Create a section for the temp file
  464. //
  465. MaxSectionSize.QuadPart = 1024 * 1024;
  466. do
  467. {
  468. Status = MmCreateSection(
  469. &pSection,
  470. STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE,
  471. NULL,
  472. &MaxSectionSize,
  473. PAGE_READWRITE,
  474. SEC_COMMIT,
  475. hFile,
  476. NULL );
  477. if( ! NT_SUCCESS( Status ) )
  478. {
  479. if( Status == STATUS_DISK_FULL )
  480. {
  481. MaxSectionSize.QuadPart /= 2;
  482. /*
  483. DbgPrint ("Buggy: MmCreateSection returned STATUS_DISK_FULL, re-trying with max section size = %I64X\n",
  484. MaxSectionSize.QuadPart );
  485. */
  486. }
  487. else
  488. {
  489. DbgPrint ("Buggy: MmCreateSection failed - status %X\n",
  490. Status );
  491. goto cleanup;
  492. }
  493. }
  494. else
  495. {
  496. /*
  497. DbgPrint ( "Buggy: created section with max size %I64X\n",
  498. MaxSectionSize.QuadPart );
  499. */
  500. break;
  501. }
  502. } while( MaxSectionSize.QuadPart > PAGE_SIZE );
  503. //
  504. // get a pointer to the section
  505. //
  506. DbgPrint ( "Buggy: Using section at %p\n",
  507. pSection );
  508. //
  509. // try to map the max size section
  510. //
  511. SizeToMap = (SIZE_T) MaxSectionSize.QuadPart;
  512. RtlZeroMemory( MappedSections, BUGGY_MAX_SECTIONS_TO_MAP * sizeof( PVOID ) );
  513. for( CrtMap = 0; CrtMap < BUGGY_MAX_SECTIONS_TO_MAP; CrtMap++ )
  514. {
  515. Status = MmMapViewInSystemSpace(
  516. pSection,
  517. &MappedSections[ CrtMap ],
  518. &SizeToMap );
  519. if( ! NT_SUCCESS( Status ) )
  520. {
  521. /*
  522. DbgPrint ( "Buggy: MmMapViewInSystemSpace failed for size %p, status %X, chunk %p\n",
  523. SizeToMap,
  524. Status
  525. );
  526. */
  527. break;
  528. }
  529. else
  530. {
  531. if( CrtMap <= 100 )
  532. {
  533. VerifyMapping( MappedSections[ CrtMap ], SizeToMap );
  534. }
  535. }
  536. }
  537. DbgPrint ( "\n\nBuggy: Result of the test:\n\n" );
  538. DbgPrint ( "Buggy: mapped %u sections with size %p, total %p\n",
  539. CrtMap,
  540. SizeToMap,
  541. SizeToMap * (SIZE_T)CrtMap );
  542. //DbgPrint ( "\n\n" );
  543. //
  544. // clean-up
  545. //
  546. cleanup:
  547. for( CrtMap = 0; CrtMap < BUGGY_MAX_SECTIONS_TO_MAP; CrtMap++ )
  548. {
  549. if( MappedSections[ CrtMap ] == NULL )
  550. {
  551. break;
  552. }
  553. Status = MmUnmapViewInSystemSpace(
  554. MappedSections[ CrtMap ] );
  555. if( ! NT_SUCCESS( Status ) )
  556. {
  557. DbgPrint ( "Buggy: MmUnmapViewInSystemSpace failed for %p, status %X\n",
  558. MappedSections[ CrtMap ],
  559. Status );
  560. DbgBreakPoint();
  561. }
  562. }
  563. DbgPrint ( "Buggy: unmapped %p sections\n",
  564. CrtMap );
  565. if( pSection != NULL )
  566. {
  567. ObDereferenceObject(
  568. pSection );
  569. DbgPrint ( "Buggy: dereferenced section at %p\n",
  570. pSection );
  571. }
  572. if( hFile != NULL )
  573. {
  574. ZwClose( hFile );
  575. DbgPrint ( "Buggy: calling ZwDeleteFile\n" );
  576. Status = ZwDeleteFile(
  577. &ObjectAttributes );
  578. if( ! NT_SUCCESS( Status ) )
  579. {
  580. DbgPrint ("Buggy: ZwDeleteFile failed - status %X\n",
  581. Status );
  582. }
  583. else
  584. {
  585. DbgPrint ("Buggy: temporary file deleted\n" );
  586. }
  587. }
  588. ExFreePool( MappedSections );
  589. }
  590. /////////////////////////////////////////////////////////////////////////////
  591. VOID MmMapViewInSessionSpaceLargest (
  592. PVOID NotUsed
  593. )
  594. {
  595. NTSTATUS Status;
  596. HANDLE hFile = NULL;
  597. SIZE_T SizeToMap;
  598. SIZE_T SizeToGrow;
  599. PVOID pMappedBase = NULL;
  600. PVOID pSection = NULL;
  601. LARGE_INTEGER MaxSectionSize;
  602. OBJECT_ATTRIBUTES ObjectAttributes;
  603. UNICODE_STRING FileName;
  604. IO_STATUS_BLOCK IoStatusBlock;
  605. // Create BUGGY_TEMPORARY_FILE3
  606. //
  607. RtlInitUnicodeString(
  608. &FileName,
  609. BUGGY_TEMPORARY_FILE3
  610. );
  611. InitializeObjectAttributes(
  612. &ObjectAttributes,
  613. &FileName,
  614. OBJ_CASE_INSENSITIVE,
  615. NULL,
  616. NULL
  617. );
  618. Status = ZwCreateFile(
  619. &hFile,
  620. GENERIC_READ | GENERIC_WRITE,
  621. &ObjectAttributes,
  622. &IoStatusBlock,
  623. NULL,
  624. FILE_ATTRIBUTE_NORMAL,
  625. 0,
  626. FILE_OVERWRITE_IF,
  627. FILE_NON_DIRECTORY_FILE,
  628. NULL,
  629. 0 );
  630. if( ! NT_SUCCESS( Status ) )
  631. {
  632. /*
  633. DbgPrint ("Buggy: ZwCreateFile failed - status %X\n",
  634. Status );
  635. */
  636. goto cleanup;
  637. }
  638. else
  639. {
  640. //DbgPrint ( "Buggy: created file\n" );
  641. }
  642. //
  643. // Create a section for the temp file
  644. //
  645. #ifdef _WIN64
  646. MaxSectionSize.QuadPart = (LONGLONG)0x40000000 * PAGE_SIZE;
  647. #else
  648. MaxSectionSize.QuadPart = 0xFFFFFFFF;
  649. #endif //#ifdef _WIN64
  650. do
  651. {
  652. Status = MmCreateSection(
  653. &pSection,
  654. STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE,
  655. NULL,
  656. &MaxSectionSize,
  657. PAGE_READWRITE,
  658. SEC_COMMIT,
  659. hFile,
  660. NULL );
  661. if( ! NT_SUCCESS( Status ) )
  662. {
  663. if( Status == STATUS_DISK_FULL )
  664. {
  665. MaxSectionSize.QuadPart /= 2;
  666. DbgPrint ("Buggy: MmCreateSection returned STATUS_DISK_FULL, re-trying with max section size = %I64X\n",
  667. MaxSectionSize.QuadPart );
  668. }
  669. else
  670. {
  671. DbgPrint ("Buggy: MmCreateSection failed - status %X\n",
  672. Status );
  673. goto cleanup;
  674. }
  675. }
  676. else
  677. {
  678. DbgPrint ( "Buggy: created section with max size %I64X\n",
  679. MaxSectionSize.QuadPart );
  680. break;
  681. }
  682. } while( MaxSectionSize.QuadPart > PAGE_SIZE );
  683. DbgPrint ( "Buggy: Using section at %p\n",
  684. pSection );
  685. //
  686. // try to map the max size section
  687. //
  688. SizeToMap = (SIZE_T) MaxSectionSize.QuadPart;
  689. while( SizeToMap > PAGE_SIZE )
  690. {
  691. Status = MmMapViewInSessionSpace(
  692. pSection,
  693. &pMappedBase,
  694. &SizeToMap );
  695. if( ! NT_SUCCESS( Status ) )
  696. {
  697. DbgPrint ( "Buggy: MmMapViewInSessionSpace failed for size %p, status %X\n",
  698. SizeToMap,
  699. Status );
  700. SizeToMap /= 2;
  701. }
  702. else
  703. {
  704. DbgPrint ( "\n\nFirst result of the test:\n\n" );
  705. DbgPrint ( "Buggy: MmMapViewInSessionSpace succeeded for size %p, mapped base %p\n",
  706. SizeToMap,
  707. pMappedBase );
  708. //DbgPrint ( "\n\n" );
  709. VerifyMapping( pMappedBase, SizeToMap );
  710. break;
  711. }
  712. }
  713. //
  714. // try to grow the size
  715. //
  716. while( pMappedBase != NULL )
  717. {
  718. //
  719. // unmap the old one
  720. //
  721. Status = MmUnmapViewInSessionSpace(
  722. pMappedBase );
  723. if( ! NT_SUCCESS( Status ) )
  724. {
  725. DbgPrint ( "Buggy: MmUnmapViewInSessionSpace failed, status %X\n",
  726. Status );
  727. DbgBreakPoint();
  728. break;
  729. }
  730. pMappedBase = NULL;
  731. //
  732. // grow the size
  733. //
  734. SizeToGrow = SizeToMap / 10;
  735. if( SizeToGrow < 10 * PAGE_SIZE )
  736. {
  737. //
  738. // don't bother
  739. //
  740. break;
  741. }
  742. SizeToMap += SizeToGrow;
  743. //
  744. // try to use this bigger size
  745. //
  746. Status = MmMapViewInSessionSpace(
  747. pSection,
  748. &pMappedBase,
  749. &SizeToMap );
  750. if( ! NT_SUCCESS( Status ) )
  751. {
  752. DbgPrint ( "Buggy: MmMapViewInSessionSpace failed for size %p, status %X\n",
  753. SizeToMap,
  754. Status );
  755. //
  756. // can't grow the size anymore
  757. //
  758. break;
  759. }
  760. else
  761. {
  762. DbgPrint ( "\n\nBetter result of the test:\n\n" );
  763. DbgPrint ( "Buggy: MmMapViewInSessionSpace succeeded for size %p, mapped base %p\n",
  764. SizeToMap,
  765. pMappedBase );
  766. //DbgPrint ( "\n\n" );
  767. VerifyMapping( pMappedBase, SizeToMap );
  768. }
  769. }
  770. //
  771. // clean-up
  772. //
  773. cleanup:
  774. if( pMappedBase != NULL )
  775. {
  776. Status = MmUnmapViewInSessionSpace(
  777. pMappedBase );
  778. if( ! NT_SUCCESS( Status ) )
  779. {
  780. DbgPrint ( "Buggy: MmUnmapViewInSessionSpace failed, status %X\n",
  781. Status );
  782. DbgBreakPoint();
  783. }
  784. }
  785. if( pSection != NULL )
  786. {
  787. ObDereferenceObject(
  788. pSection );
  789. }
  790. if( hFile != NULL )
  791. {
  792. ZwClose( hFile );
  793. Status = ZwDeleteFile(
  794. &ObjectAttributes );
  795. if( ! NT_SUCCESS( Status ) )
  796. {
  797. DbgPrint ("Buggy: ZwDeleteFile failed - status %X\n",
  798. Status );
  799. }
  800. else
  801. {
  802. //DbgPrint ("Buggy: temporary file deleted\n" );
  803. }
  804. }
  805. }
  806. ///////////////////////////////////////////////////////////////////////////
  807. VOID MmMapViewInSessionSpaceTotal (
  808. PVOID NotUsed
  809. )
  810. {
  811. NTSTATUS Status;
  812. HANDLE hFile = NULL;
  813. SIZE_T SizeToMap;
  814. PVOID pSection = NULL;
  815. SIZE_T CrtMap;
  816. LARGE_INTEGER MaxSectionSize;
  817. OBJECT_ATTRIBUTES ObjectAttributes;
  818. UNICODE_STRING FileName;
  819. IO_STATUS_BLOCK IoStatusBlock;
  820. PVOID *MappedSections;
  821. MappedSections = ExAllocatePoolWithTag(
  822. NonPagedPool,
  823. BUGGY_MAX_SECTIONS_TO_MAP * sizeof( PVOID ),
  824. TD_POOL_TAG );
  825. if( MappedSections == NULL )
  826. {
  827. DbgPrint ("Buggy: ExAllocatePoolWithTag failed - bail\n" );
  828. return;
  829. }
  830. RtlZeroMemory( MappedSections, BUGGY_MAX_SECTIONS_TO_MAP * sizeof( PVOID ) );
  831. //
  832. // Create BUGGY_TEMPORARY_FILE3
  833. //
  834. RtlInitUnicodeString(
  835. &FileName,
  836. BUGGY_TEMPORARY_FILE3
  837. );
  838. InitializeObjectAttributes(
  839. &ObjectAttributes,
  840. &FileName,
  841. OBJ_CASE_INSENSITIVE,
  842. NULL,
  843. NULL
  844. );
  845. Status = ZwCreateFile(
  846. &hFile,
  847. GENERIC_READ | GENERIC_WRITE,
  848. &ObjectAttributes,
  849. &IoStatusBlock,
  850. NULL,
  851. FILE_ATTRIBUTE_NORMAL,
  852. 0,
  853. FILE_OVERWRITE_IF,
  854. FILE_NON_DIRECTORY_FILE,
  855. NULL,
  856. 0 );
  857. if( ! NT_SUCCESS( Status ) )
  858. {
  859. DbgPrint ("Buggy: ZwCreateFile failed - status %X\n",
  860. Status );
  861. goto cleanup;
  862. }
  863. else
  864. {
  865. //DbgPrint ( "Buggy: created file\n" );
  866. }
  867. //
  868. // Create a section for the temp file
  869. //
  870. MaxSectionSize.QuadPart = 1024 * 1024;
  871. do
  872. {
  873. Status = MmCreateSection(
  874. &pSection,
  875. STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE,
  876. NULL,
  877. &MaxSectionSize,
  878. PAGE_READWRITE,
  879. SEC_COMMIT,
  880. hFile,
  881. NULL );
  882. if( ! NT_SUCCESS( Status ) )
  883. {
  884. if( Status == STATUS_DISK_FULL )
  885. {
  886. MaxSectionSize.QuadPart /= 2;
  887. /*
  888. DbgPrint ("Buggy: MmCreateSection returned STATUS_DISK_FULL, re-trying with max section size = %I64X\n",
  889. MaxSectionSize.QuadPart );
  890. */
  891. }
  892. else
  893. {
  894. DbgPrint ("Buggy: MmCreateSection failed - status %X\n",
  895. Status );
  896. goto cleanup;
  897. }
  898. }
  899. else
  900. {
  901. /*
  902. DbgPrint ( "Buggy: created section with max size %I64X\n",
  903. MaxSectionSize.QuadPart );
  904. */
  905. break;
  906. }
  907. } while( MaxSectionSize.QuadPart > PAGE_SIZE );
  908. DbgPrint ( "Buggy: Using section at %p\n",
  909. pSection );
  910. //
  911. // try to map the max size section
  912. //
  913. SizeToMap = (SIZE_T) MaxSectionSize.QuadPart;
  914. RtlZeroMemory( MappedSections, BUGGY_MAX_SECTIONS_TO_MAP * sizeof( PVOID ) );
  915. for( CrtMap = 0; CrtMap < BUGGY_MAX_SECTIONS_TO_MAP; CrtMap++ )
  916. {
  917. Status = MmMapViewInSessionSpace(
  918. pSection,
  919. &MappedSections[ CrtMap ],
  920. &SizeToMap );
  921. if( ! NT_SUCCESS( Status ) )
  922. {
  923. /*
  924. DbgPrint ( "Buggy: MmMapViewInSessionSpace failed for size %p, status %X, chunk %p\n",
  925. SizeToMap,
  926. Status
  927. );
  928. */
  929. break;
  930. }
  931. else
  932. {
  933. if( CrtMap <= 100 )
  934. {
  935. VerifyMapping( MappedSections[ CrtMap ], SizeToMap );
  936. }
  937. }
  938. }
  939. DbgPrint ( "\n\nBuggy: Result of the test:\n\n" );
  940. DbgPrint ( "Buggy: mapped %u sections with size %p, total %p\n",
  941. CrtMap,
  942. SizeToMap,
  943. SizeToMap * (SIZE_T)CrtMap );
  944. //DbgPrint ( "\n\n" );
  945. //
  946. // clean-up
  947. //
  948. cleanup:
  949. for( CrtMap = 0; CrtMap < BUGGY_MAX_SECTIONS_TO_MAP; CrtMap++ )
  950. {
  951. if( MappedSections[ CrtMap ] == NULL )
  952. {
  953. break;
  954. }
  955. Status = MmUnmapViewInSessionSpace(
  956. MappedSections[ CrtMap ] );
  957. if( ! NT_SUCCESS( Status ) )
  958. {
  959. DbgPrint ( "Buggy: MmUnmapViewInSessionSpace failed for %p, status %X\n",
  960. MappedSections[ CrtMap ],
  961. Status );
  962. DbgBreakPoint();
  963. }
  964. }
  965. if( pSection != NULL )
  966. {
  967. ObDereferenceObject(
  968. pSection );
  969. }
  970. if( hFile != NULL )
  971. {
  972. ZwClose( hFile );
  973. Status = ZwDeleteFile(
  974. &ObjectAttributes );
  975. if( ! NT_SUCCESS( Status ) )
  976. {
  977. DbgPrint ("Buggy: ZwDeleteFile failed - status %X\n",
  978. Status );
  979. }
  980. else
  981. {
  982. //DbgPrint ("Buggy: temporary file deleted\n" );
  983. }
  984. }
  985. ExFreePool( MappedSections );
  986. }
  987. ///////////////////////////////////////////////////////////////////////////
  988. VOID SessionPoolTest (
  989. PVOID NotUsed
  990. )
  991. {
  992. PVOID *SessionPoolChunks;
  993. ULONG uCrtPoolChunk;
  994. SessionPoolChunks = ExAllocatePoolWithTag(
  995. NonPagedPool,
  996. BUGGY_MAX_SECTIONS_TO_MAP * sizeof( PVOID ),
  997. TD_POOL_TAG );
  998. if( SessionPoolChunks == NULL )
  999. {
  1000. DbgPrint ("Buggy: ExAllocatePoolWithTag failed - bail\n" );
  1001. return;
  1002. }
  1003. RtlZeroMemory( SessionPoolChunks, BUGGY_MAX_SECTIONS_TO_MAP * sizeof( PVOID ) );
  1004. for( uCrtPoolChunk = 0; uCrtPoolChunk < BUGGY_MAX_SECTIONS_TO_MAP; uCrtPoolChunk++ )
  1005. {
  1006. SessionPoolChunks[ uCrtPoolChunk ] = ExAllocatePoolWithTag(
  1007. PagedPool | SESSION_POOL_MASK,
  1008. 1024 * 1024,
  1009. TD_POOL_TAG );
  1010. if( SessionPoolChunks[ uCrtPoolChunk ] == NULL )
  1011. {
  1012. DbgPrint ("\n\nBuggy: Result of the test allocated %u chunks with size 1 Mb in the session pool\n\n",
  1013. uCrtPoolChunk );
  1014. break;
  1015. }
  1016. }
  1017. DbgPrint ( "Buggy: Touching all these pool chuncks...\n" );
  1018. while( uCrtPoolChunk > 0 )
  1019. {
  1020. uCrtPoolChunk--;
  1021. if( uCrtPoolChunk <= 100 )
  1022. {
  1023. VerifyMapping( SessionPoolChunks[ uCrtPoolChunk ], 1024 * 1024 );
  1024. }
  1025. ExFreePool( SessionPoolChunks[ uCrtPoolChunk ] );
  1026. }
  1027. DbgPrint ( "Done\n" );
  1028. ExFreePool( SessionPoolChunks );
  1029. }
  1030. #endif // #if !MAPVIEW_ACTIVE