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