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. |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 31 | #ifdef ART_TARGET_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" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 37 | #include "base/enums.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 38 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 39 | #include "base/systrace.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 40 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 41 | #include "dex_file_types.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 42 | #include "elf_file.h" |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 43 | #include "elf_utils.h" |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 44 | #include "gc_root.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 45 | #include "oat.h" |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 46 | #include "mem_map.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | #include "mirror/class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 48 | #include "mirror/object-inl.h" |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 49 | #include "oat_file-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 50 | #include "oat_file_manager.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 51 | #include "os.h" |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 52 | #include "runtime.h" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 53 | #include "type_lookup_table.h" |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 54 | #include "utf-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | #include "utils.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 56 | #include "utils/dex_cache_arrays_layout-inl.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 57 | #include "vdex_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 58 | |
| 59 | namespace art { |
| 60 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 61 | // Whether OatFile::Open will try dlopen. Fallback is our own ELF loader. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 62 | static constexpr bool kUseDlopen = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 63 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 64 | // Whether OatFile::Open will try dlopen on the host. On the host we're not linking against |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 65 | // bionic, so cannot take advantage of the support for changed semantics (loading the same soname |
| 66 | // multiple times). However, if/when we switch the above, we likely want to switch this, too, |
| 67 | // to get test coverage of the code paths. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 68 | static constexpr bool kUseDlopenOnHost = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 69 | |
| 70 | // For debugging, Open will print DlOpen error message if set to true. |
| 71 | static constexpr bool kPrintDlOpenErrorMessage = false; |
| 72 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 73 | // Note for OatFileBase and descendents: |
| 74 | // |
| 75 | // These are used in OatFile::Open to try all our loaders. |
| 76 | // |
| 77 | // The process is simple: |
| 78 | // |
| 79 | // 1) Allocate an instance through the standard constructor (location, executable) |
| 80 | // 2) Load() to try to open the file. |
| 81 | // 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress. |
| 82 | // 4) PreSetup() for any steps that should be done before the final setup. |
| 83 | // 5) Setup() to complete the procedure. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 84 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 85 | class OatFileBase : public OatFile { |
| 86 | public: |
| 87 | virtual ~OatFileBase() {} |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 88 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 89 | template <typename kOatFileBaseSubType> |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 90 | static OatFileBase* OpenOatFile(const std::string& vdex_filename, |
| 91 | const std::string& elf_filename, |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 92 | const std::string& location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 93 | uint8_t* requested_base, |
| 94 | uint8_t* oat_file_begin, |
| 95 | bool writable, |
| 96 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 97 | bool low_4gb, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 98 | const char* abs_dex_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 99 | std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 100 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 101 | protected: |
| 102 | OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {} |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 103 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 104 | virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, |
| 105 | std::string* error_msg) const = 0; |
| 106 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 107 | virtual void PreLoad() = 0; |
| 108 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 109 | bool LoadVdex(const std::string& vdex_filename, |
| 110 | bool writable, |
| 111 | bool low_4gb, |
| 112 | std::string* error_msg); |
| 113 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 114 | virtual bool Load(const std::string& elf_filename, |
| 115 | uint8_t* oat_file_begin, |
| 116 | bool writable, |
| 117 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 118 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 119 | std::string* error_msg) = 0; |
| 120 | |
| 121 | bool ComputeFields(uint8_t* requested_base, |
| 122 | const std::string& file_path, |
| 123 | std::string* error_msg); |
| 124 | |
| 125 | virtual void PreSetup(const std::string& elf_filename) = 0; |
| 126 | |
| 127 | bool Setup(const char* abs_dex_location, std::string* error_msg); |
| 128 | |
| 129 | // Setters exposed for ElfOatFile. |
| 130 | |
| 131 | void SetBegin(const uint8_t* begin) { |
| 132 | begin_ = begin; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 135 | void SetEnd(const uint8_t* end) { |
| 136 | end_ = end; |
| 137 | } |
| 138 | |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 139 | void SetVdex(VdexFile* vdex) { |
| 140 | vdex_.reset(vdex); |
| 141 | } |
| 142 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 143 | private: |
| 144 | DISALLOW_COPY_AND_ASSIGN(OatFileBase); |
| 145 | }; |
| 146 | |
| 147 | template <typename kOatFileBaseSubType> |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 148 | OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename, |
| 149 | const std::string& elf_filename, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 150 | const std::string& location, |
| 151 | uint8_t* requested_base, |
| 152 | uint8_t* oat_file_begin, |
| 153 | bool writable, |
| 154 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 155 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 156 | const char* abs_dex_location, |
| 157 | std::string* error_msg) { |
| 158 | std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable)); |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 159 | |
| 160 | ret->PreLoad(); |
| 161 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 162 | if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) { |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 166 | if (!ret->Load(elf_filename, |
| 167 | oat_file_begin, |
| 168 | writable, |
| 169 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 170 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 171 | error_msg)) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 172 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 173 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 174 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 175 | if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) { |
| 176 | return nullptr; |
| 177 | } |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 178 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 179 | ret->PreSetup(elf_filename); |
| 180 | |
| 181 | if (!ret->Setup(abs_dex_location, error_msg)) { |
| 182 | return nullptr; |
| 183 | } |
| 184 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 185 | return ret.release(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 186 | } |
| 187 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 188 | bool OatFileBase::LoadVdex(const std::string& vdex_filename, |
| 189 | bool writable, |
| 190 | bool low_4gb, |
| 191 | std::string* error_msg) { |
| 192 | vdex_.reset(VdexFile::Open(vdex_filename, writable, low_4gb, error_msg)); |
| 193 | if (vdex_.get() == nullptr) { |
| 194 | *error_msg = StringPrintf("Failed to load vdex file '%s' %s", |
| 195 | vdex_filename.c_str(), |
| 196 | error_msg->c_str()); |
| 197 | return false; |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 202 | bool OatFileBase::ComputeFields(uint8_t* requested_base, |
| 203 | const std::string& file_path, |
| 204 | std::string* error_msg) { |
| 205 | std::string symbol_error_msg; |
| 206 | begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 207 | if (begin_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 208 | *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s", |
| 209 | file_path.c_str(), |
| 210 | symbol_error_msg.c_str()); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | if (requested_base != nullptr && begin_ != requested_base) { |
Nicolas Geoffray | f97cf2a | 2016-03-09 15:36:23 +0000 | [diff] [blame] | 214 | // Host can fail this check. Do not dump there to avoid polluting the output. |
Andreas Gampe | 7ec0904 | 2016-04-01 17:20:49 -0700 | [diff] [blame] | 215 | if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) { |
Nicolas Geoffray | f97cf2a | 2016-03-09 15:36:23 +0000 | [diff] [blame] | 216 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 217 | } |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 218 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 219 | "oatdata=%p != expected=%p. See process maps in the log.", |
| 220 | begin_, requested_base); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 221 | return false; |
| 222 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 223 | end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 224 | if (end_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 225 | *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s", |
| 226 | file_path.c_str(), |
| 227 | symbol_error_msg.c_str()); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | // Readjust to be non-inclusive upper bound. |
| 231 | end_ += sizeof(uint32_t); |
| 232 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 233 | bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 234 | if (bss_begin_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 235 | // No .bss section. |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 236 | bss_end_ = nullptr; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 237 | } else { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 238 | bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 239 | if (bss_end_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 240 | *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str()); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | // Readjust to be non-inclusive upper bound. |
| 244 | bss_end_ += sizeof(uint32_t); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 245 | // Find bss roots if present. |
| 246 | bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 249 | return true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 250 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 251 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 252 | // Read an unaligned entry from the OatDexFile data in OatFile and advance the read |
| 253 | // position by the number of bytes read, i.e. sizeof(T). |
| 254 | // Return true on success, false if the read would go beyond the end of the OatFile. |
| 255 | template <typename T> |
Vladimir Marko | 722fa98 | 2015-10-19 18:18:27 +0100 | [diff] [blame] | 256 | inline static bool ReadOatDexFileData(const OatFile& oat_file, |
| 257 | /*inout*/const uint8_t** oat, |
| 258 | /*out*/T* value) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 259 | DCHECK(oat != nullptr); |
| 260 | DCHECK(value != nullptr); |
| 261 | DCHECK_LE(*oat, oat_file.End()); |
| 262 | if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) { |
| 263 | return false; |
| 264 | } |
| 265 | static_assert(std::is_trivial<T>::value, "T must be a trivial type"); |
| 266 | typedef __attribute__((__aligned__(1))) T unaligned_type; |
| 267 | *value = *reinterpret_cast<const unaligned_type*>(*oat); |
| 268 | *oat += sizeof(T); |
| 269 | return true; |
| 270 | } |
| 271 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 272 | bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) { |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 273 | if (!GetOatHeader().IsValid()) { |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 274 | std::string cause = GetOatHeader().GetValidationErrorMessage(); |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 275 | *error_msg = StringPrintf("Invalid oat header for '%s': %s", |
| 276 | GetLocation().c_str(), |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 277 | cause.c_str()); |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 278 | return false; |
| 279 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 280 | const uint8_t* oat = Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 281 | oat += sizeof(OatHeader); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 282 | if (oat > End()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 283 | *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] | 284 | return false; |
| 285 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 286 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 287 | oat += GetOatHeader().GetKeyValueStoreSize(); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 288 | if (oat > End()) { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 289 | *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: " |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 290 | "%p + %zu + %u <= %p", |
| 291 | GetLocation().c_str(), |
| 292 | Begin(), |
| 293 | sizeof(OatHeader), |
| 294 | GetOatHeader().GetKeyValueStoreSize(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 295 | End()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 299 | if (!IsAligned<alignof(GcRoot<mirror::Object>)>(bss_begin_) || |
| 300 | !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_roots_) || |
| 301 | !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) { |
| 302 | *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): " |
| 303 | "begin = %p, roots = %p, end = %p", |
| 304 | GetLocation().c_str(), |
| 305 | bss_begin_, |
| 306 | bss_roots_, |
| 307 | bss_end_); |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | if (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) { |
| 312 | *error_msg = StringPrintf("In oat file '%s' found bss roots outside .bss: " |
| 313 | "%p is outside range [%p, %p]", |
| 314 | GetLocation().c_str(), |
| 315 | bss_roots_, |
| 316 | bss_begin_, |
| 317 | bss_end_); |
| 318 | return false; |
| 319 | } |
| 320 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 321 | PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet()); |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 322 | uint8_t* dex_cache_arrays = bss_begin_; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 323 | uint8_t* dex_cache_arrays_end = (bss_roots_ != nullptr) ? bss_roots_ : bss_end_; |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 324 | uint32_t dex_file_count = GetOatHeader().GetDexFileCount(); |
| 325 | oat_dex_files_storage_.reserve(dex_file_count); |
| 326 | for (size_t i = 0; i < dex_file_count; i++) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 327 | uint32_t dex_file_location_size; |
| 328 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) { |
| 329 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file " |
| 330 | "location size", |
| 331 | GetLocation().c_str(), |
| 332 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 333 | return false; |
| 334 | } |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 335 | if (UNLIKELY(dex_file_location_size == 0U)) { |
| 336 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name", |
| 337 | GetLocation().c_str(), |
| 338 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 339 | return false; |
| 340 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 341 | if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 342 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file " |
| 343 | "location", |
| 344 | GetLocation().c_str(), |
| 345 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 346 | return false; |
| 347 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 348 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 349 | oat += dex_file_location_size; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 350 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 351 | std::string dex_file_location = ResolveRelativeEncodedDexLocation( |
| 352 | abs_dex_location, |
| 353 | std::string(dex_file_location_data, dex_file_location_size)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 354 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 355 | uint32_t dex_file_checksum; |
| 356 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) { |
| 357 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after " |
| 358 | "dex file checksum", |
| 359 | GetLocation().c_str(), |
| 360 | i, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 361 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 362 | return false; |
| 363 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 364 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 365 | uint32_t dex_file_offset; |
| 366 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) { |
| 367 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated " |
| 368 | "after dex file offsets", |
| 369 | GetLocation().c_str(), |
| 370 | i, |
| 371 | dex_file_location.c_str()); |
| 372 | return false; |
| 373 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 374 | if (UNLIKELY(dex_file_offset == 0U)) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 375 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex " |
| 376 | "file offset", |
| 377 | GetLocation().c_str(), |
| 378 | i, |
| 379 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 380 | return false; |
| 381 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 382 | if (UNLIKELY(dex_file_offset > DexSize())) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 383 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 384 | "offset %u > %zu", |
| 385 | GetLocation().c_str(), |
| 386 | i, |
| 387 | dex_file_location.c_str(), |
| 388 | dex_file_offset, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 389 | DexSize()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 390 | return false; |
| 391 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 392 | if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 393 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 394 | "offset %u of %zu but the size of dex file header is %zu", |
| 395 | GetLocation().c_str(), |
| 396 | i, |
| 397 | dex_file_location.c_str(), |
| 398 | dex_file_offset, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 399 | DexSize(), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 400 | sizeof(DexFile::Header)); |
| 401 | return false; |
| 402 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 403 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 404 | const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 405 | if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 406 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " |
| 407 | "dex file magic '%s'", |
| 408 | GetLocation().c_str(), |
| 409 | i, |
| 410 | dex_file_location.c_str(), |
| 411 | dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 412 | return false; |
| 413 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 414 | if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 415 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " |
| 416 | "dex file version '%s'", |
| 417 | GetLocation().c_str(), |
| 418 | i, |
| 419 | dex_file_location.c_str(), |
| 420 | dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 421 | return false; |
| 422 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 423 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 424 | if (DexSize() - dex_file_offset < header->file_size_) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 425 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 426 | "offset %u and size %u truncated at %zu", |
| 427 | GetLocation().c_str(), |
| 428 | i, |
| 429 | dex_file_location.c_str(), |
| 430 | dex_file_offset, |
| 431 | header->file_size_, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 432 | DexSize()); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 433 | return false; |
| 434 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 435 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 436 | uint32_t class_offsets_offset; |
| 437 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) { |
| 438 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated " |
| 439 | "after class offsets offset", |
| 440 | GetLocation().c_str(), |
| 441 | i, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 442 | dex_file_location.c_str()); |
| 443 | return false; |
| 444 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 445 | if (UNLIKELY(class_offsets_offset > Size()) || |
| 446 | UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) { |
| 447 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated " |
| 448 | "class offsets, offset %u of %zu, class defs %u", |
| 449 | GetLocation().c_str(), |
| 450 | i, |
| 451 | dex_file_location.c_str(), |
| 452 | class_offsets_offset, |
| 453 | Size(), |
| 454 | header->class_defs_size_); |
| 455 | return false; |
| 456 | } |
| 457 | if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) { |
| 458 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned " |
| 459 | "class offsets, offset %u", |
| 460 | GetLocation().c_str(), |
| 461 | i, |
| 462 | dex_file_location.c_str(), |
| 463 | class_offsets_offset); |
| 464 | return false; |
| 465 | } |
| 466 | const uint32_t* class_offsets_pointer = |
| 467 | reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset); |
| 468 | |
| 469 | uint32_t lookup_table_offset; |
| 470 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) { |
| 471 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " |
| 472 | "after lookup table offset", |
| 473 | GetLocation().c_str(), |
| 474 | i, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 475 | dex_file_location.c_str()); |
| 476 | return false; |
| 477 | } |
| 478 | const uint8_t* lookup_table_data = lookup_table_offset != 0u |
| 479 | ? Begin() + lookup_table_offset |
| 480 | : nullptr; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 481 | if (lookup_table_offset != 0u && |
| 482 | (UNLIKELY(lookup_table_offset > Size()) || |
| 483 | UNLIKELY(Size() - lookup_table_offset < |
| 484 | TypeLookupTable::RawDataLength(header->class_defs_size_)))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 485 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated " |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 486 | "type lookup table, offset %u of %zu, class defs %u", |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 487 | GetLocation().c_str(), |
| 488 | i, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 489 | dex_file_location.c_str(), |
| 490 | lookup_table_offset, |
| 491 | Size(), |
| 492 | header->class_defs_size_); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 493 | return false; |
| 494 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 495 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 496 | uint8_t* current_dex_cache_arrays = nullptr; |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 497 | if (dex_cache_arrays != nullptr) { |
| 498 | DexCacheArraysLayout layout(pointer_size, *header); |
| 499 | if (layout.Size() != 0u) { |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 500 | if (static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays) < layout.Size()) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 501 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with " |
| 502 | "truncated dex cache arrays, %zu < %zu.", |
| 503 | GetLocation().c_str(), |
| 504 | i, |
| 505 | dex_file_location.c_str(), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 506 | static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays), |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 507 | layout.Size()); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 508 | return false; |
| 509 | } |
| 510 | current_dex_cache_arrays = dex_cache_arrays; |
| 511 | dex_cache_arrays += layout.Size(); |
| 512 | } |
| 513 | } |
| 514 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 515 | std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str()); |
| 516 | |
| 517 | // Create the OatDexFile and add it to the owning container. |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 518 | OatDexFile* oat_dex_file = new OatDexFile(this, |
| 519 | dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 520 | canonical_location, |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 521 | dex_file_checksum, |
| 522 | dex_file_pointer, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 523 | lookup_table_data, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 524 | class_offsets_pointer, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 525 | current_dex_cache_arrays); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 526 | oat_dex_files_storage_.push_back(oat_dex_file); |
| 527 | |
| 528 | // 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] | 529 | StringPiece key(oat_dex_file->GetDexFileLocation()); |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 530 | oat_dex_files_.Put(key, oat_dex_file); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 531 | if (canonical_location != dex_file_location) { |
| 532 | StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation()); |
| 533 | oat_dex_files_.Put(canonical_key, oat_dex_file); |
| 534 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 535 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 536 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 537 | if (dex_cache_arrays != dex_cache_arrays_end) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 538 | // We expect the bss section to be either empty (dex_cache_arrays and bss_end_ |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 539 | // both null) or contain just the dex cache arrays and optionally some GC roots. |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 540 | *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] | 541 | GetLocation().c_str(), |
| 542 | static_cast<size_t>(bss_end_ - dex_cache_arrays)); |
| 543 | return false; |
| 544 | } |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 545 | return true; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 548 | //////////////////////// |
| 549 | // OatFile via dlopen // |
| 550 | //////////////////////// |
| 551 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 552 | class DlOpenOatFile FINAL : public OatFileBase { |
| 553 | public: |
| 554 | DlOpenOatFile(const std::string& filename, bool executable) |
| 555 | : OatFileBase(filename, executable), |
| 556 | dlopen_handle_(nullptr), |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 557 | shared_objects_before_(0) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | ~DlOpenOatFile() { |
| 561 | if (dlopen_handle_ != nullptr) { |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 562 | if (!kIsTargetBuild) { |
| 563 | MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_); |
| 564 | host_dlopen_handles_.erase(dlopen_handle_); |
Mathieu Chartier | c7d3f4b | 2016-06-01 10:48:19 -0700 | [diff] [blame] | 565 | dlclose(dlopen_handle_); |
| 566 | } else { |
| 567 | dlclose(dlopen_handle_); |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 568 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 569 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | protected: |
| 573 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, |
| 574 | std::string* error_msg) const OVERRIDE { |
| 575 | const uint8_t* ptr = |
| 576 | reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str())); |
| 577 | if (ptr == nullptr) { |
| 578 | *error_msg = dlerror(); |
| 579 | } |
| 580 | return ptr; |
| 581 | } |
| 582 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 583 | void PreLoad() OVERRIDE; |
| 584 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 585 | bool Load(const std::string& elf_filename, |
| 586 | uint8_t* oat_file_begin, |
| 587 | bool writable, |
| 588 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 589 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 590 | std::string* error_msg) OVERRIDE; |
| 591 | |
| 592 | // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions. |
| 593 | void PreSetup(const std::string& elf_filename) OVERRIDE; |
| 594 | |
| 595 | private: |
| 596 | bool Dlopen(const std::string& elf_filename, |
| 597 | uint8_t* oat_file_begin, |
| 598 | std::string* error_msg); |
| 599 | |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 600 | // On the host, if the same library is loaded again with dlopen the same |
| 601 | // file handle is returned. This differs from the behavior of dlopen on the |
| 602 | // target, where dlopen reloads the library at a different address every |
| 603 | // time you load it. The runtime relies on the target behavior to ensure |
| 604 | // each instance of the loaded library has a unique dex cache. To avoid |
| 605 | // problems, we fall back to our own linker in the case when the same |
| 606 | // library is opened multiple times on host. dlopen_handles_ is used to |
| 607 | // detect that case. |
| 608 | // Guarded by host_dlopen_handles_lock_; |
| 609 | static std::unordered_set<void*> host_dlopen_handles_; |
| 610 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 611 | // dlopen handle during runtime. |
| 612 | void* dlopen_handle_; // TODO: Unique_ptr with custom deleter. |
| 613 | |
| 614 | // Dummy memory map objects corresponding to the regions mapped by dlopen. |
| 615 | std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_; |
| 616 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 617 | // The number of shared objects the linker told us about before loading. Used to |
| 618 | // (optimistically) optimize the PreSetup stage (see comment there). |
| 619 | size_t shared_objects_before_; |
| 620 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 621 | DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile); |
| 622 | }; |
| 623 | |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 624 | std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_; |
| 625 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 626 | void DlOpenOatFile::PreLoad() { |
| 627 | #ifdef __APPLE__ |
Andreas Gampe | 39004a6 | 2016-05-18 21:27:00 -0700 | [diff] [blame] | 628 | UNUSED(shared_objects_before_); |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 629 | LOG(FATAL) << "Should not reach here."; |
| 630 | UNREACHABLE(); |
| 631 | #else |
| 632 | // Count the entries in dl_iterate_phdr we get at this point in time. |
| 633 | struct dl_iterate_context { |
| 634 | static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED, |
| 635 | size_t size ATTRIBUTE_UNUSED, |
| 636 | void *data) { |
| 637 | reinterpret_cast<dl_iterate_context*>(data)->count++; |
| 638 | return 0; // Continue iteration. |
| 639 | } |
| 640 | size_t count = 0; |
| 641 | } context; |
| 642 | |
| 643 | dl_iterate_phdr(dl_iterate_context::callback, &context); |
| 644 | shared_objects_before_ = context.count; |
| 645 | #endif |
| 646 | } |
| 647 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 648 | bool DlOpenOatFile::Load(const std::string& elf_filename, |
| 649 | uint8_t* oat_file_begin, |
| 650 | bool writable, |
| 651 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 652 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 653 | std::string* error_msg) { |
| 654 | // Use dlopen only when flagged to do so, and when it's OK to load things executable. |
| 655 | // TODO: Also try when not executable? The issue here could be re-mapping as writable (as |
| 656 | // !executable is a sign that we may want to patch), which may not be allowed for |
| 657 | // various reasons. |
| 658 | if (!kUseDlopen) { |
| 659 | *error_msg = "DlOpen is disabled."; |
| 660 | return false; |
| 661 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 662 | if (low_4gb) { |
| 663 | *error_msg = "DlOpen does not support low 4gb loading."; |
| 664 | return false; |
| 665 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 666 | if (writable) { |
| 667 | *error_msg = "DlOpen does not support writable loading."; |
| 668 | return false; |
| 669 | } |
| 670 | if (!executable) { |
| 671 | *error_msg = "DlOpen does not support non-executable loading."; |
| 672 | return false; |
| 673 | } |
| 674 | |
| 675 | // dlopen always returns the same library if it is already opened on the host. For this reason |
| 676 | // we only use dlopen if we are the target or we do not already have the dex file opened. Having |
| 677 | // the same library loaded multiple times at different addresses is required for class unloading |
| 678 | // and for having dex caches arrays in the .bss section. |
| 679 | if (!kIsTargetBuild) { |
| 680 | if (!kUseDlopenOnHost) { |
| 681 | *error_msg = "DlOpen disabled for host."; |
| 682 | return false; |
| 683 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | bool success = Dlopen(elf_filename, oat_file_begin, error_msg); |
| 687 | DCHECK(dlopen_handle_ != nullptr || !success); |
| 688 | |
| 689 | return success; |
| 690 | } |
| 691 | |
| 692 | bool DlOpenOatFile::Dlopen(const std::string& elf_filename, |
| 693 | uint8_t* oat_file_begin, |
| 694 | std::string* error_msg) { |
| 695 | #ifdef __APPLE__ |
| 696 | // The dl_iterate_phdr syscall is missing. There is similar API on OSX, |
| 697 | // but let's fallback to the custom loading code for the time being. |
| 698 | UNUSED(elf_filename, oat_file_begin); |
| 699 | *error_msg = "Dlopen unsupported on Mac."; |
| 700 | return false; |
| 701 | #else |
| 702 | { |
| 703 | UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr)); |
| 704 | if (absolute_path == nullptr) { |
| 705 | *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str()); |
| 706 | return false; |
| 707 | } |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 708 | #ifdef ART_TARGET_ANDROID |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 709 | android_dlextinfo extinfo; |
| 710 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle |
| 711 | // (open oat files multiple |
| 712 | // times). |
| 713 | ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute |
| 714 | // (non-pic boot image). |
Andreas Gampe | 226b91e | 2015-12-02 10:29:33 -0800 | [diff] [blame] | 715 | if (oat_file_begin != nullptr) { // |
| 716 | extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if |
| 717 | extinfo.reserved_addr = oat_file_begin; // vaddr = 0. |
| 718 | } // (pic boot image). |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 719 | dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo); |
| 720 | #else |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 721 | UNUSED(oat_file_begin); |
Julien Duraj | 1af0c4f | 2016-11-16 14:05:48 +0000 | [diff] [blame^] | 722 | static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles"); |
Mathieu Chartier | c7d3f4b | 2016-06-01 10:48:19 -0700 | [diff] [blame] | 723 | MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_); |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 724 | dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW); |
| 725 | if (dlopen_handle_ != nullptr) { |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 726 | if (!host_dlopen_handles_.insert(dlopen_handle_).second) { |
| 727 | dlclose(dlopen_handle_); |
| 728 | dlopen_handle_ = nullptr; |
| 729 | *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str()); |
| 730 | return false; |
| 731 | } |
| 732 | } |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 733 | #endif // ART_TARGET_ANDROID |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 734 | } |
| 735 | if (dlopen_handle_ == nullptr) { |
| 736 | *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror()); |
| 737 | return false; |
| 738 | } |
| 739 | return true; |
| 740 | #endif |
| 741 | } |
| 742 | |
| 743 | void DlOpenOatFile::PreSetup(const std::string& elf_filename) { |
Andreas Gampe | 74f07b5 | 2015-12-02 11:53:26 -0800 | [diff] [blame] | 744 | #ifdef __APPLE__ |
| 745 | UNUSED(elf_filename); |
| 746 | LOG(FATAL) << "Should not reach here."; |
| 747 | UNREACHABLE(); |
| 748 | #else |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 749 | struct dl_iterate_context { |
| 750 | static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) { |
| 751 | auto* context = reinterpret_cast<dl_iterate_context*>(data); |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 752 | context->shared_objects_seen++; |
| 753 | if (context->shared_objects_seen < context->shared_objects_before) { |
| 754 | // We haven't been called yet for anything we haven't seen before. Just continue. |
| 755 | // Note: this is aggressively optimistic. If another thread was unloading a library, |
| 756 | // we may miss out here. However, this does not happen often in practice. |
| 757 | return 0; |
| 758 | } |
| 759 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 760 | // See whether this callback corresponds to the file which we have just loaded. |
| 761 | bool contains_begin = false; |
| 762 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 763 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 764 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 765 | info->dlpi_phdr[i].p_vaddr); |
| 766 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
| 767 | if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) { |
| 768 | contains_begin = true; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | // Add dummy mmaps for this file. |
| 774 | if (contains_begin) { |
| 775 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 776 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 777 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 778 | info->dlpi_phdr[i].p_vaddr); |
| 779 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
| 780 | MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz); |
| 781 | context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap)); |
| 782 | } |
| 783 | } |
| 784 | return 1; // Stop iteration and return 1 from dl_iterate_phdr. |
| 785 | } |
| 786 | return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished. |
| 787 | } |
| 788 | const uint8_t* const begin_; |
| 789 | std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_; |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 790 | const size_t shared_objects_before; |
| 791 | size_t shared_objects_seen; |
| 792 | }; |
| 793 | dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0}; |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 794 | |
| 795 | if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) { |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 796 | // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0 |
| 797 | // before giving up. This should be unusual. |
| 798 | VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before=" |
| 799 | << shared_objects_before_; |
| 800 | dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0}; |
| 801 | if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) { |
| 802 | // OK, give up and print an error. |
| 803 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 804 | LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps."; |
| 805 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 806 | } |
Andreas Gampe | 74f07b5 | 2015-12-02 11:53:26 -0800 | [diff] [blame] | 807 | #endif |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | //////////////////////////////////////////////// |
| 811 | // OatFile via our own ElfFile implementation // |
| 812 | //////////////////////////////////////////////// |
| 813 | |
| 814 | class ElfOatFile FINAL : public OatFileBase { |
| 815 | public: |
| 816 | ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {} |
| 817 | |
| 818 | static ElfOatFile* OpenElfFile(File* file, |
| 819 | const std::string& location, |
| 820 | uint8_t* requested_base, |
| 821 | uint8_t* oat_file_begin, // Override base if not null |
| 822 | bool writable, |
| 823 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 824 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 825 | const char* abs_dex_location, |
| 826 | std::string* error_msg); |
| 827 | |
| 828 | bool InitializeFromElfFile(ElfFile* elf_file, |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 829 | VdexFile* vdex_file, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 830 | const char* abs_dex_location, |
| 831 | std::string* error_msg); |
| 832 | |
| 833 | protected: |
| 834 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, |
| 835 | std::string* error_msg) const OVERRIDE { |
| 836 | const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name); |
| 837 | if (ptr == nullptr) { |
| 838 | *error_msg = "(Internal implementation could not find symbol)"; |
| 839 | } |
| 840 | return ptr; |
| 841 | } |
| 842 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 843 | void PreLoad() OVERRIDE { |
| 844 | } |
| 845 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 846 | bool Load(const std::string& elf_filename, |
| 847 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 848 | bool writable, |
| 849 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 850 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 851 | std::string* error_msg) OVERRIDE; |
| 852 | |
| 853 | void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE { |
| 854 | } |
| 855 | |
| 856 | private: |
| 857 | bool ElfFileOpen(File* file, |
| 858 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 859 | bool writable, |
| 860 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 861 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 862 | std::string* error_msg); |
| 863 | |
| 864 | private: |
| 865 | // Backing memory map for oat file during cross compilation. |
| 866 | std::unique_ptr<ElfFile> elf_file_; |
| 867 | |
| 868 | DISALLOW_COPY_AND_ASSIGN(ElfOatFile); |
| 869 | }; |
| 870 | |
| 871 | ElfOatFile* ElfOatFile::OpenElfFile(File* file, |
| 872 | const std::string& location, |
| 873 | uint8_t* requested_base, |
| 874 | uint8_t* oat_file_begin, // Override base if not null |
| 875 | bool writable, |
| 876 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 877 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 878 | const char* abs_dex_location, |
| 879 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 880 | ScopedTrace trace("Open elf file " + location); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 881 | std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable)); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 882 | bool success = oat_file->ElfFileOpen(file, |
| 883 | oat_file_begin, |
| 884 | writable, |
| 885 | low_4gb, |
| 886 | executable, |
| 887 | error_msg); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 888 | if (!success) { |
| 889 | CHECK(!error_msg->empty()); |
| 890 | return nullptr; |
| 891 | } |
| 892 | |
| 893 | // Complete the setup. |
| 894 | if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) { |
| 895 | return nullptr; |
| 896 | } |
| 897 | |
| 898 | if (!oat_file->Setup(abs_dex_location, error_msg)) { |
| 899 | return nullptr; |
| 900 | } |
| 901 | |
| 902 | return oat_file.release(); |
| 903 | } |
| 904 | |
| 905 | bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file, |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 906 | VdexFile* vdex_file, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 907 | const char* abs_dex_location, |
| 908 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 909 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 910 | if (IsExecutable()) { |
| 911 | *error_msg = "Cannot initialize from elf file in executable mode."; |
| 912 | return false; |
| 913 | } |
| 914 | elf_file_.reset(elf_file); |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 915 | SetVdex(vdex_file); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 916 | uint64_t offset, size; |
| 917 | bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size); |
| 918 | CHECK(has_section); |
| 919 | SetBegin(elf_file->Begin() + offset); |
| 920 | SetEnd(elf_file->Begin() + size + offset); |
| 921 | // Ignore the optional .bss section when opening non-executable. |
| 922 | return Setup(abs_dex_location, error_msg); |
| 923 | } |
| 924 | |
| 925 | bool ElfOatFile::Load(const std::string& elf_filename, |
| 926 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 927 | bool writable, |
| 928 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 929 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 930 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 931 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 932 | std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str())); |
| 933 | if (file == nullptr) { |
| 934 | *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno)); |
| 935 | return false; |
| 936 | } |
| 937 | return ElfOatFile::ElfFileOpen(file.get(), |
| 938 | oat_file_begin, |
| 939 | writable, |
| 940 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 941 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 942 | error_msg); |
| 943 | } |
| 944 | |
| 945 | bool ElfOatFile::ElfFileOpen(File* file, |
| 946 | uint8_t* oat_file_begin, |
| 947 | bool writable, |
| 948 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 949 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 950 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 951 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 952 | // TODO: rename requested_base to oat_data_begin |
| 953 | elf_file_.reset(ElfFile::Open(file, |
| 954 | writable, |
| 955 | /*program_header_only*/true, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 956 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 957 | error_msg, |
| 958 | oat_file_begin)); |
| 959 | if (elf_file_ == nullptr) { |
| 960 | DCHECK(!error_msg->empty()); |
| 961 | return false; |
| 962 | } |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 963 | bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 964 | DCHECK(loaded || !error_msg->empty()); |
| 965 | return loaded; |
| 966 | } |
| 967 | |
| 968 | ////////////////////////// |
| 969 | // General OatFile code // |
| 970 | ////////////////////////// |
| 971 | |
| 972 | std::string OatFile::ResolveRelativeEncodedDexLocation( |
| 973 | const char* abs_dex_location, const std::string& rel_dex_location) { |
| 974 | if (abs_dex_location != nullptr && rel_dex_location[0] != '/') { |
| 975 | // Strip :classes<N>.dex used for secondary multidex files. |
| 976 | std::string base = DexFile::GetBaseLocation(rel_dex_location); |
| 977 | std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location); |
| 978 | |
| 979 | // Check if the base is a suffix of the provided abs_dex_location. |
| 980 | std::string target_suffix = "/" + base; |
| 981 | std::string abs_location(abs_dex_location); |
| 982 | if (abs_location.size() > target_suffix.size()) { |
| 983 | size_t pos = abs_location.size() - target_suffix.size(); |
| 984 | if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) { |
| 985 | return abs_location + multidex_suffix; |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | return rel_dex_location; |
| 990 | } |
| 991 | |
| 992 | static void CheckLocation(const std::string& location) { |
| 993 | CHECK(!location.empty()); |
| 994 | } |
| 995 | |
| 996 | OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file, |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 997 | VdexFile* vdex_file, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 998 | const std::string& location, |
| 999 | const char* abs_dex_location, |
| 1000 | std::string* error_msg) { |
| 1001 | std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */)); |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 1002 | return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg) |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1003 | ? oat_file.release() |
| 1004 | : nullptr; |
| 1005 | } |
| 1006 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1007 | OatFile* OatFile::Open(const std::string& oat_filename, |
| 1008 | const std::string& oat_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1009 | uint8_t* requested_base, |
| 1010 | uint8_t* oat_file_begin, |
| 1011 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1012 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1013 | const char* abs_dex_location, |
| 1014 | std::string* error_msg) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1015 | ScopedTrace trace("Open oat file " + oat_location); |
| 1016 | CHECK(!oat_filename.empty()) << oat_location; |
| 1017 | CheckLocation(oat_location); |
Andreas Gampe | 54315c7 | 2016-05-18 21:10:42 -0700 | [diff] [blame] | 1018 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1019 | std::string vdex_filename = ReplaceFileExtension(oat_filename, "vdex"); |
| 1020 | |
| 1021 | // Check that the files even exist, fast-fail. |
| 1022 | if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) { |
| 1023 | *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str()); |
| 1024 | return nullptr; |
| 1025 | } else if (!OS::FileExists(oat_filename.c_str())) { |
| 1026 | *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str()); |
Andreas Gampe | 54315c7 | 2016-05-18 21:10:42 -0700 | [diff] [blame] | 1027 | return nullptr; |
| 1028 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1029 | |
| 1030 | // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is |
| 1031 | // disabled. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1032 | OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename, |
| 1033 | oat_filename, |
| 1034 | oat_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1035 | requested_base, |
| 1036 | oat_file_begin, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1037 | false /* writable */, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1038 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1039 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1040 | abs_dex_location, |
| 1041 | error_msg); |
| 1042 | if (with_dlopen != nullptr) { |
| 1043 | return with_dlopen; |
| 1044 | } |
| 1045 | if (kPrintDlOpenErrorMessage) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1046 | LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg; |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1047 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1048 | // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: |
| 1049 | // |
| 1050 | // On target, dlopen may fail when compiling due to selinux restrictions on installd. |
| 1051 | // |
| 1052 | // We use our own ELF loader for Quick to deal with legacy apps that |
| 1053 | // open a generated dex file by name, remove the file, then open |
| 1054 | // another generated dex file with the same name. http://b/10614658 |
| 1055 | // |
| 1056 | // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. |
| 1057 | // |
| 1058 | // |
| 1059 | // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually |
| 1060 | // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1061 | OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename, |
| 1062 | oat_filename, |
| 1063 | oat_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1064 | requested_base, |
| 1065 | oat_file_begin, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1066 | false /* writable */, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1067 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1068 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1069 | abs_dex_location, |
| 1070 | error_msg); |
| 1071 | return with_internal; |
| 1072 | } |
| 1073 | |
| 1074 | OatFile* OatFile::OpenWritable(File* file, |
| 1075 | const std::string& location, |
| 1076 | const char* abs_dex_location, |
| 1077 | std::string* error_msg) { |
| 1078 | CheckLocation(location); |
| 1079 | return ElfOatFile::OpenElfFile(file, |
| 1080 | location, |
| 1081 | nullptr, |
| 1082 | nullptr, |
| 1083 | true, |
| 1084 | false, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1085 | /*low_4gb*/false, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1086 | abs_dex_location, |
| 1087 | error_msg); |
| 1088 | } |
| 1089 | |
| 1090 | OatFile* OatFile::OpenReadable(File* file, |
| 1091 | const std::string& location, |
| 1092 | const char* abs_dex_location, |
| 1093 | std::string* error_msg) { |
| 1094 | CheckLocation(location); |
| 1095 | return ElfOatFile::OpenElfFile(file, |
| 1096 | location, |
| 1097 | nullptr, |
| 1098 | nullptr, |
| 1099 | false, |
| 1100 | false, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1101 | /*low_4gb*/false, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1102 | abs_dex_location, |
| 1103 | error_msg); |
| 1104 | } |
| 1105 | |
| 1106 | OatFile::OatFile(const std::string& location, bool is_executable) |
| 1107 | : location_(location), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1108 | vdex_(nullptr), |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1109 | begin_(nullptr), |
| 1110 | end_(nullptr), |
| 1111 | bss_begin_(nullptr), |
| 1112 | bss_end_(nullptr), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1113 | bss_roots_(nullptr), |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1114 | is_executable_(is_executable), |
| 1115 | secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) { |
| 1116 | CHECK(!location_.empty()); |
| 1117 | } |
| 1118 | |
| 1119 | OatFile::~OatFile() { |
| 1120 | STLDeleteElements(&oat_dex_files_storage_); |
| 1121 | } |
| 1122 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1123 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1124 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1127 | const uint8_t* OatFile::Begin() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 1128 | CHECK(begin_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1129 | return begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1132 | const uint8_t* OatFile::End() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 1133 | CHECK(end_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1134 | return end_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1137 | const uint8_t* OatFile::BssBegin() const { |
| 1138 | return bss_begin_; |
| 1139 | } |
| 1140 | |
| 1141 | const uint8_t* OatFile::BssEnd() const { |
| 1142 | return bss_end_; |
| 1143 | } |
| 1144 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1145 | const uint8_t* OatFile::DexBegin() const { |
| 1146 | return kIsVdexEnabled ? vdex_->Begin() : Begin(); |
| 1147 | } |
| 1148 | |
| 1149 | const uint8_t* OatFile::DexEnd() const { |
| 1150 | return kIsVdexEnabled ? vdex_->End() : End(); |
| 1151 | } |
| 1152 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1153 | ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const { |
| 1154 | if (bss_roots_ != nullptr) { |
| 1155 | auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_); |
| 1156 | auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_); |
| 1157 | return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots); |
| 1158 | } else { |
| 1159 | return ArrayRef<GcRoot<mirror::Object>>(); |
| 1160 | } |
| 1161 | } |
| 1162 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1163 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location, |
| 1164 | const uint32_t* dex_location_checksum, |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1165 | std::string* error_msg) const { |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1166 | // NOTE: We assume here that the canonical location for a given dex_location never |
| 1167 | // changes. If it does (i.e. some symlink used by the filename changes) we may return |
| 1168 | // an incorrect OatDexFile. As long as we have a checksum to check, we shall return |
| 1169 | // an identical file or fail; otherwise we may see some unpredictable failures. |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1170 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1171 | // TODO: Additional analysis of usage patterns to see if this can be simplified |
| 1172 | // without any performance loss, for example by not doing the first lock-free lookup. |
| 1173 | |
| 1174 | const OatFile::OatDexFile* oat_dex_file = nullptr; |
| 1175 | StringPiece key(dex_location); |
| 1176 | // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations |
| 1177 | // directly mentioned in the oat file and doesn't require locking. |
| 1178 | auto primary_it = oat_dex_files_.find(key); |
| 1179 | if (primary_it != oat_dex_files_.end()) { |
| 1180 | oat_dex_file = primary_it->second; |
| 1181 | DCHECK(oat_dex_file != nullptr); |
| 1182 | } else { |
| 1183 | // This dex_location is not one of the dex locations directly mentioned in the |
| 1184 | // oat file. The correct lookup is via the canonical location but first see in |
| 1185 | // the secondary_oat_dex_files_ whether we've looked up this location before. |
| 1186 | MutexLock mu(Thread::Current(), secondary_lookup_lock_); |
| 1187 | auto secondary_lb = secondary_oat_dex_files_.lower_bound(key); |
| 1188 | if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1189 | oat_dex_file = secondary_lb->second; // May be null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1190 | } else { |
| 1191 | // 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] | 1192 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1193 | if (dex_canonical_location != dex_location) { |
| 1194 | StringPiece canonical_key(dex_canonical_location); |
| 1195 | auto canonical_it = oat_dex_files_.find(canonical_key); |
| 1196 | if (canonical_it != oat_dex_files_.end()) { |
| 1197 | oat_dex_file = canonical_it->second; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1198 | } // else keep null. |
| 1199 | } // else keep null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1200 | |
| 1201 | // Copy the key to the string_cache_ and store the result in secondary map. |
| 1202 | string_cache_.emplace_back(key.data(), key.length()); |
| 1203 | StringPiece key_copy(string_cache_.back()); |
| 1204 | secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1207 | |
| 1208 | if (oat_dex_file == nullptr) { |
| 1209 | if (error_msg != nullptr) { |
| 1210 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
| 1211 | *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location) |
| 1212 | + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation(); |
| 1213 | } |
| 1214 | return nullptr; |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1215 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 1216 | |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1217 | if (dex_location_checksum != nullptr && |
| 1218 | oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) { |
| 1219 | if (error_msg != nullptr) { |
| 1220 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
| 1221 | std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum()); |
| 1222 | std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum); |
| 1223 | *error_msg = "OatDexFile for DexFile " + std::string(dex_location) |
| 1224 | + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation() |
| 1225 | + " has checksum " + checksum + " but " + required_checksum + " was required"; |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1226 | } |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1227 | return nullptr; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1228 | } |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1229 | return oat_dex_file; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1232 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 1233 | const std::string& dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1234 | const std::string& canonical_dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1235 | uint32_t dex_file_location_checksum, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1236 | const uint8_t* dex_file_pointer, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1237 | const uint8_t* lookup_table_data, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1238 | const uint32_t* oat_class_offsets_pointer, |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 1239 | uint8_t* dex_cache_arrays) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1240 | : oat_file_(oat_file), |
| 1241 | dex_file_location_(dex_file_location), |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1242 | canonical_dex_file_location_(canonical_dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1243 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1244 | dex_file_pointer_(dex_file_pointer), |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1245 | lookup_table_data_(lookup_table_data), |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1246 | oat_class_offsets_pointer_(oat_class_offsets_pointer), |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1247 | dex_cache_arrays_(dex_cache_arrays) { |
| 1248 | // Initialize TypeLookupTable. |
| 1249 | if (lookup_table_data_ != nullptr) { |
| 1250 | // Peek the number of classes from the DexFile. |
| 1251 | const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_); |
| 1252 | const uint32_t num_class_defs = dex_header->class_defs_size_; |
| 1253 | if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) { |
| 1254 | LOG(WARNING) << "found truncated lookup table in " << dex_file_location_; |
| 1255 | } else { |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1256 | lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1260 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1261 | OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table) |
| 1262 | : lookup_table_(std::move(lookup_table)) {} |
| 1263 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1264 | OatFile::OatDexFile::~OatDexFile() {} |
| 1265 | |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 1266 | size_t OatFile::OatDexFile::FileSize() const { |
| 1267 | return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 1268 | } |
| 1269 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1270 | std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1271 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1272 | static constexpr bool kVerify = false; |
| 1273 | static constexpr bool kVerifyChecksum = false; |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 1274 | return DexFile::Open(dex_file_pointer_, |
| 1275 | FileSize(), |
| 1276 | dex_file_location_, |
| 1277 | dex_file_location_checksum_, |
| 1278 | this, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1279 | kVerify, |
| 1280 | kVerifyChecksum, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 1281 | error_msg); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1284 | uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const { |
| 1285 | return oat_class_offsets_pointer_[class_def_index]; |
| 1286 | } |
| 1287 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1288 | OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1289 | uint32_t oat_class_offset = GetOatClassOffset(class_def_index); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1290 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1291 | const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1292 | CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1293 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1294 | const uint8_t* status_pointer = oat_class_pointer; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1295 | CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 1296 | mirror::Class::Status status = |
| 1297 | static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer)); |
| 1298 | CHECK_LT(status, mirror::Class::kStatusMax); |
| 1299 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1300 | const uint8_t* type_pointer = status_pointer + sizeof(uint16_t); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1301 | CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 1302 | OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer)); |
| 1303 | CHECK_LT(type, kOatClassMax); |
| 1304 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1305 | const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1306 | CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1307 | |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1308 | uint32_t bitmap_size = 0; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1309 | const uint8_t* bitmap_pointer = nullptr; |
| 1310 | const uint8_t* methods_pointer = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1311 | if (type != kOatClassNoneCompiled) { |
| 1312 | if (type == kOatClassSomeCompiled) { |
| 1313 | bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer)); |
| 1314 | bitmap_pointer = after_type_pointer + sizeof(bitmap_size); |
| 1315 | CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 1316 | methods_pointer = bitmap_pointer + bitmap_size; |
| 1317 | } else { |
| 1318 | methods_pointer = after_type_pointer; |
| 1319 | } |
| 1320 | CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1321 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1322 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1323 | return OatFile::OatClass(oat_file_, |
| 1324 | status, |
| 1325 | type, |
| 1326 | bitmap_size, |
| 1327 | reinterpret_cast<const uint32_t*>(bitmap_pointer), |
| 1328 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1331 | const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file, |
| 1332 | const char* descriptor, |
| 1333 | size_t hash) { |
| 1334 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 1335 | DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash); |
| 1336 | if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) { |
| 1337 | const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash); |
| 1338 | return (class_def_idx != DexFile::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr; |
| 1339 | } |
| 1340 | // Fast path for rare no class defs case. |
| 1341 | const uint32_t num_class_defs = dex_file.NumClassDefs(); |
| 1342 | if (num_class_defs == 0) { |
| 1343 | return nullptr; |
| 1344 | } |
| 1345 | const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor); |
| 1346 | if (type_id != nullptr) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1347 | dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1348 | return dex_file.FindClassDef(type_idx); |
| 1349 | } |
| 1350 | return nullptr; |
| 1351 | } |
| 1352 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1353 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1354 | mirror::Class::Status status, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1355 | OatClassType type, |
| 1356 | uint32_t bitmap_size, |
| 1357 | const uint32_t* bitmap_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1358 | const OatMethodOffsets* methods_pointer) |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1359 | : oat_file_(oat_file), status_(status), type_(type), |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1360 | bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1361 | switch (type_) { |
| 1362 | case kOatClassAllCompiled: { |
| 1363 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1364 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1365 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1366 | break; |
| 1367 | } |
| 1368 | case kOatClassSomeCompiled: { |
| 1369 | CHECK_NE(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1370 | CHECK(bitmap_pointer != nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1371 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1372 | break; |
| 1373 | } |
| 1374 | case kOatClassNoneCompiled: { |
| 1375 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1376 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1377 | CHECK(methods_pointer_ == nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1378 | break; |
| 1379 | } |
| 1380 | case kOatClassMax: { |
| 1381 | LOG(FATAL) << "Invalid OatClassType " << type_; |
| 1382 | break; |
| 1383 | } |
| 1384 | } |
| 1385 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1386 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1387 | uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const { |
| 1388 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 1389 | if (oat_method_offsets == nullptr) { |
| 1390 | return 0u; |
| 1391 | } |
| 1392 | return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin(); |
| 1393 | } |
| 1394 | |
| 1395 | const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1396 | // 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] | 1397 | if (methods_pointer_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1398 | CHECK_EQ(kOatClassNoneCompiled, type_); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1399 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1400 | } |
| 1401 | size_t methods_pointer_index; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1402 | if (bitmap_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1403 | CHECK_EQ(kOatClassAllCompiled, type_); |
| 1404 | methods_pointer_index = method_index; |
| 1405 | } else { |
| 1406 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1407 | if (!BitVector::IsBitSet(bitmap_, method_index)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1408 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1409 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1410 | size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index); |
| 1411 | methods_pointer_index = num_set_bits; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1412 | } |
| 1413 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index]; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1414 | return &oat_method_offsets; |
| 1415 | } |
| 1416 | |
| 1417 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 1418 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 1419 | if (oat_method_offsets == nullptr) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1420 | return OatMethod(nullptr, 0); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1421 | } |
| 1422 | if (oat_file_->IsExecutable() || |
| 1423 | Runtime::Current() == nullptr || // This case applies for oatdump. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1424 | Runtime::Current()->IsAotCompiler()) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1425 | return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_); |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1426 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1427 | // We aren't allowed to use the compiled code. We just force it down the interpreted / jit |
| 1428 | // version. |
| 1429 | return OatMethod(oat_file_->Begin(), 0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1432 | void OatFile::OatMethod::LinkMethod(ArtMethod* method) const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 1433 | CHECK(method != nullptr); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1434 | method->SetEntryPointFromQuickCompiledCode(GetQuickCode()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 1437 | bool OatFile::HasPatchInfo() const { |
| 1438 | return GetOatHeader().HasPatchInfo(); |
| 1439 | } |
| 1440 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1441 | bool OatFile::IsPic() const { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1442 | return GetOatHeader().IsPic(); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1443 | // TODO: Check against oat_patches. b/18144996 |
| 1444 | } |
| 1445 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 1446 | bool OatFile::IsDebuggable() const { |
| 1447 | return GetOatHeader().IsDebuggable(); |
| 1448 | } |
| 1449 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1450 | CompilerFilter::Filter OatFile::GetCompilerFilter() const { |
| 1451 | return GetOatHeader().GetCompilerFilter(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1454 | static constexpr char kDexClassPathEncodingSeparator = '*'; |
| 1455 | |
| 1456 | std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) { |
| 1457 | std::ostringstream out; |
| 1458 | |
| 1459 | for (const DexFile* dex_file : dex_files) { |
| 1460 | out << dex_file->GetLocation().c_str(); |
| 1461 | out << kDexClassPathEncodingSeparator; |
| 1462 | out << dex_file->GetLocationChecksum(); |
| 1463 | out << kDexClassPathEncodingSeparator; |
| 1464 | } |
| 1465 | |
| 1466 | return out.str(); |
| 1467 | } |
| 1468 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1469 | bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1470 | if (dex_dependencies == nullptr || dex_dependencies[0] == 0) { |
| 1471 | // No dependencies. |
| 1472 | return true; |
| 1473 | } |
| 1474 | |
| 1475 | // Assumption: this is not performance-critical. So it's OK to do this with a std::string and |
| 1476 | // Split() instead of manual parsing of the combined char*. |
| 1477 | std::vector<std::string> split; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1478 | Split(dex_dependencies, kDexClassPathEncodingSeparator, &split); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1479 | if (split.size() % 2 != 0) { |
| 1480 | // Expected pairs of location and checksum. |
| 1481 | *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies); |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
| 1485 | for (auto it = split.begin(), end = split.end(); it != end; it += 2) { |
| 1486 | std::string& location = *it; |
| 1487 | std::string& checksum = *(it + 1); |
| 1488 | int64_t converted = strtoll(checksum.c_str(), nullptr, 10); |
| 1489 | if (converted == 0) { |
| 1490 | // Conversion error. |
| 1491 | *msg = StringPrintf("Conversion error for %s", checksum.c_str()); |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | uint32_t dex_checksum; |
| 1496 | std::string error_msg; |
| 1497 | if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1498 | &dex_checksum, |
| 1499 | &error_msg)) { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1500 | if (converted != dex_checksum) { |
| 1501 | *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u", |
| 1502 | location.c_str(), converted, dex_checksum); |
| 1503 | return false; |
| 1504 | } |
| 1505 | } else { |
| 1506 | // Problem retrieving checksum. |
| 1507 | // TODO: odex files? |
| 1508 | *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(), |
| 1509 | error_msg.c_str()); |
| 1510 | return false; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
| 1517 | bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1518 | std::vector<std::string>* locations) { |
| 1519 | DCHECK(locations != nullptr); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1520 | if (dex_dependencies == nullptr || dex_dependencies[0] == 0) { |
| 1521 | return true; |
| 1522 | } |
| 1523 | |
| 1524 | // Assumption: this is not performance-critical. So it's OK to do this with a std::string and |
| 1525 | // Split() instead of manual parsing of the combined char*. |
| 1526 | std::vector<std::string> split; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1527 | Split(dex_dependencies, kDexClassPathEncodingSeparator, &split); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1528 | if (split.size() % 2 != 0) { |
| 1529 | // Expected pairs of location and checksum. |
| 1530 | return false; |
| 1531 | } |
| 1532 | |
| 1533 | for (auto it = split.begin(), end = split.end(); it != end; it += 2) { |
| 1534 | locations->push_back(*it); |
| 1535 | } |
| 1536 | |
| 1537 | return true; |
| 1538 | } |
| 1539 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 1540 | OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file, |
| 1541 | uint16_t class_def_idx, |
| 1542 | bool* found) { |
| 1543 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
| 1544 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1545 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 1546 | *found = false; |
| 1547 | return OatFile::OatClass::Invalid(); |
| 1548 | } |
| 1549 | *found = true; |
| 1550 | return oat_dex_file->GetOatClass(class_def_idx); |
| 1551 | } |
| 1552 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1553 | void OatFile::OatDexFile::AssertAotCompiler() { |
| 1554 | CHECK(Runtime::Current()->IsAotCompiler()); |
| 1555 | } |
| 1556 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1557 | } // namespace art |