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