none
Problem Purge Mapping RRS feed

  • Question

  • Hello

    My code source for mapping file in my disk file system.

    USER MODE :

    fkdd=CreateFile("c:\\cpourtesterlec1\\a1\\map1.txt",GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    		if ((fkdd!=INVALID_HANDLE_VALUE))
    		{
    			HANDLE fmap;
    			LPVOID p;
    			BYTE *pb;
    			
    			{
    				DWORD size=GetFileSize(fkdd,NULL);
    				fmap=CreateFileMapping(fkdd,NULL,PAGE_EXECUTE_READWRITE,0,size,NULL);
    				int er=GetLastError();
    				p=MapViewOfFile(fmap,FILE_MAP_ALL_ACCESS,0,0,size);
    				pb=(BYTE*)p;
    			}
    			SetFilePointer(fkdd,10,NULL,FILE_BEGIN);
    			DWORD dt=0;
    			char *bb="cc";
    			WriteFile(fkdd,bb,2,&dt,NULL);
    			//::FlushFileBuffers(fkdd);
    			////WHEN I STOP DEBUGGER THE MAP IS OLD 
    			{
    				UnmapViewOfFile(p);
    				CloseHandle(fmap);
    				CloseHandle(fkdd);
    			}
    			
    
    		}

    The Writefile does not update data of the pointer 'pb' , If stop processus before UnmapViewOfFile.

    KERNEL MODE :
    my kernel code IRP_MJ_WRITE :

    if (isPagingIo == FALSE)  

                         purgemap_without_uninit(file, fsc);



    void purgemap_without_uninit(PFILE_OBJECT _file, PCCB fsc)
    {
        if ((fsc != NULL))
        {
            KdPrintfd2(("[1]fsc:%.8x context:%.8x\n", fsc, fsc->Context));
            if (_file->SectionObjectPointer != NULL &&
                _file->SectionObjectPointer->DataSectionObject != NULL) {
                //fsc->debug2=TRUE;
                //KdPrintf(("FatCleanup Dump Cache\n"));
                ExAcquireResourceExclusiveLite(&fsc->Fcb->PagingIoResource, TRUE); ///ajout 05012017
                IO_STATUS_BLOCK         IoStatus;
                if (fsc->Fcb != NULL)
                {
                    CcFlushCache(&fsc->Fcb->SectionObjectPointers, NULL, 0, &IoStatus);
                    KdPrintfcriti(("CCFlushCache Status:%x\n", IoStatus.Status));
                }
                //KdPrintf(("FatCleanup Dump Cache2\n"));
                if (fsc->Fcb != NULL)
                {
                    BOOLEAN B=CcPurgeCacheSection(&fsc->Fcb->SectionObjectPointers, NULL, 0, FALSE);
                    KdPrintfcriti(("CcPurgeCacheSection Status:%x\n", B));
                }
                
                ExReleaseResourceLite(&fsc->Fcb->PagingIoResource); ///ajout 05012017
                                                                    //KdPrintf(("FatCleanup Fin Dump \n"));
                                                                    //fsc->debug2=FALSE;
    
            }
            else
                KdPrintf(("FatCleanup not mapping\n"));
            
        }
    }

    There is not update mapping file if UnMapViewofFile is not called.
    When the mapping is not updated then
    CCPurgeCacheSection return 0;




    • Edited by Sizy458 Friday, January 5, 2018 11:42 PM
    Friday, January 5, 2018 11:39 PM

Answers

  • Trying to get things unmapped or out of cache is extremely hard.   Note from the documentation "CcPurgeCacheSection will not purge mapped files"  so your attempt is not going to work.   I know of no way to reliably perform this type of operation.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Saturday, January 6, 2018 2:09 AM

All replies

  • Trying to get things unmapped or out of cache is extremely hard.   Note from the documentation "CcPurgeCacheSection will not purge mapped files"  so your attempt is not going to work.   I know of no way to reliably perform this type of operation.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Saturday, January 6, 2018 2:09 AM
  • yes,

    but how do I have to make the data of the pointer 'pb' to be updated after calling of the API 'WriteFile' ??

    Important Question :
    Is that I can get inspired from fastfat source code ?????
    For fix the mapping bug

    French

    Oui,

    mais comment faire en sorte pour que le pointer 'pb' soit mis  à jour aprés l'appel de l'API WriteFile ?

    Question important :

    Est ce que je peux m'inspirer du code source fastfat ????

    Pour fixer le bug le mapping

    • Edited by Sizy458 Saturday, January 6, 2018 10:12 AM
    Saturday, January 6, 2018 9:39 AM
  • You would have to update that pointer yourself.  You have mapped a region of the file to memory and pb points to that memory.  WriteFile is totally independent of that, it does nothing to change the mapping, only the data in the file.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Saturday, January 6, 2018 1:14 PM
  • I tested on a native fat (ntfs) windows , and the pointer is updated when i call WriteFile.
    Saturday, January 6, 2018 2:33 PM
  • IMPOSSIBLE, that would break the semantics of Windows File Systems.  The data pointed to will be updated, but pb will certainly not change.  Consider the following sequence:

    1. MapViewOfFile for 4096 bytes, getting back pb
    2. WriteFile for 8192 bytes
    3. UnMapViewofFile with pb

    If WriteFile updated pb, you would no longer have a valid pointer to unmap the file.

    WriteFile will update the data in the file, and if the data is in a mapped region, the mapped region will reflect the new data, but update the pointer that is not correct on so many levels.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Saturday, January 6, 2018 2:42 PM