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