/* * @implemented */ BOOL NTAPI FlushViewOfFile(LPCVOID lpBaseAddress, SIZE_T dwNumberOfBytesToFlush) { SIZE_T NumberOfBytesToFlush; NTSTATUS Status; IO_STATUS_BLOCK IoStatusBlock; /* Save amount of bytes to flush to a local var */ NumberOfBytesToFlush = dwNumberOfBytesToFlush; /* Flush the view */ Status = NtFlushVirtualMemory(NtCurrentProcess(), (LPVOID)lpBaseAddress, &NumberOfBytesToFlush, &IoStatusBlock); if (!NT_SUCCESS(Status)) { /* We failed */ BaseSetLastNTError(Status); return FALSE; } /* Return success */ return TRUE; }
/* * @implemented */ BOOL NTAPI FlushViewOfFile(IN LPCVOID lpBaseAddress, IN SIZE_T dwNumberOfBytesToFlush) { NTSTATUS Status; PVOID BaseAddress = (PVOID)lpBaseAddress; SIZE_T NumberOfBytesToFlush = dwNumberOfBytesToFlush; IO_STATUS_BLOCK IoStatusBlock; /* Flush the view */ Status = NtFlushVirtualMemory(NtCurrentProcess(), &BaseAddress, &NumberOfBytesToFlush, &IoStatusBlock); if (!NT_SUCCESS(Status) && (Status != STATUS_NOT_MAPPED_DATA)) { /* We failed */ BaseSetLastNTError(Status); return FALSE; } /* Return success */ return TRUE; }
/*********************************************************************** * FlushViewOfFile (KERNEL32.@) * * Writes to the disk a byte range within a mapped view of a file. * * PARAMS * base [I] Start address of byte range to flush. * size [I] Number of bytes in range. * * RETURNS * Success: TRUE. * Failure: FALSE. */ BOOL WINAPI FlushViewOfFile( LPCVOID base, SIZE_T size ) { NTSTATUS status = NtFlushVirtualMemory( GetCurrentProcess(), &base, &size, 0 ); if (status) { if (status == STATUS_NOT_MAPPED_DATA) status = STATUS_SUCCESS; else SetLastError( RtlNtStatusToDosError(status) ); } return !status; }
BOOL APIENTRY FlushViewOfFile( LPCVOID lpBaseAddress, DWORD dwNumberOfBytesToFlush ) /*++ Routine Description: A byte range within a mapped view of a file can be flushed to disk using FlushViewOfFile. A byte range within a mapped view of a file can be flushed to disk using FlushViewOfFile. Flushing a range of a mapped view causes any dirty pages within that range to be written to disk. This operation automatically happens whenever a view is unmapped (either explicitly or as a result of process termination). Arguments: lpBaseAddress - Supplies the base address of a set of bytes that are to be flushed to the on disk representation of the mapped file. dwNumberOfBytesToFlush - Supplies the number of bytes to flush. Return Value: TRUE - The operation was successful. All dirty pages within the specified range are stored in the on-disk representation of the mapped file. FALSE - The operation failed. Extended error status is available using GetLastError. --*/ { NTSTATUS Status; PVOID BaseAddress; ULONG RegionSize; IO_STATUS_BLOCK IoStatus; BaseAddress = (PVOID)lpBaseAddress; RegionSize = dwNumberOfBytesToFlush; Status = NtFlushVirtualMemory( NtCurrentProcess(), &BaseAddress, &RegionSize, &IoStatus ); if ( !NT_SUCCESS(Status) ) { if ( Status == STATUS_NOT_MAPPED_DATA ) { return TRUE; } BaseSetLastNTError(Status); return FALSE; } return TRUE; }
void Test_ImageSection(void) { UNICODE_STRING FileName; NTSTATUS Status; OBJECT_ATTRIBUTES FileObjectAttributes; IO_STATUS_BLOCK IoStatusBlock; HANDLE FileHandle, DataSectionHandle, ImageSectionHandle; PVOID DataBase, ImageBase; SIZE_T ViewSize; if (!RtlDosPathNameToNtPathName_U(L"testdata\\test.dll", &FileName, NULL, NULL)) { ok(0, "RtlDosPathNameToNtPathName_U failed\n"); return; } InitializeObjectAttributes(&FileObjectAttributes, &FileName, 0, NULL, NULL); Status = NtOpenFile(&FileHandle, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &FileObjectAttributes, &IoStatusBlock, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT); ok(Status == STATUS_SUCCESS, "NtOpenFile failed, Status 0x%lx\n", Status); /* Create a data section with write access */ Status = NtCreateSection(&DataSectionHandle, SECTION_ALL_ACCESS, // DesiredAccess NULL, // ObjectAttributes NULL, // MaximumSize PAGE_READWRITE, // SectionPageProtection SEC_COMMIT, // AllocationAttributes FileHandle); ok(Status == STATUS_SUCCESS, "NtCreateSection failed, Status 0x%lx\n", Status); /* Map the data section */ DataBase = NULL; ViewSize = 0; Status = NtMapViewOfSection(DataSectionHandle, NtCurrentProcess(), &DataBase, 0, 0, NULL, &ViewSize, ViewShare, 0, PAGE_READWRITE); ok(Status == STATUS_SUCCESS, "NtMapViewOfSection failed, Status 0x%lx\n", Status); /* Check the original data */ ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n"); /* Modify the PE header (but do not flush!) */ *(ULONG*)DataBase = 0xdeadbabe; ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n"); /* Modify data in the .data section (but do not flush!) */ ok(*(ULONG*)((PUCHAR)DataBase + 0x800) == 0x12345678, "Data in .data section invalid: 0x%lx!\n", *(ULONG*)((PUCHAR)DataBase + 0x800)); *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x87654321; /* Now try to create an image section (should fail) */ Status = NtCreateSection(&ImageSectionHandle, SECTION_ALL_ACCESS, // DesiredAccess NULL, // ObjectAttributes NULL, // MaximumSize PAGE_READWRITE, // SectionPageProtection SEC_IMAGE, // AllocationAttributes FileHandle); ok(Status == STATUS_INVALID_IMAGE_NOT_MZ, "NtCreateSection failed, Status 0x%lx\n", Status); /* Restore the original header */ *(ULONG*)DataBase = 0x00905a4d; /* Modify data in the .data section (but do not flush!) */ ok_hex(*(ULONG*)((PUCHAR)DataBase + 0x800), 0x87654321); *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xdeadbabe; /* Try to create an image section again */ Status = NtCreateSection(&ImageSectionHandle, SECTION_ALL_ACCESS, // DesiredAccess NULL, // ObjectAttributes NULL, // MaximumSize PAGE_READWRITE, // SectionPageProtection SEC_IMAGE, // AllocationAttributes FileHandle); ok(Status == STATUS_SUCCESS, "NtCreateSection failed, Status 0x%lx\n", Status); /* Map the image section */ ImageBase = NULL; ViewSize = 0; Status = NtMapViewOfSection(ImageSectionHandle, NtCurrentProcess(), &ImageBase, 0, 0, NULL, &ViewSize, ViewShare, 0, PAGE_READONLY); #ifdef _M_IX86 ok(Status == STATUS_SUCCESS, "NtMapViewOfSection failed, Status 0x%lx\n", Status); #else ok(Status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH, "NtMapViewOfSection failed, Status 0x%lx\n", Status); #endif /* Check the header */ ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n"); ok(*(ULONG*)ImageBase == 0x00905a4d, "Header not ok\n"); /* Check the data section. Either of these can be present! */ ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) || (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678), "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000)); /* Now modify the data again */ *(ULONG*)DataBase = 0xdeadbabe; *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xf00dada; /* Check the data */ ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n"); ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n"); ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) || (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678), "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000)); /* Flush the view */ ViewSize = 0x1000; Status = NtFlushVirtualMemory(NtCurrentProcess(), &DataBase, &ViewSize, &IoStatusBlock); ok(Status == STATUS_SUCCESS, "NtFlushVirtualMemory failed, Status 0x%lx\n", Status); /* Check the data again */ ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n"); ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) || (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678), "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000)); /* Restore the original header */ *(ULONG*)DataBase = 0x00905a4d; ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n"); /* Close the image mapping */ NtUnmapViewOfSection(NtCurrentProcess(), ImageBase); NtClose(ImageSectionHandle); /* Create an image section again */ Status = NtCreateSection(&ImageSectionHandle, SECTION_ALL_ACCESS, // DesiredAccess NULL, // ObjectAttributes NULL, // MaximumSize PAGE_READWRITE, // SectionPageProtection SEC_IMAGE, // AllocationAttributes FileHandle); ok(Status == STATUS_SUCCESS, "NtCreateSection failed, Status 0x%lx\n", Status); /* Map the image section again */ ImageBase = NULL; ViewSize = 0; Status = NtMapViewOfSection(ImageSectionHandle, NtCurrentProcess(), &ImageBase, 0, 0, NULL, &ViewSize, ViewShare, 0, PAGE_READONLY); #ifdef _M_IX86 ok(Status == STATUS_SUCCESS, "NtMapViewOfSection failed, Status 0x%lx\n", Status); #else ok(Status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH, "NtMapViewOfSection failed, Status 0x%lx\n", Status); #endif /* Check the .data section again */ ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada, "Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000)); /* Restore the original data */ *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x12345678; /* Close the data mapping */ NtUnmapViewOfSection(NtCurrentProcess(), DataBase); NtClose(DataSectionHandle); /* Try to allocate memory inside the image mapping */ DataBase = (PUCHAR)ImageBase + 0x20000; ViewSize = 0x1000; Status = NtAllocateVirtualMemory(NtCurrentProcess(), &DataBase, 0, &ViewSize, MEM_RESERVE, PAGE_NOACCESS); ok(Status == STATUS_CONFLICTING_ADDRESSES, "Wrong Status: 0x%lx\n", Status); /* Cleanup */ NtClose(FileHandle); NtClose(ImageSectionHandle); NtUnmapViewOfSection(NtCurrentProcess(), ImageBase); }