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.

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