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