Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "base/android/library_loader/library_prefetcher.h" | |
| 6 | |
| 7 #include <stdio.h> | |
| 8 #include <sys/resource.h> | |
| 9 #include <sys/wait.h> | |
| 10 #include <unistd.h> | |
| 11 #include <utility> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/logging.h" | |
| 15 #include "base/posix/eintr_wrapper.h" | |
| 16 #include "base/strings/string_number_conversions.h" | |
| 17 #include "base/strings/string_util.h" | |
| 18 | |
| 19 namespace base { | |
| 20 namespace android { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Android defines the background priority to this value since at least 2009 | |
| 25 // (see Process.java). | |
| 26 const int kBackgroundPriority = 10; | |
| 27 // Valid for all the Android architectures. | |
| 28 const size_t kPageSize = 4096; | |
| 29 // We may load directly from the APK. | |
| 30 const int kSuffixesToMatchCount = 2; | |
| 31 const char* kSuffixesToMatch[] = {"libchrome.so", "base.apk"}; | |
| 32 | |
| 33 bool IsReadableAndPrivate(const std::string& flags) { | |
| 34 return flags.size() == 4 && flags[0] == 'r' && flags[3] == 'p'; | |
| 35 } | |
| 36 | |
| 37 bool FilenameMatchesSuffix(const std::string& filename) { | |
| 38 for (int i = 0; i < kSuffixesToMatchCount; i++) { | |
| 39 if (EndsWith(filename, kSuffixesToMatch[i], true)) { | |
| 40 return true; | |
| 41 } | |
| 42 } | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 // For each range, reads a byte per page to force it into the page cache. | |
| 47 void Prefetch(const std::vector<std::pair<uint64_t, uint64_t>>& ranges) { | |
| 48 for (const auto& range : ranges) { | |
| 49 const uint64_t page_mask = kPageSize - 1; | |
| 50 CHECK(!(range.first & page_mask) && !(range.second & page_mask)); | |
| 51 unsigned char* start_address = (unsigned char*)range.first; | |
| 52 unsigned char* end_address = (unsigned char*)range.second; | |
| 53 unsigned char dummy; | |
| 54 for (unsigned char* addr = start_address; addr < end_address; | |
| 55 addr += kPageSize) { | |
| 56 // Volatile is required to prevent the compiler from eliminating this | |
| 57 // loop. | |
| 58 dummy ^= *((volatile unsigned char*)addr); | |
| 59 } | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 // static | |
| 66 bool NativeLibraryPrefetcher::ParseMapping(const std::string& line, | |
| 67 Mapping* mapping) { | |
| 68 // Example: | |
| 69 // b6e62000-b6e64000 r-xp 00000000 b3:15 1204 /system/lib/libstdc++.so | |
| 70 // start-end flags offset device inode [filename] | |
| 71 std::vector<std::string> pieces; | |
| 72 base::SplitStringAlongWhitespace(line, &pieces); | |
| 73 size_t pieces_count = pieces.size(); | |
| 74 // Filename is optional, so at least 5 fields. | |
| 75 if (pieces_count < 5) { | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 std::vector<std::string> start_end; | |
| 80 base::SplitString(pieces[0], '-', &start_end); | |
| 81 if (start_end.size() != 2) { | |
| 82 return false; | |
| 83 } | |
| 84 bool ok = base::HexStringToUInt64(start_end[0], &mapping->start); | |
| 85 ok &= base::HexStringToUInt64(start_end[1], &mapping->end); | |
| 86 mapping->flags = pieces[1]; | |
| 87 // Discards fields 3, 4, and 5 starting from 1 (ie offset, device and inode). | |
| 88 if (!ok) { | |
| 89 return false; | |
| 90 } | |
| 91 if (pieces_count == 5) { | |
| 92 mapping->filename = std::string(); | |
| 93 } else { | |
| 94 // Truncates filenames containing spaces, but these are not the droids | |
| 95 // you're looking for. | |
|
pasko
2015/03/27 17:51:54
still not third person :)
(one can leave it as is
Benoit L
2015/03/30 15:12:49
Acknowledged.
| |
| 96 mapping->filename = pieces[5]; | |
| 97 } | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 // static | |
| 102 bool NativeLibraryPrefetcher::MappingMatches(const Mapping& mapping) { | |
| 103 return FilenameMatchesSuffix(mapping.filename) && | |
| 104 IsReadableAndPrivate(mapping.flags); // Code mappings are private. | |
| 105 } | |
| 106 | |
| 107 // static | |
| 108 bool NativeLibraryPrefetcher::FindRanges( | |
| 109 std::vector<std::pair<uint64_t, uint64_t>>* ranges) { | |
| 110 base::ScopedFILE file(base::OpenFile(base::FilePath("/proc/self/maps"), "r")); | |
| 111 if (!file.get()) { | |
| 112 return false; | |
| 113 } | |
| 114 std::vector<Mapping> mappings; | |
| 115 const size_t kMaxLineLength = 4096; | |
| 116 char line_buffer[kMaxLineLength]; | |
| 117 while (char* line_str = fgets(line_buffer, kMaxLineLength - 1, file.get())) { | |
| 118 std::string line(line_str); | |
| 119 Mapping mapping; | |
| 120 if (!ParseMapping(line, &mapping)) { | |
| 121 continue; | |
| 122 } | |
| 123 if (MappingMatches(mapping)) { | |
| 124 mappings.push_back(mapping); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 // If libchrome.so is mapped, keep only libchrome's ranges. | |
| 129 const std::string& libchrome_suffix = kSuffixesToMatch[0]; | |
| 130 bool has_libchrome_mapping = false; | |
| 131 for (const Mapping& mapping : mappings) { | |
| 132 if (EndsWith(mapping.filename, libchrome_suffix, true)) { | |
| 133 has_libchrome_mapping = true; | |
| 134 break; | |
| 135 } | |
| 136 } | |
| 137 for (const Mapping& mapping : mappings) { | |
| 138 if (has_libchrome_mapping | |
| 139 && !EndsWith(mapping.filename, libchrome_suffix, true)) { | |
| 140 continue; | |
| 141 } | |
| 142 ranges->push_back(std::make_pair(mapping.start, mapping.end)); | |
| 143 } | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 // Forks and waits for a process prefetching the native library. This is done in | |
| 148 // a forked process for the following reasons: | |
| 149 // - Isolating the main process from mistakes in the parsing. If the parsing | |
| 150 // returns an incorrect address, only the forked process will crash. | |
| 151 // - Not inflating the memory used by the main process uselessly, which could | |
| 152 // increase its likelihood to be killed. | |
| 153 // The forked process has background priority and, since it is not declared to | |
| 154 // the android runtime, can be killed at any time, which is not an issue here. | |
| 155 // static | |
| 156 bool NativeLibraryPrefetcher::ForkAndPrefetchNativeLibrary() { | |
| 157 // Looking for ranges is done before the fork, to avoid syscalls and/or | |
| 158 // memory allocations in the forked process, that can be problematic. | |
| 159 std::vector<std::pair<uint64_t, uint64_t>> ranges; | |
| 160 if (!FindRanges(&ranges)) { | |
| 161 return false; | |
| 162 } | |
| 163 pid_t pid = fork(); | |
| 164 if (pid == 0) { | |
| 165 setpriority(PRIO_PROCESS, 0, kBackgroundPriority); | |
| 166 Prefetch(ranges); | |
| 167 _exit(0); // Don't call the atexit() handlers. | |
| 168 } else { | |
| 169 if (pid < 0) { | |
| 170 return false; | |
| 171 } | |
| 172 int status; | |
| 173 const pid_t result = HANDLE_EINTR(waitpid(pid, &status, 0)); | |
| 174 if (result == pid) { | |
| 175 if (WIFEXITED(status)) { | |
| 176 return WEXITSTATUS(status) == 0; | |
| 177 } | |
| 178 } | |
| 179 return false; | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 } // namespace android | |
| 184 } // namespace base | |
| OLD | NEW |