Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "patchoat.h" |
| 17 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 18 | #include <openssl/sha.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 21 | #include <sys/file.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 24 | |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 28 | #include "android-base/file.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 29 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 30 | #include "android-base/strings.h" |
| 31 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 32 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 33 | #include "art_method-inl.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 34 | #include "base/dumpable.h" |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 35 | #include "base/file_utils.h" |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 36 | #include "base/logging.h" // For InitLogging. |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 37 | #include "base/memory_tool.h" |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 38 | #include "base/scoped_flock.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 39 | #include "base/stringpiece.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 40 | #include "base/unix_file/fd_file.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 41 | #include "base/unix_file/random_access_file_utils.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 42 | #include "elf_file.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 43 | #include "elf_file_impl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 44 | #include "elf_utils.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 45 | #include "gc/space/image_space.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 46 | #include "image-inl.h" |
Andreas Gampe | b2d18fa | 2017-06-06 20:46:10 -0700 | [diff] [blame] | 47 | #include "intern_table.h" |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 48 | #include "leb128.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 49 | #include "mirror/dex_cache.h" |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 50 | #include "mirror/executable.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 51 | #include "mirror/method.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 52 | #include "mirror/object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 53 | #include "mirror/object-refvisitor-inl.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 54 | #include "mirror/reference.h" |
| 55 | #include "noop_compiler_callbacks.h" |
| 56 | #include "offsets.h" |
| 57 | #include "os.h" |
| 58 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 59 | #include "scoped_thread_state_change-inl.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 60 | #include "thread.h" |
| 61 | #include "utils.h" |
| 62 | |
| 63 | namespace art { |
| 64 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 65 | using android::base::StringPrintf; |
| 66 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 67 | static const OatHeader* GetOatHeader(const ElfFile* elf_file) { |
| 68 | uint64_t off = 0; |
| 69 | if (!elf_file->GetSectionOffsetAndSize(".rodata", &off, nullptr)) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | |
| 73 | OatHeader* oat_header = reinterpret_cast<OatHeader*>(elf_file->Begin() + off); |
| 74 | return oat_header; |
| 75 | } |
| 76 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 77 | static File* CreateOrOpen(const char* name) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 78 | if (OS::FileExists(name)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 79 | return OS::OpenFileReadWrite(name); |
| 80 | } else { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 81 | std::unique_ptr<File> f(OS::CreateEmptyFile(name)); |
| 82 | if (f.get() != nullptr) { |
| 83 | if (fchmod(f->Fd(), 0644) != 0) { |
| 84 | PLOG(ERROR) << "Unable to make " << name << " world readable"; |
Dimitry Ivanov | 7a1c014 | 2016-03-17 15:59:38 -0700 | [diff] [blame] | 85 | unlink(name); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 86 | return nullptr; |
| 87 | } |
| 88 | } |
| 89 | return f.release(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Either try to close the file (close=true), or erase it. |
| 94 | static bool FinishFile(File* file, bool close) { |
| 95 | if (close) { |
| 96 | if (file->FlushCloseOrErase() != 0) { |
| 97 | PLOG(ERROR) << "Failed to flush and close file."; |
| 98 | return false; |
| 99 | } |
| 100 | return true; |
| 101 | } else { |
| 102 | file->Erase(); |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 107 | static bool SymlinkFile(const std::string& input_filename, const std::string& output_filename) { |
| 108 | if (input_filename == output_filename) { |
| 109 | // Input and output are the same, nothing to do. |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | // Unlink the original filename, since we are overwriting it. |
| 114 | unlink(output_filename.c_str()); |
| 115 | |
| 116 | // Create a symlink from the source file to the target path. |
| 117 | if (symlink(input_filename.c_str(), output_filename.c_str()) < 0) { |
| 118 | PLOG(ERROR) << "Failed to create symlink " << output_filename << " -> " << input_filename; |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | if (kIsDebugBuild) { |
| 123 | LOG(INFO) << "Created symlink " << output_filename << " -> " << input_filename; |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 129 | bool PatchOat::GeneratePatch( |
| 130 | const MemMap& original, |
| 131 | const MemMap& relocated, |
| 132 | std::vector<uint8_t>* output, |
| 133 | std::string* error_msg) { |
| 134 | // FORMAT of the patch (aka image relocation) file: |
| 135 | // * SHA-256 digest (32 bytes) of original/unrelocated file (e.g., the one from /system) |
| 136 | // * List of monotonically increasing offsets (max value defined by uint32_t) at which relocations |
| 137 | // occur. |
| 138 | // Each element is represented as the delta from the previous offset in the list (first element |
| 139 | // is a delta from 0). Each delta is encoded using unsigned LEB128: little-endian |
| 140 | // variable-length 7 bits per byte encoding, where all bytes have the highest bit (0x80) set |
| 141 | // except for the final byte which does not have that bit set. For example, 0x3f is offset 0x3f, |
| 142 | // whereas 0xbf 0x05 is offset (0x3f & 0x7f) | (0x5 << 7) which is 0x2bf. Most deltas end up |
| 143 | // being encoding using just one byte, achieving ~4x decrease in relocation file size compared |
| 144 | // to the encoding where offsets are stored verbatim, as uint32_t. |
| 145 | |
| 146 | size_t original_size = original.Size(); |
| 147 | size_t relocated_size = relocated.Size(); |
| 148 | if (original_size != relocated_size) { |
| 149 | *error_msg = |
| 150 | StringPrintf( |
| 151 | "Original and relocated image sizes differ: %zu vs %zu", original_size, relocated_size); |
| 152 | return false; |
| 153 | } |
| 154 | if ((original_size % 4) != 0) { |
| 155 | *error_msg = StringPrintf("Image size not multiple of 4: %zu", original_size); |
| 156 | return false; |
| 157 | } |
| 158 | if (original_size > UINT32_MAX) { |
| 159 | *error_msg = StringPrintf("Image too large: %zu" , original_size); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | const ImageHeader& relocated_header = |
| 164 | *reinterpret_cast<const ImageHeader*>(relocated.Begin()); |
| 165 | // Offsets are supposed to differ between original and relocated by this value |
| 166 | off_t expected_diff = relocated_header.GetPatchDelta(); |
| 167 | if (expected_diff == 0) { |
| 168 | // Can't identify offsets which are supposed to differ due to relocation |
| 169 | *error_msg = "Relocation delta is 0"; |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // Output the SHA-256 digest of the original |
| 174 | output->resize(SHA256_DIGEST_LENGTH); |
| 175 | const uint8_t* original_bytes = original.Begin(); |
| 176 | SHA256(original_bytes, original_size, output->data()); |
| 177 | |
| 178 | // Output the list of offsets at which the original and patched images differ |
| 179 | size_t last_diff_offset = 0; |
| 180 | size_t diff_offset_count = 0; |
| 181 | const uint8_t* relocated_bytes = relocated.Begin(); |
| 182 | for (size_t offset = 0; offset < original_size; offset += 4) { |
| 183 | uint32_t original_value = *reinterpret_cast<const uint32_t*>(original_bytes + offset); |
| 184 | uint32_t relocated_value = *reinterpret_cast<const uint32_t*>(relocated_bytes + offset); |
| 185 | off_t diff = relocated_value - original_value; |
| 186 | if (diff == 0) { |
| 187 | continue; |
| 188 | } else if (diff != expected_diff) { |
| 189 | *error_msg = |
| 190 | StringPrintf( |
| 191 | "Unexpected diff at offset %zu. Expected: %jd, but was: %jd", |
| 192 | offset, |
| 193 | (intmax_t) expected_diff, |
| 194 | (intmax_t) diff); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | uint32_t offset_diff = offset - last_diff_offset; |
| 199 | last_diff_offset = offset; |
| 200 | diff_offset_count++; |
| 201 | |
| 202 | EncodeUnsignedLeb128(output, offset_diff); |
| 203 | } |
| 204 | |
| 205 | if (diff_offset_count == 0) { |
| 206 | *error_msg = "Original and patched images are identical"; |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | static bool WriteRelFile( |
| 214 | const MemMap& original, |
| 215 | const MemMap& relocated, |
| 216 | const std::string& rel_filename, |
| 217 | std::string* error_msg) { |
| 218 | std::vector<uint8_t> output; |
| 219 | if (!PatchOat::GeneratePatch(original, relocated, &output, error_msg)) { |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | std::unique_ptr<File> rel_file(OS::CreateEmptyFileWriteOnly(rel_filename.c_str())); |
| 224 | if (rel_file.get() == nullptr) { |
| 225 | *error_msg = StringPrintf("Failed to create/open output file %s", rel_filename.c_str()); |
| 226 | return false; |
| 227 | } |
| 228 | if (!rel_file->WriteFully(output.data(), output.size())) { |
| 229 | *error_msg = StringPrintf("Failed to write to %s", rel_filename.c_str()); |
| 230 | return false; |
| 231 | } |
| 232 | if (rel_file->FlushCloseOrErase() != 0) { |
| 233 | *error_msg = StringPrintf("Failed to flush and close %s", rel_filename.c_str()); |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | return true; |
| 238 | } |
| 239 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 240 | static bool CheckImageIdenticalToOriginalExceptForRelocation( |
| 241 | const std::string& relocated_filename, |
| 242 | const std::string& original_filename, |
| 243 | std::string* error_msg) { |
| 244 | *error_msg = ""; |
| 245 | std::string rel_filename = original_filename + ".rel"; |
| 246 | std::unique_ptr<File> rel_file(OS::OpenFileForReading(rel_filename.c_str())); |
| 247 | if (rel_file.get() == nullptr) { |
| 248 | *error_msg = StringPrintf("Failed to open image relocation file %s", rel_filename.c_str()); |
| 249 | return false; |
| 250 | } |
| 251 | int64_t rel_size = rel_file->GetLength(); |
| 252 | if (rel_size < 0) { |
| 253 | *error_msg = StringPrintf("Error while getting size of image relocation file %s", |
| 254 | rel_filename.c_str()); |
| 255 | return false; |
| 256 | } |
| 257 | std::unique_ptr<uint8_t[]> rel(new uint8_t[rel_size]); |
| 258 | if (!rel_file->ReadFully(rel.get(), rel_size)) { |
| 259 | *error_msg = StringPrintf("Failed to read image relocation file %s", rel_filename.c_str()); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | std::unique_ptr<File> image_file(OS::OpenFileForReading(relocated_filename.c_str())); |
| 264 | if (image_file.get() == nullptr) { |
| 265 | *error_msg = StringPrintf("Unable to open relocated image file %s", |
| 266 | relocated_filename.c_str()); |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | int64_t image_size = image_file->GetLength(); |
| 271 | if (image_size < 0) { |
| 272 | *error_msg = StringPrintf("Error while getting size of relocated image file %s", |
| 273 | relocated_filename.c_str()); |
| 274 | return false; |
| 275 | } |
| 276 | if ((image_size % 4) != 0) { |
| 277 | *error_msg = |
| 278 | StringPrintf( |
Orion Hodson | b348b3b | 2018-01-26 09:02:49 +0000 | [diff] [blame] | 279 | "Relocated image file %s size not multiple of 4: %" PRId64, |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 280 | relocated_filename.c_str(), image_size); |
| 281 | return false; |
| 282 | } |
Orion Hodson | b348b3b | 2018-01-26 09:02:49 +0000 | [diff] [blame] | 283 | if (image_size > std::numeric_limits<uint32_t>::max()) { |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 284 | *error_msg = |
| 285 | StringPrintf( |
Orion Hodson | b348b3b | 2018-01-26 09:02:49 +0000 | [diff] [blame] | 286 | "Relocated image file %s too large: %" PRId64, relocated_filename.c_str(), image_size); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 287 | return false; |
| 288 | } |
| 289 | |
| 290 | std::unique_ptr<uint8_t[]> image(new uint8_t[image_size]); |
| 291 | if (!image_file->ReadFully(image.get(), image_size)) { |
| 292 | *error_msg = StringPrintf("Failed to read relocated image file %s", relocated_filename.c_str()); |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | const uint8_t* original_image_digest = rel.get(); |
| 297 | if (rel_size < SHA256_DIGEST_LENGTH) { |
| 298 | *error_msg = StringPrintf("Malformed image relocation file %s: too short", |
| 299 | rel_filename.c_str()); |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | const ImageHeader& image_header = *reinterpret_cast<const ImageHeader*>(image.get()); |
| 304 | off_t expected_diff = image_header.GetPatchDelta(); |
| 305 | |
| 306 | if (expected_diff == 0) { |
| 307 | *error_msg = StringPrintf("Unsuported patch delta of zero in %s", |
| 308 | relocated_filename.c_str()); |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | // Relocated image is expected to differ from the original due to relocation. |
| 313 | // Unrelocate the image in memory to compensate. |
| 314 | uint8_t* image_start = image.get(); |
| 315 | const uint8_t* rel_end = &rel[rel_size]; |
| 316 | const uint8_t* rel_ptr = &rel[SHA256_DIGEST_LENGTH]; |
| 317 | // The remaining .rel file consists of offsets at which relocation should've occurred. |
| 318 | // For each offset, we "unrelocate" the image by subtracting the expected relocation |
| 319 | // diff value (as specified in the image header). |
| 320 | // |
| 321 | // Each offset is encoded as a delta/diff relative to the previous offset. With the |
| 322 | // very first offset being encoded relative to offset 0. |
| 323 | // Deltas are encoded using little-endian 7 bits per byte encoding, with all bytes except |
| 324 | // the last one having the highest bit set. |
| 325 | uint32_t offset = 0; |
| 326 | while (rel_ptr != rel_end) { |
| 327 | uint32_t offset_delta = 0; |
| 328 | if (DecodeUnsignedLeb128Checked(&rel_ptr, rel_end, &offset_delta)) { |
| 329 | offset += offset_delta; |
| 330 | uint32_t *image_value = reinterpret_cast<uint32_t*>(image_start + offset); |
| 331 | *image_value -= expected_diff; |
| 332 | } else { |
| 333 | *error_msg = |
| 334 | StringPrintf( |
| 335 | "Malformed image relocation file %s: " |
| 336 | "last byte has it's most significant bit set", |
| 337 | rel_filename.c_str()); |
| 338 | return false; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // Image in memory is now supposed to be identical to the original. We |
| 343 | // confirm this by comparing the digest of the in-memory image to the expected |
| 344 | // digest from relocation file. |
| 345 | uint8_t image_digest[SHA256_DIGEST_LENGTH]; |
| 346 | SHA256(image.get(), image_size, image_digest); |
| 347 | if (memcmp(image_digest, original_image_digest, SHA256_DIGEST_LENGTH) != 0) { |
| 348 | *error_msg = |
| 349 | StringPrintf( |
| 350 | "Relocated image %s does not match the original %s after unrelocation", |
| 351 | relocated_filename.c_str(), |
| 352 | original_filename.c_str()); |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | // Relocated image is identical to the original, once relocations are taken into account |
| 357 | return true; |
| 358 | } |
| 359 | |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 360 | static bool VerifySymlink(const std::string& intended_target, const std::string& link_name) { |
| 361 | std::string actual_target; |
| 362 | if (!android::base::Readlink(link_name, &actual_target)) { |
| 363 | PLOG(ERROR) << "Readlink on " << link_name << " failed."; |
| 364 | return false; |
| 365 | } |
| 366 | return actual_target == intended_target; |
| 367 | } |
| 368 | |
| 369 | static bool VerifyVdexAndOatSymlinks(const std::string& input_image_filename, |
| 370 | const std::string& output_image_filename) { |
| 371 | return VerifySymlink(ImageHeader::GetVdexLocationFromImageLocation(input_image_filename), |
| 372 | ImageHeader::GetVdexLocationFromImageLocation(output_image_filename)) |
| 373 | && VerifySymlink(ImageHeader::GetOatLocationFromImageLocation(input_image_filename), |
| 374 | ImageHeader::GetOatLocationFromImageLocation(output_image_filename)); |
| 375 | } |
| 376 | |
| 377 | bool PatchOat::CreateVdexAndOatSymlinks(const std::string& input_image_filename, |
| 378 | const std::string& output_image_filename) { |
| 379 | std::string input_vdex_filename = |
| 380 | ImageHeader::GetVdexLocationFromImageLocation(input_image_filename); |
| 381 | std::string input_oat_filename = |
| 382 | ImageHeader::GetOatLocationFromImageLocation(input_image_filename); |
| 383 | |
| 384 | std::unique_ptr<File> input_oat_file(OS::OpenFileForReading(input_oat_filename.c_str())); |
| 385 | if (input_oat_file.get() == nullptr) { |
| 386 | LOG(ERROR) << "Unable to open input oat file at " << input_oat_filename; |
| 387 | return false; |
| 388 | } |
| 389 | std::string error_msg; |
| 390 | std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat_file.get(), |
| 391 | PROT_READ | PROT_WRITE, |
| 392 | MAP_PRIVATE, |
| 393 | &error_msg)); |
| 394 | if (elf.get() == nullptr) { |
| 395 | LOG(ERROR) << "Unable to open oat file " << input_oat_filename << " : " << error_msg; |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | MaybePic is_oat_pic = IsOatPic(elf.get()); |
| 400 | if (is_oat_pic >= ERROR_FIRST) { |
| 401 | // Error logged by IsOatPic |
| 402 | return false; |
| 403 | } else if (is_oat_pic == NOT_PIC) { |
| 404 | LOG(ERROR) << "patchoat cannot be used on non-PIC oat file: " << input_oat_filename; |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | CHECK(is_oat_pic == PIC); |
| 409 | |
| 410 | std::string output_vdex_filename = |
| 411 | ImageHeader::GetVdexLocationFromImageLocation(output_image_filename); |
| 412 | std::string output_oat_filename = |
| 413 | ImageHeader::GetOatLocationFromImageLocation(output_image_filename); |
| 414 | |
| 415 | return SymlinkFile(input_oat_filename, output_oat_filename) && |
| 416 | SymlinkFile(input_vdex_filename, output_vdex_filename); |
| 417 | } |
| 418 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 419 | bool PatchOat::Patch(const std::string& image_location, |
| 420 | off_t delta, |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 421 | const std::string& output_image_directory, |
| 422 | const std::string& output_image_relocation_directory, |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 423 | InstructionSet isa, |
| 424 | TimingLogger* timings) { |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 425 | bool output_image = !output_image_directory.empty(); |
| 426 | bool output_image_relocation = !output_image_relocation_directory.empty(); |
| 427 | if ((!output_image) && (!output_image_relocation)) { |
| 428 | // Nothing to do |
| 429 | return true; |
| 430 | } |
| 431 | if ((output_image_relocation) && (delta == 0)) { |
| 432 | LOG(ERROR) << "Cannot output image relocation information when requested relocation delta is 0"; |
| 433 | return false; |
| 434 | } |
| 435 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 436 | CHECK(Runtime::Current() == nullptr); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 437 | CHECK(!image_location.empty()) << "image file must have a filename."; |
| 438 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 439 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 440 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 441 | CHECK_NE(isa, InstructionSet::kNone); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 442 | const char* isa_name = GetInstructionSetString(isa); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 443 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 444 | // Set up the runtime |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 445 | RuntimeOptions options; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 446 | NoopCompilerCallbacks callbacks; |
| 447 | options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
| 448 | std::string img = "-Ximage:" + image_location; |
| 449 | options.push_back(std::make_pair(img.c_str(), nullptr)); |
| 450 | options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name))); |
Calin Juravle | 01aaf6e | 2015-06-19 22:05:39 +0100 | [diff] [blame] | 451 | options.push_back(std::make_pair("-Xno-sig-chain", nullptr)); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 452 | if (!Runtime::Create(options, false)) { |
| 453 | LOG(ERROR) << "Unable to initialize runtime"; |
| 454 | return false; |
| 455 | } |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 456 | std::unique_ptr<Runtime> runtime(Runtime::Current()); |
| 457 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 458 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 459 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 460 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 461 | ScopedObjectAccess soa(Thread::Current()); |
| 462 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 463 | std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 464 | std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>> space_to_memmap_map; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 465 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 466 | for (size_t i = 0; i < spaces.size(); ++i) { |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 467 | t.NewTiming("Image Patching setup"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 468 | gc::space::ImageSpace* space = spaces[i]; |
| 469 | std::string input_image_filename = space->GetImageFilename(); |
| 470 | std::unique_ptr<File> input_image(OS::OpenFileForReading(input_image_filename.c_str())); |
| 471 | if (input_image.get() == nullptr) { |
| 472 | LOG(ERROR) << "Unable to open input image file at " << input_image_filename; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 473 | return false; |
| 474 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 475 | |
| 476 | int64_t image_len = input_image->GetLength(); |
| 477 | if (image_len < 0) { |
| 478 | LOG(ERROR) << "Error while getting image length"; |
| 479 | return false; |
| 480 | } |
| 481 | ImageHeader image_header; |
| 482 | if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header), |
| 483 | sizeof(image_header), 0)) { |
| 484 | LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath(); |
| 485 | } |
| 486 | |
| 487 | /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath()); |
| 488 | // Nothing special to do right now since the image always needs to get patched. |
| 489 | // Perhaps in some far-off future we may have images with relative addresses that are true-PIC. |
| 490 | |
| 491 | // Create the map where we will write the image patches to. |
| 492 | std::string error_msg; |
| 493 | std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, |
| 494 | PROT_READ | PROT_WRITE, |
| 495 | MAP_PRIVATE, |
| 496 | input_image->Fd(), |
| 497 | 0, |
| 498 | /*low_4gb*/false, |
| 499 | input_image->GetPath().c_str(), |
| 500 | &error_msg)); |
| 501 | if (image.get() == nullptr) { |
| 502 | LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg; |
| 503 | return false; |
| 504 | } |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 505 | |
| 506 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 507 | space_to_memmap_map.emplace(space, std::move(image)); |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 508 | PatchOat p = PatchOat(isa, |
| 509 | space_to_memmap_map.at(space).get(), |
| 510 | space->GetLiveBitmap(), |
| 511 | space->GetMemMap(), |
| 512 | delta, |
| 513 | &space_to_memmap_map, |
| 514 | timings); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 515 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 516 | t.NewTiming("Patching image"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 517 | if (!p.PatchImage(i == 0)) { |
| 518 | LOG(ERROR) << "Failed to patch image file " << input_image_filename; |
| 519 | return false; |
| 520 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 521 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 522 | // Write the patched image spaces. |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 523 | if (output_image) { |
| 524 | std::string output_image_filename; |
| 525 | if (!GetDalvikCacheFilename(space->GetImageLocation().c_str(), |
| 526 | output_image_directory.c_str(), |
| 527 | &output_image_filename, |
| 528 | &error_msg)) { |
| 529 | LOG(ERROR) << "Failed to find relocated image file name: " << error_msg; |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | if (!CreateVdexAndOatSymlinks(input_image_filename, output_image_filename)) |
| 534 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 535 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 536 | t.NewTiming("Writing image"); |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 537 | std::unique_ptr<File> output_image_file(CreateOrOpen(output_image_filename.c_str())); |
| 538 | if (output_image_file.get() == nullptr) { |
| 539 | LOG(ERROR) << "Failed to open output image file at " << output_image_filename; |
| 540 | return false; |
| 541 | } |
| 542 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 543 | bool success = p.WriteImage(output_image_file.get()); |
| 544 | success = FinishFile(output_image_file.get(), success); |
| 545 | if (!success) { |
| 546 | return false; |
| 547 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 550 | if (output_image_relocation) { |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 551 | t.NewTiming("Writing image relocation"); |
| 552 | std::string original_image_filename(space->GetImageLocation() + ".rel"); |
| 553 | std::string image_relocation_filename = |
| 554 | output_image_relocation_directory |
| 555 | + (android::base::StartsWith(original_image_filename, "/") ? "" : "/") |
| 556 | + original_image_filename.substr(original_image_filename.find_last_of("/")); |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 557 | int64_t input_image_size = input_image->GetLength(); |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 558 | if (input_image_size < 0) { |
| 559 | LOG(ERROR) << "Error while getting input image size"; |
| 560 | return false; |
| 561 | } |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 562 | std::unique_ptr<MemMap> original(MemMap::MapFile(input_image_size, |
| 563 | PROT_READ, |
| 564 | MAP_PRIVATE, |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 565 | input_image->Fd(), |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 566 | 0, |
| 567 | /*low_4gb*/false, |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 568 | input_image->GetPath().c_str(), |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 569 | &error_msg)); |
| 570 | if (original.get() == nullptr) { |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 571 | LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg; |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 572 | return false; |
| 573 | } |
| 574 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 575 | const MemMap* relocated = p.image_; |
| 576 | |
| 577 | if (!WriteRelFile(*original, *relocated, image_relocation_filename, &error_msg)) { |
| 578 | LOG(ERROR) << "Failed to create image relocation file " << image_relocation_filename |
| 579 | << ": " << error_msg; |
| 580 | return false; |
| 581 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 582 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 583 | } |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 584 | |
| 585 | if (!kIsDebugBuild && !(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
| 586 | // We want to just exit on non-debug builds, not bringing the runtime down |
| 587 | // in an orderly fashion. So release the following fields. |
| 588 | runtime.release(); |
| 589 | } |
| 590 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 591 | return true; |
| 592 | } |
| 593 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 594 | bool PatchOat::Verify(const std::string& image_location, |
| 595 | const std::string& output_image_directory, |
| 596 | InstructionSet isa, |
| 597 | TimingLogger* timings) { |
| 598 | if (image_location.empty()) { |
| 599 | LOG(ERROR) << "Original image file not provided"; |
| 600 | return false; |
| 601 | } |
| 602 | if (output_image_directory.empty()) { |
| 603 | LOG(ERROR) << "Relocated image directory not provided"; |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
| 608 | |
| 609 | CHECK_NE(isa, InstructionSet::kNone); |
| 610 | const char* isa_name = GetInstructionSetString(isa); |
| 611 | |
| 612 | // Set up the runtime |
| 613 | RuntimeOptions options; |
| 614 | NoopCompilerCallbacks callbacks; |
| 615 | options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
| 616 | std::string img = "-Ximage:" + image_location; |
| 617 | options.push_back(std::make_pair(img.c_str(), nullptr)); |
| 618 | options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name))); |
| 619 | options.push_back(std::make_pair("-Xno-sig-chain", nullptr)); |
| 620 | if (!Runtime::Create(options, false)) { |
| 621 | LOG(ERROR) << "Unable to initialize runtime"; |
| 622 | return false; |
| 623 | } |
| 624 | std::unique_ptr<Runtime> runtime(Runtime::Current()); |
| 625 | |
| 626 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 627 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 628 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 629 | ScopedObjectAccess soa(Thread::Current()); |
| 630 | |
| 631 | t.NewTiming("Image Verification setup"); |
| 632 | std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 633 | |
| 634 | // TODO: Check that no other .rel files exist in the original dir |
| 635 | |
| 636 | bool success = true; |
| 637 | std::string image_location_dir = android::base::Dirname(image_location); |
| 638 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 639 | gc::space::ImageSpace* space = spaces[i]; |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 640 | |
| 641 | std::string relocated_image_filename; |
| 642 | std::string error_msg; |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 643 | if (!GetDalvikCacheFilename(space->GetImageLocation().c_str(), |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 644 | output_image_directory.c_str(), &relocated_image_filename, &error_msg)) { |
| 645 | LOG(ERROR) << "Failed to find relocated image file name: " << error_msg; |
| 646 | success = false; |
| 647 | break; |
| 648 | } |
| 649 | // location: /system/framework/boot.art |
| 650 | // isa: arm64 |
| 651 | // basename: boot.art |
| 652 | // original: /system/framework/arm64/boot.art |
| 653 | // relocation: /system/framework/arm64/boot.art.rel |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 654 | std::string original_image_filename = |
| 655 | GetSystemImageFilename(space->GetImageLocation().c_str(), isa); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 656 | |
| 657 | if (!CheckImageIdenticalToOriginalExceptForRelocation( |
| 658 | relocated_image_filename, original_image_filename, &error_msg)) { |
| 659 | LOG(ERROR) << error_msg; |
| 660 | success = false; |
| 661 | break; |
| 662 | } |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 663 | |
| 664 | if (!VerifyVdexAndOatSymlinks(original_image_filename, relocated_image_filename)) { |
| 665 | LOG(ERROR) << "Verification of vdex and oat symlinks for " |
| 666 | << space->GetImageLocation() << " failed."; |
| 667 | success = false; |
| 668 | break; |
| 669 | } |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | if (!kIsDebugBuild && !(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
| 673 | // We want to just exit on non-debug builds, not bringing the runtime down |
| 674 | // in an orderly fashion. So release the following fields. |
| 675 | runtime.release(); |
| 676 | } |
| 677 | |
| 678 | return success; |
| 679 | } |
| 680 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 681 | bool PatchOat::WriteImage(File* out) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 682 | TimingLogger::ScopedTiming t("Writing image File", timings_); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 683 | std::string error_msg; |
| 684 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 685 | // No error checking here, this is best effort. The locking may or may not |
| 686 | // succeed and we don't really care either way. |
| 687 | ScopedFlock img_flock = LockedFile::DupOf(out->Fd(), out->GetPath(), |
| 688 | true /* read_only_mode */, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 689 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 690 | CHECK(image_ != nullptr); |
| 691 | CHECK(out != nullptr); |
| 692 | size_t expect = image_->Size(); |
| 693 | if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) && |
| 694 | out->SetLength(expect) == 0) { |
| 695 | return true; |
| 696 | } else { |
| 697 | LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed."; |
| 698 | return false; |
| 699 | } |
| 700 | } |
| 701 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 702 | bool PatchOat::IsImagePic(const ImageHeader& image_header, const std::string& image_path) { |
| 703 | if (!image_header.CompilePic()) { |
| 704 | if (kIsDebugBuild) { |
| 705 | LOG(INFO) << "image at location " << image_path << " was *not* compiled pic"; |
| 706 | } |
| 707 | return false; |
| 708 | } |
| 709 | |
| 710 | if (kIsDebugBuild) { |
| 711 | LOG(INFO) << "image at location " << image_path << " was compiled PIC"; |
| 712 | } |
| 713 | |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) { |
| 718 | if (oat_in == nullptr) { |
| 719 | LOG(ERROR) << "No ELF input oat fie available"; |
| 720 | return ERROR_OAT_FILE; |
| 721 | } |
| 722 | |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 723 | const std::string& file_path = oat_in->GetFilePath(); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 724 | |
| 725 | const OatHeader* oat_header = GetOatHeader(oat_in); |
| 726 | if (oat_header == nullptr) { |
| 727 | LOG(ERROR) << "Failed to find oat header in oat file " << file_path; |
| 728 | return ERROR_OAT_FILE; |
| 729 | } |
| 730 | |
| 731 | if (!oat_header->IsValid()) { |
| 732 | LOG(ERROR) << "Elf file " << file_path << " has an invalid oat header"; |
| 733 | return ERROR_OAT_FILE; |
| 734 | } |
| 735 | |
| 736 | bool is_pic = oat_header->IsPic(); |
| 737 | if (kIsDebugBuild) { |
| 738 | LOG(INFO) << "Oat file at " << file_path << " is " << (is_pic ? "PIC" : "not pic"); |
| 739 | } |
| 740 | |
| 741 | return is_pic ? PIC : NOT_PIC; |
| 742 | } |
| 743 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 744 | class PatchOat::PatchOatArtFieldVisitor : public ArtFieldVisitor { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 745 | public: |
| 746 | explicit PatchOatArtFieldVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 747 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 748 | void Visit(ArtField* field) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 749 | ArtField* const dest = patch_oat_->RelocatedCopyOf(field); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 750 | dest->SetDeclaringClass( |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 751 | patch_oat_->RelocatedAddressOfPointer(field->GetDeclaringClass().Ptr())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 752 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 753 | |
| 754 | private: |
| 755 | PatchOat* const patch_oat_; |
| 756 | }; |
| 757 | |
| 758 | void PatchOat::PatchArtFields(const ImageHeader* image_header) { |
| 759 | PatchOatArtFieldVisitor visitor(this); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 760 | image_header->VisitPackedArtFields(&visitor, heap_->Begin()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 763 | class PatchOat::PatchOatArtMethodVisitor : public ArtMethodVisitor { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 764 | public: |
| 765 | explicit PatchOatArtMethodVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 766 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 767 | void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 768 | ArtMethod* const dest = patch_oat_->RelocatedCopyOf(method); |
| 769 | patch_oat_->FixupMethod(method, dest); |
| 770 | } |
| 771 | |
| 772 | private: |
| 773 | PatchOat* const patch_oat_; |
| 774 | }; |
| 775 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 776 | void PatchOat::PatchArtMethods(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 777 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 778 | PatchOatArtMethodVisitor visitor(this); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 779 | image_header->VisitPackedArtMethods(&visitor, heap_->Begin(), pointer_size); |
| 780 | } |
| 781 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 782 | void PatchOat::PatchImTables(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 783 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 784 | // We can safely walk target image since the conflict tables are independent. |
| 785 | image_header->VisitPackedImTables( |
| 786 | [this](ArtMethod* method) { |
| 787 | return RelocatedAddressOfPointer(method); |
| 788 | }, |
| 789 | image_->Begin(), |
| 790 | pointer_size); |
| 791 | } |
| 792 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 793 | void PatchOat::PatchImtConflictTables(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 794 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 795 | // We can safely walk target image since the conflict tables are independent. |
| 796 | image_header->VisitPackedImtConflictTables( |
| 797 | [this](ArtMethod* method) { |
| 798 | return RelocatedAddressOfPointer(method); |
| 799 | }, |
| 800 | image_->Begin(), |
| 801 | pointer_size); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 804 | class PatchOat::FixupRootVisitor : public RootVisitor { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 805 | public: |
| 806 | explicit FixupRootVisitor(const PatchOat* patch_oat) : patch_oat_(patch_oat) { |
| 807 | } |
| 808 | |
| 809 | void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 810 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 811 | for (size_t i = 0; i < count; ++i) { |
| 812 | *roots[i] = patch_oat_->RelocatedAddressOfPointer(*roots[i]); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 817 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 818 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 819 | for (size_t i = 0; i < count; ++i) { |
| 820 | roots[i]->Assign(patch_oat_->RelocatedAddressOfPointer(roots[i]->AsMirrorPtr())); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | private: |
| 825 | const PatchOat* const patch_oat_; |
| 826 | }; |
| 827 | |
| 828 | void PatchOat::PatchInternedStrings(const ImageHeader* image_header) { |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 829 | const auto& section = image_header->GetInternedStringsSection(); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 830 | if (section.Size() == 0) { |
| 831 | return; |
| 832 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 833 | InternTable temp_table; |
| 834 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 835 | // This also relies on visit roots not doing any verification which could fail after we update |
| 836 | // the roots to be the image addresses. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 837 | temp_table.AddTableFromMemory(image_->Begin() + section.Offset()); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 838 | FixupRootVisitor visitor(this); |
| 839 | temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots); |
| 840 | } |
| 841 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 842 | void PatchOat::PatchClassTable(const ImageHeader* image_header) { |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 843 | const auto& section = image_header->GetClassTableSection(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 844 | if (section.Size() == 0) { |
| 845 | return; |
| 846 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 847 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 848 | // This also relies on visit roots not doing any verification which could fail after we update |
| 849 | // the roots to be the image addresses. |
| 850 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 851 | ClassTable temp_table; |
| 852 | temp_table.ReadFromMemory(image_->Begin() + section.Offset()); |
| 853 | FixupRootVisitor visitor(this); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 854 | temp_table.VisitRoots(UnbufferedRootVisitor(&visitor, RootInfo(kRootUnknown))); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 858 | class PatchOat::RelocatedPointerVisitor { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 859 | public: |
| 860 | explicit RelocatedPointerVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 861 | |
| 862 | template <typename T> |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 863 | T* operator()(T* ptr, void** dest_addr ATTRIBUTE_UNUSED = 0) const { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 864 | return patch_oat_->RelocatedAddressOfPointer(ptr); |
| 865 | } |
| 866 | |
| 867 | private: |
| 868 | PatchOat* const patch_oat_; |
| 869 | }; |
| 870 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 871 | void PatchOat::PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) { |
| 872 | auto* dex_caches = down_cast<mirror::ObjectArray<mirror::DexCache>*>( |
| 873 | img_roots->Get(ImageHeader::kDexCaches)); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 874 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 875 | for (size_t i = 0, count = dex_caches->GetLength(); i < count; ++i) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 876 | auto* orig_dex_cache = dex_caches->GetWithoutChecks(i); |
| 877 | auto* copy_dex_cache = RelocatedCopyOf(orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 878 | // Though the DexCache array fields are usually treated as native pointers, we set the full |
| 879 | // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is |
| 880 | // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e. |
| 881 | // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))). |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 882 | mirror::StringDexCacheType* orig_strings = orig_dex_cache->GetStrings(); |
| 883 | mirror::StringDexCacheType* relocated_strings = RelocatedAddressOfPointer(orig_strings); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 884 | copy_dex_cache->SetField64<false>( |
| 885 | mirror::DexCache::StringsOffset(), |
| 886 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_strings))); |
| 887 | if (orig_strings != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 888 | orig_dex_cache->FixupStrings(RelocatedCopyOf(orig_strings), RelocatedPointerVisitor(this)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 889 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 890 | mirror::TypeDexCacheType* orig_types = orig_dex_cache->GetResolvedTypes(); |
| 891 | mirror::TypeDexCacheType* relocated_types = RelocatedAddressOfPointer(orig_types); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 892 | copy_dex_cache->SetField64<false>( |
| 893 | mirror::DexCache::ResolvedTypesOffset(), |
| 894 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_types))); |
| 895 | if (orig_types != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 896 | orig_dex_cache->FixupResolvedTypes(RelocatedCopyOf(orig_types), |
| 897 | RelocatedPointerVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 898 | } |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 899 | mirror::MethodDexCacheType* orig_methods = orig_dex_cache->GetResolvedMethods(); |
| 900 | mirror::MethodDexCacheType* relocated_methods = RelocatedAddressOfPointer(orig_methods); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 901 | copy_dex_cache->SetField64<false>( |
| 902 | mirror::DexCache::ResolvedMethodsOffset(), |
| 903 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_methods))); |
| 904 | if (orig_methods != nullptr) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 905 | mirror::MethodDexCacheType* copy_methods = RelocatedCopyOf(orig_methods); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 906 | for (size_t j = 0, num = orig_dex_cache->NumResolvedMethods(); j != num; ++j) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 907 | mirror::MethodDexCachePair orig = |
| 908 | mirror::DexCache::GetNativePairPtrSize(orig_methods, j, pointer_size); |
| 909 | mirror::MethodDexCachePair copy(RelocatedAddressOfPointer(orig.object), orig.index); |
| 910 | mirror::DexCache::SetNativePairPtrSize(copy_methods, j, copy, pointer_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 911 | } |
| 912 | } |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 913 | mirror::FieldDexCacheType* orig_fields = orig_dex_cache->GetResolvedFields(); |
| 914 | mirror::FieldDexCacheType* relocated_fields = RelocatedAddressOfPointer(orig_fields); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 915 | copy_dex_cache->SetField64<false>( |
| 916 | mirror::DexCache::ResolvedFieldsOffset(), |
| 917 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_fields))); |
| 918 | if (orig_fields != nullptr) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 919 | mirror::FieldDexCacheType* copy_fields = RelocatedCopyOf(orig_fields); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 920 | for (size_t j = 0, num = orig_dex_cache->NumResolvedFields(); j != num; ++j) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 921 | mirror::FieldDexCachePair orig = |
| 922 | mirror::DexCache::GetNativePairPtrSize(orig_fields, j, pointer_size); |
| 923 | mirror::FieldDexCachePair copy(RelocatedAddressOfPointer(orig.object), orig.index); |
| 924 | mirror::DexCache::SetNativePairPtrSize(copy_fields, j, copy, pointer_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 925 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 926 | } |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 927 | mirror::MethodTypeDexCacheType* orig_method_types = orig_dex_cache->GetResolvedMethodTypes(); |
| 928 | mirror::MethodTypeDexCacheType* relocated_method_types = |
| 929 | RelocatedAddressOfPointer(orig_method_types); |
| 930 | copy_dex_cache->SetField64<false>( |
| 931 | mirror::DexCache::ResolvedMethodTypesOffset(), |
| 932 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_method_types))); |
| 933 | if (orig_method_types != nullptr) { |
| 934 | orig_dex_cache->FixupResolvedMethodTypes(RelocatedCopyOf(orig_method_types), |
| 935 | RelocatedPointerVisitor(this)); |
| 936 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 937 | |
| 938 | GcRoot<mirror::CallSite>* orig_call_sites = orig_dex_cache->GetResolvedCallSites(); |
| 939 | GcRoot<mirror::CallSite>* relocated_call_sites = RelocatedAddressOfPointer(orig_call_sites); |
| 940 | copy_dex_cache->SetField64<false>( |
| 941 | mirror::DexCache::ResolvedCallSitesOffset(), |
| 942 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_call_sites))); |
| 943 | if (orig_call_sites != nullptr) { |
| 944 | orig_dex_cache->FixupResolvedCallSites(RelocatedCopyOf(orig_call_sites), |
| 945 | RelocatedPointerVisitor(this)); |
| 946 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 950 | bool PatchOat::PatchImage(bool primary_image) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 951 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
| 952 | CHECK_GT(image_->Size(), sizeof(ImageHeader)); |
| 953 | // These are the roots from the original file. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 954 | auto* img_roots = image_header->GetImageRoots(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 955 | image_header->RelocateImage(delta_); |
| 956 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 957 | PatchArtFields(image_header); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 958 | PatchArtMethods(image_header); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 959 | PatchImTables(image_header); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 960 | PatchImtConflictTables(image_header); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 961 | PatchInternedStrings(image_header); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 962 | PatchClassTable(image_header); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 963 | // Patch dex file int/long arrays which point to ArtFields. |
| 964 | PatchDexFileArrays(img_roots); |
| 965 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 966 | if (primary_image) { |
| 967 | VisitObject(img_roots); |
| 968 | } |
| 969 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 970 | if (!image_header->IsValid()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 971 | LOG(ERROR) << "relocation renders image header invalid"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 972 | return false; |
| 973 | } |
| 974 | |
| 975 | { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 976 | TimingLogger::ScopedTiming t("Walk Bitmap", timings_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 977 | // Walk the bitmap. |
| 978 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 979 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 980 | VisitObject(obj); |
| 981 | }; |
| 982 | bitmap_->Walk(visitor); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 983 | } |
| 984 | return true; |
| 985 | } |
| 986 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 987 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 988 | void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Object> obj, |
| 989 | MemberOffset off, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 990 | bool is_static_unused ATTRIBUTE_UNUSED) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 991 | mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 992 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 993 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 994 | } |
| 995 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 996 | void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Class> cls ATTRIBUTE_UNUSED, |
| 997 | ObjPtr<mirror::Reference> ref) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 998 | MemberOffset off = mirror::Reference::ReferentOffset(); |
| 999 | mirror::Object* referent = ref->GetReferent(); |
Mathieu Chartier | a13abba | 2016-04-21 10:23:16 -0700 | [diff] [blame] | 1000 | DCHECK(referent == nullptr || |
| 1001 | Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(referent)) << referent; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1002 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1003 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 1004 | } |
| 1005 | |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1006 | // Called by PatchImage. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1007 | void PatchOat::VisitObject(mirror::Object* object) { |
| 1008 | mirror::Object* copy = RelocatedCopyOf(object); |
| 1009 | CHECK(copy != nullptr); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1010 | if (kUseBakerReadBarrier) { |
| 1011 | object->AssertReadBarrierState(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1012 | } |
| 1013 | PatchOat::PatchVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1014 | object->VisitReferences<kVerifyNone>(visitor, visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1015 | if (object->IsClass<kVerifyNone>()) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1016 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1017 | mirror::Class* klass = object->AsClass(); |
| 1018 | mirror::Class* copy_klass = down_cast<mirror::Class*>(copy); |
| 1019 | RelocatedPointerVisitor native_visitor(this); |
| 1020 | klass->FixupNativePointers(copy_klass, pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1021 | auto* vtable = klass->GetVTable(); |
| 1022 | if (vtable != nullptr) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1023 | vtable->Fixup(RelocatedCopyOfFollowImages(vtable), pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1024 | } |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 1025 | mirror::IfTable* iftable = klass->GetIfTable(); |
| 1026 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1027 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 1028 | auto* method_array = iftable->GetMethodArray(i); |
| 1029 | CHECK(method_array != nullptr); |
| 1030 | method_array->Fixup(RelocatedCopyOfFollowImages(method_array), |
| 1031 | pointer_size, |
| 1032 | native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1035 | } else if (object->GetClass() == mirror::Method::StaticClass() || |
| 1036 | object->GetClass() == mirror::Constructor::StaticClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1037 | // Need to go update the ArtMethod. |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 1038 | auto* dest = down_cast<mirror::Executable*>(copy); |
| 1039 | auto* src = down_cast<mirror::Executable*>(object); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1040 | dest->SetArtMethod(RelocatedAddressOfPointer(src->GetArtMethod())); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1044 | void PatchOat::FixupMethod(ArtMethod* object, ArtMethod* copy) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1045 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1046 | copy->CopyFrom(object, pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1047 | // Just update the entry points if it looks like we should. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1048 | // TODO: sanity check all the pointers' values |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1049 | copy->SetDeclaringClass(RelocatedAddressOfPointer(object->GetDeclaringClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1050 | copy->SetEntryPointFromQuickCompiledCodePtrSize(RelocatedAddressOfPointer( |
| 1051 | object->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)), pointer_size); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1052 | // No special handling for IMT conflict table since all pointers are moved by the same offset. |
Andreas Gampe | 75f0885 | 2016-07-19 08:06:07 -0700 | [diff] [blame] | 1053 | copy->SetDataPtrSize(RelocatedAddressOfPointer( |
| 1054 | object->GetDataPtrSize(pointer_size)), pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1057 | static int orig_argc; |
| 1058 | static char** orig_argv; |
| 1059 | |
| 1060 | static std::string CommandLine() { |
| 1061 | std::vector<std::string> command; |
| 1062 | for (int i = 0; i < orig_argc; ++i) { |
| 1063 | command.push_back(orig_argv[i]); |
| 1064 | } |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 1065 | return android::base::Join(command, ' '); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 1069 | std::string error; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 1070 | android::base::StringAppendV(&error, fmt, ap); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1071 | LOG(ERROR) << error; |
| 1072 | } |
| 1073 | |
| 1074 | static void UsageError(const char* fmt, ...) { |
| 1075 | va_list ap; |
| 1076 | va_start(ap, fmt); |
| 1077 | UsageErrorV(fmt, ap); |
| 1078 | va_end(ap); |
| 1079 | } |
| 1080 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 1081 | NO_RETURN static void Usage(const char *fmt, ...) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1082 | va_list ap; |
| 1083 | va_start(ap, fmt); |
| 1084 | UsageErrorV(fmt, ap); |
| 1085 | va_end(ap); |
| 1086 | |
| 1087 | UsageError("Command: %s", CommandLine().c_str()); |
| 1088 | UsageError("Usage: patchoat [options]..."); |
| 1089 | UsageError(""); |
| 1090 | UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is"); |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 1091 | UsageError(" compiled for (required)."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1092 | UsageError(""); |
| 1093 | UsageError(" --input-image-location=<file.art>: Specifies the 'location' of the image file to"); |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 1094 | UsageError(" be patched."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1095 | UsageError(""); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame^] | 1096 | UsageError(" --output-image-directory=<dir>: Specifies the directory to write the patched"); |
| 1097 | UsageError(" image file(s) to."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1098 | UsageError(""); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame^] | 1099 | UsageError(" --output-image-relocation-directory=<dir>: Specifies the directory to write"); |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 1100 | UsageError(" the image relocation information to."); |
| 1101 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1102 | UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by."); |
| 1103 | UsageError(" This value may be negative."); |
| 1104 | UsageError(""); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1105 | UsageError(" --verify: Verify an existing patched file instead of creating one."); |
| 1106 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1107 | UsageError(" --dump-timings: dump out patch timing information"); |
| 1108 | UsageError(""); |
| 1109 | UsageError(" --no-dump-timings: do not dump out patch timing information"); |
| 1110 | UsageError(""); |
| 1111 | |
| 1112 | exit(EXIT_FAILURE); |
| 1113 | } |
| 1114 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1115 | static int patchoat_patch_image(TimingLogger& timings, |
| 1116 | InstructionSet isa, |
| 1117 | const std::string& input_image_location, |
| 1118 | const std::string& output_image_directory, |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame^] | 1119 | const std::string& output_image_relocation_directory, |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1120 | off_t base_delta, |
| 1121 | bool base_delta_set, |
| 1122 | bool debug) { |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1123 | CHECK(!input_image_location.empty()); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame^] | 1124 | if ((output_image_directory.empty()) && (output_image_relocation_directory.empty())) { |
| 1125 | Usage("Image patching requires --output-image-directory or --output-image-relocation-directory"); |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | if (!base_delta_set) { |
| 1129 | Usage("Must supply a desired new offset or delta."); |
| 1130 | } |
| 1131 | |
| 1132 | if (!IsAligned<kPageSize>(base_delta)) { |
| 1133 | Usage("Base offset/delta must be aligned to a pagesize (0x%08x) boundary.", kPageSize); |
| 1134 | } |
| 1135 | |
| 1136 | if (debug) { |
| 1137 | LOG(INFO) << "moving offset by " << base_delta |
| 1138 | << " (0x" << std::hex << base_delta << ") bytes or " |
| 1139 | << std::dec << (base_delta/kPageSize) << " pages."; |
| 1140 | } |
| 1141 | |
| 1142 | TimingLogger::ScopedTiming pt("patch image and oat", &timings); |
| 1143 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 1144 | bool ret = |
| 1145 | PatchOat::Patch( |
| 1146 | input_image_location, |
| 1147 | base_delta, |
| 1148 | output_image_directory, |
| 1149 | output_image_relocation_directory, |
| 1150 | isa, |
| 1151 | &timings); |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1152 | |
| 1153 | if (kIsDebugBuild) { |
| 1154 | LOG(INFO) << "Exiting with return ... " << ret; |
| 1155 | } |
| 1156 | return ret ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1157 | } |
| 1158 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1159 | static int patchoat_verify_image(TimingLogger& timings, |
| 1160 | InstructionSet isa, |
| 1161 | const std::string& input_image_location, |
| 1162 | const std::string& output_image_directory) { |
| 1163 | CHECK(!input_image_location.empty()); |
| 1164 | TimingLogger::ScopedTiming pt("verify image and oat", &timings); |
| 1165 | |
| 1166 | bool ret = |
| 1167 | PatchOat::Verify( |
| 1168 | input_image_location, |
| 1169 | output_image_directory, |
| 1170 | isa, |
| 1171 | &timings); |
| 1172 | |
| 1173 | if (kIsDebugBuild) { |
| 1174 | LOG(INFO) << "Exiting with return ... " << ret; |
| 1175 | } |
| 1176 | return ret ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1177 | } |
| 1178 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1179 | static int patchoat(int argc, char **argv) { |
Andreas Gampe | 51d80cc | 2017-06-21 21:05:13 -0700 | [diff] [blame] | 1180 | InitLogging(argv, Runtime::Abort); |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 1181 | MemMap::Init(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1182 | const bool debug = kIsDebugBuild; |
| 1183 | orig_argc = argc; |
| 1184 | orig_argv = argv; |
| 1185 | TimingLogger timings("patcher", false, false); |
| 1186 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1187 | // Skip over the command name. |
| 1188 | argv++; |
| 1189 | argc--; |
| 1190 | |
| 1191 | if (argc == 0) { |
| 1192 | Usage("No arguments specified"); |
| 1193 | } |
| 1194 | |
| 1195 | timings.StartTiming("Patchoat"); |
| 1196 | |
| 1197 | // cmd line args |
| 1198 | bool isa_set = false; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1199 | InstructionSet isa = InstructionSet::kNone; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1200 | std::string input_image_location; |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame^] | 1201 | std::string output_image_directory; |
| 1202 | std::string output_image_relocation_directory; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1203 | off_t base_delta = 0; |
| 1204 | bool base_delta_set = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1205 | bool dump_timings = kIsDebugBuild; |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1206 | bool verify = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1207 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1208 | for (int i = 0; i < argc; ++i) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1209 | const StringPiece option(argv[i]); |
| 1210 | const bool log_options = false; |
| 1211 | if (log_options) { |
| 1212 | LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i]; |
| 1213 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1214 | if (option.starts_with("--instruction-set=")) { |
| 1215 | isa_set = true; |
| 1216 | const char* isa_str = option.substr(strlen("--instruction-set=")).data(); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 1217 | isa = GetInstructionSetFromString(isa_str); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1218 | if (isa == InstructionSet::kNone) { |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 1219 | Usage("Unknown or invalid instruction set %s", isa_str); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1220 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1221 | } else if (option.starts_with("--input-image-location=")) { |
| 1222 | input_image_location = option.substr(strlen("--input-image-location=")).data(); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame^] | 1223 | } else if (option.starts_with("--output-image-directory=")) { |
| 1224 | output_image_directory = option.substr(strlen("--output-image-directory=")).data(); |
| 1225 | } else if (option.starts_with("--output-image-relocation-directory=")) { |
| 1226 | output_image_relocation_directory = |
| 1227 | option.substr(strlen("--output-image-relocation-directory=")).data(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1228 | } else if (option.starts_with("--base-offset-delta=")) { |
| 1229 | const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data(); |
| 1230 | base_delta_set = true; |
| 1231 | if (!ParseInt(base_delta_str, &base_delta)) { |
| 1232 | Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str); |
| 1233 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1234 | } else if (option == "--dump-timings") { |
| 1235 | dump_timings = true; |
| 1236 | } else if (option == "--no-dump-timings") { |
| 1237 | dump_timings = false; |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1238 | } else if (option == "--verify") { |
| 1239 | verify = true; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1240 | } else { |
| 1241 | Usage("Unknown argument %s", option.data()); |
| 1242 | } |
| 1243 | } |
| 1244 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1245 | // The instruction set is mandatory. This simplifies things... |
| 1246 | if (!isa_set) { |
| 1247 | Usage("Instruction set must be set."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1250 | int ret; |
| 1251 | if (verify) { |
| 1252 | ret = patchoat_verify_image(timings, |
| 1253 | isa, |
| 1254 | input_image_location, |
| 1255 | output_image_directory); |
| 1256 | } else { |
| 1257 | ret = patchoat_patch_image(timings, |
| 1258 | isa, |
| 1259 | input_image_location, |
| 1260 | output_image_directory, |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame^] | 1261 | output_image_relocation_directory, |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1262 | base_delta, |
| 1263 | base_delta_set, |
| 1264 | debug); |
| 1265 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1266 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1267 | timings.EndTiming(); |
| 1268 | if (dump_timings) { |
| 1269 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1272 | return ret; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | } // namespace art |
| 1276 | |
| 1277 | int main(int argc, char **argv) { |
| 1278 | return art::patchoat(argc, argv); |
| 1279 | } |