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