Change DtmfSenderHandler to handle events on the signaling thread.
[chromium-blink-merge.git] / chrome / app / image_pre_reader_win.cc
blob6c5ef6bcb924e248b39d23d5203cac2d0a261d7e
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 void StaticAssertions() {
32 COMPILE_ASSERT(kMinHeaderBufferSize >= sizeof(IMAGE_DOS_HEADER),
33 min_header_buffer_size_at_least_as_big_as_the_dos_header);
36 // This struct provides a deallocation functor for use with scoped_ptr<T>
37 // allocated with ::VirtualAlloc().
38 struct VirtualFreeDeleter {
39 void operator() (void* ptr) {
40 ::VirtualFree(ptr, 0, MEM_RELEASE);
44 // A wrapper for the Win32 ::SetFilePointer() function with some error checking.
45 bool SetFilePointer(HANDLE file_handle, size_t position) {
46 return position <= static_cast<size_t>(std::numeric_limits<LONG>::max()) &&
47 ::SetFilePointer(file_handle,
48 static_cast<LONG>(position),
49 NULL,
50 FILE_BEGIN) != INVALID_SET_FILE_POINTER;
53 // A helper function to read the next |bytes_to_read| bytes from the file
54 // given by |file_handle| into |buffer|.
55 bool ReadNextBytes(HANDLE file_handle, void* buffer, size_t bytes_to_read) {
56 DCHECK(file_handle != INVALID_HANDLE_VALUE);
57 DCHECK(buffer != NULL);
58 DCHECK(bytes_to_read > 0);
60 DWORD bytes_read = 0;
61 return bytes_to_read <= std::numeric_limits<DWORD>::max() &&
62 ::ReadFile(file_handle,
63 buffer,
64 static_cast<DWORD>(bytes_to_read),
65 &bytes_read,
66 NULL) &&
67 bytes_read == bytes_to_read;
70 // A helper function to extend the |current_buffer| of bytes such that it
71 // contains |desired_length| bytes read from the file given by |file_handle|.
73 // It is assumed that |file_handle| has been used to sequentially populate
74 // |current_buffer| thus far and is already positioned at the appropriate
75 // read location.
76 bool ReadMissingBytes(HANDLE file_handle,
77 std::vector<uint8>* current_buffer,
78 size_t desired_length) {
79 DCHECK(file_handle != INVALID_HANDLE_VALUE);
80 DCHECK(current_buffer != NULL);
82 size_t current_length = current_buffer->size();
83 if (current_length >= desired_length)
84 return true;
86 size_t bytes_to_read = desired_length - current_length;
87 current_buffer->resize(desired_length);
88 return ReadNextBytes(file_handle,
89 &(current_buffer->at(current_length)),
90 bytes_to_read);
93 // Return a |percentage| of the number of initialized bytes in the given
94 // |section|.
96 // This returns a percentage of the lesser of the size of the raw data in
97 // the section and the virtual size of the section.
99 // Note that sections can have their tails implicitly initialized to zero
100 // (i.e., their virtual size is larger than the raw size) and that raw data
101 // is padded to the PE page size if the entire section is initialized (i.e.,
102 // their raw data size will be larger than the virtual size).
104 // Any data after the initialized portion of the section will be soft-faulted
105 // in (very quickly) as needed, so we don't need to include it in the returned
106 // length.
107 size_t GetPercentageOfSectionLength(const IMAGE_SECTION_HEADER* section,
108 size_t percentage) {
109 DCHECK(section != NULL);
110 DCHECK_GT(percentage, 0u);
111 DCHECK_LE(percentage, kOneHundredPercent);
113 size_t initialized_length = std::min(section->SizeOfRawData,
114 section->Misc.VirtualSize);
116 if (initialized_length == 0)
117 return 0;
119 size_t length = (initialized_length * percentage) / kOneHundredPercent;
121 return std::max<size_t>(length, 1);
124 // Helper function to read through a |percentage| of the given |section|
125 // of the file denoted by |file_handle|. The |temp_buffer| is (re)used as
126 // a transient storage area as the section is read in chunks of
127 // |temp_buffer_size| bytes.
128 bool ReadThroughSection(HANDLE file_handle,
129 const IMAGE_SECTION_HEADER* section,
130 size_t percentage,
131 void* temp_buffer,
132 size_t temp_buffer_size) {
133 DCHECK(file_handle != INVALID_HANDLE_VALUE);
134 DCHECK(section != NULL);
135 DCHECK_LE(percentage, kOneHundredPercent);
136 DCHECK(temp_buffer != NULL);
137 DCHECK(temp_buffer_size > 0);
139 size_t bytes_to_read = GetPercentageOfSectionLength(section, percentage);
140 if (bytes_to_read == 0)
141 return true;
143 if (!SetFilePointer(file_handle, section->PointerToRawData))
144 return false;
146 // Read all chunks except the last one.
147 while (bytes_to_read > temp_buffer_size) {
148 if (!ReadNextBytes(file_handle, temp_buffer, temp_buffer_size))
149 return false;
150 bytes_to_read -= temp_buffer_size;
153 // Read the last (possibly partial) chunk and return.
154 DCHECK(bytes_to_read > 0);
155 DCHECK(bytes_to_read <= temp_buffer_size);
156 return ReadNextBytes(file_handle, temp_buffer, bytes_to_read);
159 // A helper function to touch all pages in the range
160 // [base_addr, base_addr + length).
161 void TouchPagesInRange(void* base_addr, size_t length) {
162 DCHECK(base_addr != NULL);
163 DCHECK(length > 0);
165 // Get the system info so we know the page size. Also, make sure we use a
166 // non-zero value for the page size; GetSystemInfo() is hookable/patchable,
167 // and you never know what shenanigans someone could get up to.
168 SYSTEM_INFO system_info = {};
169 GetSystemInfo(&system_info);
170 if (system_info.dwPageSize == 0)
171 system_info.dwPageSize = 4096;
173 // We don't want to read outside the byte range (which could trigger an
174 // access violation), so let's figure out the exact locations of the first
175 // and final bytes we want to read.
176 volatile uint8* touch_ptr = reinterpret_cast<uint8*>(base_addr);
177 volatile uint8* final_touch_ptr = touch_ptr + length - 1;
179 // Read the memory in the range [touch_ptr, final_touch_ptr] with a stride
180 // of the system page size, to ensure that it's been paged in.
181 uint8 dummy;
182 while (touch_ptr < final_touch_ptr) {
183 dummy = *touch_ptr;
184 touch_ptr += system_info.dwPageSize;
186 dummy = *final_touch_ptr;
189 } // namespace
191 bool ImagePreReader::PartialPreReadImageOnDisk(const wchar_t* file_path,
192 size_t percentage,
193 size_t max_chunk_size) {
194 // TODO(rogerm): change this to have the number of bytes pre-read per
195 // section be driven by a static table within the PE file (defaulting to
196 // full read if it's not there?) that's initialized by the optimization
197 // toolchain.
198 DCHECK(file_path != NULL);
200 if (percentage == 0)
201 return true;
203 if (percentage > kOneHundredPercent)
204 percentage = kOneHundredPercent;
206 // Validate/setup max_chunk_size, imposing a 1MB minimum on the chunk size.
207 const size_t kMinChunkSize = 1024 * 1024;
208 max_chunk_size = std::max(max_chunk_size, kMinChunkSize);
210 // Open the file.
211 base::win::ScopedHandle file(
212 CreateFile(file_path,
213 GENERIC_READ,
214 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
215 NULL,
216 OPEN_EXISTING,
217 FILE_FLAG_SEQUENTIAL_SCAN,
218 NULL));
220 if (!file.IsValid())
221 return false;
223 // Allocate a resizable buffer for the headers. We initially reserve as much
224 // space as we typically see as the header size for chrome.dll and other
225 // PE images.
226 std::vector<uint8> headers;
227 headers.reserve(kMinHeaderBufferSize);
229 // Read, hopefully, all of the headers.
230 if (!ReadMissingBytes(file.Get(), &headers, kMinHeaderBufferSize))
231 return false;
233 // The DOS header starts at offset 0 and allows us to get the offset of the
234 // NT headers. Let's ensure we've read enough to capture the NT headers.
235 size_t nt_headers_start =
236 reinterpret_cast<IMAGE_DOS_HEADER*>(&headers[0])->e_lfanew;
237 size_t nt_headers_end = nt_headers_start + sizeof(IMAGE_NT_HEADERS);
238 if (!ReadMissingBytes(file.Get(), &headers, nt_headers_end))
239 return false;
241 // Now that we've got the NT headers we can get the total header size,
242 // including all of the section headers. Let's ensure we've read enough
243 // to capture all of the header data.
244 size_t size_of_headers = reinterpret_cast<IMAGE_NT_HEADERS*>(
245 &headers[nt_headers_start])->OptionalHeader.SizeOfHeaders;
246 if (!ReadMissingBytes(file.Get(), &headers, size_of_headers))
247 return false;
249 // Now we have all of the headers. This is enough to let us use the PEImage
250 // wrapper to query the structure of the image.
251 base::win::PEImage pe_image(reinterpret_cast<HMODULE>(&headers[0]));
252 CHECK(pe_image.VerifyMagic());
254 // Allocate a buffer to hold the pre-read bytes.
255 scoped_ptr<uint8, VirtualFreeDeleter> buffer(
256 static_cast<uint8*>(
257 ::VirtualAlloc(NULL, max_chunk_size, MEM_COMMIT, PAGE_READWRITE)));
258 if (buffer.get() == NULL)
259 return false;
261 // Iterate over each section, reading in a percentage of each.
262 const IMAGE_SECTION_HEADER* section = NULL;
263 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
264 CHECK_LE(reinterpret_cast<const uint8*>(section + 1),
265 &headers[0] + headers.size());
266 if (!ReadThroughSection(file.Get(), section, percentage, buffer.get(),
267 max_chunk_size)) {
268 return false;
272 // We're done.
273 return true;
276 bool ImagePreReader::PartialPreReadImageInMemory(const wchar_t* file_path,
277 size_t percentage) {
278 // TODO(rogerm): change this to have the number of bytes pre-read per
279 // section be driven by a static table within the PE file (defaulting to
280 // full read if it's not there?) that's initialized by the optimization
281 // toolchain.
282 DCHECK(file_path != NULL);
284 if (percentage == 0)
285 return true;
287 if (percentage > kOneHundredPercent)
288 percentage = kOneHundredPercent;
290 HMODULE dll_module = ::LoadLibraryExW(
291 file_path,
292 NULL,
293 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
295 if (!dll_module)
296 return false;
298 base::win::PEImage pe_image(dll_module);
299 CHECK(pe_image.VerifyMagic());
301 // Iterate over each section, stepping through a percentage of each to page
302 // it in off the disk.
303 const IMAGE_SECTION_HEADER* section = NULL;
304 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
305 // Get the extent we want to touch.
306 size_t length = GetPercentageOfSectionLength(section, percentage);
307 if (length == 0)
308 continue;
309 uint8* start =
310 static_cast<uint8*>(pe_image.RVAToAddr(section->VirtualAddress));
312 // Verify that the extent we're going to touch falls inside the section
313 // we expect it to (and by implication, inside the pe_image).
314 CHECK_EQ(section,
315 pe_image.GetImageSectionFromAddr(start));
316 CHECK_EQ(section,
317 pe_image.GetImageSectionFromAddr(start + length - 1));
319 // Page in the section range.
320 TouchPagesInRange(start, length);
323 FreeLibrary(dll_module);
325 return true;
328 bool ImagePreReader::PreReadImage(const wchar_t* file_path,
329 size_t size_to_read,
330 size_t step_size) {
331 base::ThreadRestrictions::AssertIOAllowed();
332 if (base::win::GetVersion() > base::win::VERSION_XP) {
333 // Vista+ branch. On these OSes, the forced reads through the DLL actually
334 // slows warm starts. The solution is to sequentially read file contents
335 // with an optional cap on total amount to read.
336 base::win::ScopedHandle file(
337 CreateFile(file_path,
338 GENERIC_READ,
339 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
340 NULL,
341 OPEN_EXISTING,
342 FILE_FLAG_SEQUENTIAL_SCAN,
343 NULL));
345 if (!file.IsValid())
346 return false;
348 // Default to 1MB sequential reads.
349 const DWORD actual_step_size = std::max(static_cast<DWORD>(step_size),
350 static_cast<DWORD>(1024*1024));
351 LPVOID buffer = ::VirtualAlloc(NULL,
352 actual_step_size,
353 MEM_COMMIT,
354 PAGE_READWRITE);
356 if (buffer == NULL)
357 return false;
359 DWORD len;
360 size_t total_read = 0;
361 while (::ReadFile(file.Get(), buffer, actual_step_size, &len, NULL) &&
362 len > 0 &&
363 (size_to_read ? total_read < size_to_read : true)) {
364 total_read += static_cast<size_t>(len);
366 ::VirtualFree(buffer, 0, MEM_RELEASE);
367 } else {
368 // WinXP branch. Here, reading the DLL from disk doesn't do
369 // what we want so instead we pull the pages into memory by loading
370 // the DLL and touching pages at a stride. We use the system's page
371 // size as the stride, ignoring the passed in step_size, to make sure
372 // each page in the range is touched.
373 HMODULE dll_module = ::LoadLibraryExW(
374 file_path,
375 NULL,
376 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
378 if (!dll_module)
379 return false;
381 base::win::PEImage pe_image(dll_module);
382 CHECK(pe_image.VerifyMagic());
384 // We don't want to read past the end of the module (which could trigger
385 // an access violation), so make sure to check the image size.
386 PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders();
387 size_t dll_module_length = std::min(
388 size_to_read ? size_to_read : ~0,
389 static_cast<size_t>(nt_headers->OptionalHeader.SizeOfImage));
391 // Page in then release the module.
392 TouchPagesInRange(dll_module, dll_module_length);
393 FreeLibrary(dll_module);
396 return true;
399 bool ImagePreReader::PartialPreReadImage(const wchar_t* file_path,
400 size_t percentage,
401 size_t max_chunk_size) {
402 base::ThreadRestrictions::AssertIOAllowed();
404 if (percentage >= kOneHundredPercent) {
405 // If we're reading the whole image, we don't need to parse headers and
406 // navigate sections, the basic PreReadImage() can be used to just step
407 // blindly through the entire file / address-space.
408 return PreReadImage(file_path, 0, max_chunk_size);
411 if (base::win::GetVersion() > base::win::VERSION_XP) {
412 // Vista+ branch. On these OSes, we warm up the Image by reading its
413 // file off the disk.
414 return PartialPreReadImageOnDisk(file_path, percentage, max_chunk_size);
417 // WinXP branch. For XP, reading the image from disk doesn't do what we want
418 // so instead we pull the pages into memory by loading the DLL and touching
419 // initialized pages at a stride.
420 return PartialPreReadImageInMemory(file_path, percentage);