OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/app/image_pre_reader_win.h" | |
6 | |
7 #include <windows.h> | |
8 | |
chrisha
2012/02/03 16:31:08
No blank line here, as per the coding standard. Ho
Roger McFarlane (Chromium)
2012/02/03 17:45:42
Done.
| |
9 #include <algorithm> | |
10 #include <limits> | |
11 #include <vector> | |
12 | |
13 #include "base/logging.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/threading/thread_restrictions.h" | |
16 #include "base/win/pe_image.h" | |
17 #include "base/win/scoped_handle.h" | |
18 #include "base/win/windows_version.h" | |
19 | |
20 namespace { | |
21 | |
22 // The minimum buffer size to allocate when reading the PE file headers. | |
23 // | |
24 // The PE file headers usually fit into a single 1KB page, and a PE file must | |
25 // at least contain the initial page with the headers. That said, as long as | |
26 // we expect at least sizeof(IMAGE_DOS_HEADER) bytes, we're ok. | |
27 const size_t kMinHeaderBufferSize = 0x400; | |
28 | |
29 // A handy symbolic constant. | |
30 const uint8 kOneHundredPercent = 100; | |
31 | |
32 void StaticAssertions() { | |
33 COMPILE_ASSERT(kMinHeaderBufferSize >= sizeof(IMAGE_DOS_HEADER), | |
34 min_header_buffer_size_at_least_as_big_as_the_dos_header); | |
35 } | |
36 | |
37 // This struct provides a deallocation functor for use with scoped_ptr<T> | |
38 // allocated with ::VirtualAlloc(). | |
39 struct ScopedPtrVirtualFree { | |
40 void operator() (void* ptr) { | |
41 ::VirtualFree(ptr, 0, MEM_RELEASE); | |
42 } | |
43 }; | |
44 | |
45 // A wrapper for the Win32 ::SetFilePointer() function with some error checking. | |
46 bool SetFilePointer(HANDLE file_handle, size_t position) { | |
47 return position <= static_cast<size_t>(std::numeric_limits<LONG>::max()) && | |
48 ::SetFilePointer(file_handle, | |
49 static_cast<LONG>(position), | |
50 NULL, | |
51 FILE_BEGIN) != INVALID_SET_FILE_POINTER; | |
52 } | |
53 | |
54 // A helper function to read the next @p bytes_to_read bytes from the file | |
55 // given by @p file_handle into @p buffer. | |
Sigurður Ásgeirsson
2012/02/03 16:36:39
Chrome does not use doxy comments.
Roger McFarlane (Chromium)
2012/02/03 17:45:42
Done.
| |
56 bool ReadNextBytes(HANDLE file_handle, void* buffer, size_t bytes_to_read) { | |
57 DCHECK(file_handle != INVALID_HANDLE_VALUE); | |
58 DCHECK(buffer != NULL); | |
59 DCHECK(bytes_to_read > 0); | |
60 | |
61 DWORD bytes_read = 0; | |
62 return bytes_to_read <= std::numeric_limits<DWORD>::max() && | |
63 ::ReadFile(file_handle, | |
64 buffer, | |
65 static_cast<DWORD>(bytes_to_read), | |
66 &bytes_read, | |
67 NULL) && | |
68 bytes_read == bytes_to_read; | |
69 } | |
70 | |
71 // A helper function to extend the @p current_buffer of bytes such that it | |
72 // contains @p desired_length bytes read from @p file. | |
73 // @pre It is assumed that file has been used to sequentially populate | |
chrisha
2012/02/03 16:31:08
that THE file
Roger McFarlane (Chromium)
2012/02/03 17:45:42
Done.
| |
74 // @p current_buffer thus far and is already positioned at the | |
chrisha
2012/02/03 16:31:08
Doxy-style comments are not used in Chrome, but ra
Roger McFarlane (Chromium)
2012/02/03 17:45:42
Done.
| |
75 // appropriate read position. | |
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); | |
81 | |
82 size_t current_length = current_buffer->size(); | |
83 if (current_length >= desired_length) | |
84 return true; | |
85 | |
86 size_t bytes_to_read = desired_length - current_length; | |
87 current_buffer->resize(desired_length); | |
88 return ReadNextBytes(file_handle, | |
89 &((*current_buffer)[current_length]), | |
Sigurður Ásgeirsson
2012/02/03 16:36:39
nit: maybe a little less ugly as &(current_buffer-
Roger McFarlane (Chromium)
2012/02/03 17:45:42
Done.
| |
90 bytes_to_read); | |
91 } | |
92 | |
93 // Return a @p percentage of the number of initialized bytes in the given | |
94 // @p section. | |
95 // | |
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. | |
98 // | |
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). | |
103 // | |
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 uint8 percentage) { | |
109 DCHECK(section != NULL); | |
110 DCHECK_GT(percentage, 0); | |
111 DCHECK_LE(percentage, kOneHundredPercent); | |
112 | |
113 size_t initialized_length = std::min(section->SizeOfRawData, | |
114 section->Misc.VirtualSize); | |
115 | |
116 if (initialized_length == 0) | |
117 return 0; | |
118 | |
119 size_t length = (initialized_length * percentage) / kOneHundredPercent; | |
120 | |
121 return std::max<size_t>(length, 1); | |
122 } | |
123 | |
124 // Helper function to read through a @p percentage of the given @p section | |
125 // of the file given by @p file_handle. The @p temp_buffer is (re)used as | |
126 // a transient storage area as the section is read in chunks of | |
127 // @p temp_buffer_size bytes. | |
128 bool ReadThroughSection(HANDLE file_handle, | |
129 const IMAGE_SECTION_HEADER* section, | |
130 uint8 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); | |
138 | |
139 size_t bytes_to_read = GetPercentageOfSectionLength(section, percentage); | |
140 if (bytes_to_read == 0) | |
141 return true; | |
142 | |
143 if (!SetFilePointer(file_handle, section->PointerToRawData)) | |
144 return false; | |
145 | |
146 // Read all chunks except the last one. | |
147 size_t max_chunk_size = temp_buffer_size; | |
Sigurður Ásgeirsson
2012/02/03 16:36:39
why the new variable?
Roger McFarlane (Chromium)
2012/02/03 17:45:42
It seemed more readable at the time.
Removed.
| |
148 while (bytes_to_read > max_chunk_size) { | |
149 if (!ReadNextBytes(file_handle, temp_buffer, max_chunk_size)) | |
150 return false; | |
151 bytes_to_read -= max_chunk_size; | |
152 } | |
153 | |
Sigurður Ásgeirsson
2012/02/03 16:36:39
maybe DCHECK(bytes_to_read <= temp_buffer_size)?
Roger McFarlane (Chromium)
2012/02/03 17:45:42
Done.
Also DCHECK that there are bytes left to re
| |
154 // Read the last (possibly partial) chunk and return. | |
155 return ReadNextBytes(file_handle, temp_buffer, bytes_to_read); | |
156 } | |
157 | |
158 // A helper function to touch all pages in the range | |
159 // [base_addr, base_addr + length). | |
160 void TouchPagesInRange(void* base_addr, size_t length) { | |
161 DCHECK(base_addr != NULL); | |
162 DCHECK(length > 0); | |
163 | |
164 // Get the system info so we know the page size. | |
165 SYSTEM_INFO system_info; | |
Sigurður Ásgeirsson
2012/02/03 16:36:39
nit: initialize at declaration.
Roger McFarlane (Chromium)
2012/02/03 17:45:42
Done.
| |
166 GetSystemInfo(&system_info); | |
167 | |
Sigurður Ásgeirsson
2012/02/03 16:36:39
DCHECK(system_info.dwPageSize != 0) - which would
Roger McFarlane (Chromium)
2012/02/03 17:45:42
Handled as a defensive measure, with an explanator
| |
168 // We don't want to read outside the byte range (which could trigger an | |
169 // access violation), so let's figure out the exact locations of the first | |
170 // and final bytes we want to read. | |
171 volatile uint8* touch_ptr = reinterpret_cast<uint8*>(base_addr); | |
172 volatile uint8* final_touch_ptr = touch_ptr + length - 1; | |
173 | |
174 // Read the memory in the range [touch_ptr, final_touch_ptr] with a stride | |
175 // of the system page size, to ensure that it's been paged in. | |
176 uint8 dummy; | |
177 while (touch_ptr < final_touch_ptr) { | |
178 dummy = *touch_ptr; | |
179 touch_ptr += system_info.dwPageSize; | |
180 } | |
181 dummy = *final_touch_ptr; | |
182 } | |
183 | |
184 } // namespace | |
185 | |
186 bool ImagePreReader::PartialPreReadImageOnDisk(const wchar_t* file_path, | |
187 uint8 percentage, | |
188 size_t max_chunk_size) { | |
189 // TODO(rogerm): change this to have the number of bytes pre-read per | |
190 // section be driven by a static table within the PE file (defaulting to | |
191 // full read if it's not there?) that's initialized by the optimization | |
192 // toolchain. | |
193 DCHECK(file_path != NULL); | |
194 | |
195 if (percentage == 0) | |
196 return true; | |
197 | |
198 if (percentage > kOneHundredPercent) | |
199 percentage = kOneHundredPercent; | |
200 | |
201 // Validate/setup max_chunk_size, imposing a 1MB minimum on the chunk size. | |
202 const size_t kMinChunkSize = 1024 * 1024; | |
203 max_chunk_size = std::max(max_chunk_size, kMinChunkSize); | |
204 | |
205 // Open the file. | |
206 base::win::ScopedHandle file( | |
207 CreateFile(file_path, | |
208 GENERIC_READ, | |
209 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | |
210 NULL, | |
211 OPEN_EXISTING, | |
212 FILE_FLAG_SEQUENTIAL_SCAN, | |
213 NULL)); | |
214 | |
215 if (!file.IsValid()) | |
216 return false; | |
217 | |
218 // Allocate a resizable buffer for the headers. We initially reserve as much | |
219 // space as we typically see as the header size for chrome.dll and other | |
220 // PE images. | |
221 std::vector<uint8> headers; | |
222 headers.reserve(kMinHeaderBufferSize); | |
223 | |
224 // Read, hopefully, all of the headers. | |
225 if (!ReadMissingBytes(file, &headers, kMinHeaderBufferSize)) | |
226 return false; | |
227 | |
228 // The DOS header starts at offset 0 and allows us to get the offset of the | |
229 // NT headers. Let's ensure we've read enough to capture the NT headers. | |
230 size_t nt_headers_start = | |
231 reinterpret_cast<IMAGE_DOS_HEADER*>(&headers[0])->e_lfanew; | |
232 size_t nt_headers_end = nt_headers_start + sizeof(IMAGE_NT_HEADERS); | |
233 if (!ReadMissingBytes(file, &headers, nt_headers_end)) | |
234 return false; | |
235 | |
236 // Now that we've got the NT headers we can get the total header size, | |
237 // including all of the section headers. Let's ensure we've read enough | |
238 // to capture all of the header data. | |
239 size_t size_of_headers = reinterpret_cast<IMAGE_NT_HEADERS*>( | |
240 &headers[nt_headers_start])->OptionalHeader.SizeOfHeaders; | |
241 if (!ReadMissingBytes(file, &headers, size_of_headers)) | |
242 return false; | |
243 | |
244 // Now we have all of the headers. This is enough to let us use the PEImage | |
245 // wrapper to query the structure of the image. | |
246 base::win::PEImage pe_image(reinterpret_cast<HMODULE>(&headers[0])); | |
247 CHECK(pe_image.VerifyMagic()); | |
248 | |
249 // Allocate a buffer to hold the pre-read bytes. | |
250 scoped_ptr_malloc<uint8, ScopedPtrVirtualFree> buffer( | |
251 reinterpret_cast<uint8*>( | |
252 ::VirtualAlloc(NULL, max_chunk_size, MEM_COMMIT, PAGE_READWRITE))); | |
253 if (buffer.get() == NULL) | |
254 return false; | |
255 | |
256 // Iterate over each section, reading in a percentage of each. | |
257 const IMAGE_SECTION_HEADER* section = NULL; | |
258 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) { | |
259 CHECK_LE(reinterpret_cast<const uint8*>(section + 1), | |
260 &headers[0] + headers.size()); | |
261 if (!ReadThroughSection( | |
262 file, section, percentage, buffer.get(), max_chunk_size)) | |
263 return false; | |
264 } | |
265 | |
266 // We're done. | |
267 return true; | |
268 } | |
269 | |
270 bool ImagePreReader::PartialPreReadImageInMemory(const wchar_t* file_path, | |
271 uint8 percentage) { | |
272 // TODO(rogerm): change this to have the number of bytes pre-read per | |
273 // section be driven by a static table within the PE file (defaulting to | |
274 // full read if it's not there?) that's initialized by the optimization | |
275 // toolchain. | |
276 DCHECK(file_path != NULL); | |
277 | |
278 if (percentage == 0) | |
279 return true; | |
280 | |
281 if (percentage > kOneHundredPercent) | |
282 percentage = kOneHundredPercent; | |
283 | |
284 HMODULE dll_module = ::LoadLibraryExW( | |
285 file_path, | |
286 NULL, | |
287 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES); | |
288 | |
289 if (!dll_module) | |
290 return false; | |
291 | |
292 base::win::PEImage pe_image(dll_module); | |
293 CHECK(pe_image.VerifyMagic()); | |
294 | |
295 // Iterate over each section, stepping through a percentage of each to page | |
296 // it in off the disk. | |
297 const IMAGE_SECTION_HEADER* section = NULL; | |
298 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) { | |
299 // Get the extent we want to touch. | |
300 size_t length = GetPercentageOfSectionLength(section, percentage); | |
301 if (length == 0) | |
302 continue; | |
303 uint8* start = | |
304 static_cast<uint8*>(pe_image.RVAToAddr(section->VirtualAddress)); | |
305 | |
306 // Verify that the extent we're going to touch falls inside the section | |
307 // we expect it to (and by implication, inside the pe_image). | |
308 CHECK_EQ(section, | |
309 pe_image.GetImageSectionFromAddr(start)); | |
310 CHECK_EQ(section, | |
311 pe_image.GetImageSectionFromAddr(start + length - 1)); | |
312 | |
313 // Page in the section range. | |
314 TouchPagesInRange(start, length); | |
315 } | |
316 | |
317 FreeLibrary(dll_module); | |
318 | |
319 return true; | |
320 } | |
321 | |
322 bool ImagePreReader::PreReadImage(const wchar_t* file_path, | |
323 size_t size_to_read, | |
Sigurður Ásgeirsson
2012/02/03 16:36:39
nit: indent.
Roger McFarlane (Chromium)
2012/02/03 17:45:42
Oops!
Done... here and elsewhere.
| |
324 size_t step_size) { | |
325 base::ThreadRestrictions::AssertIOAllowed(); | |
326 if (base::win::GetVersion() > base::win::VERSION_XP) { | |
327 // Vista+ branch. On these OSes, the forced reads through the DLL actually | |
328 // slows warm starts. The solution is to sequentially read file contents | |
329 // with an optional cap on total amount to read. | |
330 base::win::ScopedHandle file( | |
331 CreateFile(file_path, | |
332 GENERIC_READ, | |
333 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | |
334 NULL, | |
335 OPEN_EXISTING, | |
336 FILE_FLAG_SEQUENTIAL_SCAN, | |
337 NULL)); | |
338 | |
339 if (!file.IsValid()) | |
340 return false; | |
341 | |
342 // Default to 1MB sequential reads. | |
343 const DWORD actual_step_size = std::max(static_cast<DWORD>(step_size), | |
344 static_cast<DWORD>(1024*1024)); | |
345 LPVOID buffer = ::VirtualAlloc(NULL, | |
346 actual_step_size, | |
347 MEM_COMMIT, | |
348 PAGE_READWRITE); | |
349 | |
350 if (buffer == NULL) | |
351 return false; | |
352 | |
353 DWORD len; | |
354 size_t total_read = 0; | |
355 while (::ReadFile(file, buffer, actual_step_size, &len, NULL) && | |
356 len > 0 && | |
357 (size_to_read ? total_read < size_to_read : true)) { | |
358 total_read += static_cast<size_t>(len); | |
359 } | |
360 ::VirtualFree(buffer, 0, MEM_RELEASE); | |
361 } else { | |
362 // WinXP branch. Here, reading the DLL from disk doesn't do | |
363 // what we want so instead we pull the pages into memory by loading | |
364 // the DLL and touching pages at a stride. We use the system's page | |
365 // size as the stride, ignoring the passed in step_size, to make sure | |
366 // each page in the range is touched. | |
367 HMODULE dll_module = ::LoadLibraryExW( | |
368 file_path, | |
369 NULL, | |
370 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES); | |
371 | |
372 if (!dll_module) | |
373 return false; | |
374 | |
375 base::win::PEImage pe_image(dll_module); | |
376 CHECK(pe_image.VerifyMagic()); | |
377 | |
378 // We don't want to read past the end of the module (which could trigger | |
379 // an access violation), so make sure to check the image size. | |
380 PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders(); | |
381 size_t dll_module_length = std::min( | |
382 size_to_read ? size_to_read : ~0, | |
383 static_cast<size_t>(nt_headers->OptionalHeader.SizeOfImage)); | |
384 | |
385 // Page in then release the module. | |
386 TouchPagesInRange(dll_module, dll_module_length); | |
387 FreeLibrary(dll_module); | |
388 } | |
389 | |
390 return true; | |
391 } | |
392 | |
393 bool ImagePreReader::PartialPreReadImage(const wchar_t* file_path, | |
394 uint8 percentage, | |
395 size_t max_chunk_size) { | |
396 base::ThreadRestrictions::AssertIOAllowed(); | |
397 | |
398 if (percentage >= kOneHundredPercent) { | |
399 // If we're reading the whole image, we don't need to parse headers and | |
400 // navigate sections, the basic PreReadImage() can be used to just step | |
401 // blindly through the entire file / address-space. | |
402 return PreReadImage(file_path, 0, max_chunk_size); | |
403 } | |
404 | |
405 if (base::win::GetVersion() > base::win::VERSION_XP) { | |
406 // Vista+ branch. On these OSes, we warm up the Image by reading its | |
407 // file off the disk. | |
408 return PartialPreReadImageOnDisk(file_path, percentage, max_chunk_size); | |
409 } | |
410 | |
411 // WinXP branch. For XP, reading the image from disk doesn't do what we want | |
412 // so instead we pull the pages into memory by loading the DLL and touching | |
413 // initialized pages at a stride. | |
414 return PartialPreReadImageInMemory(file_path, percentage); | |
415 } | |
OLD | NEW |