Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(729)

Side by Side Diff: chrome/app/file_pre_reader_win.cc

Issue 1432973004: Transform ImagePreReader into PreReadFile (v2). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Initial implementation, with grt comments addressed. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/app/file_pre_reader_win.h ('k') | chrome/app/image_pre_reader_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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<size_t>(0));
grt (UTC plus 2) 2015/11/10 20:29:22 nit: size_t -> uint32_t
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 }
OLDNEW
« no previous file with comments | « chrome/app/file_pre_reader_win.h ('k') | chrome/app/image_pre_reader_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698