Home | History | Annotate | Download | only in app
      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 #include <algorithm>
      9 #include <limits>
     10 #include <vector>
     11 
     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"
     18 
     19 namespace {
     20 
     21 // The minimum buffer size to allocate when reading the PE file headers.
     22 //
     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;
     27 
     28 // A handy symbolic constant.
     29 const uint8 kOneHundredPercent = 100;
     30 
     31 void StaticAssertions() {
     32   COMPILE_ASSERT(kMinHeaderBufferSize >= sizeof(IMAGE_DOS_HEADER),
     33                  min_header_buffer_size_at_least_as_big_as_the_dos_header);
     34 }
     35 
     36 // This struct provides a deallocation functor for use with scoped_ptr<T>
     37 // allocated with ::VirtualAlloc().
     38 struct ScopedPtrVirtualFree {
     39   void operator() (void* ptr) {
     40     ::VirtualFree(ptr, 0, MEM_RELEASE);
     41   }
     42 };
     43 
     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;
     51 }
     52 
     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);
     59 
     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;
     68 }
     69 
     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|.
     72 //
     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);
     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->at(current_length)),
     90                        bytes_to_read);
     91 }
     92 
     93 // Return a |percentage| of the number of initialized bytes in the given
     94 // |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 |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                         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   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;
    151   }
    152 
    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);
    157 }
    158 
    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);
    164 
    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;
    172 
    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;
    178 
    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;
    185   }
    186   dummy = *final_touch_ptr;
    187 }
    188 
    189 }  // namespace
    190 
    191 bool ImagePreReader::PartialPreReadImageOnDisk(const wchar_t* file_path,
    192                                                uint8 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);
    199 
    200   if (percentage == 0)
    201     return true;
    202 
    203   if (percentage > kOneHundredPercent)
    204     percentage = kOneHundredPercent;
    205 
    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);
    209 
    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));
    219 
    220   if (!file.IsValid())
    221     return false;
    222 
    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);
    228 
    229   // Read, hopefully, all of the headers.
    230   if (!ReadMissingBytes(file, &headers, kMinHeaderBufferSize))
    231     return false;
    232 
    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, &headers, nt_headers_end))
    239     return false;
    240 
    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, &headers, size_of_headers))
    247     return false;
    248 
    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());
    253 
    254   // Allocate a buffer to hold the pre-read bytes.
    255   scoped_ptr_malloc<uint8, ScopedPtrVirtualFree> buffer(
    256       reinterpret_cast<uint8*>(
    257           ::VirtualAlloc(NULL, max_chunk_size, MEM_COMMIT, PAGE_READWRITE)));
    258   if (buffer.get() == NULL)
    259     return false;
    260 
    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(
    267             file, section, percentage, buffer.get(), max_chunk_size))
    268       return false;
    269   }
    270 
    271   // We're done.
    272   return true;
    273 }
    274 
    275 bool ImagePreReader::PartialPreReadImageInMemory(const wchar_t* file_path,
    276                                                  uint8 percentage) {
    277   // TODO(rogerm): change this to have the number of bytes pre-read per
    278   //     section be driven by a static table within the PE file (defaulting to
    279   //     full read if it's not there?) that's initialized by the optimization
    280   //     toolchain.
    281   DCHECK(file_path != NULL);
    282 
    283   if (percentage == 0)
    284     return true;
    285 
    286   if (percentage > kOneHundredPercent)
    287     percentage = kOneHundredPercent;
    288 
    289   HMODULE dll_module = ::LoadLibraryExW(
    290       file_path,
    291       NULL,
    292       LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
    293 
    294   if (!dll_module)
    295     return false;
    296 
    297   base::win::PEImage pe_image(dll_module);
    298   CHECK(pe_image.VerifyMagic());
    299 
    300   // Iterate over each section, stepping through a percentage of each to page
    301   // it in off the disk.
    302   const IMAGE_SECTION_HEADER* section = NULL;
    303   for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
    304     // Get the extent we want to touch.
    305     size_t length = GetPercentageOfSectionLength(section, percentage);
    306     if (length == 0)
    307       continue;
    308     uint8* start =
    309         static_cast<uint8*>(pe_image.RVAToAddr(section->VirtualAddress));
    310 
    311     // Verify that the extent we're going to touch falls inside the section
    312     // we expect it to (and by implication, inside the pe_image).
    313     CHECK_EQ(section,
    314              pe_image.GetImageSectionFromAddr(start));
    315     CHECK_EQ(section,
    316              pe_image.GetImageSectionFromAddr(start + length - 1));
    317 
    318     // Page in the section range.
    319     TouchPagesInRange(start, length);
    320   }
    321 
    322   FreeLibrary(dll_module);
    323 
    324   return true;
    325 }
    326 
    327 bool ImagePreReader::PreReadImage(const wchar_t* file_path,
    328                                   size_t size_to_read,
    329                                   size_t step_size) {
    330   base::ThreadRestrictions::AssertIOAllowed();
    331   if (base::win::GetVersion() > base::win::VERSION_XP) {
    332     // Vista+ branch. On these OSes, the forced reads through the DLL actually
    333     // slows warm starts. The solution is to sequentially read file contents
    334     // with an optional cap on total amount to read.
    335     base::win::ScopedHandle file(
    336         CreateFile(file_path,
    337                    GENERIC_READ,
    338                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
    339                    NULL,
    340                    OPEN_EXISTING,
    341                    FILE_FLAG_SEQUENTIAL_SCAN,
    342                    NULL));
    343 
    344     if (!file.IsValid())
    345       return false;
    346 
    347     // Default to 1MB sequential reads.
    348     const DWORD actual_step_size = std::max(static_cast<DWORD>(step_size),
    349                                             static_cast<DWORD>(1024*1024));
    350     LPVOID buffer = ::VirtualAlloc(NULL,
    351                                    actual_step_size,
    352                                    MEM_COMMIT,
    353                                    PAGE_READWRITE);
    354 
    355     if (buffer == NULL)
    356       return false;
    357 
    358     DWORD len;
    359     size_t total_read = 0;
    360     while (::ReadFile(file, buffer, actual_step_size, &len, NULL) &&
    361            len > 0 &&
    362            (size_to_read ? total_read < size_to_read : true)) {
    363       total_read += static_cast<size_t>(len);
    364     }
    365     ::VirtualFree(buffer, 0, MEM_RELEASE);
    366   } else {
    367     // WinXP branch. Here, reading the DLL from disk doesn't do
    368     // what we want so instead we pull the pages into memory by loading
    369     // the DLL and touching pages at a stride. We use the system's page
    370     // size as the stride, ignoring the passed in step_size, to make sure
    371     // each page in the range is touched.
    372     HMODULE dll_module = ::LoadLibraryExW(
    373         file_path,
    374         NULL,
    375         LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
    376 
    377     if (!dll_module)
    378       return false;
    379 
    380     base::win::PEImage pe_image(dll_module);
    381     CHECK(pe_image.VerifyMagic());
    382 
    383     // We don't want to read past the end of the module (which could trigger
    384     // an access violation), so make sure to check the image size.
    385     PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders();
    386     size_t dll_module_length = std::min(
    387         size_to_read ? size_to_read : ~0,
    388         static_cast<size_t>(nt_headers->OptionalHeader.SizeOfImage));
    389 
    390     // Page in then release the module.
    391     TouchPagesInRange(dll_module, dll_module_length);
    392     FreeLibrary(dll_module);
    393   }
    394 
    395   return true;
    396 }
    397 
    398 bool ImagePreReader::PartialPreReadImage(const wchar_t* file_path,
    399                                          uint8 percentage,
    400                                          size_t max_chunk_size) {
    401   base::ThreadRestrictions::AssertIOAllowed();
    402 
    403   if (percentage >= kOneHundredPercent) {
    404     // If we're reading the whole image, we don't need to parse headers and
    405     // navigate sections, the basic PreReadImage() can be used to just step
    406     // blindly through the entire file / address-space.
    407     return PreReadImage(file_path, 0, max_chunk_size);
    408   }
    409 
    410   if (base::win::GetVersion() > base::win::VERSION_XP) {
    411     // Vista+ branch. On these OSes, we warm up the Image by reading its
    412     // file off the disk.
    413     return PartialPreReadImageOnDisk(file_path, percentage, max_chunk_size);
    414   }
    415 
    416   // WinXP branch. For XP, reading the image from disk doesn't do what we want
    417   // so instead we pull the pages into memory by loading the DLL and touching
    418   // initialized pages at a stride.
    419   return PartialPreReadImageInMemory(file_path, percentage);
    420 }
    421