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