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