Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / chrome / app / image_pre_reader_win.cc
blob9998a036905a03eb32e3add6572d958d2fcd4791
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/app/image_pre_reader_win.h"
7 #include <windows.h>
8 #include <algorithm>
9 #include <limits>
10 #include <vector>
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "base/win/pe_image.h"
16 #include "base/win/scoped_handle.h"
17 #include "base/win/windows_version.h"
19 namespace {
21 // The minimum buffer size to allocate when reading the PE file headers.
23 // The PE file headers usually fit into a single 1KB page, and a PE file must
24 // at least contain the initial page with the headers. That said, as long as
25 // we expect at least sizeof(IMAGE_DOS_HEADER) bytes, we're ok.
26 const size_t kMinHeaderBufferSize = 0x400;
28 // A handy symbolic constant.
29 const size_t kOneHundredPercent = 100;
31 static_assert(kMinHeaderBufferSize >= sizeof(IMAGE_DOS_HEADER),
32 "kMinHeaderBufferSize must be at least as big as the dos header");
34 // This struct provides a deallocation functor for use with scoped_ptr<T>
35 // allocated with ::VirtualAlloc().
36 struct VirtualFreeDeleter {
37 void operator() (void* ptr) {
38 ::VirtualFree(ptr, 0, MEM_RELEASE);
42 // A wrapper for the Win32 ::SetFilePointer() function with some error checking.
43 bool SetFilePointer(HANDLE file_handle, size_t position) {
44 return position <= static_cast<size_t>(std::numeric_limits<LONG>::max()) &&
45 ::SetFilePointer(file_handle,
46 static_cast<LONG>(position),
47 NULL,
48 FILE_BEGIN) != INVALID_SET_FILE_POINTER;
51 // A helper function to read the next |bytes_to_read| bytes from the file
52 // given by |file_handle| into |buffer|.
53 bool ReadNextBytes(HANDLE file_handle, void* buffer, size_t bytes_to_read) {
54 DCHECK(file_handle != INVALID_HANDLE_VALUE);
55 DCHECK(buffer != NULL);
56 DCHECK(bytes_to_read > 0);
58 DWORD bytes_read = 0;
59 return bytes_to_read <= std::numeric_limits<DWORD>::max() &&
60 ::ReadFile(file_handle,
61 buffer,
62 static_cast<DWORD>(bytes_to_read),
63 &bytes_read,
64 NULL) &&
65 bytes_read == bytes_to_read;
68 // A helper function to extend the |current_buffer| of bytes such that it
69 // contains |desired_length| bytes read from the file given by |file_handle|.
71 // It is assumed that |file_handle| has been used to sequentially populate
72 // |current_buffer| thus far and is already positioned at the appropriate
73 // read location.
74 bool ReadMissingBytes(HANDLE file_handle,
75 std::vector<uint8>* current_buffer,
76 size_t desired_length) {
77 DCHECK(file_handle != INVALID_HANDLE_VALUE);
78 DCHECK(current_buffer != NULL);
80 size_t current_length = current_buffer->size();
81 if (current_length >= desired_length)
82 return true;
84 size_t bytes_to_read = desired_length - current_length;
85 current_buffer->resize(desired_length);
86 return ReadNextBytes(file_handle,
87 &(current_buffer->at(current_length)),
88 bytes_to_read);
91 // Return a |percentage| of the number of initialized bytes in the given
92 // |section|.
94 // This returns a percentage of the lesser of the size of the raw data in
95 // the section and the virtual size of the section.
97 // Note that sections can have their tails implicitly initialized to zero
98 // (i.e., their virtual size is larger than the raw size) and that raw data
99 // is padded to the PE page size if the entire section is initialized (i.e.,
100 // their raw data size will be larger than the virtual size).
102 // Any data after the initialized portion of the section will be soft-faulted
103 // in (very quickly) as needed, so we don't need to include it in the returned
104 // length.
105 size_t GetPercentageOfSectionLength(const IMAGE_SECTION_HEADER* section,
106 size_t percentage) {
107 DCHECK(section != NULL);
108 DCHECK_GT(percentage, 0u);
109 DCHECK_LE(percentage, kOneHundredPercent);
111 size_t initialized_length = std::min(section->SizeOfRawData,
112 section->Misc.VirtualSize);
114 if (initialized_length == 0)
115 return 0;
117 size_t length = (initialized_length * percentage) / kOneHundredPercent;
119 return std::max<size_t>(length, 1);
122 // Helper function to read through a |percentage| of the given |section|
123 // of the file denoted by |file_handle|. The |temp_buffer| is (re)used as
124 // a transient storage area as the section is read in chunks of
125 // |temp_buffer_size| bytes.
126 bool ReadThroughSection(HANDLE file_handle,
127 const IMAGE_SECTION_HEADER* section,
128 size_t percentage,
129 void* temp_buffer,
130 size_t temp_buffer_size) {
131 DCHECK(file_handle != INVALID_HANDLE_VALUE);
132 DCHECK(section != NULL);
133 DCHECK_LE(percentage, kOneHundredPercent);
134 DCHECK(temp_buffer != NULL);
135 DCHECK(temp_buffer_size > 0);
137 size_t bytes_to_read = GetPercentageOfSectionLength(section, percentage);
138 if (bytes_to_read == 0)
139 return true;
141 if (!SetFilePointer(file_handle, section->PointerToRawData))
142 return false;
144 // Read all chunks except the last one.
145 while (bytes_to_read > temp_buffer_size) {
146 if (!ReadNextBytes(file_handle, temp_buffer, temp_buffer_size))
147 return false;
148 bytes_to_read -= temp_buffer_size;
151 // Read the last (possibly partial) chunk and return.
152 DCHECK(bytes_to_read > 0);
153 DCHECK(bytes_to_read <= temp_buffer_size);
154 return ReadNextBytes(file_handle, temp_buffer, bytes_to_read);
157 // A helper function to touch all pages in the range
158 // [base_addr, base_addr + length).
159 void TouchPagesInRange(void* base_addr, size_t length) {
160 DCHECK(base_addr != NULL);
161 DCHECK(length > 0);
163 // Get the system info so we know the page size. Also, make sure we use a
164 // non-zero value for the page size; GetSystemInfo() is hookable/patchable,
165 // and you never know what shenanigans someone could get up to.
166 SYSTEM_INFO system_info = {};
167 GetSystemInfo(&system_info);
168 if (system_info.dwPageSize == 0)
169 system_info.dwPageSize = 4096;
171 // We don't want to read outside the byte range (which could trigger an
172 // access violation), so let's figure out the exact locations of the first
173 // and final bytes we want to read.
174 volatile uint8* touch_ptr = reinterpret_cast<uint8*>(base_addr);
175 volatile uint8* final_touch_ptr = touch_ptr + length - 1;
177 // Read the memory in the range [touch_ptr, final_touch_ptr] with a stride
178 // of the system page size, to ensure that it's been paged in.
179 uint8 dummy;
180 while (touch_ptr < final_touch_ptr) {
181 dummy = *touch_ptr;
182 touch_ptr += system_info.dwPageSize;
184 dummy = *final_touch_ptr;
187 } // namespace
189 bool ImagePreReader::PartialPreReadImageOnDisk(const wchar_t* file_path,
190 size_t percentage,
191 size_t max_chunk_size) {
192 // TODO(rogerm): change this to have the number of bytes pre-read per
193 // section be driven by a static table within the PE file (defaulting to
194 // full read if it's not there?) that's initialized by the optimization
195 // toolchain.
196 DCHECK(file_path != NULL);
198 if (percentage == 0)
199 return true;
201 if (percentage > kOneHundredPercent)
202 percentage = kOneHundredPercent;
204 // Validate/setup max_chunk_size, imposing a 1MB minimum on the chunk size.
205 const size_t kMinChunkSize = 1024 * 1024;
206 max_chunk_size = std::max(max_chunk_size, kMinChunkSize);
208 // Open the file.
209 base::win::ScopedHandle file(
210 CreateFile(file_path,
211 GENERIC_READ,
212 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
213 NULL,
214 OPEN_EXISTING,
215 FILE_FLAG_SEQUENTIAL_SCAN,
216 NULL));
218 if (!file.IsValid())
219 return false;
221 // Allocate a resizable buffer for the headers. We initially reserve as much
222 // space as we typically see as the header size for chrome.dll and other
223 // PE images.
224 std::vector<uint8> headers;
225 headers.reserve(kMinHeaderBufferSize);
227 // Read, hopefully, all of the headers.
228 if (!ReadMissingBytes(file.Get(), &headers, kMinHeaderBufferSize))
229 return false;
231 // The DOS header starts at offset 0 and allows us to get the offset of the
232 // NT headers. Let's ensure we've read enough to capture the NT headers.
233 size_t nt_headers_start =
234 reinterpret_cast<IMAGE_DOS_HEADER*>(&headers[0])->e_lfanew;
235 size_t nt_headers_end = nt_headers_start + sizeof(IMAGE_NT_HEADERS);
236 if (!ReadMissingBytes(file.Get(), &headers, nt_headers_end))
237 return false;
239 // Now that we've got the NT headers we can get the total header size,
240 // including all of the section headers. Let's ensure we've read enough
241 // to capture all of the header data.
242 size_t size_of_headers = reinterpret_cast<IMAGE_NT_HEADERS*>(
243 &headers[nt_headers_start])->OptionalHeader.SizeOfHeaders;
244 if (!ReadMissingBytes(file.Get(), &headers, size_of_headers))
245 return false;
247 // Now we have all of the headers. This is enough to let us use the PEImage
248 // wrapper to query the structure of the image.
249 base::win::PEImage pe_image(reinterpret_cast<HMODULE>(&headers[0]));
250 CHECK(pe_image.VerifyMagic());
252 // Allocate a buffer to hold the pre-read bytes.
253 scoped_ptr<uint8, VirtualFreeDeleter> buffer(
254 static_cast<uint8*>(
255 ::VirtualAlloc(NULL, max_chunk_size, MEM_COMMIT, PAGE_READWRITE)));
256 if (buffer.get() == NULL)
257 return false;
259 // Iterate over each section, reading in a percentage of each.
260 const IMAGE_SECTION_HEADER* section = NULL;
261 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
262 CHECK_LE(reinterpret_cast<const uint8*>(section + 1),
263 &headers[0] + headers.size());
264 if (!ReadThroughSection(file.Get(), section, percentage, buffer.get(),
265 max_chunk_size)) {
266 return false;
270 // We're done.
271 return true;
274 bool ImagePreReader::PartialPreReadImageInMemory(const wchar_t* file_path,
275 size_t percentage) {
276 // TODO(rogerm): change this to have the number of bytes pre-read per
277 // section be driven by a static table within the PE file (defaulting to
278 // full read if it's not there?) that's initialized by the optimization
279 // toolchain.
280 DCHECK(file_path != NULL);
282 if (percentage == 0)
283 return true;
285 if (percentage > kOneHundredPercent)
286 percentage = kOneHundredPercent;
288 HMODULE dll_module = ::LoadLibraryExW(
289 file_path,
290 NULL,
291 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
293 if (!dll_module)
294 return false;
296 base::win::PEImage pe_image(dll_module);
297 CHECK(pe_image.VerifyMagic());
299 // Iterate over each section, stepping through a percentage of each to page
300 // it in off the disk.
301 const IMAGE_SECTION_HEADER* section = NULL;
302 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
303 // Get the extent we want to touch.
304 size_t length = GetPercentageOfSectionLength(section, percentage);
305 if (length == 0)
306 continue;
307 uint8* start =
308 static_cast<uint8*>(pe_image.RVAToAddr(section->VirtualAddress));
310 // Verify that the extent we're going to touch falls inside the section
311 // we expect it to (and by implication, inside the pe_image).
312 CHECK_EQ(section,
313 pe_image.GetImageSectionFromAddr(start));
314 CHECK_EQ(section,
315 pe_image.GetImageSectionFromAddr(start + length - 1));
317 // Page in the section range.
318 TouchPagesInRange(start, length);
321 FreeLibrary(dll_module);
323 return true;
326 bool ImagePreReader::PreReadImage(const wchar_t* file_path,
327 size_t size_to_read,
328 size_t step_size) {
329 base::ThreadRestrictions::AssertIOAllowed();
330 if (base::win::GetVersion() > base::win::VERSION_XP) {
331 // Vista+ branch. On these OSes, the forced reads through the DLL actually
332 // slows warm starts. The solution is to sequentially read file contents
333 // with an optional cap on total amount to read.
334 base::win::ScopedHandle file(
335 CreateFile(file_path,
336 GENERIC_READ,
337 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
338 NULL,
339 OPEN_EXISTING,
340 FILE_FLAG_SEQUENTIAL_SCAN,
341 NULL));
343 if (!file.IsValid())
344 return false;
346 // Default to 1MB sequential reads.
347 const DWORD actual_step_size = std::max(static_cast<DWORD>(step_size),
348 static_cast<DWORD>(1024*1024));
349 LPVOID buffer = ::VirtualAlloc(NULL,
350 actual_step_size,
351 MEM_COMMIT,
352 PAGE_READWRITE);
354 if (buffer == NULL)
355 return false;
357 DWORD len;
358 size_t total_read = 0;
359 while (::ReadFile(file.Get(), buffer, actual_step_size, &len, NULL) &&
360 len > 0 &&
361 (size_to_read ? total_read < size_to_read : true)) {
362 total_read += static_cast<size_t>(len);
364 ::VirtualFree(buffer, 0, MEM_RELEASE);
365 } else {
366 // WinXP branch. Here, reading the DLL from disk doesn't do
367 // what we want so instead we pull the pages into memory by loading
368 // the DLL and touching pages at a stride. We use the system's page
369 // size as the stride, ignoring the passed in step_size, to make sure
370 // each page in the range is touched.
371 HMODULE dll_module = ::LoadLibraryExW(
372 file_path,
373 NULL,
374 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
376 if (!dll_module)
377 return false;
379 base::win::PEImage pe_image(dll_module);
380 CHECK(pe_image.VerifyMagic());
382 // We don't want to read past the end of the module (which could trigger
383 // an access violation), so make sure to check the image size.
384 PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders();
385 size_t dll_module_length = std::min(
386 size_to_read ? size_to_read : ~0,
387 static_cast<size_t>(nt_headers->OptionalHeader.SizeOfImage));
389 // Page in then release the module.
390 TouchPagesInRange(dll_module, dll_module_length);
391 FreeLibrary(dll_module);
394 return true;
397 bool ImagePreReader::PartialPreReadImage(const wchar_t* file_path,
398 size_t percentage,
399 size_t max_chunk_size) {
400 base::ThreadRestrictions::AssertIOAllowed();
402 if (percentage >= kOneHundredPercent) {
403 // If we're reading the whole image, we don't need to parse headers and
404 // navigate sections, the basic PreReadImage() can be used to just step
405 // blindly through the entire file / address-space.
406 return PreReadImage(file_path, 0, max_chunk_size);
409 if (base::win::GetVersion() > base::win::VERSION_XP) {
410 // Vista+ branch. On these OSes, we warm up the Image by reading its
411 // file off the disk.
412 return PartialPreReadImageOnDisk(file_path, percentage, max_chunk_size);
415 // WinXP branch. For XP, reading the image from disk doesn't do what we want
416 // so instead we pull the pages into memory by loading the DLL and touching
417 // initialized pages at a stride.
418 return PartialPreReadImageInMemory(file_path, percentage);