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/file_pre_reader_win.h" |
| 6 |
| 7 #include <windows.h> |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/files/file.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/scoped_native_library.h" |
| 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "base/win/pe_image.h" |
| 15 #include "base/win/windows_version.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 // A helper function to touch all pages in the range |
| 20 // [base_addr, base_addr + length). |
| 21 void TouchPagesInRange(const void* base_addr, uint32_t length) { |
| 22 DCHECK(base_addr); |
| 23 DCHECK_GT(length, static_cast<uint32_t>(0)); |
| 24 |
| 25 // Get the system info so we know the page size. Also, make sure we use a |
| 26 // non-zero value for the page size; GetSystemInfo() is hookable/patchable, |
| 27 // and you never know what shenanigans someone could get up to. |
| 28 SYSTEM_INFO system_info = {}; |
| 29 ::GetSystemInfo(&system_info); |
| 30 if (system_info.dwPageSize == 0) |
| 31 system_info.dwPageSize = 4096; |
| 32 |
| 33 // We don't want to read outside the byte range (which could trigger an |
| 34 // access violation), so let's figure out the exact locations of the first |
| 35 // and final bytes we want to read. |
| 36 volatile uint8_t const* touch_ptr = |
| 37 reinterpret_cast<uint8_t const*>(base_addr); |
| 38 volatile uint8_t const* final_touch_ptr = touch_ptr + length - 1; |
| 39 |
| 40 // Read the memory in the range [touch_ptr, final_touch_ptr] with a stride |
| 41 // of the system page size, to ensure that it's been paged in. |
| 42 uint8_t dummy; |
| 43 for (; touch_ptr < final_touch_ptr; touch_ptr += system_info.dwPageSize) |
| 44 dummy = *touch_ptr; |
| 45 dummy = *final_touch_ptr; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 bool PreReadFile(const base::FilePath& file_path, int step_size) { |
| 51 DCHECK_GT(step_size, 0); |
| 52 base::ThreadRestrictions::AssertIOAllowed(); |
| 53 |
| 54 if (base::win::GetVersion() > base::win::VERSION_XP) { |
| 55 // Vista+ branch. On these OSes, the forced reads through the file actually |
| 56 // slows warm starts. The solution is to sequentially read file contents. |
| 57 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 58 base::File::FLAG_SEQUENTIAL_SCAN); |
| 59 if (!file.IsValid()) |
| 60 return false; |
| 61 |
| 62 char* buffer = reinterpret_cast<char*>(::VirtualAlloc( |
| 63 nullptr, static_cast<DWORD>(step_size), MEM_COMMIT, PAGE_READWRITE)); |
| 64 if (!buffer) |
| 65 return false; |
| 66 |
| 67 while (file.ReadAtCurrentPos(buffer, step_size) > 0) {} |
| 68 |
| 69 ::VirtualFree(buffer, 0, MEM_RELEASE); |
| 70 } else { |
| 71 // WinXP branch. Here, reading the DLL from disk doesn't do what we want so |
| 72 // instead we pull the pages into memory and touch pages at a stride. We use |
| 73 // the system's page size as the stride, ignoring the passed in |step_size|, |
| 74 // to make sure each page in the range is touched. |
| 75 |
| 76 // Don't show an error popup when |file_path| is not a valid PE file. |
| 77 UINT previous_error_mode = ::SetErrorMode(SEM_FAILCRITICALERRORS); |
| 78 ::SetErrorMode(previous_error_mode | SEM_FAILCRITICALERRORS); |
| 79 |
| 80 base::ScopedNativeLibrary dll_module(::LoadLibraryExW( |
| 81 file_path.value().c_str(), NULL, |
| 82 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES)); |
| 83 |
| 84 ::SetErrorMode(previous_error_mode); |
| 85 |
| 86 // Pre-reading non-PE files is not supported on XP. |
| 87 if (!dll_module.is_valid()) |
| 88 return false; |
| 89 |
| 90 base::win::PEImage pe_image(dll_module.get()); |
| 91 if (!pe_image.VerifyMagic()) |
| 92 return false; |
| 93 |
| 94 // We don't want to read past the end of the module (which could trigger |
| 95 // an access violation), so make sure to check the image size. |
| 96 PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders(); |
| 97 const uint32_t dll_module_length = nt_headers->OptionalHeader.SizeOfImage; |
| 98 |
| 99 // Page in the module. |
| 100 TouchPagesInRange(dll_module.get(), dll_module_length); |
| 101 } |
| 102 |
| 103 return true; |
| 104 } |
OLD | NEW |