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