Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 16 | |
| 17 | #include "oat_file.h" |
| 18 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 19 | #include <dlfcn.h> |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 20 | #include <sstream> |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 21 | #include <string.h> |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 23 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 24 | #include "base/bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 25 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 26 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 27 | #include "elf_file.h" |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 28 | #include "elf_utils.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 29 | #include "oat.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 30 | #include "mirror/art_method.h" |
| 31 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "mirror/class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 33 | #include "mirror/object-inl.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 34 | #include "os.h" |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 35 | #include "runtime.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "utils.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 37 | #include "vmap_table.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 41 | void OatFile::CheckLocation(const std::string& location) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 42 | CHECK(!location.empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 45 | OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file, |
| 46 | const std::string& location, |
| 47 | std::string* error_msg) { |
| 48 | std::unique_ptr<OatFile> oat_file(new OatFile(location, false)); |
| 49 | oat_file->elf_file_.reset(elf_file); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 50 | uint64_t offset, size; |
| 51 | bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size); |
| 52 | CHECK(has_section); |
| 53 | oat_file->begin_ = elf_file->Begin() + offset; |
| 54 | oat_file->end_ = elf_file->Begin() + size + offset; |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 55 | return oat_file->Setup(error_msg) ? oat_file.release() : nullptr; |
| 56 | } |
| 57 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 58 | OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 59 | const std::string& location, |
| 60 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 61 | CHECK(!oat_contents.empty()) << location; |
| 62 | CheckLocation(location); |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 63 | std::unique_ptr<OatFile> oat_file(new OatFile(location, false)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 64 | oat_file->begin_ = &oat_contents[0]; |
| 65 | oat_file->end_ = &oat_contents[oat_contents.size()]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 66 | return oat_file->Setup(error_msg) ? oat_file.release() : nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | OatFile* OatFile::Open(const std::string& filename, |
| 70 | const std::string& location, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 71 | uint8_t* requested_base, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 72 | bool executable, |
| 73 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 74 | CHECK(!filename.empty()) << location; |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame^] | 75 | CheckLocation(location); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 76 | std::unique_ptr<OatFile> ret; |
| 77 | if (kUsePortableCompiler && executable) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 78 | // If we are using PORTABLE, use dlopen to deal with relocations. |
| 79 | // |
| 80 | // We use our own ELF loader for Quick to deal with legacy apps that |
| 81 | // open a generated dex file by name, remove the file, then open |
| 82 | // another generated dex file with the same name. http://b/10614658 |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 83 | ret.reset(OpenDlopen(filename, location, requested_base, error_msg)); |
| 84 | } else { |
| 85 | // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: |
| 86 | // |
| 87 | // On target, dlopen may fail when compiling due to selinux restrictions on installd. |
| 88 | // |
| 89 | // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. |
| 90 | // This won't work for portable runtime execution because it doesn't process relocations. |
| 91 | std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str())); |
| 92 | if (file.get() == NULL) { |
| 93 | *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno)); |
| 94 | return nullptr; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 95 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 96 | ret.reset(OpenElfFile(file.get(), location, requested_base, false, executable, error_msg)); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 97 | |
Andreas Gampe | 9ef2b6c | 2014-09-15 22:25:24 -0700 | [diff] [blame] | 98 | // It would be nice to unlink here. But we might have opened the file created by the |
| 99 | // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API |
| 100 | // to allow removal when we know the ELF must be borked. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 101 | } |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 102 | return ret.release(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 105 | OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 106 | CheckLocation(location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 107 | return OpenElfFile(file, location, NULL, true, false, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 110 | OatFile* OatFile::OpenReadable(File* file, const std::string& location, std::string* error_msg) { |
| 111 | CheckLocation(location); |
| 112 | return OpenElfFile(file, location, NULL, false, false, error_msg); |
| 113 | } |
| 114 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 115 | OatFile* OatFile::OpenDlopen(const std::string& elf_filename, |
| 116 | const std::string& location, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 117 | uint8_t* requested_base, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 118 | std::string* error_msg) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 119 | std::unique_ptr<OatFile> oat_file(new OatFile(location, true)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 120 | bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 121 | if (!success) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 122 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 123 | } |
| 124 | return oat_file.release(); |
| 125 | } |
| 126 | |
| 127 | OatFile* OatFile::OpenElfFile(File* file, |
| 128 | const std::string& location, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 129 | uint8_t* requested_base, |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 130 | bool writable, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 131 | bool executable, |
| 132 | std::string* error_msg) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 133 | std::unique_ptr<OatFile> oat_file(new OatFile(location, executable)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 134 | bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 135 | if (!success) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 136 | CHECK(!error_msg->empty()); |
| 137 | return nullptr; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 138 | } |
| 139 | return oat_file.release(); |
| 140 | } |
| 141 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 142 | OatFile::OatFile(const std::string& location, bool is_executable) |
| 143 | : location_(location), begin_(NULL), end_(NULL), is_executable_(is_executable), |
| 144 | dlopen_handle_(NULL), |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 145 | secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 146 | CHECK(!location_.empty()); |
| 147 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 148 | |
| 149 | OatFile::~OatFile() { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 150 | STLDeleteElements(&oat_dex_files_storage_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 151 | if (dlopen_handle_ != NULL) { |
| 152 | dlclose(dlopen_handle_); |
| 153 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 156 | bool OatFile::Dlopen(const std::string& elf_filename, uint8_t* requested_base, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 157 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 158 | char* absolute_path = realpath(elf_filename.c_str(), NULL); |
| 159 | if (absolute_path == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 160 | *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 161 | return false; |
| 162 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 163 | dlopen_handle_ = dlopen(absolute_path, RTLD_NOW); |
| 164 | free(absolute_path); |
| 165 | if (dlopen_handle_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 166 | *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 167 | return false; |
| 168 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 169 | begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatdata")); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 170 | if (begin_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 171 | *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(), |
| 172 | dlerror()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 173 | return false; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 174 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 175 | if (requested_base != NULL && begin_ != requested_base) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 176 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
| 177 | "oatdata=%p != expected=%p /proc/self/maps:\n", |
| 178 | begin_, requested_base); |
| 179 | ReadFileToString("/proc/self/maps", error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 180 | return false; |
| 181 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 182 | end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatlastword")); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 183 | if (end_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 184 | *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(), |
| 185 | dlerror()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | // Readjust to be non-inclusive upper bound. |
| 189 | end_ += sizeof(uint32_t); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 190 | return Setup(error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 191 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 192 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 193 | bool OatFile::ElfFileOpen(File* file, uint8_t* requested_base, bool writable, bool executable, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 194 | std::string* error_msg) { |
| 195 | elf_file_.reset(ElfFile::Open(file, writable, true, error_msg)); |
| 196 | if (elf_file_.get() == nullptr) { |
| 197 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 198 | return false; |
| 199 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 200 | bool loaded = elf_file_->Load(executable, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 201 | if (!loaded) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 202 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | begin_ = elf_file_->FindDynamicSymbolAddress("oatdata"); |
| 206 | if (begin_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 207 | *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | if (requested_base != NULL && begin_ != requested_base) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 211 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
| 212 | "oatdata=%p != expected=%p /proc/self/maps:\n", |
| 213 | begin_, requested_base); |
| 214 | ReadFileToString("/proc/self/maps", error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | end_ = elf_file_->FindDynamicSymbolAddress("oatlastword"); |
| 218 | if (end_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 219 | *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | // Readjust to be non-inclusive upper bound. |
| 223 | end_ += sizeof(uint32_t); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 224 | return Setup(error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 225 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 226 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 227 | bool OatFile::Setup(std::string* error_msg) { |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 228 | if (!GetOatHeader().IsValid()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 229 | *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str()); |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 230 | return false; |
| 231 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 232 | const uint8_t* oat = Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 233 | oat += sizeof(OatHeader); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 234 | if (oat > End()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 235 | *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 236 | return false; |
| 237 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 238 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 239 | oat += GetOatHeader().GetKeyValueStoreSize(); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 240 | if (oat > End()) { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 241 | *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: " |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 242 | "%p + %zd + %ud <= %p", GetLocation().c_str(), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 243 | Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 244 | End()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 248 | uint32_t dex_file_count = GetOatHeader().GetDexFileCount(); |
| 249 | oat_dex_files_storage_.reserve(dex_file_count); |
| 250 | for (size_t i = 0; i < dex_file_count; i++) { |
Dmitry Petrochenko | 83bef92 | 2014-02-03 12:34:59 +0700 | [diff] [blame] | 251 | uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 252 | if (UNLIKELY(dex_file_location_size == 0U)) { |
| 253 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name", |
| 254 | GetLocation().c_str(), i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 257 | oat += sizeof(dex_file_location_size); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 258 | if (UNLIKELY(oat > End())) { |
| 259 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file " |
| 260 | "location size", GetLocation().c_str(), i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 263 | |
| 264 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 265 | oat += dex_file_location_size; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 266 | if (UNLIKELY(oat > End())) { |
| 267 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file " |
| 268 | "location", GetLocation().c_str(), i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 269 | return false; |
| 270 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 271 | |
| 272 | std::string dex_file_location(dex_file_location_data, dex_file_location_size); |
| 273 | |
| 274 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 275 | oat += sizeof(dex_file_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 276 | if (UNLIKELY(oat > End())) { |
| 277 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after " |
| 278 | "dex file checksum", GetLocation().c_str(), i, |
| 279 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 282 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 283 | uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 284 | if (UNLIKELY(dex_file_offset == 0U)) { |
| 285 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex " |
| 286 | "file offset", GetLocation().c_str(), i, dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 287 | return false; |
| 288 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 289 | if (UNLIKELY(dex_file_offset > Size())) { |
| 290 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file " |
| 291 | "offset %ud > %zd", GetLocation().c_str(), i, |
| 292 | dex_file_location.c_str(), dex_file_offset, Size()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 293 | return false; |
| 294 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 295 | oat += sizeof(dex_file_offset); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 296 | if (UNLIKELY(oat > End())) { |
| 297 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " |
| 298 | " after dex file offsets", GetLocation().c_str(), i, |
| 299 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 302 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 303 | const uint8_t* dex_file_pointer = Begin() + dex_file_offset; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 304 | if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) { |
| 305 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid " |
| 306 | " dex file magic '%s'", GetLocation().c_str(), i, |
| 307 | dex_file_location.c_str(), dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 310 | if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) { |
| 311 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid " |
| 312 | " dex file version '%s'", GetLocation().c_str(), i, |
| 313 | dex_file_location.c_str(), dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 314 | return false; |
| 315 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 316 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
| 317 | const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 318 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 319 | oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 320 | if (UNLIKELY(oat > End())) { |
| 321 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated " |
| 322 | " method offsets", GetLocation().c_str(), i, |
| 323 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 324 | return false; |
| 325 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 326 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 327 | std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str()); |
| 328 | |
| 329 | // Create the OatDexFile and add it to the owning container. |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 330 | OatDexFile* oat_dex_file = new OatDexFile(this, |
| 331 | dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 332 | canonical_location, |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 333 | dex_file_checksum, |
| 334 | dex_file_pointer, |
| 335 | methods_offsets_pointer); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 336 | oat_dex_files_storage_.push_back(oat_dex_file); |
| 337 | |
| 338 | // Add the location and canonical location (if different) to the oat_dex_files_ table. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 339 | StringPiece key(oat_dex_file->GetDexFileLocation()); |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 340 | oat_dex_files_.Put(key, oat_dex_file); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 341 | if (canonical_location != dex_file_location) { |
| 342 | StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation()); |
| 343 | oat_dex_files_.Put(canonical_key, oat_dex_file); |
| 344 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 345 | } |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 346 | return true; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 350 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 353 | const uint8_t* OatFile::Begin() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 354 | CHECK(begin_ != NULL); |
| 355 | return begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 358 | const uint8_t* OatFile::End() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 359 | CHECK(end_ != NULL); |
| 360 | return end_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 363 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location, |
| 364 | const uint32_t* dex_location_checksum, |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 365 | bool warn_if_not_found) const { |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 366 | // NOTE: We assume here that the canonical location for a given dex_location never |
| 367 | // changes. If it does (i.e. some symlink used by the filename changes) we may return |
| 368 | // an incorrect OatDexFile. As long as we have a checksum to check, we shall return |
| 369 | // an identical file or fail; otherwise we may see some unpredictable failures. |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 370 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 371 | // TODO: Additional analysis of usage patterns to see if this can be simplified |
| 372 | // without any performance loss, for example by not doing the first lock-free lookup. |
| 373 | |
| 374 | const OatFile::OatDexFile* oat_dex_file = nullptr; |
| 375 | StringPiece key(dex_location); |
| 376 | // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations |
| 377 | // directly mentioned in the oat file and doesn't require locking. |
| 378 | auto primary_it = oat_dex_files_.find(key); |
| 379 | if (primary_it != oat_dex_files_.end()) { |
| 380 | oat_dex_file = primary_it->second; |
| 381 | DCHECK(oat_dex_file != nullptr); |
| 382 | } else { |
| 383 | // This dex_location is not one of the dex locations directly mentioned in the |
| 384 | // oat file. The correct lookup is via the canonical location but first see in |
| 385 | // the secondary_oat_dex_files_ whether we've looked up this location before. |
| 386 | MutexLock mu(Thread::Current(), secondary_lookup_lock_); |
| 387 | auto secondary_lb = secondary_oat_dex_files_.lower_bound(key); |
| 388 | if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) { |
| 389 | oat_dex_file = secondary_lb->second; // May be nullptr. |
| 390 | } else { |
| 391 | // We haven't seen this dex_location before, we must check the canonical location. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 392 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 393 | if (dex_canonical_location != dex_location) { |
| 394 | StringPiece canonical_key(dex_canonical_location); |
| 395 | auto canonical_it = oat_dex_files_.find(canonical_key); |
| 396 | if (canonical_it != oat_dex_files_.end()) { |
| 397 | oat_dex_file = canonical_it->second; |
| 398 | } // else keep nullptr. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 399 | } // else keep nullptr. |
| 400 | |
| 401 | // Copy the key to the string_cache_ and store the result in secondary map. |
| 402 | string_cache_.emplace_back(key.data(), key.length()); |
| 403 | StringPiece key_copy(string_cache_.back()); |
| 404 | secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 407 | if (oat_dex_file != nullptr && |
| 408 | (dex_location_checksum == nullptr || |
| 409 | oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) { |
| 410 | return oat_dex_file; |
| 411 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 412 | |
| 413 | if (warn_if_not_found) { |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 414 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 415 | std::string checksum("<unspecified>"); |
| 416 | if (dex_location_checksum != NULL) { |
| 417 | checksum = StringPrintf("0x%08x", *dex_location_checksum); |
| 418 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 419 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 420 | << " ( canonical path " << dex_canonical_location << ")" |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 421 | << " with checksum " << checksum << " in OatFile " << GetLocation(); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 422 | if (kIsDebugBuild) { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 423 | for (const OatDexFile* odf : oat_dex_files_storage_) { |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 424 | LOG(WARNING) << "OatFile " << GetLocation() |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 425 | << " contains OatDexFile " << odf->GetDexFileLocation() |
| 426 | << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")" |
| 427 | << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum(); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 428 | } |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 429 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 430 | } |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 431 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 432 | return NULL; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 435 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 436 | const std::string& dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 437 | const std::string& canonical_dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 438 | uint32_t dex_file_location_checksum, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 439 | const uint8_t* dex_file_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 440 | const uint32_t* oat_class_offsets_pointer) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 441 | : oat_file_(oat_file), |
| 442 | dex_file_location_(dex_file_location), |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 443 | canonical_dex_file_location_(canonical_dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 444 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 445 | dex_file_pointer_(dex_file_pointer), |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 446 | oat_class_offsets_pointer_(oat_class_offsets_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 447 | |
| 448 | OatFile::OatDexFile::~OatDexFile() {} |
| 449 | |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 450 | size_t OatFile::OatDexFile::FileSize() const { |
| 451 | return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 452 | } |
| 453 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 454 | const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const { |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 455 | return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 456 | dex_file_location_checksum_, error_msg); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 459 | uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const { |
| 460 | return oat_class_offsets_pointer_[class_def_index]; |
| 461 | } |
| 462 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 463 | OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 464 | uint32_t oat_class_offset = GetOatClassOffset(class_def_index); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 465 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 466 | const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 467 | CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 468 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 469 | const uint8_t* status_pointer = oat_class_pointer; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 470 | CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 471 | mirror::Class::Status status = |
| 472 | static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer)); |
| 473 | CHECK_LT(status, mirror::Class::kStatusMax); |
| 474 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 475 | const uint8_t* type_pointer = status_pointer + sizeof(uint16_t); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 476 | CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 477 | OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer)); |
| 478 | CHECK_LT(type, kOatClassMax); |
| 479 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 480 | const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 481 | CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 482 | |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 483 | uint32_t bitmap_size = 0; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 484 | const uint8_t* bitmap_pointer = nullptr; |
| 485 | const uint8_t* methods_pointer = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 486 | if (type != kOatClassNoneCompiled) { |
| 487 | if (type == kOatClassSomeCompiled) { |
| 488 | bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer)); |
| 489 | bitmap_pointer = after_type_pointer + sizeof(bitmap_size); |
| 490 | CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 491 | methods_pointer = bitmap_pointer + bitmap_size; |
| 492 | } else { |
| 493 | methods_pointer = after_type_pointer; |
| 494 | } |
| 495 | CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 496 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 497 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 498 | return OatClass(oat_file_, |
| 499 | status, |
| 500 | type, |
| 501 | bitmap_size, |
| 502 | reinterpret_cast<const uint32_t*>(bitmap_pointer), |
| 503 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 506 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 507 | mirror::Class::Status status, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 508 | OatClassType type, |
| 509 | uint32_t bitmap_size, |
| 510 | const uint32_t* bitmap_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 511 | const OatMethodOffsets* methods_pointer) |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 512 | : oat_file_(oat_file), status_(status), type_(type), |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 513 | bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 514 | switch (type_) { |
| 515 | case kOatClassAllCompiled: { |
| 516 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 517 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 518 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 519 | break; |
| 520 | } |
| 521 | case kOatClassSomeCompiled: { |
| 522 | CHECK_NE(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 523 | CHECK(bitmap_pointer != nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 524 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | case kOatClassNoneCompiled: { |
| 528 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 529 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 530 | CHECK(methods_pointer_ == nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 531 | break; |
| 532 | } |
| 533 | case kOatClassMax: { |
| 534 | LOG(FATAL) << "Invalid OatClassType " << type_; |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 539 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 540 | uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const { |
| 541 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 542 | if (oat_method_offsets == nullptr) { |
| 543 | return 0u; |
| 544 | } |
| 545 | return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin(); |
| 546 | } |
| 547 | |
| 548 | const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 549 | // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index. |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 550 | if (methods_pointer_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 551 | CHECK_EQ(kOatClassNoneCompiled, type_); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 552 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 553 | } |
| 554 | size_t methods_pointer_index; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 555 | if (bitmap_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 556 | CHECK_EQ(kOatClassAllCompiled, type_); |
| 557 | methods_pointer_index = method_index; |
| 558 | } else { |
| 559 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 560 | if (!BitVector::IsBitSet(bitmap_, method_index)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 561 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 562 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 563 | size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index); |
| 564 | methods_pointer_index = num_set_bits; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 565 | } |
| 566 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index]; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 567 | return &oat_method_offsets; |
| 568 | } |
| 569 | |
| 570 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 571 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 572 | if (oat_method_offsets == nullptr) { |
| 573 | return OatMethod(nullptr, 0, 0); |
| 574 | } |
| 575 | if (oat_file_->IsExecutable() || |
| 576 | Runtime::Current() == nullptr || // This case applies for oatdump. |
| 577 | Runtime::Current()->IsCompiler()) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 578 | return OatMethod( |
| 579 | oat_file_->Begin(), |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 580 | oat_method_offsets->code_offset_, |
| 581 | oat_method_offsets->gc_map_offset_); |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 582 | } else { |
| 583 | // We aren't allowed to use the compiled code. We just force it down the interpreted version. |
| 584 | return OatMethod(oat_file_->Begin(), 0, 0); |
| 585 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 588 | void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 589 | CHECK(method != NULL); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 590 | method->SetEntryPointFromPortableCompiledCode(GetPortableCode()); |
| 591 | method->SetEntryPointFromQuickCompiledCode(GetQuickCode()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 592 | method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame^] | 595 | bool OatFile::IsPic() const { |
| 596 | const char* pic_string = GetOatHeader().GetStoreValueByKey(OatHeader::kPicKey); |
| 597 | return (pic_string != nullptr && strncmp(pic_string, "true", 5) == 0); |
| 598 | // TODO: Check against oat_patches. b/18144996 |
| 599 | } |
| 600 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 601 | } // namespace art |