This source file includes following definitions.
- StaticAssertions
- SetFilePointer
- ReadNextBytes
- ReadMissingBytes
- GetPercentageOfSectionLength
- ReadThroughSection
- TouchPagesInRange
- PartialPreReadImageOnDisk
- PartialPreReadImageInMemory
- PreReadImage
- PartialPreReadImage
#include "chrome/app/image_pre_reader_win.h"
#include <windows.h>
#include <algorithm>
#include <limits>
#include <vector>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread_restrictions.h"
#include "base/win/pe_image.h"
#include "base/win/scoped_handle.h"
#include "base/win/windows_version.h"
namespace {
const size_t kMinHeaderBufferSize = 0x400;
const size_t kOneHundredPercent = 100;
void StaticAssertions() {
COMPILE_ASSERT(kMinHeaderBufferSize >= sizeof(IMAGE_DOS_HEADER),
min_header_buffer_size_at_least_as_big_as_the_dos_header);
}
struct VirtualFreeDeleter {
void operator() (void* ptr) {
::VirtualFree(ptr, 0, MEM_RELEASE);
}
};
bool SetFilePointer(HANDLE file_handle, size_t position) {
return position <= static_cast<size_t>(std::numeric_limits<LONG>::max()) &&
::SetFilePointer(file_handle,
static_cast<LONG>(position),
NULL,
FILE_BEGIN) != INVALID_SET_FILE_POINTER;
}
bool ReadNextBytes(HANDLE file_handle, void* buffer, size_t bytes_to_read) {
DCHECK(file_handle != INVALID_HANDLE_VALUE);
DCHECK(buffer != NULL);
DCHECK(bytes_to_read > 0);
DWORD bytes_read = 0;
return bytes_to_read <= std::numeric_limits<DWORD>::max() &&
::ReadFile(file_handle,
buffer,
static_cast<DWORD>(bytes_to_read),
&bytes_read,
NULL) &&
bytes_read == bytes_to_read;
}
bool ReadMissingBytes(HANDLE file_handle,
std::vector<uint8>* current_buffer,
size_t desired_length) {
DCHECK(file_handle != INVALID_HANDLE_VALUE);
DCHECK(current_buffer != NULL);
size_t current_length = current_buffer->size();
if (current_length >= desired_length)
return true;
size_t bytes_to_read = desired_length - current_length;
current_buffer->resize(desired_length);
return ReadNextBytes(file_handle,
&(current_buffer->at(current_length)),
bytes_to_read);
}
size_t GetPercentageOfSectionLength(const IMAGE_SECTION_HEADER* section,
size_t percentage) {
DCHECK(section != NULL);
DCHECK_GT(percentage, 0u);
DCHECK_LE(percentage, kOneHundredPercent);
size_t initialized_length = std::min(section->SizeOfRawData,
section->Misc.VirtualSize);
if (initialized_length == 0)
return 0;
size_t length = (initialized_length * percentage) / kOneHundredPercent;
return std::max<size_t>(length, 1);
}
bool ReadThroughSection(HANDLE file_handle,
const IMAGE_SECTION_HEADER* section,
size_t percentage,
void* temp_buffer,
size_t temp_buffer_size) {
DCHECK(file_handle != INVALID_HANDLE_VALUE);
DCHECK(section != NULL);
DCHECK_LE(percentage, kOneHundredPercent);
DCHECK(temp_buffer != NULL);
DCHECK(temp_buffer_size > 0);
size_t bytes_to_read = GetPercentageOfSectionLength(section, percentage);
if (bytes_to_read == 0)
return true;
if (!SetFilePointer(file_handle, section->PointerToRawData))
return false;
while (bytes_to_read > temp_buffer_size) {
if (!ReadNextBytes(file_handle, temp_buffer, temp_buffer_size))
return false;
bytes_to_read -= temp_buffer_size;
}
DCHECK(bytes_to_read > 0);
DCHECK(bytes_to_read <= temp_buffer_size);
return ReadNextBytes(file_handle, temp_buffer, bytes_to_read);
}
void TouchPagesInRange(void* base_addr, size_t length) {
DCHECK(base_addr != NULL);
DCHECK(length > 0);
SYSTEM_INFO system_info = {};
GetSystemInfo(&system_info);
if (system_info.dwPageSize == 0)
system_info.dwPageSize = 4096;
volatile uint8* touch_ptr = reinterpret_cast<uint8*>(base_addr);
volatile uint8* final_touch_ptr = touch_ptr + length - 1;
uint8 dummy;
while (touch_ptr < final_touch_ptr) {
dummy = *touch_ptr;
touch_ptr += system_info.dwPageSize;
}
dummy = *final_touch_ptr;
}
}
bool ImagePreReader::PartialPreReadImageOnDisk(const wchar_t* file_path,
size_t percentage,
size_t max_chunk_size) {
DCHECK(file_path != NULL);
if (percentage == 0)
return true;
if (percentage > kOneHundredPercent)
percentage = kOneHundredPercent;
const size_t kMinChunkSize = 1024 * 1024;
max_chunk_size = std::max(max_chunk_size, kMinChunkSize);
base::win::ScopedHandle file(
CreateFile(file_path,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL));
if (!file.IsValid())
return false;
std::vector<uint8> headers;
headers.reserve(kMinHeaderBufferSize);
if (!ReadMissingBytes(file, &headers, kMinHeaderBufferSize))
return false;
size_t nt_headers_start =
reinterpret_cast<IMAGE_DOS_HEADER*>(&headers[0])->e_lfanew;
size_t nt_headers_end = nt_headers_start + sizeof(IMAGE_NT_HEADERS);
if (!ReadMissingBytes(file, &headers, nt_headers_end))
return false;
size_t size_of_headers = reinterpret_cast<IMAGE_NT_HEADERS*>(
&headers[nt_headers_start])->OptionalHeader.SizeOfHeaders;
if (!ReadMissingBytes(file, &headers, size_of_headers))
return false;
base::win::PEImage pe_image(reinterpret_cast<HMODULE>(&headers[0]));
CHECK(pe_image.VerifyMagic());
scoped_ptr<uint8, VirtualFreeDeleter> buffer(
static_cast<uint8*>(
::VirtualAlloc(NULL, max_chunk_size, MEM_COMMIT, PAGE_READWRITE)));
if (buffer.get() == NULL)
return false;
const IMAGE_SECTION_HEADER* section = NULL;
for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
CHECK_LE(reinterpret_cast<const uint8*>(section + 1),
&headers[0] + headers.size());
if (!ReadThroughSection(
file, section, percentage, buffer.get(), max_chunk_size))
return false;
}
return true;
}
bool ImagePreReader::PartialPreReadImageInMemory(const wchar_t* file_path,
size_t percentage) {
DCHECK(file_path != NULL);
if (percentage == 0)
return true;
if (percentage > kOneHundredPercent)
percentage = kOneHundredPercent;
HMODULE dll_module = ::LoadLibraryExW(
file_path,
NULL,
LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
if (!dll_module)
return false;
base::win::PEImage pe_image(dll_module);
CHECK(pe_image.VerifyMagic());
const IMAGE_SECTION_HEADER* section = NULL;
for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
size_t length = GetPercentageOfSectionLength(section, percentage);
if (length == 0)
continue;
uint8* start =
static_cast<uint8*>(pe_image.RVAToAddr(section->VirtualAddress));
CHECK_EQ(section,
pe_image.GetImageSectionFromAddr(start));
CHECK_EQ(section,
pe_image.GetImageSectionFromAddr(start + length - 1));
TouchPagesInRange(start, length);
}
FreeLibrary(dll_module);
return true;
}
bool ImagePreReader::PreReadImage(const wchar_t* file_path,
size_t size_to_read,
size_t step_size) {
base::ThreadRestrictions::AssertIOAllowed();
if (base::win::GetVersion() > base::win::VERSION_XP) {
base::win::ScopedHandle file(
CreateFile(file_path,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL));
if (!file.IsValid())
return false;
const DWORD actual_step_size = std::max(static_cast<DWORD>(step_size),
static_cast<DWORD>(1024*1024));
LPVOID buffer = ::VirtualAlloc(NULL,
actual_step_size,
MEM_COMMIT,
PAGE_READWRITE);
if (buffer == NULL)
return false;
DWORD len;
size_t total_read = 0;
while (::ReadFile(file, buffer, actual_step_size, &len, NULL) &&
len > 0 &&
(size_to_read ? total_read < size_to_read : true)) {
total_read += static_cast<size_t>(len);
}
::VirtualFree(buffer, 0, MEM_RELEASE);
} else {
HMODULE dll_module = ::LoadLibraryExW(
file_path,
NULL,
LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
if (!dll_module)
return false;
base::win::PEImage pe_image(dll_module);
CHECK(pe_image.VerifyMagic());
PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders();
size_t dll_module_length = std::min(
size_to_read ? size_to_read : ~0,
static_cast<size_t>(nt_headers->OptionalHeader.SizeOfImage));
TouchPagesInRange(dll_module, dll_module_length);
FreeLibrary(dll_module);
}
return true;
}
bool ImagePreReader::PartialPreReadImage(const wchar_t* file_path,
size_t percentage,
size_t max_chunk_size) {
base::ThreadRestrictions::AssertIOAllowed();
if (percentage >= kOneHundredPercent) {
return PreReadImage(file_path, 0, max_chunk_size);
}
if (base::win::GetVersion() > base::win::VERSION_XP) {
return PartialPreReadImageOnDisk(file_path, percentage, max_chunk_size);
}
return PartialPreReadImageInMemory(file_path, percentage);
}