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> |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 20 | #include <string.h> |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 21 | #include <type_traits> |
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 | |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 24 | #include <cstdlib> |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 25 | #ifndef __APPLE__ |
| 26 | #include <link.h> // for dl_iterate_phdr. |
| 27 | #endif |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 28 | #include <sstream> |
| 29 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 30 | // dlopen_ext support from bionic. |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 31 | #ifdef __ANDROID__ |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 32 | #include "android/dlext.h" |
| 33 | #endif |
| 34 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | #include "art_method-inl.h" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 36 | #include "base/bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 37 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 38 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 39 | #include "elf_file.h" |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 40 | #include "elf_utils.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 41 | #include "oat.h" |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 42 | #include "mem_map.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | #include "mirror/class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 44 | #include "mirror/object-inl.h" |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 45 | #include "oat_file-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 46 | #include "oat_file_manager.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 47 | #include "os.h" |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 48 | #include "runtime.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | #include "utils.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 50 | #include "utils/dex_cache_arrays_layout-inl.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 51 | #include "vmap_table.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 52 | |
| 53 | namespace art { |
| 54 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 55 | // Whether OatFile::Open will try DlOpen() first. Fallback is our own ELF loader. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 56 | static constexpr bool kUseDlopen = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 57 | |
| 58 | // Whether OatFile::Open will try DlOpen() on the host. On the host we're not linking against |
| 59 | // bionic, so cannot take advantage of the support for changed semantics (loading the same soname |
| 60 | // multiple times). However, if/when we switch the above, we likely want to switch this, too, |
| 61 | // to get test coverage of the code paths. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 62 | static constexpr bool kUseDlopenOnHost = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 63 | |
| 64 | // For debugging, Open will print DlOpen error message if set to true. |
| 65 | static constexpr bool kPrintDlOpenErrorMessage = false; |
| 66 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 67 | std::string OatFile::ResolveRelativeEncodedDexLocation( |
| 68 | const char* abs_dex_location, const std::string& rel_dex_location) { |
| 69 | if (abs_dex_location != nullptr && rel_dex_location[0] != '/') { |
| 70 | // Strip :classes<N>.dex used for secondary multidex files. |
| 71 | std::string base = DexFile::GetBaseLocation(rel_dex_location); |
| 72 | std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location); |
| 73 | |
| 74 | // Check if the base is a suffix of the provided abs_dex_location. |
| 75 | std::string target_suffix = "/" + base; |
| 76 | std::string abs_location(abs_dex_location); |
| 77 | if (abs_location.size() > target_suffix.size()) { |
| 78 | size_t pos = abs_location.size() - target_suffix.size(); |
| 79 | if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) { |
| 80 | return abs_location + multidex_suffix; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return rel_dex_location; |
| 85 | } |
| 86 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 87 | void OatFile::CheckLocation(const std::string& location) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 88 | CHECK(!location.empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 91 | OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file, |
| 92 | const std::string& location, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 93 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 94 | std::string* error_msg) { |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 95 | std::unique_ptr<OatFile> oat_file(new OatFile(location, false)); |
| 96 | oat_file->elf_file_.reset(elf_file); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 97 | uint64_t offset, size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 98 | bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 99 | CHECK(has_section); |
| 100 | oat_file->begin_ = elf_file->Begin() + offset; |
| 101 | oat_file->end_ = elf_file->Begin() + size + offset; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 102 | // Ignore the optional .bss section when opening non-executable. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 103 | return oat_file->Setup(abs_dex_location, error_msg) ? oat_file.release() : nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | OatFile* OatFile::Open(const std::string& filename, |
| 107 | const std::string& location, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 108 | uint8_t* requested_base, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 109 | uint8_t* oat_file_begin, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 110 | bool executable, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 111 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 112 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 113 | CHECK(!filename.empty()) << location; |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 114 | CheckLocation(location); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 115 | std::unique_ptr<OatFile> ret; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 116 | |
| 117 | // Use dlopen only when flagged to do so, and when it's OK to load things executable. |
| 118 | // TODO: Also try when not executable? The issue here could be re-mapping as writable (as |
| 119 | // !executable is a sign that we may want to patch), which may not be allowed for |
| 120 | // various reasons. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 121 | // dlopen always returns the same library if it is already opened on the host. For this reason |
| 122 | // we only use dlopen if we are the target or we do not already have the dex file opened. Having |
| 123 | // the same library loaded multiple times at different addresses is required for class unloading |
| 124 | // and for having dex caches arrays in the .bss section. |
| 125 | Runtime* const runtime = Runtime::Current(); |
| 126 | OatFileManager* const manager = (runtime != nullptr) ? &runtime->GetOatFileManager() : nullptr; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 127 | if (kUseDlopen && executable) { |
| 128 | bool success = kIsTargetBuild; |
| 129 | bool reserved_location = false; |
| 130 | // Manager may be null if we are running without a runtime. |
| 131 | if (!success && kUseDlopenOnHost && manager != nullptr) { |
Mathieu Chartier | 24a0fc8 | 2015-10-13 16:38:52 -0700 | [diff] [blame] | 132 | // RegisterOatFileLocation returns false if we are not the first caller to register that |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 133 | // location. |
| 134 | reserved_location = manager->RegisterOatFileLocation(location); |
| 135 | success = reserved_location; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 136 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 137 | if (success) { |
| 138 | // Try to use dlopen. This may fail for various reasons, outlined below. We try dlopen, as |
| 139 | // this will register the oat file with the linker and allows libunwind to find our info. |
| 140 | ret.reset(OpenDlopen(filename, location, requested_base, abs_dex_location, error_msg)); |
| 141 | if (reserved_location) { |
| 142 | manager->UnRegisterOatFileLocation(location); |
| 143 | } |
| 144 | if (ret != nullptr) { |
| 145 | return ret.release(); |
| 146 | } |
| 147 | if (kPrintDlOpenErrorMessage) { |
| 148 | LOG(ERROR) << "Failed to dlopen: " << *error_msg; |
| 149 | } |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 153 | // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: |
| 154 | // |
| 155 | // On target, dlopen may fail when compiling due to selinux restrictions on installd. |
| 156 | // |
| 157 | // We use our own ELF loader for Quick to deal with legacy apps that |
| 158 | // open a generated dex file by name, remove the file, then open |
| 159 | // another generated dex file with the same name. http://b/10614658 |
| 160 | // |
| 161 | // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 162 | // |
| 163 | // |
| 164 | // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually |
| 165 | // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 166 | std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str())); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 167 | if (file == nullptr) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 168 | *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno)); |
| 169 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 170 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 171 | ret.reset(OpenElfFile(file.get(), location, requested_base, oat_file_begin, false, executable, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 172 | abs_dex_location, error_msg)); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 173 | |
| 174 | // It would be nice to unlink here. But we might have opened the file created by the |
| 175 | // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API |
| 176 | // to allow removal when we know the ELF must be borked. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 177 | return ret.release(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 180 | OatFile* OatFile::OpenWritable(File* file, const std::string& location, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 181 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 182 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 183 | CheckLocation(location); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 184 | return OpenElfFile(file, location, nullptr, nullptr, true, false, abs_dex_location, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 187 | OatFile* OatFile::OpenReadable(File* file, const std::string& location, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 188 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 189 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 190 | CheckLocation(location); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 191 | return OpenElfFile(file, location, nullptr, nullptr, false, false, abs_dex_location, error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 194 | OatFile* OatFile::OpenDlopen(const std::string& elf_filename, |
| 195 | const std::string& location, |
| 196 | uint8_t* requested_base, |
| 197 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 198 | std::string* error_msg) { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 199 | std::unique_ptr<OatFile> oat_file(new OatFile(location, true)); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 200 | bool success = oat_file->Dlopen(elf_filename, requested_base, abs_dex_location, error_msg); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 201 | if (!success) { |
| 202 | return nullptr; |
| 203 | } |
| 204 | return oat_file.release(); |
| 205 | } |
| 206 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 207 | OatFile* OatFile::OpenElfFile(File* file, |
| 208 | const std::string& location, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 209 | uint8_t* requested_base, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 210 | uint8_t* oat_file_begin, |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 211 | bool writable, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 212 | bool executable, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 213 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 214 | std::string* error_msg) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 215 | std::unique_ptr<OatFile> oat_file(new OatFile(location, executable)); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 216 | bool success = oat_file->ElfFileOpen(file, requested_base, oat_file_begin, writable, executable, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 217 | abs_dex_location, error_msg); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 218 | if (!success) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 219 | CHECK(!error_msg->empty()); |
| 220 | return nullptr; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 221 | } |
| 222 | return oat_file.release(); |
| 223 | } |
| 224 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 225 | OatFile::OatFile(const std::string& location, bool is_executable) |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 226 | : location_(location), begin_(nullptr), end_(nullptr), bss_begin_(nullptr), bss_end_(nullptr), |
| 227 | is_executable_(is_executable), dlopen_handle_(nullptr), |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 228 | secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 229 | CHECK(!location_.empty()); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 230 | Runtime* const runtime = Runtime::Current(); |
| 231 | if (runtime != nullptr && !runtime->IsAotCompiler()) { |
| 232 | runtime->GetOatFileManager().RegisterOatFileLocation(location); |
| 233 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 234 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 235 | |
| 236 | OatFile::~OatFile() { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 237 | STLDeleteElements(&oat_dex_files_storage_); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 238 | if (dlopen_handle_ != nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 239 | dlclose(dlopen_handle_); |
| 240 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 241 | Runtime* const runtime = Runtime::Current(); |
| 242 | if (runtime != nullptr && !runtime->IsAotCompiler()) { |
| 243 | runtime->GetOatFileManager().UnRegisterOatFileLocation(location_); |
| 244 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 247 | bool OatFile::Dlopen(const std::string& elf_filename, uint8_t* requested_base, |
| 248 | const char* abs_dex_location, std::string* error_msg) { |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 249 | #ifdef __APPLE__ |
| 250 | // The dl_iterate_phdr syscall is missing. There is similar API on OSX, |
| 251 | // but let's fallback to the custom loading code for the time being. |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 252 | UNUSED(elf_filename, requested_base, abs_dex_location, error_msg); |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 253 | return false; |
| 254 | #else |
Vladimir Marko | 6958e4f | 2015-09-17 20:22:02 +0100 | [diff] [blame] | 255 | { |
| 256 | UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr)); |
| 257 | if (absolute_path == nullptr) { |
| 258 | *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str()); |
| 259 | return false; |
| 260 | } |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 261 | #ifdef __ANDROID__ |
Vladimir Marko | 6958e4f | 2015-09-17 20:22:02 +0100 | [diff] [blame] | 262 | android_dlextinfo extinfo; |
| 263 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | ANDROID_DLEXT_FORCE_FIXED_VADDR; |
| 264 | dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 265 | #else |
Vladimir Marko | 6958e4f | 2015-09-17 20:22:02 +0100 | [diff] [blame] | 266 | dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 267 | #endif |
Vladimir Marko | 6958e4f | 2015-09-17 20:22:02 +0100 | [diff] [blame] | 268 | } |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 269 | if (dlopen_handle_ == nullptr) { |
| 270 | *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror()); |
| 271 | return false; |
| 272 | } |
| 273 | begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatdata")); |
| 274 | if (begin_ == nullptr) { |
| 275 | *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(), |
| 276 | dlerror()); |
| 277 | return false; |
| 278 | } |
| 279 | if (requested_base != nullptr && begin_ != requested_base) { |
| 280 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 281 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
| 282 | "oatdata=%p != expected=%p, %s. See process maps in the log.", |
| 283 | begin_, requested_base, elf_filename.c_str()); |
| 284 | return false; |
| 285 | } |
| 286 | end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatlastword")); |
| 287 | if (end_ == nullptr) { |
| 288 | *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(), |
| 289 | dlerror()); |
| 290 | return false; |
| 291 | } |
| 292 | // Readjust to be non-inclusive upper bound. |
| 293 | end_ += sizeof(uint32_t); |
| 294 | |
| 295 | bss_begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbss")); |
| 296 | if (bss_begin_ == nullptr) { |
| 297 | // No .bss section. Clear dlerror(). |
| 298 | bss_end_ = nullptr; |
| 299 | dlerror(); |
| 300 | } else { |
| 301 | bss_end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbsslastword")); |
| 302 | if (bss_end_ == nullptr) { |
| 303 | *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", |
| 304 | elf_filename.c_str()); |
| 305 | return false; |
| 306 | } |
| 307 | // Readjust to be non-inclusive upper bound. |
| 308 | bss_end_ += sizeof(uint32_t); |
| 309 | } |
| 310 | |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 311 | // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions. |
| 312 | struct dl_iterate_context { |
| 313 | static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) { |
| 314 | auto* context = reinterpret_cast<dl_iterate_context*>(data); |
David Srbecky | aa03870 | 2015-06-17 02:11:07 +0100 | [diff] [blame] | 315 | // See whether this callback corresponds to the file which we have just loaded. |
| 316 | bool contains_begin = false; |
| 317 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 318 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 319 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 320 | info->dlpi_phdr[i].p_vaddr); |
| 321 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
| 322 | if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) { |
| 323 | contains_begin = true; |
| 324 | break; |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | } |
David Srbecky | aa03870 | 2015-06-17 02:11:07 +0100 | [diff] [blame] | 328 | // Add dummy mmaps for this file. |
| 329 | if (contains_begin) { |
| 330 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 331 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 332 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 333 | info->dlpi_phdr[i].p_vaddr); |
| 334 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
David Srbecky | 82e73dc | 2015-06-17 18:36:23 +0100 | [diff] [blame] | 335 | MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz); |
| 336 | context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap)); |
David Srbecky | aa03870 | 2015-06-17 02:11:07 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | return 1; // Stop iteration and return 1 from dl_iterate_phdr. |
| 340 | } |
| 341 | return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 342 | } |
David Srbecky | 82e73dc | 2015-06-17 18:36:23 +0100 | [diff] [blame] | 343 | const uint8_t* const begin_; |
| 344 | std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_; |
| 345 | } context = { begin_, &dlopen_mmaps_ }; |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 346 | |
David Srbecky | aa03870 | 2015-06-17 02:11:07 +0100 | [diff] [blame] | 347 | if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) { |
David Srbecky | 5dedb80 | 2015-06-17 00:08:02 +0100 | [diff] [blame] | 348 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 349 | LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but can not find its mmaps."; |
| 350 | } |
| 351 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 352 | return Setup(abs_dex_location, error_msg); |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 353 | #endif // __APPLE__ |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 356 | bool OatFile::ElfFileOpen(File* file, uint8_t* requested_base, uint8_t* oat_file_begin, |
| 357 | bool writable, bool executable, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 358 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 359 | std::string* error_msg) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 360 | // TODO: rename requested_base to oat_data_begin |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 361 | elf_file_.reset(ElfFile::Open(file, writable, /*program_header_only*/true, error_msg, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 362 | oat_file_begin)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 363 | if (elf_file_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 364 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 365 | return false; |
| 366 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 367 | bool loaded = elf_file_->Load(executable, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 368 | if (!loaded) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 369 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | begin_ = elf_file_->FindDynamicSymbolAddress("oatdata"); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 373 | if (begin_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 374 | *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] | 375 | return false; |
| 376 | } |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 377 | if (requested_base != nullptr && begin_ != requested_base) { |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 378 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 379 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 380 | "oatdata=%p != expected=%p. See process maps in the log.", |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 381 | begin_, requested_base); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 382 | return false; |
| 383 | } |
| 384 | end_ = elf_file_->FindDynamicSymbolAddress("oatlastword"); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 385 | if (end_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 386 | *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] | 387 | return false; |
| 388 | } |
| 389 | // Readjust to be non-inclusive upper bound. |
| 390 | end_ += sizeof(uint32_t); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 391 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 392 | bss_begin_ = const_cast<uint8_t*>(elf_file_->FindDynamicSymbolAddress("oatbss")); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 393 | if (bss_begin_ == nullptr) { |
| 394 | // No .bss section. Clear dlerror(). |
| 395 | bss_end_ = nullptr; |
| 396 | dlerror(); |
| 397 | } else { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 398 | bss_end_ = const_cast<uint8_t*>(elf_file_->FindDynamicSymbolAddress("oatbsslastword")); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 399 | if (bss_end_ == nullptr) { |
| 400 | *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", |
| 401 | file->GetPath().c_str()); |
| 402 | return false; |
| 403 | } |
| 404 | // Readjust to be non-inclusive upper bound. |
| 405 | bss_end_ += sizeof(uint32_t); |
| 406 | } |
| 407 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 408 | return Setup(abs_dex_location, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 409 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 410 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 411 | // Read an unaligned entry from the OatDexFile data in OatFile and advance the read |
| 412 | // position by the number of bytes read, i.e. sizeof(T). |
| 413 | // Return true on success, false if the read would go beyond the end of the OatFile. |
| 414 | template <typename T> |
Vladimir Marko | 722fa98 | 2015-10-19 18:18:27 +0100 | [diff] [blame] | 415 | inline static bool ReadOatDexFileData(const OatFile& oat_file, |
| 416 | /*inout*/const uint8_t** oat, |
| 417 | /*out*/T* value) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 418 | DCHECK(oat != nullptr); |
| 419 | DCHECK(value != nullptr); |
| 420 | DCHECK_LE(*oat, oat_file.End()); |
| 421 | if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) { |
| 422 | return false; |
| 423 | } |
| 424 | static_assert(std::is_trivial<T>::value, "T must be a trivial type"); |
| 425 | typedef __attribute__((__aligned__(1))) T unaligned_type; |
| 426 | *value = *reinterpret_cast<const unaligned_type*>(*oat); |
| 427 | *oat += sizeof(T); |
| 428 | return true; |
| 429 | } |
| 430 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 431 | bool OatFile::Setup(const char* abs_dex_location, std::string* error_msg) { |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 432 | if (!GetOatHeader().IsValid()) { |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 433 | std::string cause = GetOatHeader().GetValidationErrorMessage(); |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 434 | *error_msg = StringPrintf("Invalid oat header for '%s': %s", |
| 435 | GetLocation().c_str(), |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 436 | cause.c_str()); |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 437 | return false; |
| 438 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 439 | const uint8_t* oat = Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 440 | oat += sizeof(OatHeader); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 441 | if (oat > End()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 442 | *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] | 443 | return false; |
| 444 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 445 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 446 | oat += GetOatHeader().GetKeyValueStoreSize(); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 447 | if (oat > End()) { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 448 | *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: " |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 449 | "%p + %zu + %u <= %p", |
| 450 | GetLocation().c_str(), |
| 451 | Begin(), |
| 452 | sizeof(OatHeader), |
| 453 | GetOatHeader().GetKeyValueStoreSize(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 454 | End()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 455 | return false; |
| 456 | } |
| 457 | |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 458 | size_t pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet()); |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 459 | uint8_t* dex_cache_arrays = bss_begin_; |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 460 | uint32_t dex_file_count = GetOatHeader().GetDexFileCount(); |
| 461 | oat_dex_files_storage_.reserve(dex_file_count); |
| 462 | for (size_t i = 0; i < dex_file_count; i++) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 463 | uint32_t dex_file_location_size; |
| 464 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) { |
| 465 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file " |
| 466 | "location size", |
| 467 | GetLocation().c_str(), |
| 468 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 469 | return false; |
| 470 | } |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 471 | if (UNLIKELY(dex_file_location_size == 0U)) { |
| 472 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name", |
| 473 | GetLocation().c_str(), |
| 474 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 475 | return false; |
| 476 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 477 | |
| 478 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 479 | oat += dex_file_location_size; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 480 | if (UNLIKELY(oat > End())) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 481 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file " |
| 482 | "location", |
| 483 | GetLocation().c_str(), |
| 484 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 485 | return false; |
| 486 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 487 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 488 | std::string dex_file_location = ResolveRelativeEncodedDexLocation( |
| 489 | abs_dex_location, |
| 490 | std::string(dex_file_location_data, dex_file_location_size)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 491 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 492 | uint32_t dex_file_checksum; |
| 493 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) { |
| 494 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after " |
| 495 | "dex file checksum", |
| 496 | GetLocation().c_str(), |
| 497 | i, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 498 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 499 | return false; |
| 500 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 501 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 502 | uint32_t dex_file_offset; |
| 503 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) { |
| 504 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated " |
| 505 | "after dex file offsets", |
| 506 | GetLocation().c_str(), |
| 507 | i, |
| 508 | dex_file_location.c_str()); |
| 509 | return false; |
| 510 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 511 | if (UNLIKELY(dex_file_offset == 0U)) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 512 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex " |
| 513 | "file offset", |
| 514 | GetLocation().c_str(), |
| 515 | i, |
| 516 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 517 | return false; |
| 518 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 519 | if (UNLIKELY(dex_file_offset > Size())) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 520 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 521 | "offset %u > %zu", |
| 522 | GetLocation().c_str(), |
| 523 | i, |
| 524 | dex_file_location.c_str(), |
| 525 | dex_file_offset, |
| 526 | Size()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 527 | return false; |
| 528 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 529 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 530 | const uint8_t* dex_file_pointer = Begin() + dex_file_offset; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 531 | if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 532 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " |
| 533 | "dex file magic '%s'", |
| 534 | GetLocation().c_str(), |
| 535 | i, |
| 536 | dex_file_location.c_str(), |
| 537 | dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 538 | return false; |
| 539 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 540 | if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 541 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " |
| 542 | "dex file version '%s'", |
| 543 | GetLocation().c_str(), |
| 544 | i, |
| 545 | dex_file_location.c_str(), |
| 546 | dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 547 | return false; |
| 548 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 549 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 550 | |
| 551 | if (UNLIKELY(oat > End())) { |
| 552 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated " |
| 553 | "lookup table offset", GetLocation().c_str(), i, |
| 554 | dex_file_location.c_str()); |
| 555 | return false; |
| 556 | } |
| 557 | uint32_t lookup_table_offset = *reinterpret_cast<const uint32_t*>(oat); |
| 558 | oat += sizeof(lookup_table_offset); |
| 559 | if (Begin() + lookup_table_offset > End()) { |
| 560 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated " |
| 561 | "lookup table", GetLocation().c_str(), i, |
| 562 | dex_file_location.c_str()); |
| 563 | return false; |
| 564 | } |
| 565 | const uint8_t* lookup_table_data = lookup_table_offset != 0u |
| 566 | ? Begin() + lookup_table_offset |
| 567 | : nullptr; |
| 568 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 569 | const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 570 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 571 | oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 572 | if (UNLIKELY(oat > End())) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 573 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated " |
| 574 | "method offsets", |
| 575 | GetLocation().c_str(), |
| 576 | i, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 577 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 578 | return false; |
| 579 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 580 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 581 | uint8_t* current_dex_cache_arrays = nullptr; |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 582 | if (dex_cache_arrays != nullptr) { |
| 583 | DexCacheArraysLayout layout(pointer_size, *header); |
| 584 | if (layout.Size() != 0u) { |
| 585 | if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 586 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with " |
| 587 | "truncated dex cache arrays, %zu < %zu.", |
| 588 | GetLocation().c_str(), |
| 589 | i, |
| 590 | dex_file_location.c_str(), |
| 591 | static_cast<size_t>(bss_end_ - dex_cache_arrays), |
| 592 | layout.Size()); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 593 | return false; |
| 594 | } |
| 595 | current_dex_cache_arrays = dex_cache_arrays; |
| 596 | dex_cache_arrays += layout.Size(); |
| 597 | } |
| 598 | } |
| 599 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 600 | std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str()); |
| 601 | |
| 602 | // Create the OatDexFile and add it to the owning container. |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 603 | OatDexFile* oat_dex_file = new OatDexFile(this, |
| 604 | dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 605 | canonical_location, |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 606 | dex_file_checksum, |
| 607 | dex_file_pointer, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 608 | lookup_table_data, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 609 | methods_offsets_pointer, |
| 610 | current_dex_cache_arrays); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 611 | oat_dex_files_storage_.push_back(oat_dex_file); |
| 612 | |
| 613 | // 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] | 614 | StringPiece key(oat_dex_file->GetDexFileLocation()); |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 615 | oat_dex_files_.Put(key, oat_dex_file); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 616 | if (canonical_location != dex_file_location) { |
| 617 | StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation()); |
| 618 | oat_dex_files_.Put(canonical_key, oat_dex_file); |
| 619 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 620 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 621 | |
| 622 | if (dex_cache_arrays != bss_end_) { |
| 623 | // We expect the bss section to be either empty (dex_cache_arrays and bss_end_ |
| 624 | // both null) or contain just the dex cache arrays and nothing else. |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 625 | *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.", |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 626 | GetLocation().c_str(), |
| 627 | static_cast<size_t>(bss_end_ - dex_cache_arrays)); |
| 628 | return false; |
| 629 | } |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 630 | return true; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 634 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 637 | const uint8_t* OatFile::Begin() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 638 | CHECK(begin_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 639 | return begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 642 | const uint8_t* OatFile::End() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 643 | CHECK(end_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 644 | return end_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 647 | const uint8_t* OatFile::BssBegin() const { |
| 648 | return bss_begin_; |
| 649 | } |
| 650 | |
| 651 | const uint8_t* OatFile::BssEnd() const { |
| 652 | return bss_end_; |
| 653 | } |
| 654 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 655 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location, |
| 656 | const uint32_t* dex_location_checksum, |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 657 | bool warn_if_not_found) const { |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 658 | // NOTE: We assume here that the canonical location for a given dex_location never |
| 659 | // changes. If it does (i.e. some symlink used by the filename changes) we may return |
| 660 | // an incorrect OatDexFile. As long as we have a checksum to check, we shall return |
| 661 | // an identical file or fail; otherwise we may see some unpredictable failures. |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 662 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 663 | // TODO: Additional analysis of usage patterns to see if this can be simplified |
| 664 | // without any performance loss, for example by not doing the first lock-free lookup. |
| 665 | |
| 666 | const OatFile::OatDexFile* oat_dex_file = nullptr; |
| 667 | StringPiece key(dex_location); |
| 668 | // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations |
| 669 | // directly mentioned in the oat file and doesn't require locking. |
| 670 | auto primary_it = oat_dex_files_.find(key); |
| 671 | if (primary_it != oat_dex_files_.end()) { |
| 672 | oat_dex_file = primary_it->second; |
| 673 | DCHECK(oat_dex_file != nullptr); |
| 674 | } else { |
| 675 | // This dex_location is not one of the dex locations directly mentioned in the |
| 676 | // oat file. The correct lookup is via the canonical location but first see in |
| 677 | // the secondary_oat_dex_files_ whether we've looked up this location before. |
| 678 | MutexLock mu(Thread::Current(), secondary_lookup_lock_); |
| 679 | auto secondary_lb = secondary_oat_dex_files_.lower_bound(key); |
| 680 | if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 681 | oat_dex_file = secondary_lb->second; // May be null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 682 | } else { |
| 683 | // 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] | 684 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 685 | if (dex_canonical_location != dex_location) { |
| 686 | StringPiece canonical_key(dex_canonical_location); |
| 687 | auto canonical_it = oat_dex_files_.find(canonical_key); |
| 688 | if (canonical_it != oat_dex_files_.end()) { |
| 689 | oat_dex_file = canonical_it->second; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 690 | } // else keep null. |
| 691 | } // else keep null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 692 | |
| 693 | // Copy the key to the string_cache_ and store the result in secondary map. |
| 694 | string_cache_.emplace_back(key.data(), key.length()); |
| 695 | StringPiece key_copy(string_cache_.back()); |
| 696 | secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 697 | } |
| 698 | } |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 699 | if (oat_dex_file != nullptr && |
| 700 | (dex_location_checksum == nullptr || |
| 701 | oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) { |
| 702 | return oat_dex_file; |
| 703 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 704 | |
| 705 | if (warn_if_not_found) { |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 706 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 707 | std::string checksum("<unspecified>"); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 708 | if (dex_location_checksum != nullptr) { |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 709 | checksum = StringPrintf("0x%08x", *dex_location_checksum); |
| 710 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 711 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 712 | << " ( canonical path " << dex_canonical_location << ")" |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 713 | << " with checksum " << checksum << " in OatFile " << GetLocation(); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 714 | if (kIsDebugBuild) { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 715 | for (const OatDexFile* odf : oat_dex_files_storage_) { |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 716 | LOG(WARNING) << "OatFile " << GetLocation() |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 717 | << " contains OatDexFile " << odf->GetDexFileLocation() |
| 718 | << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")" |
| 719 | << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum(); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 720 | } |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 721 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 722 | } |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 723 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 724 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 727 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 728 | const std::string& dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 729 | const std::string& canonical_dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 730 | uint32_t dex_file_location_checksum, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 731 | const uint8_t* dex_file_pointer, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 732 | const uint8_t* lookup_table_data, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 733 | const uint32_t* oat_class_offsets_pointer, |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 734 | uint8_t* dex_cache_arrays) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 735 | : oat_file_(oat_file), |
| 736 | dex_file_location_(dex_file_location), |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 737 | canonical_dex_file_location_(canonical_dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 738 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 739 | dex_file_pointer_(dex_file_pointer), |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 740 | lookup_table_data_(lookup_table_data), |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 741 | oat_class_offsets_pointer_(oat_class_offsets_pointer), |
| 742 | dex_cache_arrays_(dex_cache_arrays) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 743 | |
| 744 | OatFile::OatDexFile::~OatDexFile() {} |
| 745 | |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 746 | size_t OatFile::OatDexFile::FileSize() const { |
| 747 | return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 748 | } |
| 749 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 750 | std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const { |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 751 | return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 752 | dex_file_location_checksum_, this, error_msg); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 755 | uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const { |
| 756 | return oat_class_offsets_pointer_[class_def_index]; |
| 757 | } |
| 758 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 759 | OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 760 | uint32_t oat_class_offset = GetOatClassOffset(class_def_index); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 761 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 762 | const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 763 | CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 764 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 765 | const uint8_t* status_pointer = oat_class_pointer; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 766 | CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 767 | mirror::Class::Status status = |
| 768 | static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer)); |
| 769 | CHECK_LT(status, mirror::Class::kStatusMax); |
| 770 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 771 | const uint8_t* type_pointer = status_pointer + sizeof(uint16_t); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 772 | CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 773 | OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer)); |
| 774 | CHECK_LT(type, kOatClassMax); |
| 775 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 776 | const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 777 | CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 778 | |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 779 | uint32_t bitmap_size = 0; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 780 | const uint8_t* bitmap_pointer = nullptr; |
| 781 | const uint8_t* methods_pointer = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 782 | if (type != kOatClassNoneCompiled) { |
| 783 | if (type == kOatClassSomeCompiled) { |
| 784 | bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer)); |
| 785 | bitmap_pointer = after_type_pointer + sizeof(bitmap_size); |
| 786 | CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 787 | methods_pointer = bitmap_pointer + bitmap_size; |
| 788 | } else { |
| 789 | methods_pointer = after_type_pointer; |
| 790 | } |
| 791 | CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 792 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 793 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 794 | return OatFile::OatClass(oat_file_, |
| 795 | status, |
| 796 | type, |
| 797 | bitmap_size, |
| 798 | reinterpret_cast<const uint32_t*>(bitmap_pointer), |
| 799 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 802 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 803 | mirror::Class::Status status, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 804 | OatClassType type, |
| 805 | uint32_t bitmap_size, |
| 806 | const uint32_t* bitmap_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 807 | const OatMethodOffsets* methods_pointer) |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 808 | : oat_file_(oat_file), status_(status), type_(type), |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 809 | bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 810 | switch (type_) { |
| 811 | case kOatClassAllCompiled: { |
| 812 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 813 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 814 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 815 | break; |
| 816 | } |
| 817 | case kOatClassSomeCompiled: { |
| 818 | CHECK_NE(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 819 | CHECK(bitmap_pointer != nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 820 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 821 | break; |
| 822 | } |
| 823 | case kOatClassNoneCompiled: { |
| 824 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 825 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 826 | CHECK(methods_pointer_ == nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 827 | break; |
| 828 | } |
| 829 | case kOatClassMax: { |
| 830 | LOG(FATAL) << "Invalid OatClassType " << type_; |
| 831 | break; |
| 832 | } |
| 833 | } |
| 834 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 835 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 836 | uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const { |
| 837 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 838 | if (oat_method_offsets == nullptr) { |
| 839 | return 0u; |
| 840 | } |
| 841 | return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin(); |
| 842 | } |
| 843 | |
| 844 | const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 845 | // 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] | 846 | if (methods_pointer_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 847 | CHECK_EQ(kOatClassNoneCompiled, type_); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 848 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 849 | } |
| 850 | size_t methods_pointer_index; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 851 | if (bitmap_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 852 | CHECK_EQ(kOatClassAllCompiled, type_); |
| 853 | methods_pointer_index = method_index; |
| 854 | } else { |
| 855 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 856 | if (!BitVector::IsBitSet(bitmap_, method_index)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 857 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 858 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 859 | size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index); |
| 860 | methods_pointer_index = num_set_bits; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 861 | } |
| 862 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index]; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 863 | return &oat_method_offsets; |
| 864 | } |
| 865 | |
| 866 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 867 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 868 | if (oat_method_offsets == nullptr) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 869 | return OatMethod(nullptr, 0); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 870 | } |
| 871 | if (oat_file_->IsExecutable() || |
| 872 | Runtime::Current() == nullptr || // This case applies for oatdump. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 873 | Runtime::Current()->IsAotCompiler()) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 874 | return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_); |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 875 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 876 | // We aren't allowed to use the compiled code. We just force it down the interpreted / jit |
| 877 | // version. |
| 878 | return OatMethod(oat_file_->Begin(), 0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 881 | void OatFile::OatMethod::LinkMethod(ArtMethod* method) const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 882 | CHECK(method != nullptr); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 883 | method->SetEntryPointFromQuickCompiledCode(GetQuickCode()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 886 | bool OatFile::IsPic() const { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 887 | return GetOatHeader().IsPic(); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 888 | // TODO: Check against oat_patches. b/18144996 |
| 889 | } |
| 890 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 891 | bool OatFile::IsDebuggable() const { |
| 892 | return GetOatHeader().IsDebuggable(); |
| 893 | } |
| 894 | |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 895 | static constexpr char kDexClassPathEncodingSeparator = '*'; |
| 896 | |
| 897 | std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) { |
| 898 | std::ostringstream out; |
| 899 | |
| 900 | for (const DexFile* dex_file : dex_files) { |
| 901 | out << dex_file->GetLocation().c_str(); |
| 902 | out << kDexClassPathEncodingSeparator; |
| 903 | out << dex_file->GetLocationChecksum(); |
| 904 | out << kDexClassPathEncodingSeparator; |
| 905 | } |
| 906 | |
| 907 | return out.str(); |
| 908 | } |
| 909 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 910 | bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 911 | if (dex_dependencies == nullptr || dex_dependencies[0] == 0) { |
| 912 | // No dependencies. |
| 913 | return true; |
| 914 | } |
| 915 | |
| 916 | // Assumption: this is not performance-critical. So it's OK to do this with a std::string and |
| 917 | // Split() instead of manual parsing of the combined char*. |
| 918 | std::vector<std::string> split; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 919 | Split(dex_dependencies, kDexClassPathEncodingSeparator, &split); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 920 | if (split.size() % 2 != 0) { |
| 921 | // Expected pairs of location and checksum. |
| 922 | *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies); |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | for (auto it = split.begin(), end = split.end(); it != end; it += 2) { |
| 927 | std::string& location = *it; |
| 928 | std::string& checksum = *(it + 1); |
| 929 | int64_t converted = strtoll(checksum.c_str(), nullptr, 10); |
| 930 | if (converted == 0) { |
| 931 | // Conversion error. |
| 932 | *msg = StringPrintf("Conversion error for %s", checksum.c_str()); |
| 933 | return false; |
| 934 | } |
| 935 | |
| 936 | uint32_t dex_checksum; |
| 937 | std::string error_msg; |
| 938 | if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 939 | &dex_checksum, |
| 940 | &error_msg)) { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 941 | if (converted != dex_checksum) { |
| 942 | *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u", |
| 943 | location.c_str(), converted, dex_checksum); |
| 944 | return false; |
| 945 | } |
| 946 | } else { |
| 947 | // Problem retrieving checksum. |
| 948 | // TODO: odex files? |
| 949 | *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(), |
| 950 | error_msg.c_str()); |
| 951 | return false; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | return true; |
| 956 | } |
| 957 | |
| 958 | bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 959 | std::vector<std::string>* locations) { |
| 960 | DCHECK(locations != nullptr); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 961 | if (dex_dependencies == nullptr || dex_dependencies[0] == 0) { |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | // Assumption: this is not performance-critical. So it's OK to do this with a std::string and |
| 966 | // Split() instead of manual parsing of the combined char*. |
| 967 | std::vector<std::string> split; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 968 | Split(dex_dependencies, kDexClassPathEncodingSeparator, &split); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 969 | if (split.size() % 2 != 0) { |
| 970 | // Expected pairs of location and checksum. |
| 971 | return false; |
| 972 | } |
| 973 | |
| 974 | for (auto it = split.begin(), end = split.end(); it != end; it += 2) { |
| 975 | locations->push_back(*it); |
| 976 | } |
| 977 | |
| 978 | return true; |
| 979 | } |
| 980 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 981 | } // namespace art |