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 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 20 | #include <sys/file.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 23 | |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 28 | #include "android-base/strings.h" |
| 29 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 30 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 31 | #include "art_method-inl.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 32 | #include "base/dumpable.h" |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 33 | #include "base/memory_tool.h" |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 34 | #include "base/scoped_flock.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 35 | #include "base/stringpiece.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 36 | #include "base/unix_file/fd_file.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 37 | #include "base/unix_file/random_access_file_utils.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 38 | #include "elf_utils.h" |
| 39 | #include "elf_file.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 40 | #include "elf_file_impl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 41 | #include "gc/space/image_space.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 42 | #include "image-inl.h" |
Andreas Gampe | b2d18fa | 2017-06-06 20:46:10 -0700 | [diff] [blame] | 43 | #include "intern_table.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 44 | #include "mirror/dex_cache.h" |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 45 | #include "mirror/executable.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 46 | #include "mirror/object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 47 | #include "mirror/object-refvisitor-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 48 | #include "mirror/method.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 49 | #include "mirror/reference.h" |
| 50 | #include "noop_compiler_callbacks.h" |
| 51 | #include "offsets.h" |
| 52 | #include "os.h" |
| 53 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 54 | #include "scoped_thread_state_change-inl.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 55 | #include "thread.h" |
| 56 | #include "utils.h" |
| 57 | |
| 58 | namespace art { |
| 59 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 60 | static const OatHeader* GetOatHeader(const ElfFile* elf_file) { |
| 61 | uint64_t off = 0; |
| 62 | if (!elf_file->GetSectionOffsetAndSize(".rodata", &off, nullptr)) { |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
| 66 | OatHeader* oat_header = reinterpret_cast<OatHeader*>(elf_file->Begin() + off); |
| 67 | return oat_header; |
| 68 | } |
| 69 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 70 | static File* CreateOrOpen(const char* name) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 71 | if (OS::FileExists(name)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 72 | return OS::OpenFileReadWrite(name); |
| 73 | } else { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 74 | std::unique_ptr<File> f(OS::CreateEmptyFile(name)); |
| 75 | if (f.get() != nullptr) { |
| 76 | if (fchmod(f->Fd(), 0644) != 0) { |
| 77 | PLOG(ERROR) << "Unable to make " << name << " world readable"; |
Dimitry Ivanov | 7a1c014 | 2016-03-17 15:59:38 -0700 | [diff] [blame] | 78 | unlink(name); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 79 | return nullptr; |
| 80 | } |
| 81 | } |
| 82 | return f.release(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Either try to close the file (close=true), or erase it. |
| 87 | static bool FinishFile(File* file, bool close) { |
| 88 | if (close) { |
| 89 | if (file->FlushCloseOrErase() != 0) { |
| 90 | PLOG(ERROR) << "Failed to flush and close file."; |
| 91 | return false; |
| 92 | } |
| 93 | return true; |
| 94 | } else { |
| 95 | file->Erase(); |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 100 | static bool SymlinkFile(const std::string& input_filename, const std::string& output_filename) { |
| 101 | if (input_filename == output_filename) { |
| 102 | // Input and output are the same, nothing to do. |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // Unlink the original filename, since we are overwriting it. |
| 107 | unlink(output_filename.c_str()); |
| 108 | |
| 109 | // Create a symlink from the source file to the target path. |
| 110 | if (symlink(input_filename.c_str(), output_filename.c_str()) < 0) { |
| 111 | PLOG(ERROR) << "Failed to create symlink " << output_filename << " -> " << input_filename; |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | if (kIsDebugBuild) { |
| 116 | LOG(INFO) << "Created symlink " << output_filename << " -> " << input_filename; |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 122 | bool PatchOat::Patch(const std::string& image_location, |
| 123 | off_t delta, |
| 124 | const std::string& output_directory, |
| 125 | InstructionSet isa, |
| 126 | TimingLogger* timings) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 127 | CHECK(Runtime::Current() == nullptr); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 128 | CHECK(!image_location.empty()) << "image file must have a filename."; |
| 129 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 130 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 131 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 132 | CHECK_NE(isa, kNone); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 133 | const char* isa_name = GetInstructionSetString(isa); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 134 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 135 | // Set up the runtime |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 136 | RuntimeOptions options; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 137 | NoopCompilerCallbacks callbacks; |
| 138 | options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
| 139 | std::string img = "-Ximage:" + image_location; |
| 140 | options.push_back(std::make_pair(img.c_str(), nullptr)); |
| 141 | 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] | 142 | options.push_back(std::make_pair("-Xno-sig-chain", nullptr)); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 143 | if (!Runtime::Create(options, false)) { |
| 144 | LOG(ERROR) << "Unable to initialize runtime"; |
| 145 | return false; |
| 146 | } |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 147 | std::unique_ptr<Runtime> runtime(Runtime::Current()); |
| 148 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 149 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 150 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 151 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 152 | ScopedObjectAccess soa(Thread::Current()); |
| 153 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 154 | t.NewTiming("Image Patching setup"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 155 | std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 156 | std::map<gc::space::ImageSpace*, std::unique_ptr<File>> space_to_file_map; |
| 157 | std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>> space_to_memmap_map; |
| 158 | std::map<gc::space::ImageSpace*, PatchOat> space_to_patchoat_map; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 159 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 160 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 161 | gc::space::ImageSpace* space = spaces[i]; |
| 162 | std::string input_image_filename = space->GetImageFilename(); |
| 163 | std::unique_ptr<File> input_image(OS::OpenFileForReading(input_image_filename.c_str())); |
| 164 | if (input_image.get() == nullptr) { |
| 165 | LOG(ERROR) << "Unable to open input image file at " << input_image_filename; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 166 | return false; |
| 167 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 168 | |
| 169 | int64_t image_len = input_image->GetLength(); |
| 170 | if (image_len < 0) { |
| 171 | LOG(ERROR) << "Error while getting image length"; |
| 172 | return false; |
| 173 | } |
| 174 | ImageHeader image_header; |
| 175 | if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header), |
| 176 | sizeof(image_header), 0)) { |
| 177 | LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath(); |
| 178 | } |
| 179 | |
| 180 | /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath()); |
| 181 | // Nothing special to do right now since the image always needs to get patched. |
| 182 | // Perhaps in some far-off future we may have images with relative addresses that are true-PIC. |
| 183 | |
| 184 | // Create the map where we will write the image patches to. |
| 185 | std::string error_msg; |
| 186 | std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, |
| 187 | PROT_READ | PROT_WRITE, |
| 188 | MAP_PRIVATE, |
| 189 | input_image->Fd(), |
| 190 | 0, |
| 191 | /*low_4gb*/false, |
| 192 | input_image->GetPath().c_str(), |
| 193 | &error_msg)); |
| 194 | if (image.get() == nullptr) { |
| 195 | LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg; |
| 196 | return false; |
| 197 | } |
| 198 | space_to_file_map.emplace(space, std::move(input_image)); |
| 199 | space_to_memmap_map.emplace(space, std::move(image)); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 202 | // Symlink PIC oat and vdex files and patch the image spaces in memory. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 203 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 204 | gc::space::ImageSpace* space = spaces[i]; |
| 205 | std::string input_image_filename = space->GetImageFilename(); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 206 | std::string input_vdex_filename = |
| 207 | ImageHeader::GetVdexLocationFromImageLocation(input_image_filename); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 208 | std::string input_oat_filename = |
| 209 | ImageHeader::GetOatLocationFromImageLocation(input_image_filename); |
| 210 | std::unique_ptr<File> input_oat_file(OS::OpenFileForReading(input_oat_filename.c_str())); |
| 211 | if (input_oat_file.get() == nullptr) { |
| 212 | LOG(ERROR) << "Unable to open input oat file at " << input_oat_filename; |
| 213 | return false; |
| 214 | } |
| 215 | std::string error_msg; |
| 216 | std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat_file.get(), |
| 217 | PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg)); |
| 218 | if (elf.get() == nullptr) { |
| 219 | LOG(ERROR) << "Unable to open oat file " << input_oat_file->GetPath() << " : " << error_msg; |
| 220 | return false; |
| 221 | } |
| 222 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 223 | MaybePic is_oat_pic = IsOatPic(elf.get()); |
| 224 | if (is_oat_pic >= ERROR_FIRST) { |
| 225 | // Error logged by IsOatPic |
| 226 | return false; |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 227 | } else if (is_oat_pic == NOT_PIC) { |
| 228 | LOG(ERROR) << "patchoat cannot be used on non-PIC oat file: " << input_oat_file->GetPath(); |
| 229 | return false; |
| 230 | } else { |
| 231 | CHECK(is_oat_pic == PIC); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 232 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 233 | // Create a symlink. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 234 | std::string converted_image_filename = space->GetImageLocation(); |
| 235 | std::replace(converted_image_filename.begin() + 1, converted_image_filename.end(), '/', '@'); |
| 236 | std::string output_image_filename = output_directory + |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 237 | (android::base::StartsWith(converted_image_filename, "/") ? "" : "/") + |
| 238 | converted_image_filename; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 239 | std::string output_vdex_filename = |
| 240 | ImageHeader::GetVdexLocationFromImageLocation(output_image_filename); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 241 | std::string output_oat_filename = |
| 242 | ImageHeader::GetOatLocationFromImageLocation(output_image_filename); |
| 243 | |
| 244 | if (!ReplaceOatFileWithSymlink(input_oat_file->GetPath(), |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 245 | output_oat_filename) || |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 246 | !SymlinkFile(input_vdex_filename, output_vdex_filename)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 247 | // Errors already logged by above call. |
| 248 | return false; |
| 249 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | PatchOat& p = space_to_patchoat_map.emplace(space, |
| 253 | PatchOat( |
| 254 | isa, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 255 | space_to_memmap_map.find(space)->second.get(), |
| 256 | space->GetLiveBitmap(), |
| 257 | space->GetMemMap(), |
| 258 | delta, |
| 259 | &space_to_memmap_map, |
| 260 | timings)).first->second; |
| 261 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 262 | t.NewTiming("Patching image"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 263 | if (!p.PatchImage(i == 0)) { |
| 264 | LOG(ERROR) << "Failed to patch image file " << input_image_filename; |
| 265 | return false; |
| 266 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 269 | // Write the patched image spaces. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 270 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 271 | gc::space::ImageSpace* space = spaces[i]; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 272 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 273 | t.NewTiming("Writing image"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 274 | std::string converted_image_filename = space->GetImageLocation(); |
| 275 | std::replace(converted_image_filename.begin() + 1, converted_image_filename.end(), '/', '@'); |
| 276 | std::string output_image_filename = output_directory + |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 277 | (android::base::StartsWith(converted_image_filename, "/") ? "" : "/") + |
| 278 | converted_image_filename; |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 279 | std::unique_ptr<File> output_image_file(CreateOrOpen(output_image_filename.c_str())); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 280 | if (output_image_file.get() == nullptr) { |
| 281 | LOG(ERROR) << "Failed to open output image file at " << output_image_filename; |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | PatchOat& p = space_to_patchoat_map.find(space)->second; |
| 286 | |
Serdjuk, Nikolay Y | d12f9c1 | 2016-03-22 10:06:33 +0600 | [diff] [blame] | 287 | bool success = p.WriteImage(output_image_file.get()); |
| 288 | success = FinishFile(output_image_file.get(), success); |
| 289 | if (!success) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 290 | return false; |
| 291 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 292 | } |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 293 | |
| 294 | if (!kIsDebugBuild && !(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
| 295 | // We want to just exit on non-debug builds, not bringing the runtime down |
| 296 | // in an orderly fashion. So release the following fields. |
| 297 | runtime.release(); |
| 298 | } |
| 299 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 300 | return true; |
| 301 | } |
| 302 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 303 | bool PatchOat::WriteImage(File* out) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 304 | TimingLogger::ScopedTiming t("Writing image File", timings_); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 305 | std::string error_msg; |
| 306 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 307 | ScopedFlock img_flock; |
| 308 | img_flock.Init(out, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 309 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 310 | CHECK(image_ != nullptr); |
| 311 | CHECK(out != nullptr); |
| 312 | size_t expect = image_->Size(); |
| 313 | if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) && |
| 314 | out->SetLength(expect) == 0) { |
| 315 | return true; |
| 316 | } else { |
| 317 | LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed."; |
| 318 | return false; |
| 319 | } |
| 320 | } |
| 321 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 322 | bool PatchOat::IsImagePic(const ImageHeader& image_header, const std::string& image_path) { |
| 323 | if (!image_header.CompilePic()) { |
| 324 | if (kIsDebugBuild) { |
| 325 | LOG(INFO) << "image at location " << image_path << " was *not* compiled pic"; |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | if (kIsDebugBuild) { |
| 331 | LOG(INFO) << "image at location " << image_path << " was compiled PIC"; |
| 332 | } |
| 333 | |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) { |
| 338 | if (oat_in == nullptr) { |
| 339 | LOG(ERROR) << "No ELF input oat fie available"; |
| 340 | return ERROR_OAT_FILE; |
| 341 | } |
| 342 | |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 343 | const std::string& file_path = oat_in->GetFilePath(); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 344 | |
| 345 | const OatHeader* oat_header = GetOatHeader(oat_in); |
| 346 | if (oat_header == nullptr) { |
| 347 | LOG(ERROR) << "Failed to find oat header in oat file " << file_path; |
| 348 | return ERROR_OAT_FILE; |
| 349 | } |
| 350 | |
| 351 | if (!oat_header->IsValid()) { |
| 352 | LOG(ERROR) << "Elf file " << file_path << " has an invalid oat header"; |
| 353 | return ERROR_OAT_FILE; |
| 354 | } |
| 355 | |
| 356 | bool is_pic = oat_header->IsPic(); |
| 357 | if (kIsDebugBuild) { |
| 358 | LOG(INFO) << "Oat file at " << file_path << " is " << (is_pic ? "PIC" : "not pic"); |
| 359 | } |
| 360 | |
| 361 | return is_pic ? PIC : NOT_PIC; |
| 362 | } |
| 363 | |
| 364 | bool PatchOat::ReplaceOatFileWithSymlink(const std::string& input_oat_filename, |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 365 | const std::string& output_oat_filename) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 366 | // Delete the original file, since we won't need it. |
Dimitry Ivanov | 7a1c014 | 2016-03-17 15:59:38 -0700 | [diff] [blame] | 367 | unlink(output_oat_filename.c_str()); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 368 | |
| 369 | // Create a symlink from the old oat to the new oat |
| 370 | if (symlink(input_oat_filename.c_str(), output_oat_filename.c_str()) < 0) { |
| 371 | int err = errno; |
| 372 | LOG(ERROR) << "Failed to create symlink at " << output_oat_filename |
| 373 | << " error(" << err << "): " << strerror(err); |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | if (kIsDebugBuild) { |
| 378 | LOG(INFO) << "Created symlink " << output_oat_filename << " -> " << input_oat_filename; |
| 379 | } |
| 380 | |
| 381 | return true; |
| 382 | } |
| 383 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 384 | class PatchOat::PatchOatArtFieldVisitor : public ArtFieldVisitor { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 385 | public: |
| 386 | explicit PatchOatArtFieldVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 387 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 388 | void Visit(ArtField* field) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 389 | ArtField* const dest = patch_oat_->RelocatedCopyOf(field); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 390 | dest->SetDeclaringClass( |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 391 | patch_oat_->RelocatedAddressOfPointer(field->GetDeclaringClass().Ptr())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 392 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 393 | |
| 394 | private: |
| 395 | PatchOat* const patch_oat_; |
| 396 | }; |
| 397 | |
| 398 | void PatchOat::PatchArtFields(const ImageHeader* image_header) { |
| 399 | PatchOatArtFieldVisitor visitor(this); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 400 | image_header->VisitPackedArtFields(&visitor, heap_->Begin()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 403 | class PatchOat::PatchOatArtMethodVisitor : public ArtMethodVisitor { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 404 | public: |
| 405 | explicit PatchOatArtMethodVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 406 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 407 | void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 408 | ArtMethod* const dest = patch_oat_->RelocatedCopyOf(method); |
| 409 | patch_oat_->FixupMethod(method, dest); |
| 410 | } |
| 411 | |
| 412 | private: |
| 413 | PatchOat* const patch_oat_; |
| 414 | }; |
| 415 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 416 | void PatchOat::PatchArtMethods(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 417 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 418 | PatchOatArtMethodVisitor visitor(this); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 419 | image_header->VisitPackedArtMethods(&visitor, heap_->Begin(), pointer_size); |
| 420 | } |
| 421 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 422 | void PatchOat::PatchImTables(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 423 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 424 | // We can safely walk target image since the conflict tables are independent. |
| 425 | image_header->VisitPackedImTables( |
| 426 | [this](ArtMethod* method) { |
| 427 | return RelocatedAddressOfPointer(method); |
| 428 | }, |
| 429 | image_->Begin(), |
| 430 | pointer_size); |
| 431 | } |
| 432 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 433 | void PatchOat::PatchImtConflictTables(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 434 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 435 | // We can safely walk target image since the conflict tables are independent. |
| 436 | image_header->VisitPackedImtConflictTables( |
| 437 | [this](ArtMethod* method) { |
| 438 | return RelocatedAddressOfPointer(method); |
| 439 | }, |
| 440 | image_->Begin(), |
| 441 | pointer_size); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 444 | class PatchOat::FixupRootVisitor : public RootVisitor { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 445 | public: |
| 446 | explicit FixupRootVisitor(const PatchOat* patch_oat) : patch_oat_(patch_oat) { |
| 447 | } |
| 448 | |
| 449 | 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] | 450 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 451 | for (size_t i = 0; i < count; ++i) { |
| 452 | *roots[i] = patch_oat_->RelocatedAddressOfPointer(*roots[i]); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 457 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 458 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 459 | for (size_t i = 0; i < count; ++i) { |
| 460 | roots[i]->Assign(patch_oat_->RelocatedAddressOfPointer(roots[i]->AsMirrorPtr())); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | private: |
| 465 | const PatchOat* const patch_oat_; |
| 466 | }; |
| 467 | |
| 468 | void PatchOat::PatchInternedStrings(const ImageHeader* image_header) { |
| 469 | const auto& section = image_header->GetImageSection(ImageHeader::kSectionInternedStrings); |
| 470 | InternTable temp_table; |
| 471 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 472 | // This also relies on visit roots not doing any verification which could fail after we update |
| 473 | // the roots to be the image addresses. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 474 | temp_table.AddTableFromMemory(image_->Begin() + section.Offset()); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 475 | FixupRootVisitor visitor(this); |
| 476 | temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots); |
| 477 | } |
| 478 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 479 | void PatchOat::PatchClassTable(const ImageHeader* image_header) { |
| 480 | const auto& section = image_header->GetImageSection(ImageHeader::kSectionClassTable); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 481 | if (section.Size() == 0) { |
| 482 | return; |
| 483 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 484 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 485 | // This also relies on visit roots not doing any verification which could fail after we update |
| 486 | // the roots to be the image addresses. |
| 487 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 488 | ClassTable temp_table; |
| 489 | temp_table.ReadFromMemory(image_->Begin() + section.Offset()); |
| 490 | FixupRootVisitor visitor(this); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 491 | temp_table.VisitRoots(UnbufferedRootVisitor(&visitor, RootInfo(kRootUnknown))); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 495 | class PatchOat::RelocatedPointerVisitor { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 496 | public: |
| 497 | explicit RelocatedPointerVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 498 | |
| 499 | template <typename T> |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 500 | T* operator()(T* ptr, void** dest_addr ATTRIBUTE_UNUSED = 0) const { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 501 | return patch_oat_->RelocatedAddressOfPointer(ptr); |
| 502 | } |
| 503 | |
| 504 | private: |
| 505 | PatchOat* const patch_oat_; |
| 506 | }; |
| 507 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 508 | void PatchOat::PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) { |
| 509 | auto* dex_caches = down_cast<mirror::ObjectArray<mirror::DexCache>*>( |
| 510 | img_roots->Get(ImageHeader::kDexCaches)); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 511 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 512 | for (size_t i = 0, count = dex_caches->GetLength(); i < count; ++i) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 513 | auto* orig_dex_cache = dex_caches->GetWithoutChecks(i); |
| 514 | auto* copy_dex_cache = RelocatedCopyOf(orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 515 | // Though the DexCache array fields are usually treated as native pointers, we set the full |
| 516 | // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is |
| 517 | // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e. |
| 518 | // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))). |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 519 | mirror::StringDexCacheType* orig_strings = orig_dex_cache->GetStrings(); |
| 520 | mirror::StringDexCacheType* relocated_strings = RelocatedAddressOfPointer(orig_strings); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 521 | copy_dex_cache->SetField64<false>( |
| 522 | mirror::DexCache::StringsOffset(), |
| 523 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_strings))); |
| 524 | if (orig_strings != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 525 | orig_dex_cache->FixupStrings(RelocatedCopyOf(orig_strings), RelocatedPointerVisitor(this)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 526 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 527 | mirror::TypeDexCacheType* orig_types = orig_dex_cache->GetResolvedTypes(); |
| 528 | mirror::TypeDexCacheType* relocated_types = RelocatedAddressOfPointer(orig_types); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 529 | copy_dex_cache->SetField64<false>( |
| 530 | mirror::DexCache::ResolvedTypesOffset(), |
| 531 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_types))); |
| 532 | if (orig_types != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 533 | orig_dex_cache->FixupResolvedTypes(RelocatedCopyOf(orig_types), |
| 534 | RelocatedPointerVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 535 | } |
| 536 | ArtMethod** orig_methods = orig_dex_cache->GetResolvedMethods(); |
| 537 | ArtMethod** relocated_methods = RelocatedAddressOfPointer(orig_methods); |
| 538 | copy_dex_cache->SetField64<false>( |
| 539 | mirror::DexCache::ResolvedMethodsOffset(), |
| 540 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_methods))); |
| 541 | if (orig_methods != nullptr) { |
| 542 | ArtMethod** copy_methods = RelocatedCopyOf(orig_methods); |
| 543 | for (size_t j = 0, num = orig_dex_cache->NumResolvedMethods(); j != num; ++j) { |
| 544 | ArtMethod* orig = mirror::DexCache::GetElementPtrSize(orig_methods, j, pointer_size); |
| 545 | ArtMethod* copy = RelocatedAddressOfPointer(orig); |
| 546 | mirror::DexCache::SetElementPtrSize(copy_methods, j, copy, pointer_size); |
| 547 | } |
| 548 | } |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 549 | mirror::FieldDexCacheType* orig_fields = orig_dex_cache->GetResolvedFields(); |
| 550 | mirror::FieldDexCacheType* relocated_fields = RelocatedAddressOfPointer(orig_fields); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 551 | copy_dex_cache->SetField64<false>( |
| 552 | mirror::DexCache::ResolvedFieldsOffset(), |
| 553 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_fields))); |
| 554 | if (orig_fields != nullptr) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 555 | mirror::FieldDexCacheType* copy_fields = RelocatedCopyOf(orig_fields); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 556 | 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] | 557 | mirror::FieldDexCachePair orig = |
| 558 | mirror::DexCache::GetNativePairPtrSize(orig_fields, j, pointer_size); |
| 559 | mirror::FieldDexCachePair copy(RelocatedAddressOfPointer(orig.object), orig.index); |
| 560 | mirror::DexCache::SetNativePairPtrSize(copy_fields, j, copy, pointer_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 561 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 562 | } |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 563 | mirror::MethodTypeDexCacheType* orig_method_types = orig_dex_cache->GetResolvedMethodTypes(); |
| 564 | mirror::MethodTypeDexCacheType* relocated_method_types = |
| 565 | RelocatedAddressOfPointer(orig_method_types); |
| 566 | copy_dex_cache->SetField64<false>( |
| 567 | mirror::DexCache::ResolvedMethodTypesOffset(), |
| 568 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_method_types))); |
| 569 | if (orig_method_types != nullptr) { |
| 570 | orig_dex_cache->FixupResolvedMethodTypes(RelocatedCopyOf(orig_method_types), |
| 571 | RelocatedPointerVisitor(this)); |
| 572 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 573 | |
| 574 | GcRoot<mirror::CallSite>* orig_call_sites = orig_dex_cache->GetResolvedCallSites(); |
| 575 | GcRoot<mirror::CallSite>* relocated_call_sites = RelocatedAddressOfPointer(orig_call_sites); |
| 576 | copy_dex_cache->SetField64<false>( |
| 577 | mirror::DexCache::ResolvedCallSitesOffset(), |
| 578 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_call_sites))); |
| 579 | if (orig_call_sites != nullptr) { |
| 580 | orig_dex_cache->FixupResolvedCallSites(RelocatedCopyOf(orig_call_sites), |
| 581 | RelocatedPointerVisitor(this)); |
| 582 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 586 | bool PatchOat::PatchImage(bool primary_image) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 587 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
| 588 | CHECK_GT(image_->Size(), sizeof(ImageHeader)); |
| 589 | // These are the roots from the original file. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 590 | auto* img_roots = image_header->GetImageRoots(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 591 | image_header->RelocateImage(delta_); |
| 592 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 593 | PatchArtFields(image_header); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 594 | PatchArtMethods(image_header); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 595 | PatchImTables(image_header); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 596 | PatchImtConflictTables(image_header); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 597 | PatchInternedStrings(image_header); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 598 | PatchClassTable(image_header); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 599 | // Patch dex file int/long arrays which point to ArtFields. |
| 600 | PatchDexFileArrays(img_roots); |
| 601 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 602 | if (primary_image) { |
| 603 | VisitObject(img_roots); |
| 604 | } |
| 605 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 606 | if (!image_header->IsValid()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 607 | LOG(ERROR) << "relocation renders image header invalid"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 608 | return false; |
| 609 | } |
| 610 | |
| 611 | { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 612 | TimingLogger::ScopedTiming t("Walk Bitmap", timings_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 613 | // Walk the bitmap. |
| 614 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 615 | bitmap_->Walk(PatchOat::BitmapCallback, this); |
| 616 | } |
| 617 | return true; |
| 618 | } |
| 619 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 620 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 621 | void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Object> obj, |
| 622 | MemberOffset off, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 623 | bool is_static_unused ATTRIBUTE_UNUSED) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 624 | mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 625 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 626 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 627 | } |
| 628 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 629 | void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Class> cls ATTRIBUTE_UNUSED, |
| 630 | ObjPtr<mirror::Reference> ref) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 631 | MemberOffset off = mirror::Reference::ReferentOffset(); |
| 632 | mirror::Object* referent = ref->GetReferent(); |
Mathieu Chartier | a13abba | 2016-04-21 10:23:16 -0700 | [diff] [blame] | 633 | DCHECK(referent == nullptr || |
| 634 | Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(referent)) << referent; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 635 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 636 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 637 | } |
| 638 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 639 | // Called by BitmapCallback |
| 640 | void PatchOat::VisitObject(mirror::Object* object) { |
| 641 | mirror::Object* copy = RelocatedCopyOf(object); |
| 642 | CHECK(copy != nullptr); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 643 | if (kUseBakerReadBarrier) { |
| 644 | object->AssertReadBarrierState(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 645 | } |
| 646 | PatchOat::PatchVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 647 | object->VisitReferences<kVerifyNone>(visitor, visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 648 | if (object->IsClass<kVerifyNone>()) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 649 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 650 | mirror::Class* klass = object->AsClass(); |
| 651 | mirror::Class* copy_klass = down_cast<mirror::Class*>(copy); |
| 652 | RelocatedPointerVisitor native_visitor(this); |
| 653 | klass->FixupNativePointers(copy_klass, pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 654 | auto* vtable = klass->GetVTable(); |
| 655 | if (vtable != nullptr) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 656 | vtable->Fixup(RelocatedCopyOfFollowImages(vtable), pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 657 | } |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 658 | mirror::IfTable* iftable = klass->GetIfTable(); |
| 659 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 660 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 661 | auto* method_array = iftable->GetMethodArray(i); |
| 662 | CHECK(method_array != nullptr); |
| 663 | method_array->Fixup(RelocatedCopyOfFollowImages(method_array), |
| 664 | pointer_size, |
| 665 | native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 668 | } else if (object->GetClass() == mirror::Method::StaticClass() || |
| 669 | object->GetClass() == mirror::Constructor::StaticClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 670 | // Need to go update the ArtMethod. |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 671 | auto* dest = down_cast<mirror::Executable*>(copy); |
| 672 | auto* src = down_cast<mirror::Executable*>(object); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 673 | dest->SetArtMethod(RelocatedAddressOfPointer(src->GetArtMethod())); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 677 | void PatchOat::FixupMethod(ArtMethod* object, ArtMethod* copy) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 678 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 679 | copy->CopyFrom(object, pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 680 | // Just update the entry points if it looks like we should. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 681 | // TODO: sanity check all the pointers' values |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 682 | copy->SetDeclaringClass(RelocatedAddressOfPointer(object->GetDeclaringClass())); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 683 | copy->SetDexCacheResolvedMethods( |
| 684 | RelocatedAddressOfPointer(object->GetDexCacheResolvedMethods(pointer_size)), pointer_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 685 | copy->SetEntryPointFromQuickCompiledCodePtrSize(RelocatedAddressOfPointer( |
| 686 | object->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)), pointer_size); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 687 | // 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] | 688 | copy->SetDataPtrSize(RelocatedAddressOfPointer( |
| 689 | object->GetDataPtrSize(pointer_size)), pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 692 | static int orig_argc; |
| 693 | static char** orig_argv; |
| 694 | |
| 695 | static std::string CommandLine() { |
| 696 | std::vector<std::string> command; |
| 697 | for (int i = 0; i < orig_argc; ++i) { |
| 698 | command.push_back(orig_argv[i]); |
| 699 | } |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 700 | return android::base::Join(command, ' '); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 704 | std::string error; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 705 | android::base::StringAppendV(&error, fmt, ap); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 706 | LOG(ERROR) << error; |
| 707 | } |
| 708 | |
| 709 | static void UsageError(const char* fmt, ...) { |
| 710 | va_list ap; |
| 711 | va_start(ap, fmt); |
| 712 | UsageErrorV(fmt, ap); |
| 713 | va_end(ap); |
| 714 | } |
| 715 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 716 | NO_RETURN static void Usage(const char *fmt, ...) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 717 | va_list ap; |
| 718 | va_start(ap, fmt); |
| 719 | UsageErrorV(fmt, ap); |
| 720 | va_end(ap); |
| 721 | |
| 722 | UsageError("Command: %s", CommandLine().c_str()); |
| 723 | UsageError("Usage: patchoat [options]..."); |
| 724 | UsageError(""); |
| 725 | UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is"); |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 726 | UsageError(" compiled for (required)."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 727 | UsageError(""); |
| 728 | 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] | 729 | UsageError(" be patched."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 730 | UsageError(""); |
| 731 | UsageError(" --output-image-file=<file.art>: Specifies the exact file to write the patched"); |
| 732 | UsageError(" image file to."); |
| 733 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 734 | UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by."); |
| 735 | UsageError(" This value may be negative."); |
| 736 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 737 | UsageError(" --dump-timings: dump out patch timing information"); |
| 738 | UsageError(""); |
| 739 | UsageError(" --no-dump-timings: do not dump out patch timing information"); |
| 740 | UsageError(""); |
| 741 | |
| 742 | exit(EXIT_FAILURE); |
| 743 | } |
| 744 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 745 | static int patchoat_image(TimingLogger& timings, |
| 746 | InstructionSet isa, |
| 747 | const std::string& input_image_location, |
| 748 | const std::string& output_image_filename, |
| 749 | off_t base_delta, |
| 750 | bool base_delta_set, |
| 751 | bool debug) { |
| 752 | CHECK(!input_image_location.empty()); |
| 753 | if (output_image_filename.empty()) { |
| 754 | Usage("Image patching requires --output-image-file"); |
| 755 | } |
| 756 | |
| 757 | if (!base_delta_set) { |
| 758 | Usage("Must supply a desired new offset or delta."); |
| 759 | } |
| 760 | |
| 761 | if (!IsAligned<kPageSize>(base_delta)) { |
| 762 | Usage("Base offset/delta must be aligned to a pagesize (0x%08x) boundary.", kPageSize); |
| 763 | } |
| 764 | |
| 765 | if (debug) { |
| 766 | LOG(INFO) << "moving offset by " << base_delta |
| 767 | << " (0x" << std::hex << base_delta << ") bytes or " |
| 768 | << std::dec << (base_delta/kPageSize) << " pages."; |
| 769 | } |
| 770 | |
| 771 | TimingLogger::ScopedTiming pt("patch image and oat", &timings); |
| 772 | |
| 773 | std::string output_directory = |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 774 | output_image_filename.substr(0, output_image_filename.find_last_of('/')); |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 775 | bool ret = PatchOat::Patch(input_image_location, base_delta, output_directory, isa, &timings); |
| 776 | |
| 777 | if (kIsDebugBuild) { |
| 778 | LOG(INFO) << "Exiting with return ... " << ret; |
| 779 | } |
| 780 | return ret ? EXIT_SUCCESS : EXIT_FAILURE; |
| 781 | } |
| 782 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 783 | static int patchoat(int argc, char **argv) { |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 784 | InitLogging(argv, Runtime::Aborter); |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 785 | MemMap::Init(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 786 | const bool debug = kIsDebugBuild; |
| 787 | orig_argc = argc; |
| 788 | orig_argv = argv; |
| 789 | TimingLogger timings("patcher", false, false); |
| 790 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 791 | // Skip over the command name. |
| 792 | argv++; |
| 793 | argc--; |
| 794 | |
| 795 | if (argc == 0) { |
| 796 | Usage("No arguments specified"); |
| 797 | } |
| 798 | |
| 799 | timings.StartTiming("Patchoat"); |
| 800 | |
| 801 | // cmd line args |
| 802 | bool isa_set = false; |
| 803 | InstructionSet isa = kNone; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 804 | std::string input_image_location; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 805 | std::string output_image_filename; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 806 | off_t base_delta = 0; |
| 807 | bool base_delta_set = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 808 | bool dump_timings = kIsDebugBuild; |
| 809 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 810 | for (int i = 0; i < argc; ++i) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 811 | const StringPiece option(argv[i]); |
| 812 | const bool log_options = false; |
| 813 | if (log_options) { |
| 814 | LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i]; |
| 815 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 816 | if (option.starts_with("--instruction-set=")) { |
| 817 | isa_set = true; |
| 818 | const char* isa_str = option.substr(strlen("--instruction-set=")).data(); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 819 | isa = GetInstructionSetFromString(isa_str); |
| 820 | if (isa == kNone) { |
| 821 | Usage("Unknown or invalid instruction set %s", isa_str); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 822 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 823 | } else if (option.starts_with("--input-image-location=")) { |
| 824 | input_image_location = option.substr(strlen("--input-image-location=")).data(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 825 | } else if (option.starts_with("--output-image-file=")) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 826 | output_image_filename = option.substr(strlen("--output-image-file=")).data(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 827 | } else if (option.starts_with("--base-offset-delta=")) { |
| 828 | const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data(); |
| 829 | base_delta_set = true; |
| 830 | if (!ParseInt(base_delta_str, &base_delta)) { |
| 831 | Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str); |
| 832 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 833 | } else if (option == "--dump-timings") { |
| 834 | dump_timings = true; |
| 835 | } else if (option == "--no-dump-timings") { |
| 836 | dump_timings = false; |
| 837 | } else { |
| 838 | Usage("Unknown argument %s", option.data()); |
| 839 | } |
| 840 | } |
| 841 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 842 | // The instruction set is mandatory. This simplifies things... |
| 843 | if (!isa_set) { |
| 844 | Usage("Instruction set must be set."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 847 | int ret = patchoat_image(timings, |
| 848 | isa, |
| 849 | input_image_location, |
| 850 | output_image_filename, |
| 851 | base_delta, |
| 852 | base_delta_set, |
| 853 | debug); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 854 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 855 | timings.EndTiming(); |
| 856 | if (dump_timings) { |
| 857 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 860 | return ret; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | } // namespace art |
| 864 | |
| 865 | int main(int argc, char **argv) { |
| 866 | return art::patchoat(argc, argv); |
| 867 | } |